Skip to content

Commit

Permalink
fix IAffectedElement interface. #163 AG-19624
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit bac0bdc
Author: Slava Leleka <[email protected]>
Date:   Wed Feb 15 16:08:51 2023 +0200

    fix readme

commit a09522d
Author: Slava Leleka <[email protected]>
Date:   Wed Feb 15 16:06:49 2023 +0200

    revert AffectedElementProto for internal and api interfaces

commit ec1c316
Author: Slava Leleka <[email protected]>
Date:   Tue Feb 14 18:59:51 2023 +0200

    fix AffectedElement.originalStyle
  • Loading branch information
slavaleleka committed Feb 16, 2023
1 parent ff59678 commit 76db246
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 17 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# ExtendedCss Changelog


## v2.0.50
## v2.0.51

### Changed

- Property `content` of `style` in `rules` of API type `IAffectedElement` is marked as optional
- Property `content` of `style` in `rules` of API type `IAffectedElement` is marked as optional [#163](https://github.com/AdguardTeam/ExtendedCss/issues/163)


## v2.0.49
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ type BeforeStyleAppliedCallback = (x:IAffectedElement) => IAffectedElement;
* Simplified just for representation.
* Its optional property 'content' may contain the applied rule text
*/
type IAffectedElement {
interface IAffectedElement {
rules: { style: { content?: string }}[]
node: HTMLElement;
}
Expand Down
45 changes: 36 additions & 9 deletions src/extended-css/helpers/style-setter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { AffectedElement, Context } from './types';
import {
AffectedElement,
Context,
IAffectedElement,
} from './types';
import { CssStyleMap } from '../../css-rule';

import { getElementSelectorPath } from '../../common/utils/nodes';
Expand Down Expand Up @@ -74,18 +78,37 @@ export const setStyleToElement = (node: Node, style: CssStyleMap): void => {
};

/**
* Checks whether the `affectedElement` has required `node` and `rules` properties
* after `beforeStyleApplied()` execution.
* Checks the required properties of `affectedElement`
* **before** `beforeStyleApplied()` execution.
*
* @param affectedElement Affected element.
*
* @returns False if there is no `node` or `rules`
* or `rules` is not an array.
*/
const isIAffectedElement = (
affectedElement: AffectedElement | IAffectedElement,
): affectedElement is IAffectedElement => {
return 'node' in affectedElement
&& 'rules' in affectedElement
&& affectedElement.rules instanceof Array;
};

/**
* Checks the required properties of `affectedElement`
* **after** `beforeStyleApplied()` execution.
* These properties are needed for proper internal usage.
*
* @param affectedElement Affected element.
*
* @returns False if there is no `node` or `rules`
* or `rules` is not an array.
*/
const isAffectedElement = (affectedElement: AffectedElement): boolean => {
// simple checking of properties needed for following affectedElement usage
const isAffectedElement = (
affectedElement: AffectedElement | IAffectedElement,
): affectedElement is AffectedElement => {
return 'node' in affectedElement
&& 'originalStyle' in affectedElement
&& 'rules' in affectedElement
&& affectedElement.rules instanceof Array;
};
Expand All @@ -103,19 +126,23 @@ export const applyStyle = (context: Context, rawAffectedElement: AffectedElement
// style is already applied and protected by the observer
return;
}
let affectedElement: AffectedElement;
let affectedElement: AffectedElement | IAffectedElement;
if (context.beforeStyleApplied) {
if (!isIAffectedElement(rawAffectedElement)) {
throw new Error("Returned IAffectedElement should have 'node' and 'rules' properties");
}
affectedElement = context.beforeStyleApplied(rawAffectedElement);
if (!affectedElement) {
throw new Error("Callback 'beforeStyleApplied' should return IAffectedElement");
}
if (!isAffectedElement(affectedElement)) {
throw new Error("Returned IAffectedElement should have 'node' and 'rules' properties");
}
} else {
affectedElement = rawAffectedElement;
}

if (!isAffectedElement(affectedElement)) {
throw new Error("Returned IAffectedElement should have 'node' and 'rules' properties");
}

const { node, rules } = affectedElement;
for (let i = 0; i < rules.length; i += 1) {
const rule = rules[i];
Expand Down
22 changes: 17 additions & 5 deletions src/extended-css/helpers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,33 @@ export type MainCallback = () => void;
export type ProtectionCallback = (m: MutationRecord[], o: ExtMutationObserver) => void;

/**
* Interface for internal lib usage.
* Properties `node` and `rules` are required.
* Prototype interface for:
* - `AffectedElement` for internal lib usage
* where no required style properties in rules;
* - `IAffectedElement` for export
* where 'originalStyle' property is not required.
*/
export interface AffectedElement {
interface AffectedElementProto {
node: HTMLElement;
rules: ExtCssRuleData[];
originalStyle: string;
protectionObserver?: ExtMutationObserver | null;
removed?: boolean;
}

/**
* Interface for internal lib usage.
*/
export interface AffectedElement extends AffectedElementProto {
rules: ExtCssRuleData[];
}

/**
* API interface.
*/
export type IAffectedElement = AffectedElement;
export interface IAffectedElement extends Partial<AffectedElementProto> {
node: HTMLElement;
rules: Partial<ExtCssRuleData>[];
}

/**
* Data pairs for selector and number of times the element was removed by ExtendedCss.
Expand Down

0 comments on commit 76db246

Please sign in to comment.