diff --git a/.eslintrc b/.eslintrc index 7c2692ca..a5840738 100644 --- a/.eslintrc +++ b/.eslintrc @@ -33,6 +33,8 @@ { "SwitchCase": 1 } - ] + ], + "jsdoc/require-param-type": 0, + "jsdoc/require-returns-type": 0 } } diff --git a/README.md b/README.md index 1754ffcf..f49f719e 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ will gives you this AST: { "category": "Cosmetic", "type": "ScriptletRule", - "syntax": "AdGuard", + "syntax": "Adg", "exception": false, "modifiers": [], "domains": [ @@ -99,7 +99,7 @@ example.com,~example.net#%#//scriptlet('prevent-setInterval', 'check', '!300') > *Please note that unnecessary spaces will disappear and CSS selectors will be regenerated according to uniform formatting* ```typescript -import { RuleParser } from "aglint/parser"; +import { RuleParser } from "aglint"; // General rules (ADG/uBO/ABP) console.log(RuleParser.generate(RuleParser.parse("##.ad"))); diff --git a/package.json b/package.json index 8203ab5e..77678f2f 100644 --- a/package.json +++ b/package.json @@ -23,31 +23,21 @@ }, "homepage": "https://github.com/AdguardTeam/AGLint#readme", "type": "module", - "main": "dist/index.cjs", - "module": "dist/index.mjs", - "exports": { - ".": { - "import": "./dist/index.mjs", - "require": "./dist/index.cjs" - }, - "./parser": { - "import": "./dist/parser/index.mjs", - "require": "./dist/parser/index.cjs" - } - }, - "typings": "dist/types", + "main": "dist/aglint.cjs", + "module": "dist/aglint.mjs", + "types": "dist/aglint.d.ts", "files": [ "dist", "src" ], "engines": { - "node": ">=12" + "node": ">=14" }, "scripts": { "lint": "eslint . --ext .ts", "test": "jest --runInBand .", "coverage": "jest --coverage", - "build": "yarn rimraf dist && yarn rollup --config rollup.config.ts --configPlugin typescript && tsc --declaration --emitDeclarationOnly --outdir dist/types" + "build": "yarn rimraf dist && tsc --declaration --emitDeclarationOnly --outdir dist/types && yarn rollup --config rollup.config.ts --configPlugin typescript && yarn rimraf dist/types" }, "devDependencies": { "@rollup/plugin-commonjs": "^23.0.2", diff --git a/rollup.config.ts b/rollup.config.ts index e3670378..048aef45 100644 --- a/rollup.config.ts +++ b/rollup.config.ts @@ -2,29 +2,35 @@ import typescript from "@rollup/plugin-typescript"; import resolve from "@rollup/plugin-node-resolve"; import commonjs from "@rollup/plugin-commonjs"; import externals from "rollup-plugin-node-externals"; +import dts from "rollup-plugin-dts"; const esm = { - input: "src/parser/index.ts", + input: "./src/index.ts", output: [ { - file: `dist/parser/index.mjs`, + file: `./dist/aglint.mjs`, format: "esm", sourcemap: false, }, ], plugins: [ externals(), - typescript({ declaration: false }), + typescript({ + declaration: false, + compilerOptions: { + moduleResolution: "node16", + }, + }), commonjs({ sourceMap: false }), resolve({ preferBuiltins: false }), ], }; const cjs = { - input: "src/parser/index.ts", + input: "./src/index.ts", output: [ { - file: `dist/parser/index.cjs`, + file: `./dist/aglint.cjs`, format: "cjs", exports: "auto", sourcemap: false, @@ -38,4 +44,12 @@ const cjs = { ], }; -export default [esm, cjs]; +export default [ + esm, + cjs, + { + input: "./dist/types/index.d.ts", + output: [{ file: "dist/aglint.d.ts", format: "es" }], + plugins: [dts()], + }, +]; diff --git a/src/parser/comment/agent.ts b/src/parser/comment/agent.ts index 61a5189b..da00de0b 100644 --- a/src/parser/comment/agent.ts +++ b/src/parser/comment/agent.ts @@ -1,8 +1,8 @@ import { AdblockSyntax } from "../../utils/adblockers"; import { SPACE } from "../../utils/constants"; import { StringUtils } from "../../utils/string"; -import { RuleCategories } from "../common"; -import { CommentMarker, CommentRuleType, IComment } from "./common"; +import { RuleCategory } from "../common"; +import { CommentMarker, CommentRuleType, Comment } from "./common"; const AGENT_LIST_OPEN = "["; const AGENT_LIST_CLOSE = "]"; @@ -12,7 +12,7 @@ const AGENT_SEPARATOR = ";"; * Represents an agent (eg `Adblock Plus 2.0`, where adblock is `Adblock Plus` and version is `2.0`). * Specifying the version is optional. */ -export interface IAgentMember { +export interface AgentMember { adblock: string; version?: string; } @@ -34,9 +34,9 @@ export interface IAgentMember { * [uBlock Origin] * ``` */ -export interface IAgent extends IComment { +export interface Agent extends Comment { type: CommentRuleType.Agent; - agents: IAgentMember[]; + agents: AgentMember[]; } /** @@ -59,8 +59,8 @@ export class AgentParser { /** * Determines whether a rule is an adblock agent. * - * @param {string} raw - Raw rule - * @returns {boolean} true/false + * @param raw - Raw rule + * @returns true/false */ public static isAgent(raw: string): boolean { const trimmed = raw.trim(); @@ -77,15 +77,15 @@ export class AgentParser { /** * Parses an adblock agent member. * - * @param {string} raw - Raw agent member, eg `Adblock Plus 2.0` - * @returns {IAgentMember} - Agent member AST + * @param raw - Raw agent member, eg `Adblock Plus 2.0` + * @returns - Agent member AST * @example * ```js * AgentParser.parseAgent('Adblock Plus 2.0'); * AgentParser.parseAgent('uBlock Origin 1.40.1'); * ``` */ - private static parseAgent(raw: string): IAgentMember { + private static parseAgent(raw: string): AgentMember { const trimmed = raw.trim(); const splitted = trimmed.split(SPACE); @@ -112,10 +112,10 @@ export class AgentParser { /** * Parses a raw rule as an adblock agent comment. * - * @param {string} raw - Raw rule - * @returns {IAgent | null} Adblock agent AST or null (if the raw rule cannot be parsed as an adblock agent comment) + * @param raw - Raw rule + * @returns Adblock agent AST or null (if the raw rule cannot be parsed as an adblock agent comment) */ - public static parse(raw: string): IAgent | null { + public static parse(raw: string): Agent | null { const trimmed = raw.trim(); // Check basic adblock agents pattern: [...], ![...], ! [...], #[...], etc. @@ -141,7 +141,7 @@ export class AgentParser { // Parse content between brackets if (openingBracketIndex != -1) { - const collectedAgents: IAgentMember[] = []; + const collectedAgents: AgentMember[] = []; const rawAgents = trimmed.slice(openingBracketIndex + 1, -1); const agents = rawAgents.split(AGENT_SEPARATOR); for (const agent of agents) { @@ -155,7 +155,7 @@ export class AgentParser { } return { - category: RuleCategories.Comment, + category: RuleCategory.Comment, type: CommentRuleType.Agent, syntax: AdblockSyntax.Unknown, agents: collectedAgents, @@ -168,10 +168,10 @@ export class AgentParser { /** * Converts an adblock agent AST to a string. * - * @param {IAgent} ast - Agent AST - * @returns {string} Raw string + * @param ast - Agent AST + * @returns Raw string */ - public static generate(ast: IAgent): string { + public static generate(ast: Agent): string { let result = AGENT_LIST_OPEN; result += ast.agents diff --git a/src/parser/comment/comment.ts b/src/parser/comment/comment.ts index afb152a8..0a5dbec3 100644 --- a/src/parser/comment/comment.ts +++ b/src/parser/comment/comment.ts @@ -1,13 +1,13 @@ import { StringUtils } from "../../utils/string"; import { CosmeticRuleSeparatorUtils } from "../../utils/cosmetic-rule-separator"; -import { CommentMarker, CommentRuleType, IComment } from "./common"; -import { AgentParser, IAgent } from "./agent"; -import { HintParser, IHint } from "./hint"; -import { IMetadata, MetadataParser } from "./metadata"; -import { IPreProcessor, PreProcessorParser } from "./preprocessor"; -import { RuleCategories } from "../common"; +import { CommentMarker, CommentRuleType, Comment } from "./common"; +import { AgentParser, Agent } from "./agent"; +import { HintParser, Hint } from "./hint"; +import { Metadata, MetadataParser } from "./metadata"; +import { PreProcessor, PreProcessorParser } from "./preprocessor"; +import { RuleCategory } from "../common"; import { AdblockSyntax } from "../../utils/adblockers"; -import { ConfigCommentParser, IConfigComment } from "./inline-config"; +import { ConfigCommentParser, ConfigComment } from "./inline-config"; /** * Represents a simple comment. @@ -21,7 +21,7 @@ import { ConfigCommentParser, IConfigComment } from "./inline-config"; * ``` * - etc. */ -export interface ISimpleComment extends IComment { +export interface SimpleComment extends Comment { type: CommentRuleType.Comment; /** Comment marker: `!` or `#` */ @@ -86,8 +86,8 @@ export class CommentParser { /** * Determines whether a rule is a regular comment. * - * @param {string} raw - Raw data - * @returns {boolean} true/false + * @param raw - Raw data + * @returns true/false */ public static isRegularComment(raw: string): boolean { return raw.trim()[0] == CommentMarker.Regular; @@ -96,8 +96,8 @@ export class CommentParser { /** * Determines whether a rule is a comment. * - * @param {string} raw - Raw rule - * @returns {boolean} true/false + * @param raw - Raw rule + * @returns true/false */ public static isComment(raw: string): boolean { const trimmed = raw.trim(); @@ -133,10 +133,10 @@ export class CommentParser { /** * Parses a raw rule as comment. * - * @param {string} raw - Raw rule - * @returns {IComment | null} Comment AST or null (if the raw rule cannot be parsed as comment) + * @param raw - Raw rule + * @returns Comment AST or null (if the raw rule cannot be parsed as comment) */ - public static parse(raw: string): IComment | null { + public static parse(raw: string): Comment | null { const trimmed = raw.trim(); if (!CommentParser.isComment(trimmed)) { @@ -149,8 +149,8 @@ export class CommentParser { PreProcessorParser.parse(trimmed) || MetadataParser.parse(trimmed) || ConfigCommentParser.parse(trimmed) || - { - category: RuleCategories.Comment, + { + category: RuleCategory.Comment, type: CommentRuleType.Comment, syntax: AdblockSyntax.Unknown, marker: trimmed[0], @@ -162,23 +162,23 @@ export class CommentParser { /** * Converts a comment AST to a string. * - * @param {IComment} ast - Comment AST - * @returns {string} Raw string + * @param ast - Comment AST + * @returns Raw string */ - public static generate(ast: IComment): string { + public static generate(ast: Comment): string { switch (ast.type) { case CommentRuleType.Agent: - return AgentParser.generate(ast); + return AgentParser.generate(ast); case CommentRuleType.Hint: - return HintParser.generate(ast); + return HintParser.generate(ast); case CommentRuleType.PreProcessor: - return PreProcessorParser.generate(ast); + return PreProcessorParser.generate(ast); case CommentRuleType.Metadata: - return MetadataParser.generate(ast); + return MetadataParser.generate(ast); case CommentRuleType.ConfigComment: - return ConfigCommentParser.generate(ast); + return ConfigCommentParser.generate(ast); case CommentRuleType.Comment: - return (ast).marker + (ast).text; + return (ast).marker + (ast).text; } } } diff --git a/src/parser/comment/common.ts b/src/parser/comment/common.ts index e13b76ce..b47859b3 100644 --- a/src/parser/comment/common.ts +++ b/src/parser/comment/common.ts @@ -1,4 +1,4 @@ -import { IRule, RuleCategories } from "../common"; +import { Rule, RuleCategory } from "../common"; /** Represents possible comment markers. */ export enum CommentMarker { @@ -16,60 +16,8 @@ export enum CommentRuleType { ConfigComment = "ConfigComment", } -/** - * Represents the basic comment rule interface. - * - * Example rules: - * - Agent comments: - * - ```adblock - * [AdGuard] - * ``` - * - ```adblock - * [Adblock Plus 2.0] - * ``` - * - etc. - * - AdGuard hints: - * - ```adblock - * !+ NOT_OPTIMIZED - * ``` - * - ```adblock - * !+ NOT_OPTIMIZED PLATFORM(windows) - * ``` - * - etc. - * - Pre-processor comments: - * - ```adblock - * !#if (adguard) - * ``` - * - ```adblock - * !#endif - * ``` - * - etc. - * - Metadata headers: - * - ```adblock - * ! Title: My List - * ``` - * - ```adblock - * ! Version: 2.0.150 - * ``` - * - etc. - * - AGLint inline config comments: - * - ```adblock - * ! aglint-enable some-rule - * ``` - * - ```adblock - * ! aglint-disable some-rule - * ``` - * - etc. - * - Simple comments: - * - ```adblock - * ! This is just a comment - * ``` - * - ```adblock - * # This is just a comment - * ``` - * - etc. - */ -export interface IComment extends IRule { - category: RuleCategories.Comment; +/** Represents the basic comment rule interface. */ +export interface Comment extends Rule { + category: RuleCategory.Comment; type: CommentRuleType; } diff --git a/src/parser/comment/hint.ts b/src/parser/comment/hint.ts index 4a2ef58d..130a03ee 100644 --- a/src/parser/comment/hint.ts +++ b/src/parser/comment/hint.ts @@ -7,11 +7,11 @@ import { AdblockSyntax } from "../../utils/adblockers"; import { EMPTY, SPACE, UNDERSCORE } from "../../utils/constants"; import { StringUtils } from "../../utils/string"; -import { RuleCategories } from "../common"; -import { CommentRuleType, IComment } from "./common"; +import { RuleCategory } from "../common"; +import { CommentRuleType, Comment } from "./common"; const HINT_MARKER = "!+"; -const HINT_MARKER_LEN = 2; +const HINT_MARKER_LEN = HINT_MARKER.length; const HINT_PARAMS_OPEN = "("; const HINT_PARAMS_CLOSE = ")"; const HINT_PARAMS_SEPARATOR = ","; @@ -23,7 +23,7 @@ const HINT_PARAMS_SEPARATOR = ","; * ``` * the name would be `PLATFORM` and the params would be `["windows", "mac"]`. */ -export interface IHintMember { +export interface HintMember { name: string; params: string[]; } @@ -37,11 +37,11 @@ export interface IHintMember { * ``` * then there are two hint members: `NOT_OPTIMIZED` and `PLATFORM`. */ -export interface IHint extends IComment { - category: RuleCategories.Comment; +export interface Hint extends Comment { + category: RuleCategory.Comment; type: CommentRuleType.Hint; - syntax: AdblockSyntax.AdGuard; - hints: IHintMember[]; + syntax: AdblockSyntax.Adg; + hints: HintMember[]; } /** @@ -53,8 +53,8 @@ export class HintParser { /** * Determines whether the rule is a hint rule. * - * @param {string} raw - Raw rule - * @returns {boolean} true/false + * @param raw - Raw rule + * @returns true/false */ public static isHint(raw: string): boolean { return raw.trim().startsWith(HINT_MARKER); @@ -63,19 +63,19 @@ export class HintParser { /** * Parses a raw rule as a hint comment. * - * @param {string} raw - Raw rule - * @returns {IHint | null} Hint AST or null (if the raw rule cannot be parsed as a hint comment) + * @param raw - Raw rule + * @returns Hint AST or null (if the raw rule cannot be parsed as a hint comment) * @throws If the input matches the HINT pattern but syntactically invalid * @see {@link https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#hints-1} */ - public static parse(raw: string): IHint | null { + public static parse(raw: string): Hint | null { const trimmed = raw.trim(); if (!HintParser.isHint(trimmed)) { return null; } - const collectedMembers: IHintMember[] = []; + const collectedMembers: HintMember[] = []; let openingBracketIndex = -1; let collectedHintName = EMPTY; @@ -169,9 +169,9 @@ export class HintParser { } return { - category: RuleCategories.Comment, + category: RuleCategory.Comment, type: CommentRuleType.Hint, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, hints: collectedMembers, }; } @@ -179,10 +179,10 @@ export class HintParser { /** * Converts a hint AST to a string. * - * @param {IHint} ast - Hint AST - * @returns {string} Raw string + * @param ast - Hint AST + * @returns Raw string */ - public static generate(ast: IHint): string { + public static generate(ast: Hint): string { let result = HINT_MARKER + SPACE; result += ast.hints diff --git a/src/parser/comment/inline-config.ts b/src/parser/comment/inline-config.ts index d653570c..00e6d7d9 100644 --- a/src/parser/comment/inline-config.ts +++ b/src/parser/comment/inline-config.ts @@ -6,8 +6,8 @@ import { AdblockSyntax } from "../../utils/adblockers"; import { COMMA, EMPTY, SPACE } from "../../utils/constants"; -import { RuleCategories } from "../common"; -import { CommentMarker, CommentRuleType, IComment } from "./common"; +import { RuleCategory } from "../common"; +import { CommentMarker, CommentRuleType, Comment } from "./common"; import JSON5 from "json5"; const AGLINT_COMMAND_PREFIX = "aglint"; @@ -23,8 +23,8 @@ const CONFIG_COMMENT_MARKER = "--"; * ``` * then the command is `aglint-disable` and its params is `["some-rule", "another-rule"]`. */ -export interface IConfigComment extends IComment { - category: RuleCategories.Comment; +export interface ConfigComment extends Comment { + category: RuleCategory.Comment; type: CommentRuleType.ConfigComment; /** Comment marker: `!` or `#` */ @@ -51,8 +51,8 @@ export class ConfigCommentParser { /** * Determines whether the rule is an inline configuration comment rule. * - * @param {string} raw - Raw rule - * @returns {boolean} true/false + * @param raw - Raw rule + * @returns true/false */ public static isConfigComment(raw: string): boolean { const trimmed = raw.trim(); @@ -79,11 +79,11 @@ export class ConfigCommentParser { /** * Parses a raw rule as an inline configuration comment. * - * @param {string} raw - Raw rule - * @returns {IConfigComment | null} + * @param raw - Raw rule + * @returns * Inline configuration comment AST or null (if the raw rule cannot be parsed as configuration comment) */ - public static parse(raw: string): IConfigComment | null { + public static parse(raw: string): ConfigComment | null { const trimmed = raw.trim(); if (!ConfigCommentParser.isConfigComment(trimmed)) { @@ -102,8 +102,8 @@ export class ConfigCommentParser { } // Prepare result - const result: IConfigComment = { - category: RuleCategories.Comment, + const result: ConfigComment = { + category: RuleCategory.Comment, type: CommentRuleType.ConfigComment, syntax: AdblockSyntax.Unknown, marker: raw[0] as CommentMarker, @@ -144,10 +144,10 @@ export class ConfigCommentParser { /** * Converts an inline configuration comment AST to a string. * - * @param {IConfigComment} ast - Inline configuration comment AST - * @returns {string} Raw string + * @param ast - Inline configuration comment AST + * @returns Raw string */ - public static generate(ast: IConfigComment): string { + public static generate(ast: ConfigComment): string { let result = EMPTY; result += ast.marker; diff --git a/src/parser/comment/metadata.ts b/src/parser/comment/metadata.ts index deaa4fd2..affed8a4 100644 --- a/src/parser/comment/metadata.ts +++ b/src/parser/comment/metadata.ts @@ -5,8 +5,8 @@ import { METADATA_HEADERS } from "../../converter/metadata"; import { AdblockSyntax } from "../../utils/adblockers"; import { SPACE } from "../../utils/constants"; -import { RuleCategories } from "../common"; -import { CommentMarker, CommentRuleType, IComment } from "./common"; +import { RuleCategory } from "../common"; +import { CommentMarker, CommentRuleType, Comment } from "./common"; const METADATA_SEPARATOR = ":"; @@ -20,8 +20,8 @@ const METADATA_SEPARATOR = ":"; * ``` * the name of the header is `Title`, and the value is `My List`. */ -export interface IMetadata extends IComment { - category: RuleCategories.Comment; +export interface Metadata extends Comment { + category: RuleCategory.Comment; type: CommentRuleType.Metadata; marker: string; header: string; @@ -37,10 +37,10 @@ export class MetadataParser { /** * Parses a raw rule as a metadata comment. * - * @param {string} raw - Raw rule - * @returns {IMetadata | null} Metadata comment AST or null (if the raw rule cannot be parsed as a metadata comment) + * @param raw - Raw rule + * @returns Metadata comment AST or null (if the raw rule cannot be parsed as a metadata comment) */ - public static parse(raw: string): IMetadata | null { + public static parse(raw: string): Metadata | null { const trimmed = raw.trim(); if (trimmed[0] == CommentMarker.Regular || trimmed[0] == CommentMarker.Hashmark) { @@ -53,7 +53,7 @@ export class MetadataParser { for (let i = 0; i < METADATA_HEADERS.length; i++) { if (headerLower === METADATA_HEADERS[i].toLocaleLowerCase()) { return { - category: RuleCategories.Comment, + category: RuleCategory.Comment, type: CommentRuleType.Metadata, syntax: AdblockSyntax.Unknown, marker: trimmed[0], @@ -71,10 +71,10 @@ export class MetadataParser { /** * Converts a metadata comment AST to a string. * - * @param {IMetadata} ast - Metadata comment AST - * @returns {string} Raw string + * @param ast - Metadata comment AST + * @returns Raw string */ - public static generate(ast: IMetadata): string { + public static generate(ast: Metadata): string { let result = ast.marker + SPACE; result += ast.header; diff --git a/src/parser/comment/preprocessor.ts b/src/parser/comment/preprocessor.ts index d1313be8..b34f9297 100644 --- a/src/parser/comment/preprocessor.ts +++ b/src/parser/comment/preprocessor.ts @@ -8,11 +8,11 @@ import { AdblockSyntax } from "../../utils/adblockers"; import { EMPTY, HASHMARK } from "../../utils/constants"; import { StringUtils } from "../../utils/string"; -import { RuleCategories } from "../common"; -import { CommentRuleType, IComment } from "./common"; +import { RuleCategory } from "../common"; +import { CommentRuleType, Comment } from "./common"; const PREPROCESSOR_MARKER = "!#"; -const PREPROCESSOR_MARKER_LEN = 2; +const PREPROCESSOR_MARKER_LEN = PREPROCESSOR_MARKER.length; const PREPROCESSOR_SEPARATOR = " "; /** @@ -27,8 +27,8 @@ const PREPROCESSOR_SEPARATOR = " "; * In such a case, the parameters must be submitted for further parsing and validation, as this parser only handles * the general syntax. */ -export interface IPreProcessor extends IComment { - category: RuleCategories.Comment; +export interface PreProcessor extends Comment { + category: RuleCategory.Comment; type: CommentRuleType.PreProcessor; /** Name of the directive */ @@ -56,8 +56,8 @@ export class PreProcessorParser { /** * Determines whether the rule is a pre-processor rule. * - * @param {string} raw - Raw rule - * @returns {boolean} true/false + * @param raw - Raw rule + * @returns true/false */ public static isPreProcessor(raw: string): boolean { const trimmed = raw.trim(); @@ -69,19 +69,19 @@ export class PreProcessorParser { /** * Parses a raw rule as a pre-processor comment. * - * @param {string} raw - Raw rule - * @returns {IPreProcessor | null} + * @param raw - Raw rule + * @returns * Pre-processor comment AST or null (if the raw rule cannot be parsed as a pre-processor comment) */ - public static parse(raw: string): IPreProcessor | null { + public static parse(raw: string): PreProcessor | null { const trimmed = raw.trim(); if (!PreProcessorParser.isPreProcessor(trimmed)) { return null; } - const result: IPreProcessor = { - category: RuleCategories.Comment, + const result: PreProcessor = { + category: RuleCategory.Comment, type: CommentRuleType.PreProcessor, syntax: AdblockSyntax.Unknown, name: EMPTY, @@ -106,10 +106,10 @@ export class PreProcessorParser { /** * Converts a pre-processor comment AST to a string. * - * @param {IPreProcessor} ast - Pre-processor comment AST - * @returns {string} Raw string + * @param ast - Pre-processor comment AST + * @returns Raw string */ - public static generate(ast: IPreProcessor): string { + public static generate(ast: PreProcessor): string { let result = PREPROCESSOR_MARKER; result += ast.name; diff --git a/src/parser/common.ts b/src/parser/common.ts index 3e18159e..7206737b 100644 --- a/src/parser/common.ts +++ b/src/parser/common.ts @@ -4,7 +4,7 @@ import { AdblockSyntax } from "../utils/adblockers"; * Represents the main categories that an adblock rule can belong to. * Of course, these include additional subcategories. */ -export enum RuleCategories { +export enum RuleCategory { Comment = "Comment", Cosmetic = "Cosmetic", Network = "Network", @@ -14,8 +14,8 @@ export enum RuleCategories { * Specifies the general structure of a rule. This information must * be included in all rules, regardless of category. */ -export interface IRule { +export interface Rule { syntax: AdblockSyntax; - category: RuleCategories; + category: RuleCategory; type: string; } diff --git a/src/parser/common/domain-list.ts b/src/parser/common/domain-list.ts index 99a58694..bad6cc0f 100644 --- a/src/parser/common/domain-list.ts +++ b/src/parser/common/domain-list.ts @@ -27,15 +27,15 @@ export const DOMAIN_LIST_TYPE = "DomainList"; export type DomainListSeparator = typeof CLASSIC_DOMAIN_SEPARATOR | typeof MODIFIER_DOMAIN_SEPARATOR; /** Represents a list of domains, e.g. `example.com,~example.net`. */ -export interface IDomainList { +export interface DomainList { // Basically, the idea is that each main AST part should have a type type: typeof DOMAIN_LIST_TYPE; separator: DomainListSeparator; - domains: IDomain[]; + domains: Domain[]; } /** Represents an element of the domain list (a domain). */ -export interface IDomain { +export interface Domain { /** Domain name */ domain: string; @@ -52,13 +52,13 @@ export class DomainListParser { /** * Parses a domain list, eg. `example.com,example.org,~example.org` * - * @param {string} raw - Raw domain list - * @param {DomainListSeparator} separator - Separator character - * @returns {IDomainList} Domain list AST + * @param raw - Raw domain list + * @param separator - Separator character + * @returns Domain list AST * @throws If the domain list is syntactically invalid */ - public static parse(raw: string, separator: DomainListSeparator = CLASSIC_DOMAIN_SEPARATOR): IDomainList { - const domains: IDomain[] = []; + public static parse(raw: string, separator: DomainListSeparator = CLASSIC_DOMAIN_SEPARATOR): DomainList { + const domains: Domain[] = []; const rawDomains = raw.trim().split(separator); for (const rawDomain of rawDomains) { @@ -94,10 +94,10 @@ export class DomainListParser { /** * Converts a domain list AST to a string. * - * @param {IDomainList} ast - Domain list AST - * @returns {string} Raw string + * @param ast - Domain list AST + * @returns Raw string */ - public static generate(ast: IDomainList): string { + public static generate(ast: DomainList): string { const result = ast.domains .map(({ domain, exception }) => { let subresult = EMPTY; diff --git a/src/parser/common/modifier-list.ts b/src/parser/common/modifier-list.ts index 907946ac..a000cedd 100644 --- a/src/parser/common/modifier-list.ts +++ b/src/parser/common/modifier-list.ts @@ -7,10 +7,10 @@ const MODIFIER_ASSIGN_OPERATOR = "="; export const MODIFIER_LIST_TYPE = "ModifierList"; /** Represents a modifier list, eg `script,domain=example.com`. */ -export interface IModifierList { +export interface ModifierList { // Basically, the idea is that each main AST part should have a type type: typeof MODIFIER_LIST_TYPE; - modifiers: IRuleModifier[]; + modifiers: RuleModifier[]; } /** @@ -22,7 +22,7 @@ export interface IModifierList { * But if the modifier is `domain=example.com`, then the modifier property will be * `domain` and the value property will be `example.com`. */ -export interface IRuleModifier { +export interface RuleModifier { /** Modifier name */ modifier: string; @@ -42,16 +42,16 @@ export class ModifierListParser { /** * Parses the cosmetic rule modifiers, eg. `script,key=value` * - * @param {string} rawModifiers - Raw modifiers - * @returns {IRuleModifier} Parsed modifiers interface + * @param raw - Raw modifiers + * @returns Parsed modifiers interface */ - public static parse(rawModifiers: string): IModifierList { - const result: IModifierList = { + public static parse(raw: string): ModifierList { + const result: ModifierList = { type: MODIFIER_LIST_TYPE, modifiers: [], }; - const rawModifiersSplitted = StringUtils.splitStringByUnescapedCharacter(rawModifiers, MODIFIERS_SEPARATOR); + const rawModifiersSplitted = StringUtils.splitStringByUnescapedCharacter(raw, MODIFIERS_SEPARATOR); // Skip empty modifiers if (rawModifiersSplitted.length == 1 && rawModifiersSplitted[0].trim() == EMPTY) { @@ -84,10 +84,10 @@ export class ModifierListParser { /** * Converts a modifier list AST to a string. * - * @param {IModifierList} ast - Modifier list AST - * @returns {string} Raw string + * @param ast - Modifier list AST + * @returns Raw string */ - public static generate(ast: IModifierList): string { + public static generate(ast: ModifierList): string { const result = ast.modifiers .map(({ modifier, value }) => { let subresult = modifier.trim(); diff --git a/src/parser/cosmetic/body/css.ts b/src/parser/cosmetic/body/css.ts index ef3e1044..a0132725 100644 --- a/src/parser/cosmetic/body/css.ts +++ b/src/parser/cosmetic/body/css.ts @@ -5,10 +5,13 @@ import { Selector, SelectorList, MediaQueryList, Block, Rule, generate as generateCss } from "css-tree"; import { AdblockSyntax } from "../../../utils/adblockers"; import { + CSS_BLOCK_CLOSE, + CSS_BLOCK_OPEN, CSS_MEDIA_MARKER, CSS_PSEUDO_CLOSE, CSS_PSEUDO_MARKER, CSS_PSEUDO_OPEN, + CSS_SELECTORS_SEPARATOR, EMPTY, SPACE, } from "../../../utils/constants"; @@ -29,9 +32,6 @@ const UBO_CSS_INJECTION_PATTERN = const ADG_CSS_INJECTION_PATTERN = /^(?:.+){(?:.+)}$/; const REMOVE_BLOCK = "{ remove: true; }"; -const CSS_SELECTORS_SEPARATOR = ","; -const CSS_BLOCK_OPEN = "{"; -const CSS_BLOCK_CLOSE = "}"; const UBO_STYLE = "style"; const UBO_REMOVE = "remove"; const UBO_STYLE_MARKER = CSS_PSEUDO_MARKER + UBO_STYLE + CSS_PSEUDO_OPEN; @@ -47,7 +47,7 @@ export const REMOVE_BLOCK_TYPE = "remove"; export type CssInjectionBlock = Block | typeof REMOVE_BLOCK_TYPE; /** Represents a CSS injection body. */ -export interface ICssRuleBody { +export interface CssRuleBody { mediaQueryList?: MediaQueryList; selectors: Selector[]; block?: CssInjectionBlock; @@ -88,10 +88,10 @@ export class CssInjectionBodyParser { /** * Checks if a selector is a uBlock CSS injection. * - * @param {string} raw - Raw selector body - * @returns {boolean} true/false + * @param raw - Raw selector body + * @returns true/false */ - public static isUblockCssInjection(raw: string): boolean { + public static isUboCssInjection(raw: string): boolean { const trimmed = raw.trim(); // Since it has to run for every elementhide rule, the regex would be slow, @@ -106,18 +106,18 @@ export class CssInjectionBodyParser { /** * Checks if a selector is an AdGuard CSS injection. * - * @param {string} raw - Raw selector body - * @returns {boolean} true/false + * @param raw - Raw selector body + * @returns true/false */ - public static isAdGuardCssInjection(raw: string) { + public static isAdgCssInjection(raw: string) { return ADG_CSS_INJECTION_PATTERN.test(raw.trim()); } /** * Parses a raw selector as an AdGuard CSS injection. * - * @param {string} raw - Raw rule - * @returns {ICssRuleBody | null} CSS injection AST or null (if the raw rule cannot be parsed + * @param raw - Raw rule + * @returns CSS injection AST or null (if the raw rule cannot be parsed * as AdGuard CSS injection) * @throws * - If the selector is invalid according to the CSS syntax @@ -125,11 +125,11 @@ export class CssInjectionBodyParser { * - If several remove properties have been declared * - If there are other declarations in addition to the remove property */ - public static parseAdGuardCssInjection(raw: string): ICssRuleBody | null { + public static parseAdgCssInjection(raw: string): CssRuleBody | null { const trimmed = raw.trim(); // Check pattern first - if (!CssInjectionBodyParser.isAdGuardCssInjection(trimmed)) { + if (!CssInjectionBodyParser.isAdgCssInjection(trimmed)) { return null; } @@ -202,22 +202,22 @@ export class CssInjectionBodyParser { /** * Parses a raw selector as a uBlock CSS injection. * - * @param {string} raw - Raw rule - * @returns {ICssRuleBody | null} CSS injection AST or null (if the raw rule cannot be parsed + * @param raw - Raw rule + * @returns CSS injection AST or null (if the raw rule cannot be parsed * as uBlock CSS injection) */ - public static parseUblockCssInjection(raw: string): ICssRuleBody | null { + public static parseUboCssInjection(raw: string): CssRuleBody | null { const trimmed = raw.trim(); // Check pattern first - const uBlockCssInjection = trimmed.match(UBO_CSS_INJECTION_PATTERN); - if (!(uBlockCssInjection && uBlockCssInjection.groups)) { + const uboCssInjection = trimmed.match(UBO_CSS_INJECTION_PATTERN); + if (!(uboCssInjection && uboCssInjection.groups)) { return null; } const selectors: Selector[] = []; - const rawSelectorList = uBlockCssInjection.groups.selectors.trim(); + const rawSelectorList = uboCssInjection.groups.selectors.trim(); const selectorListAst = CssTree.parse(rawSelectorList, CssTreeParserContext.selectorList); selectorListAst.children.forEach((node) => { @@ -233,8 +233,8 @@ export class CssInjectionBodyParser { let block: CssInjectionBlock = REMOVE_BLOCK_TYPE; - if (uBlockCssInjection.groups.declarations) { - const rawDeclarations = uBlockCssInjection.groups.declarations.trim(); + if (uboCssInjection.groups.declarations) { + const rawDeclarations = uboCssInjection.groups.declarations.trim(); // Hack: CSS parser waits for `{declarations}` pattern, so we need { and } chars: block = CssTree.parse(`{${rawDeclarations}}`, CssTreeParserContext.block); @@ -249,38 +249,38 @@ export class CssInjectionBodyParser { /** * Parses a raw selector as a CSS injection. It determines the syntax automatically. * - * @param {string} raw - Raw rule - * @returns {ICssRuleBody | null} CSS injection AST or null (if the raw rule cannot be parsed + * @param raw - Raw rule + * @returns CSS injection AST or null (if the raw rule cannot be parsed * as CSS injection) */ - public static parse(raw: string): ICssRuleBody | null { + public static parse(raw: string): CssRuleBody | null { const trimmed = raw.trim(); // AdGuard CSS injection - const result = CssInjectionBodyParser.parseAdGuardCssInjection(trimmed); + const result = CssInjectionBodyParser.parseAdgCssInjection(trimmed); if (result) { return result; } // uBlock CSS injection - return CssInjectionBodyParser.parseUblockCssInjection(trimmed); + return CssInjectionBodyParser.parseUboCssInjection(trimmed); } /** * Converts a CSS injection AST to a string. * - * @param {ICssRuleBody} ast - CSS injection rule body AST - * @param {AdblockSyntax} syntax - Desired syntax of the generated result - * @returns {string} Raw string + * @param ast - CSS injection rule body AST + * @param syntax - Desired syntax of the generated result + * @returns Raw string * @throws * - If you generate a media query with uBlock syntax * - If you enter unsupported syntax */ - public static generate(ast: ICssRuleBody, syntax: AdblockSyntax): string { + public static generate(ast: CssRuleBody, syntax: AdblockSyntax): string { let result = EMPTY; switch (syntax) { - case AdblockSyntax.AdGuard: { + case AdblockSyntax.Adg: { if (ast.mediaQueryList) { result += CSS_MEDIA_MARKER; result += SPACE; @@ -317,7 +317,7 @@ export class CssInjectionBodyParser { break; } - case AdblockSyntax.uBlockOrigin: { + case AdblockSyntax.Ubo: { if (ast.mediaQueryList !== undefined) { throw new SyntaxError("uBlock doesn't support media queries"); } diff --git a/src/parser/cosmetic/body/elementhiding.ts b/src/parser/cosmetic/body/elementhiding.ts index 47a3456f..e3230144 100644 --- a/src/parser/cosmetic/body/elementhiding.ts +++ b/src/parser/cosmetic/body/elementhiding.ts @@ -3,18 +3,16 @@ */ import { Selector, SelectorList } from "css-tree"; -import { SPACE } from "../../../utils/constants"; +import { CSS_SELECTORS_SEPARATOR, SPACE } from "../../../utils/constants"; import { CssTree } from "../../../utils/csstree"; import { CssTreeNodeType, CssTreeParserContext } from "../../../utils/csstree-constants"; import { StringUtils } from "../../../utils/string"; -const CSS_SELECTORS_SEPARATOR = ","; - /** * Represents an element hiding rule body. There can even be several selectors in a rule, * but the best practice is to place the selectors in separate rules. */ -export interface IElementHidingRuleBody { +export interface ElementHidingRuleBody { selectors: Selector[]; } @@ -38,12 +36,12 @@ export class ElementHidingBodyParser { /** * Parses a raw cosmetic rule body as an element hiding rule body. * - * @param {string} raw - Raw body - * @returns {IElementHidingRuleBody | null} Element hiding rule body AST + * @param raw - Raw body + * @returns Element hiding rule body AST * @throws * - If the selector is invalid according to the CSS syntax */ - public static parse(raw: string): IElementHidingRuleBody { + public static parse(raw: string): ElementHidingRuleBody { const trimmed = raw.trim(); const selectors: Selector[] = []; @@ -71,10 +69,10 @@ export class ElementHidingBodyParser { /** * Converts an element hiding rule body AST to a string. * - * @param {IElementHidingRuleBody} ast - Element hiding rule body AST - * @returns {string} Raw string + * @param ast - Element hiding rule body AST + * @returns Raw string */ - public static generate(ast: IElementHidingRuleBody): string { + public static generate(ast: ElementHidingRuleBody): string { return ast.selectors .map((selector) => CssTree.generateSelector(selector)) .join(CSS_SELECTORS_SEPARATOR + SPACE); diff --git a/src/parser/cosmetic/body/html.ts b/src/parser/cosmetic/body/html.ts index 638c853a..66a2742e 100644 --- a/src/parser/cosmetic/body/html.ts +++ b/src/parser/cosmetic/body/html.ts @@ -4,15 +4,13 @@ import { Selector, SelectorList } from "css-tree"; import { AdblockSyntax } from "../../../utils/adblockers"; -import { EMPTY, ESCAPE_CHARACTER, SPACE } from "../../../utils/constants"; +import { CSS_SELECTORS_SEPARATOR, EMPTY, ESCAPE_CHARACTER, SPACE } from "../../../utils/constants"; import { CssTree } from "../../../utils/csstree"; import { CssTreeNodeType, CssTreeParserContext } from "../../../utils/csstree-constants"; import { DOUBLE_QUOTE_MARKER, StringUtils } from "../../../utils/string"; -const CSS_SELECTORS_SEPARATOR = ","; - /**Represents an HTML filtering rule body. */ -export interface IHtmlRuleBody { +export interface HtmlRuleBody { selectors: Selector[]; } @@ -30,8 +28,8 @@ export class HtmlBodyParser { /** * Convert "" to \" within strings, because CSSTree does not recognize "". * - * @param {string} selector - CSS selector string - * @returns {string} Escaped CSS selector + * @param selector - CSS selector string + * @returns Escaped CSS selector * @throws * - If the selector is invalid according to the CSS syntax * @see {@link https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#tag-content} @@ -61,8 +59,8 @@ export class HtmlBodyParser { /** * Convert \" to "" within strings. * - * @param {string} selector - CSS selector string - * @returns {string} Unescaped CSS selector + * @param selector - CSS selector string + * @returns Unescaped CSS selector * @see {@link https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#tag-content} */ public static unescapeDoubleQuotes(selector: string): string { @@ -87,10 +85,10 @@ export class HtmlBodyParser { * Parses a raw cosmetic rule body as an HTML filtering rule body. * Please note that compatibility is not yet checked at this point. * - * @param {string} raw - Raw body - * @returns {IHtmlRuleBody | null} HTML filtering rule body AST + * @param raw - Raw body + * @returns HTML filtering rule body AST */ - public static parse(raw: string): IHtmlRuleBody { + public static parse(raw: string): HtmlRuleBody { const trimmed = raw.trim(); const selectors: Selector[] = []; @@ -121,17 +119,17 @@ export class HtmlBodyParser { /** * Converts an HTML filtering rule body AST to a string. * - * @param {IHtmlRuleBody} ast - HTML filtering rule body AST - * @param {AdblockSyntax} syntax - Desired syntax of the generated result - * @returns {string} Raw string + * @param ast - HTML filtering rule body AST + * @param syntax - Desired syntax of the generated result + * @returns Raw string */ - public static generate(ast: IHtmlRuleBody, syntax: AdblockSyntax): string { + public static generate(ast: HtmlRuleBody, syntax: AdblockSyntax): string { let result = ast.selectors .map((selector) => CssTree.generateSelector(selector)) .join(CSS_SELECTORS_SEPARATOR + SPACE); // In the case of AdGuard syntax, the "" case must be handled - if (syntax == AdblockSyntax.AdGuard) { + if (syntax == AdblockSyntax.Adg) { result = HtmlBodyParser.unescapeDoubleQuotes(result); } diff --git a/src/parser/cosmetic/body/scriptlet.ts b/src/parser/cosmetic/body/scriptlet.ts index 6d580db1..a8322fda 100644 --- a/src/parser/cosmetic/body/scriptlet.ts +++ b/src/parser/cosmetic/body/scriptlet.ts @@ -18,18 +18,18 @@ const ABP_PARAM_SEPARATOR = " "; * Adblock Plus supports multiple scriptlets within a rule, so the data structure represents the * scriptlets as an array. AdGuard and uBlock Origin ONLY support one scriptlet per rule. */ -export interface IScriptletRuleBody { - scriptlets: IScriptlet[]; +export interface ScriptletRuleBody { + scriptlets: Scriptlet[]; } /** Represents a specific scriptlet and its parameters. */ -export interface IScriptlet { - scriptlet: IScriptletParameter; - parameters?: IScriptletParameter[]; +export interface Scriptlet { + scriptlet: ScriptletParameter; + parameters?: ScriptletParameter[]; } /** Represents a scriptlet parameter. */ -export interface IScriptletParameter { +export interface ScriptletParameter { type: ScriptletParameterType; value: string; } @@ -73,11 +73,11 @@ export class ScriptletBodyParser { /** * Parses a raw ADG/uBO scriptlet call body. * - * @param {string} raw - Raw body - * @returns {IScriptletRuleBody} Scriptlet rule body AST + * @param raw - Raw body + * @returns Scriptlet rule body AST * @throws If there is no opening/closing parenthesis */ - public static parseAdgAndUboScriptletCall(raw: string): IScriptletRuleBody { + public static parseAdgAndUboScriptletCall(raw: string): ScriptletRuleBody { const trimmed = raw.trim(); // Call should contain: (arg0, arg1,...) @@ -122,12 +122,12 @@ export class ScriptletBodyParser { /** * Parses a raw ABP snippet call body. * - * @param {string} raw - Raw body - * @returns {IScriptletRuleBody | null} Scriptlet rule body AST + * @param raw - Raw body + * @returns Scriptlet rule body AST * @throws If no scriptlet is specified */ - public static parseAbpSnippetCall(raw: string): IScriptletRuleBody { - const scriptlets: IScriptlet[] = []; + public static parseAbpSnippetCall(raw: string): ScriptletRuleBody { + const scriptlets: Scriptlet[] = []; let trimmed = raw.trim(); @@ -169,10 +169,10 @@ export class ScriptletBodyParser { /** * Converts an array of strings into an array of parameter interfaces. * - * @param {string[]} params - Parameter list as array of strings - * @returns {IScriptletParameter[]} Parameter list as array of parameter interfaces + * @param params - Parameter list as array of strings + * @returns Parameter list as array of parameter interfaces */ - private static decodeParameters(params: string[]): IScriptletParameter[] { + private static decodeParameters(params: string[]): ScriptletParameter[] { return params.map((param) => { let type: ScriptletParameterType = ScriptletParameterType.Unquoted; @@ -196,10 +196,10 @@ export class ScriptletBodyParser { /** * Converts an array of parameter interfaces into an array of strings. * - * @param {IScriptletParameter[]} params - Parameter list as array of parameter interfaces - * @returns {string[]} Parameter list as array of strings + * @param params - Parameter list as array of parameter interfaces + * @returns Parameter list as array of strings */ - private static encodeParameters(params: IScriptletParameter[]): string[] { + private static encodeParameters(params: ScriptletParameter[]): string[] { return params.map(({ value, type }) => { switch (type) { case ScriptletParameterType.SingleQuoted: @@ -227,10 +227,10 @@ export class ScriptletBodyParser { /** * Parses a raw cosmetic rule body as a scriptlet injection rule body. * - * @param {string} raw - Raw body - * @returns {IScriptletRuleBody | null} Scriptlet injection rule body AST + * @param raw - Raw body + * @returns Scriptlet injection rule body AST */ - public static parse(raw: string): IScriptletRuleBody { + public static parse(raw: string): ScriptletRuleBody { const trimmed = raw.trim(); // ADG and uBO calls always begins with parenthesis @@ -244,16 +244,16 @@ export class ScriptletBodyParser { /** * Converts a scriptlet injection rule body AST to a string. * - * @param {IScriptletRuleBody} ast - Scriptlet injection rule body AST - * @param {AdblockSyntax} syntax - Desired syntax of the generated result - * @returns {string} Raw string + * @param ast - Scriptlet injection rule body AST + * @param syntax - Desired syntax of the generated result + * @returns Raw string */ - public static generate(ast: IScriptletRuleBody, syntax: AdblockSyntax): string[] { + public static generate(ast: ScriptletRuleBody, syntax: AdblockSyntax): string[] { const scriptlets = ast.scriptlets.map(({ scriptlet, parameters }) => { return ScriptletBodyParser.encodeParameters([scriptlet, ...(parameters || [])]); }); - if (syntax == AdblockSyntax.AdGuard || syntax == AdblockSyntax.uBlockOrigin) { + if (syntax == AdblockSyntax.Adg || syntax == AdblockSyntax.Ubo) { return scriptlets.map((scriptlet) => `(${scriptlet.join(ADG_UBO_PARAM_SEPARATOR + SPACE)})`); } diff --git a/src/parser/cosmetic/cosmetic.ts b/src/parser/cosmetic/cosmetic.ts index 4c3db475..90c2cd2a 100644 --- a/src/parser/cosmetic/cosmetic.ts +++ b/src/parser/cosmetic/cosmetic.ts @@ -1,4 +1,4 @@ -import { IRule, RuleCategories } from "../common"; +import { Rule, RuleCategory } from "../common"; // Utils import { CosmeticRuleSeparator, CosmeticRuleSeparatorUtils } from "../../utils/cosmetic-rule-separator"; @@ -6,14 +6,14 @@ import { AdblockSyntax } from "../../utils/adblockers"; // Parsers import { CommentParser } from "../comment/comment"; -import { CssInjectionBodyParser, ICssRuleBody } from "./body/css"; -import { ElementHidingBodyParser, IElementHidingRuleBody } from "./body/elementhiding"; -import { ScriptletBodyParser, IScriptletRuleBody } from "./body/scriptlet"; -import { HtmlBodyParser, IHtmlRuleBody } from "./body/html"; -import { DomainListParser, DOMAIN_LIST_TYPE, IDomain } from "../common/domain-list"; -import { AdGuardModifierListParser, ADG_MODIFIER_LIST_TYPE } from "./specific/adg-options"; -import { IRuleModifier } from "../common/modifier-list"; -import { UblockModifier, UBlockModifierListParser, UBO_MODIFIER_LIST_TYPE } from "./specific/ubo-options"; +import { CssInjectionBodyParser, CssRuleBody } from "./body/css"; +import { ElementHidingBodyParser, ElementHidingRuleBody } from "./body/elementhiding"; +import { ScriptletBodyParser, ScriptletRuleBody } from "./body/scriptlet"; +import { HtmlBodyParser, HtmlRuleBody } from "./body/html"; +import { DomainListParser, DOMAIN_LIST_TYPE, Domain } from "../common/domain-list"; +import { AdgModifierListParser, ADG_MODIFIER_LIST_TYPE } from "./specific/adg-modifiers"; +import { RuleModifier } from "../common/modifier-list"; +import { UboModifier, UboModifierListParser, UBO_MODIFIER_LIST_TYPE } from "./specific/ubo-modifiers"; import { CosmeticRuleType } from "./common"; import { COMMA, EMPTY, NEWLINE, SEMICOLON, SPACE } from "../../utils/constants"; import { UBO_RESPONSEHEADER_INDICATOR } from "../network/network"; @@ -24,11 +24,11 @@ import { UBO_RESPONSEHEADER_INDICATOR } from "../network/network"; * Regarding the categories, there is only a difference in the body, * all other properties can be defined at this level. */ -export interface ICosmeticRule extends IRule { - category: RuleCategories.Cosmetic; +export interface CosmeticRule extends Rule { + category: RuleCategory.Cosmetic; type: CosmeticRuleType; - modifiers: IRuleModifier[] | UblockModifier[]; - domains: IDomain[]; + modifiers: RuleModifier[] | UboModifier[]; + domains: Domain[]; separator: CosmeticRuleSeparator; exception: boolean; body: unknown; @@ -51,9 +51,9 @@ export interface ICosmeticRule extends IRule { * example.com#@?#.ads:has(> .something) * ``` */ -export interface IElementHidingRule extends ICosmeticRule { +export interface ElementHidingRule extends CosmeticRule { type: CosmeticRuleType.ElementHidingRule; - body: IElementHidingRuleBody; + body: ElementHidingRuleBody; } /** @@ -81,9 +81,9 @@ export interface IElementHidingRule extends ICosmeticRule { * example.com##.ads:remove() * ``` */ -export interface ICssRule extends ICosmeticRule { +export interface CssRule extends CosmeticRule { type: CosmeticRuleType.CssRule; - body: ICssRuleBody; + body: CssRuleBody; } /** @@ -116,9 +116,9 @@ export interface ICssRule extends ICosmeticRule { * example.com#$#scriptlet0 arg00 arg01; scriptlet1 arg10 arg11 * ``` */ -export interface IScriptletRule extends ICosmeticRule { +export interface ScriptletRule extends CosmeticRule { type: CosmeticRuleType.ScriptletRule; - body: IScriptletRuleBody; + body: ScriptletRuleBody; } /** @@ -140,9 +140,9 @@ export interface IScriptletRule extends ICosmeticRule { * example.com#@#^script:has-text(detect) * ``` */ -export interface IHtmlRule extends ICosmeticRule { +export interface HtmlRule extends CosmeticRule { type: CosmeticRuleType.HtmlRule; - body: IHtmlRuleBody; + body: HtmlRuleBody; } /** @@ -156,7 +156,7 @@ export interface IHtmlRule extends ICosmeticRule { * example.com#@%#let a = 2; * ``` */ -export interface IJsRule extends ICosmeticRule { +export interface JsRule extends CosmeticRule { type: CosmeticRuleType.JsRule; body: string; } @@ -179,8 +179,8 @@ export class CosmeticRuleParser { /** * Determines whether a rule is a cosmetic rule. * - * @param {string} raw - Raw rule - * @returns {boolean} true/false + * @param raw - Raw rule + * @returns true/false */ public static isCosmetic(raw: string) { const trimmed = raw.trim(); @@ -200,44 +200,44 @@ export class CosmeticRuleParser { * - separator * - body * - * @param {string} rawRule - Raw cosmetic rule - * @returns {IElementHidingRule | ICssRule | IScriptletRule | IHtmlRule | IJsRule | null} + * @param raw - Raw cosmetic rule + * @returns * Parsed cosmetic rule AST or null if it failed to parse based on the known cosmetic rules * @throws If the input matches the cosmetic rule pattern but syntactically invalid */ - public static parse(rawRule: string): IElementHidingRule | ICssRule | IScriptletRule | IHtmlRule | IJsRule | null { + public static parse(raw: string): ElementHidingRule | CssRule | ScriptletRule | HtmlRule | JsRule | null { // Skip regular comments - if (CommentParser.isRegularComment(rawRule)) { + if (CommentParser.isRegularComment(raw)) { return null; } // Find separator (every cosmetic rule has a separator) - const [start, end, separator, exception] = CosmeticRuleSeparatorUtils.find(rawRule); + const [start, end, separator, exception] = CosmeticRuleSeparatorUtils.find(raw); if (!separator) { return null; } // Find main structural elements - let rawPattern = rawRule.substring(0, start).trim(); - let rawBody = rawRule.substring(end).trim(); + let rawPattern = raw.substring(0, start).trim(); + let rawBody = raw.substring(end).trim(); // The syntax is initially unknown let syntax = AdblockSyntax.Unknown; - const modifiers: IRuleModifier[] = []; + const modifiers: RuleModifier[] = []; // Handle pattern - const { modifiers: adgModifiers, rest: adgRest } = AdGuardModifierListParser.parse(rawPattern); + const { modifiers: adgModifiers, rest: adgRest } = AdgModifierListParser.parse(rawPattern); if (adgModifiers.length > 0) { modifiers.push(...adgModifiers); - syntax = AdblockSyntax.AdGuard; + syntax = AdblockSyntax.Adg; rawPattern = adgRest; } - let domains: IDomain[] = []; + let domains: Domain[] = []; if (rawPattern.length > 0) { domains = DomainListParser.parse(rawPattern).domains; @@ -247,32 +247,32 @@ export class CosmeticRuleParser { // Element hiding / uBO CSS inject if (CosmeticRuleSeparatorUtils.isElementHiding(separator)) { - if (UBlockModifierListParser.hasUblockModifierIndicators(rawBody)) { - const { modifiers: uboModifiers, rest: uboRest } = UBlockModifierListParser.parse(rawBody); + if (UboModifierListParser.hasUboModifierIndicators(rawBody)) { + const { modifiers: uboModifiers, rest: uboRest } = UboModifierListParser.parse(rawBody); if (uboModifiers.length > 0) { - if (syntax == AdblockSyntax.AdGuard) { - throw new SyntaxError(`Cannot use AdGuard modifier list with uBO options`); + if (syntax == AdblockSyntax.Adg) { + throw new SyntaxError(`Cannot use AdGuard modifier list with uBO modifiers`); } modifiers.push(...uboModifiers); - syntax = AdblockSyntax.uBlockOrigin; + syntax = AdblockSyntax.Ubo; rawBody = uboRest; } } - if (CssInjectionBodyParser.isUblockCssInjection(rawBody)) { - if (syntax == AdblockSyntax.AdGuard) { + if (CssInjectionBodyParser.isUboCssInjection(rawBody)) { + if (syntax == AdblockSyntax.Adg) { throw new SyntaxError(`Cannot use AdGuard modifier list with uBO's CSS injection`); } - syntax = AdblockSyntax.uBlockOrigin; + syntax = AdblockSyntax.Ubo; - const body = CssInjectionBodyParser.parseUblockCssInjection(rawBody); + const body = CssInjectionBodyParser.parseUboCssInjection(rawBody); - return { - category: RuleCategories.Cosmetic, + return { + category: RuleCategory.Cosmetic, type: CosmeticRuleType.CssRule, syntax, exception, @@ -284,8 +284,8 @@ export class CosmeticRuleParser { } // Regular elemhide rules - return { - category: RuleCategories.Cosmetic, + return { + category: RuleCategory.Cosmetic, type: CosmeticRuleType.ElementHidingRule, syntax, exception, @@ -297,14 +297,14 @@ export class CosmeticRuleParser { } // ADG CSS inject / ABP snippet inject - else if (CosmeticRuleSeparatorUtils.isAdGuardCss(separator)) { - if (CssInjectionBodyParser.isAdGuardCssInjection(rawBody)) { - const body = CssInjectionBodyParser.parseAdGuardCssInjection(rawBody); + else if (CosmeticRuleSeparatorUtils.isAdgCss(separator)) { + if (CssInjectionBodyParser.isAdgCssInjection(rawBody)) { + const body = CssInjectionBodyParser.parseAdgCssInjection(rawBody); - return { - category: RuleCategories.Cosmetic, + return { + category: RuleCategory.Cosmetic, type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception, modifiers, domains, @@ -313,10 +313,10 @@ export class CosmeticRuleParser { }; } - return { - category: RuleCategories.Cosmetic, + return { + category: RuleCategory.Cosmetic, type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.AdblockPlus, + syntax: AdblockSyntax.Abp, exception, modifiers, domains, @@ -327,17 +327,17 @@ export class CosmeticRuleParser { // ADG/uBO scriptlets else if ( - CosmeticRuleSeparatorUtils.isAdGuardScriptlet(separator) || - CosmeticRuleSeparatorUtils.isUblockScriptlet(separator) + CosmeticRuleSeparatorUtils.isAdgScriptlet(separator) || + CosmeticRuleSeparatorUtils.isUboScriptlet(separator) ) { // Set syntax - syntax = AdblockSyntax.AdGuard; - if (CosmeticRuleSeparatorUtils.isUblockScriptlet(separator)) { - syntax = AdblockSyntax.uBlockOrigin; + syntax = AdblockSyntax.Adg; + if (CosmeticRuleSeparatorUtils.isUboScriptlet(separator)) { + syntax = AdblockSyntax.Ubo; } - return { - category: RuleCategories.Cosmetic, + return { + category: RuleCategory.Cosmetic, type: CosmeticRuleType.ScriptletRule, syntax, exception, @@ -349,51 +349,45 @@ export class CosmeticRuleParser { } // ADG/uBO HTML filters - else if ( - CosmeticRuleSeparatorUtils.isUblockHtml(separator) || - CosmeticRuleSeparatorUtils.isAdGuardHtml(separator) - ) { + else if (CosmeticRuleSeparatorUtils.isUboHtml(separator) || CosmeticRuleSeparatorUtils.isAdgHtml(separator)) { /** * Special case: uBO's responseheader rule. This rule follows the syntax of cosmetic rules, * but is only parsed at the network level. * * @see {@link NetworkRuleParser.parse} */ - if ( - CosmeticRuleSeparatorUtils.isUblockHtml(separator) && - rawBody.startsWith(UBO_RESPONSEHEADER_INDICATOR) - ) { + if (CosmeticRuleSeparatorUtils.isUboHtml(separator) && rawBody.startsWith(UBO_RESPONSEHEADER_INDICATOR)) { return null; } // Set syntax - if (CosmeticRuleSeparatorUtils.isUblockHtml(separator)) { - syntax = AdblockSyntax.uBlockOrigin; + if (CosmeticRuleSeparatorUtils.isUboHtml(separator)) { + syntax = AdblockSyntax.Ubo; } else { - syntax = AdblockSyntax.AdGuard; + syntax = AdblockSyntax.Adg; } - if (UBlockModifierListParser.hasUblockModifierIndicators(rawBody)) { - const { modifiers: uboModifiers, rest: uboRest } = UBlockModifierListParser.parse(rawBody); + if (UboModifierListParser.hasUboModifierIndicators(rawBody)) { + const { modifiers: uboModifiers, rest: uboRest } = UboModifierListParser.parse(rawBody); if (uboModifiers.length > 0) { - if (CosmeticRuleSeparatorUtils.isAdGuardHtml(separator)) { - throw new SyntaxError(`Cannot use uBO options with ADG HTML filtering`); + if (CosmeticRuleSeparatorUtils.isAdgHtml(separator)) { + throw new SyntaxError(`Cannot use uBO modifiers with ADG HTML filtering`); } - if (syntax == AdblockSyntax.AdGuard || adgModifiers.length > 0) { - throw new SyntaxError(`Cannot use AdGuard modifier list with uBO options`); + if (syntax == AdblockSyntax.Adg || adgModifiers.length > 0) { + throw new SyntaxError(`Cannot use AdGuard modifier list with uBO modifiers`); } modifiers.push(...uboModifiers); - syntax = AdblockSyntax.uBlockOrigin; + syntax = AdblockSyntax.Ubo; rawBody = uboRest; } } - return { - category: RuleCategories.Cosmetic, + return { + category: RuleCategory.Cosmetic, type: CosmeticRuleType.HtmlRule, syntax, exception, @@ -405,11 +399,11 @@ export class CosmeticRuleParser { } // ADG JS inject - else if (CosmeticRuleSeparatorUtils.isAdGuardJs(separator)) { - return { - category: RuleCategories.Cosmetic, + else if (CosmeticRuleSeparatorUtils.isAdgJs(separator)) { + return { + category: RuleCategory.Cosmetic, type: CosmeticRuleType.JsRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception, modifiers, domains, @@ -424,15 +418,15 @@ export class CosmeticRuleParser { /** * Converts a cosmetic rule AST into a string. * - * @param {ICosmeticRule} ast - Cosmetic rule AST - * @returns {string} Raw string + * @param ast - Cosmetic rule AST + * @returns Raw string */ - public static generate(ast: ICosmeticRule): string { + public static generate(ast: CosmeticRule): string { let result = EMPTY; // AdGuard modifiers - if (ast.syntax == AdblockSyntax.AdGuard && ast.modifiers.length > 0) { - result += AdGuardModifierListParser.generate({ + if (ast.syntax == AdblockSyntax.Adg && ast.modifiers.length > 0) { + result += AdgModifierListParser.generate({ type: ADG_MODIFIER_LIST_TYPE, modifiers: ast.modifiers, rest: EMPTY, @@ -450,42 +444,42 @@ export class CosmeticRuleParser { case CosmeticRuleType.ElementHidingRule: result += ast.separator; - if (ast.syntax == AdblockSyntax.uBlockOrigin && ast.modifiers.length > 0) { - result += UBlockModifierListParser.generate({ + if (ast.syntax == AdblockSyntax.Ubo && ast.modifiers.length > 0) { + result += UboModifierListParser.generate({ type: UBO_MODIFIER_LIST_TYPE, modifiers: ast.modifiers, - rest: ElementHidingBodyParser.generate(ast.body), + rest: ElementHidingBodyParser.generate(ast.body), }); } else { - result += ElementHidingBodyParser.generate(ast.body); + result += ElementHidingBodyParser.generate(ast.body); } break; case CosmeticRuleType.CssRule: result += ast.separator; - if (ast.syntax == AdblockSyntax.uBlockOrigin && ast.modifiers.length > 0) { - result += UBlockModifierListParser.generate({ + if (ast.syntax == AdblockSyntax.Ubo && ast.modifiers.length > 0) { + result += UboModifierListParser.generate({ type: UBO_MODIFIER_LIST_TYPE, modifiers: ast.modifiers, - rest: CssInjectionBodyParser.generate(ast.body, ast.syntax), + rest: CssInjectionBodyParser.generate(ast.body, ast.syntax), }); } else { - result += CssInjectionBodyParser.generate(ast.body, ast.syntax); + result += CssInjectionBodyParser.generate(ast.body, ast.syntax); } break; case CosmeticRuleType.HtmlRule: result += ast.separator; - if (ast.syntax == AdblockSyntax.uBlockOrigin && ast.modifiers.length > 0) { - result += UBlockModifierListParser.generate({ + if (ast.syntax == AdblockSyntax.Ubo && ast.modifiers.length > 0) { + result += UboModifierListParser.generate({ type: UBO_MODIFIER_LIST_TYPE, modifiers: ast.modifiers, - rest: HtmlBodyParser.generate(ast.body, ast.syntax), + rest: HtmlBodyParser.generate(ast.body, ast.syntax), }); } else { - result += HtmlBodyParser.generate(ast.body, ast.syntax); + result += HtmlBodyParser.generate(ast.body, ast.syntax); } break; @@ -500,9 +494,9 @@ export class CosmeticRuleParser { result += ast.separator; // eslint-disable-next-line no-case-declarations - const scriptlets = ScriptletBodyParser.generate(ast.body, ast.syntax); + const scriptlets = ScriptletBodyParser.generate(ast.body, ast.syntax); - if (ast.syntax == AdblockSyntax.AdblockPlus) { + if (ast.syntax == AdblockSyntax.Abp) { result += scriptlets.join(SEMICOLON + SPACE); return result; } else { diff --git a/src/parser/cosmetic/specific/adg-options.ts b/src/parser/cosmetic/specific/adg-modifiers.ts similarity index 76% rename from src/parser/cosmetic/specific/adg-options.ts rename to src/parser/cosmetic/specific/adg-modifiers.ts index 623a3551..26131554 100644 --- a/src/parser/cosmetic/specific/adg-options.ts +++ b/src/parser/cosmetic/specific/adg-modifiers.ts @@ -1,23 +1,23 @@ import { EMPTY } from "../../../utils/constants"; import { StringUtils } from "../../../utils/string"; -import { ModifierListParser, IRuleModifier, MODIFIER_LIST_TYPE } from "../../common/modifier-list"; +import { ModifierListParser, RuleModifier, MODIFIER_LIST_TYPE } from "../../common/modifier-list"; const MODIFIER_LIST_OPEN = "["; const MODIFIER_LIST_CLOSE = "]"; const MODIFIERS_MARKER = "$"; -export const ADG_MODIFIER_LIST_TYPE = "AdGuardModifierList"; +export const ADG_MODIFIER_LIST_TYPE = "AdgModifierList"; /** Represents AdGuard's cosmetic rule modifiers. */ -export interface IAdGuardModifierList { +export interface AdgModifierList { // Basically, the idea is that each main AST part should have a type type: typeof ADG_MODIFIER_LIST_TYPE; - modifiers: IRuleModifier[]; + modifiers: RuleModifier[]; rest: string; } /** - * AdGuardModifierListParser is responsible for parsing AdGuard cosmetic rule modifiers. + * AdgModifierListParser is responsible for parsing AdGuard cosmetic rule modifiers. * * For example: * ```adblock @@ -26,19 +26,19 @@ export interface IAdGuardModifierList { * * @see {@link https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#non-basic-rules-modifiers} */ -export class AdGuardModifierListParser { +export class AdgModifierListParser { /** * Parses an AdGuard modifier (option) list. * - * @param {string} raw - Raw pattern - * @returns {IAdGuardModifierList} Modifier list AST + * @param raw - Raw pattern + * @returns Modifier list AST * @throws If the modifier list is syntactically invalid * @see {@link https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#non-basic-rules-modifiers} */ - public static parse(raw: string): IAdGuardModifierList { + public static parse(raw: string): AdgModifierList { const trimmed = raw.trim(); - let modifiers: IRuleModifier[] = []; + let modifiers: RuleModifier[] = []; let closeIndex = -1; @@ -74,10 +74,10 @@ export class AdGuardModifierListParser { /** * Converts an AdGuard modifier (option) list AST to a string. * - * @param {IAdGuardModifierList} ast - Modifier list AST - * @returns {string} Raw string + * @param ast - Modifier list AST + * @returns Raw string */ - public static generate(ast: IAdGuardModifierList) { + public static generate(ast: AdgModifierList) { let result = EMPTY; result += MODIFIER_LIST_OPEN; diff --git a/src/parser/cosmetic/specific/ubo-options.ts b/src/parser/cosmetic/specific/ubo-modifiers.ts similarity index 85% rename from src/parser/cosmetic/specific/ubo-options.ts rename to src/parser/cosmetic/specific/ubo-modifiers.ts index fff5d46b..377a1586 100644 --- a/src/parser/cosmetic/specific/ubo-options.ts +++ b/src/parser/cosmetic/specific/ubo-modifiers.ts @@ -7,18 +7,24 @@ import { PseudoClassSelector, } from "css-tree"; import { UBO_COSMETIC_MODIFIERS } from "../../../converter/cosmetic-modifiers"; -import { CSS_PSEUDO_CLOSE, CSS_PSEUDO_MARKER, CSS_PSEUDO_OPEN, EMPTY, SPACE } from "../../../utils/constants"; +import { + CSS_NOT_PSEUDO, + CSS_PSEUDO_CLOSE, + CSS_PSEUDO_MARKER, + CSS_PSEUDO_OPEN, + EMPTY, + SPACE, +} from "../../../utils/constants"; import { CssTreeNodeType, CssTreeParserContext } from "../../../utils/csstree-constants"; -import { IRuleModifier } from "../../common/modifier-list"; +import { RuleModifier } from "../../common/modifier-list"; -export const UBO_MODIFIER_LIST_TYPE = "uBlockModifierList"; -const CSS_NOT_PSEUDO = "not"; +export const UBO_MODIFIER_LIST_TYPE = "UboModifierList"; /** Represents uBlock's cosmetic rule modifiers. */ -export interface IuBlockModifierList { +export interface UboModifierList { // Basically, the idea is that each main AST part should have a type type: typeof UBO_MODIFIER_LIST_TYPE; - modifiers: UblockModifier[]; + modifiers: UboModifier[]; rest: string; } @@ -30,12 +36,12 @@ export interface IuBlockModifierList { * example.com##:not(:matches-path(/path)) .ad * ``` */ -export interface UblockModifier extends IRuleModifier { +export interface UboModifier extends RuleModifier { not?: boolean; } /** - * UBlockModifierListParser is responsible for parsing uBlock cosmetic rule modifiers. + * UboModifierListParser is responsible for parsing uBlock cosmetic rule modifiers. * * They follow the syntax of pseudo classes, but they are actually part of the targeting, * not the selector. @@ -45,15 +51,15 @@ export interface UblockModifier extends IRuleModifier { * example.com##:matches-path(/path) .ads * ``` */ -export class UBlockModifierListParser { +export class UboModifierListParser { /** * Checks if there is a uBO modifier indicator in the selector. * The motivation is to have a lightweight check before expensive parsing. * - * @param {string} raw - Raw selector - * @returns {boolean} true/false + * @param raw - Raw selector + * @returns true/false */ - public static hasUblockModifierIndicators(raw: string): boolean { + public static hasUboModifierIndicators(raw: string): boolean { return ( UBO_COSMETIC_MODIFIERS.find( // eslint-disable-next-line @typescript-eslint/no-loop-func @@ -101,18 +107,18 @@ export class UBlockModifierListParser { * * @see {@link https://github.com/gorhill/uBlock/wiki/Procedural-cosmetic-filters#subjectmatches-patharg} * @see {@link https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#non-basic-rules-modifiers-path} - * @param {string} raw - Raw selector - * @returns {IuBlockModifierList} Parsed uBO modifiers and rest of the selector + * @param raw - Raw selector + * @returns Parsed uBO modifiers and rest of the selector */ - public static parse(raw: string): IuBlockModifierList { + public static parse(raw: string): UboModifierList { const trimmed = raw.trim(); // Handle empty case (otherwise CSSTree throws error for the empty selector) if (trimmed.length == 0) { - return { + return { type: UBO_MODIFIER_LIST_TYPE, modifiers: [], - rest: "", + rest: EMPTY, }; } @@ -123,7 +129,7 @@ export class UBlockModifierListParser { }); let prevPseudo: PseudoClassSelector | null = null; - const modifiers: UblockModifier[] = []; + const modifiers: UboModifier[] = []; // Initially, we keep the entire selector const keep = Array(trimmed.length).fill(true); @@ -134,12 +140,12 @@ export class UBlockModifierListParser { if (UBO_COSMETIC_MODIFIERS.includes(node.name)) { // If the previous pseudo selector was :not(), then the // entire :not(:ubo-modifier(...)) must be omitted - const common: IRuleModifier = { + const common: RuleModifier = { modifier: node.name, // TODO: Fix CSSTree typedefs (first is getter now, not method) // See: https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/62536 // eslint-disable-next-line @typescript-eslint/no-explicit-any - value: node && node.children ? generateCss(node.children.first) : "", + value: node && node.children ? generateCss(node.children.first) : EMPTY, }; if (prevPseudo && prevPseudo.name == CSS_NOT_PSEUDO) { @@ -184,7 +190,7 @@ export class UBlockModifierListParser { } } - return { + return { type: UBO_MODIFIER_LIST_TYPE, modifiers, rest: rest.trim(), @@ -194,10 +200,10 @@ export class UBlockModifierListParser { /** * Converts a uBO modifier (option) list AST to a string. * - * @param {IuBlockModifierList} ast - Modifier list AST - * @returns {string} Raw string + * @param ast - Modifier list AST + * @returns Raw string */ - public static generate(ast: IuBlockModifierList): string { + public static generate(ast: UboModifierList): string { let result = EMPTY; result += ast.modifiers diff --git a/src/parser/index.ts b/src/parser/index.ts index e7c4f786..26723c4e 100644 --- a/src/parser/index.ts +++ b/src/parser/index.ts @@ -2,3 +2,5 @@ export * from "./rule"; export * from "./comment/comment"; export * from "./cosmetic/cosmetic"; export * from "./network/network"; +export * from "./common/domain-list"; +export * from "./common/modifier-list"; diff --git a/src/parser/network/network.ts b/src/parser/network/network.ts index 75d6e180..69f6ed12 100644 --- a/src/parser/network/network.ts +++ b/src/parser/network/network.ts @@ -1,7 +1,7 @@ import { AdblockSyntax } from "../../utils/adblockers"; import { REGEX_MARKER, StringUtils } from "../../utils/string"; -import { IRuleModifier, ModifierListParser, MODIFIER_LIST_TYPE } from "../common/modifier-list"; -import { IRule, RuleCategories } from "../common"; +import { RuleModifier, ModifierListParser, MODIFIER_LIST_TYPE } from "../common/modifier-list"; +import { Rule, RuleCategory } from "../common"; import { NetworkRuleType } from "./common"; import { ASSIGN_OPERATOR, CLOSE_PARENTHESIS, EMPTY, OPEN_PARENTHESIS } from "../../utils/constants"; import { CosmeticRuleSeparator, CosmeticRuleSeparatorUtils } from "../../utils/cosmetic-rule-separator"; @@ -17,8 +17,8 @@ const ADG_REMOVEHEADER = "removeheader"; /** * Represents the common properties of network rules */ -export interface INetworkRule extends IRule { - category: RuleCategories.Network; +export interface NetworkRule extends Rule { + category: RuleCategory.Network; type: NetworkRuleType; syntax: AdblockSyntax; exception: boolean; @@ -40,9 +40,9 @@ export interface INetworkRule extends IRule { * ``` * - etc. */ -export interface IBasicNetworkRule extends INetworkRule { +export interface BasicNetworkRule extends NetworkRule { type: NetworkRuleType.BasicNetworkRule; - modifiers: IRuleModifier[]; + modifiers: RuleModifier[]; } /** @@ -56,7 +56,7 @@ export interface IBasicNetworkRule extends INetworkRule { * example.org##^responseheader(header-name) * ``` */ -export interface IRemoveHeaderNetworkRule extends INetworkRule { +export interface RemoveHeaderNetworkRule extends NetworkRule { type: NetworkRuleType.RemoveHeaderNetworkRule; syntax: AdblockSyntax; header: string; @@ -75,10 +75,10 @@ export class NetworkRuleParser { /** * Parses a network rule (also known as basic rule). Make sure you parse the cosmetic rules first! * - * @param {string} raw - Raw rule - * @returns {INetworkRule} Network rule AST + * @param raw - Raw rule + * @returns Network rule AST */ - public static parse(raw: string): IBasicNetworkRule | IRemoveHeaderNetworkRule { + public static parse(raw: string): BasicNetworkRule | RemoveHeaderNetworkRule { let rule = raw.trim(); // Special case @@ -87,8 +87,8 @@ export class NetworkRuleParser { return uboRemoveHeader; } - const common: INetworkRule = { - category: RuleCategories.Network, + const common: NetworkRule = { + category: RuleCategory.Network, type: NetworkRuleType.BasicNetworkRule, syntax: AdblockSyntax.Unknown, exception: false, @@ -115,7 +115,7 @@ export class NetworkRuleParser { ); // Get rule parts - const modifiers: IRuleModifier[] = []; + const modifiers: RuleModifier[] = []; if (separatorIndex != -1) { common.pattern = rule.substring(0, separatorIndex); @@ -131,16 +131,16 @@ export class NetworkRuleParser { } common.type = NetworkRuleType.RemoveHeaderNetworkRule; - common.syntax = AdblockSyntax.AdGuard; + common.syntax = AdblockSyntax.Adg; - return { + return { ...common, header, }; } } - return { + return { ...common, modifiers, }; @@ -152,13 +152,13 @@ export class NetworkRuleParser { * uBO calls this rule a "special case of HTML filtering", so this follows uBO's HTML filtering * syntax, which is a cosmetic pattern. However, we only parse this rule in the network parser. * - * @param {string} raw - Raw uBO response header filtering rule - * @returns {IRemoveHeaderNetworkRule | null} + * @param raw - Raw uBO response header filtering rule + * @returns * AST of the parsed rule, or null if there is no response header filtering indicator in the raw input * @throws If the response header indicator is present, but the rule is syntactically invalid * @see {@link https://github.com/gorhill/uBlock/wiki/Static-filter-syntax#response-header-filtering} */ - private static parseUboResponseHeader(raw: string): IRemoveHeaderNetworkRule | null { + private static parseUboResponseHeader(raw: string): RemoveHeaderNetworkRule | null { const trimmed = raw.trim(); // In order to operate quickly, we only check the presence of the indicator at first @@ -171,7 +171,7 @@ export class NetworkRuleParser { if ( start == -1 || - !(separator == CosmeticRuleSeparator.UboHTML || separator == CosmeticRuleSeparator.UboHTMLException) + !(separator == CosmeticRuleSeparator.UboHtml || separator == CosmeticRuleSeparator.UboHtmlException) ) { throw new SyntaxError(`uBO responseheader filtering requires a valid uBO HTML rule separator`); } @@ -195,9 +195,9 @@ export class NetworkRuleParser { } return { - category: RuleCategories.Network, + category: RuleCategory.Network, type: NetworkRuleType.RemoveHeaderNetworkRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: !!exception, pattern: trimmed.substring(0, start).trim(), header, @@ -207,14 +207,14 @@ export class NetworkRuleParser { /** * Converts the AST of a header removal network rule to a string. * - * @param {IRemoveHeaderNetworkRule} ast - Header remover rule AST - * @returns {string} Raw string + * @param ast - Header remover rule AST + * @returns Raw string */ - private static generateUboResponseHeader(ast: IRemoveHeaderNetworkRule): string { + private static generateUboResponseHeader(ast: RemoveHeaderNetworkRule): string { let result = EMPTY; result += ast.pattern; - result += ast.exception ? CosmeticRuleSeparator.UboHTMLException : CosmeticRuleSeparator.UboHTML; + result += ast.exception ? CosmeticRuleSeparator.UboHtmlException : CosmeticRuleSeparator.UboHtml; result += UBO_RESPONSEHEADER_INDICATOR; result += ast.header; result += CLOSE_PARENTHESIS; @@ -225,15 +225,15 @@ export class NetworkRuleParser { /** * Converts a network rule (basic rule) AST to a string. * - * @param {INetworkRule} ast - Network rule AST - * @returns {string} Raw string + * @param ast - Network rule AST + * @returns Raw string */ - public static generate(ast: IBasicNetworkRule | IRemoveHeaderNetworkRule): string { + public static generate(ast: BasicNetworkRule | RemoveHeaderNetworkRule): string { let result = EMPTY; // Special case - if (ast.type == NetworkRuleType.RemoveHeaderNetworkRule && ast.syntax == AdblockSyntax.uBlockOrigin) { - return NetworkRuleParser.generateUboResponseHeader(ast); + if (ast.type == NetworkRuleType.RemoveHeaderNetworkRule && ast.syntax == AdblockSyntax.Ubo) { + return NetworkRuleParser.generateUboResponseHeader(ast); } // Common parts diff --git a/src/parser/rule.ts b/src/parser/rule.ts index 4f1f428e..190ce86b 100644 --- a/src/parser/rule.ts +++ b/src/parser/rule.ts @@ -1,16 +1,16 @@ import { CommentParser } from "./comment/comment"; -import { IComment } from "./comment/common"; -import { RuleCategories } from "./common"; +import { Comment } from "./comment/common"; +import { RuleCategory } from "./common"; import { CosmeticRuleParser, - ICosmeticRule, - ICssRule, - IElementHidingRule, - IHtmlRule, - IJsRule, - IScriptletRule, + CosmeticRule, + CssRule, + ElementHidingRule, + HtmlRule, + JsRule, + ScriptletRule, } from "./cosmetic/cosmetic"; -import { IBasicNetworkRule, IRemoveHeaderNetworkRule, INetworkRule, NetworkRuleParser } from "./network/network"; +import { BasicNetworkRule, RemoveHeaderNetworkRule, NetworkRuleParser } from "./network/network"; /** * RuleParser is responsible for parsing the rules. @@ -21,22 +21,21 @@ export class RuleParser { /** * Parse an adblock rule. The type and syntax of the rule is determined automatically if possible. * - * @param {string} raw - Raw rule - * @returns {IComment | ICssRule | IElementHidingRule | IScriptletRule | IHtmlRule | IJsRule | INetworkRule} - * Network rule AST + * @param raw - Raw adblock rule + * @returns Adblock rule AST * @throws If the input matches a pattern but syntactically invalid */ public static parse( raw: string ): - | IComment - | ICssRule - | IElementHidingRule - | IScriptletRule - | IHtmlRule - | IJsRule - | IBasicNetworkRule - | IRemoveHeaderNetworkRule { + | Comment + | CssRule + | ElementHidingRule + | ScriptletRule + | HtmlRule + | JsRule + | BasicNetworkRule + | RemoveHeaderNetworkRule { const trimmed = raw.trim(); // Comments (agent / metadata / hint / pre-processor / comment) @@ -60,16 +59,16 @@ export class RuleParser { /** * Converts a rule AST to a string. * - * @param {INetworkRule} ast - Adblock rule AST - * @returns {string} Raw string + * @param ast - Adblock rule AST + * @returns Raw string */ - public static generate(ast: IComment | ICosmeticRule | IBasicNetworkRule | IRemoveHeaderNetworkRule): string { + public static generate(ast: Comment | CosmeticRule | BasicNetworkRule | RemoveHeaderNetworkRule): string { switch (ast.category) { - case RuleCategories.Comment: + case RuleCategory.Comment: return CommentParser.generate(ast); - case RuleCategories.Cosmetic: + case RuleCategory.Cosmetic: return CosmeticRuleParser.generate(ast); - case RuleCategories.Network: + case RuleCategory.Network: return NetworkRuleParser.generate(ast); } } diff --git a/src/utils/adblockers.ts b/src/utils/adblockers.ts index e22c662f..3ff1c335 100644 --- a/src/utils/adblockers.ts +++ b/src/utils/adblockers.ts @@ -3,17 +3,29 @@ * It also allows unknown syntax */ export enum AdblockSyntax { + /** Unknown / common */ Unknown = "Unknown", - AdblockPlus = "AdblockPlus", - uBlockOrigin = "uBlockOrigin", - AdGuard = "AdGuard", + + /** Adblock Plus */ + Abp = "Abp", + + /** uBlock Origin */ + Ubo = "Ubo", + + /** AdGuard */ + Adg = "Adg", } /** * Possible adblock syntaxes (supported by this library) */ export enum StrictAdblockSyntax { - AdblockPlus = "AdblockPlus", - uBlockOrigin = "uBlockOrigin", - AdGuard = "AdGuard", + /** Adblock Plus */ + Abp = "Abp", + + /** uBlock Origin */ + Ubo = "Ubo", + + /** AdGuard */ + Adg = "Adg", } diff --git a/src/utils/constants.ts b/src/utils/constants.ts index f70e6dfc..9d9cbc05 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -25,12 +25,19 @@ export const ASSIGN_OPERATOR = "="; export const CSS_CLASS_MARKER = "."; export const CSS_ID_MARKER = "#"; +export const CSS_SELECTORS_SEPARATOR = ","; + export const CSS_MEDIA_MARKER = "@media"; export const CSS_PSEUDO_MARKER = ":"; export const CSS_PSEUDO_OPEN = "("; export const CSS_PSEUDO_CLOSE = ")"; +export const CSS_NOT_PSEUDO = "not"; + +export const CSS_BLOCK_OPEN = "{"; +export const CSS_BLOCK_CLOSE = "}"; + export const CSS_ATTRIBUTE_SELECTOR_OPEN = "["; export const CSS_ATTRIBUTE_SELECTOR_CLOSE = "]"; diff --git a/src/utils/cosmetic-rule-separator.ts b/src/utils/cosmetic-rule-separator.ts index 127a1b83..6dd8dd9f 100644 --- a/src/utils/cosmetic-rule-separator.ts +++ b/src/utils/cosmetic-rule-separator.ts @@ -27,10 +27,10 @@ export enum CosmeticRuleSeparator { UboScriptletException = "#@#+js", /** @see {@link https://github.com/gorhill/uBlock/wiki/Static-filter-syntax#html-filters} */ - UboHTML = "##^", + UboHtml = "##^", /** @see {@link https://github.com/gorhill/uBlock/wiki/Static-filter-syntax#html-filters} */ - UboHTMLException = "#@#^", + UboHtmlException = "#@#^", /** @see {@link https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#cosmetic-css-rules} */ AdgCSSInject = "#$#", @@ -98,8 +98,8 @@ export class CosmeticRuleSeparatorUtils { /** * Returns whether the specified separator is an element hiding separator. * - * @param {string} separator - Separator as string - * @returns {boolean} true/false + * @param separator - Separator as string + * @returns true/false */ public static isElementHiding(separator: string): boolean { return ( @@ -113,10 +113,10 @@ export class CosmeticRuleSeparatorUtils { /** * Returns whether the specified separator is an AdGuard CSS injection separator. * - * @param {string} separator - Separator as string - * @returns {boolean} true/false + * @param separator - Separator as string + * @returns true/false */ - public static isAdGuardCss(separator: string): boolean { + public static isAdgCss(separator: string): boolean { return ( separator == CosmeticRuleSeparator.AdgCSSInject || separator == CosmeticRuleSeparator.AdgCSSInjectException || @@ -128,20 +128,20 @@ export class CosmeticRuleSeparatorUtils { /** * Returns whether the specified separator is an Adblock Plus snippet separator. * - * @param {string} separator - Separator as string - * @returns {boolean} true/false + * @param separator - Separator as string + * @returns true/false */ - public static isAdblockPlusSnippet(separator: string): boolean { + public static isAbpSnippet(separator: string): boolean { return separator == CosmeticRuleSeparator.AbpSnippet || separator == CosmeticRuleSeparator.AbpSnippetException; } /** * Returns whether the specified separator is a uBlock scriptlet separator. * - * @param {string} separator - Separator as string - * @returns {boolean} true/false + * @param separator - Separator as string + * @returns true/false */ - public static isUblockScriptlet(separator: string): boolean { + public static isUboScriptlet(separator: string): boolean { return ( separator == CosmeticRuleSeparator.UboScriptlet || separator == CosmeticRuleSeparator.UboScriptletException ); @@ -150,10 +150,10 @@ export class CosmeticRuleSeparatorUtils { /** * Returns whether the specified separator is an AdGuard scriptlet separator. * - * @param {string} separator - Separator as string - * @returns {boolean} true/false + * @param separator - Separator as string + * @returns true/false */ - public static isAdGuardScriptlet(separator: string): boolean { + public static isAdgScriptlet(separator: string): boolean { return ( separator == CosmeticRuleSeparator.AdgScriptlet || separator == CosmeticRuleSeparator.AdgScriptletException ); @@ -162,10 +162,10 @@ export class CosmeticRuleSeparatorUtils { /** * Returns whether the specified separator is an AdGuard JS separator. * - * @param {string} separator - Separator as string - * @returns {boolean} true/false + * @param separator - Separator as string + * @returns true/false */ - public static isAdGuardJs(separator: string): boolean { + public static isAdgJs(separator: string): boolean { return ( separator == CosmeticRuleSeparator.AdgJsInject || separator == CosmeticRuleSeparator.AdgJsInjectException ); @@ -174,42 +174,42 @@ export class CosmeticRuleSeparatorUtils { /** * Returns whether the specified separator is a uBlock HTML separator. * - * @param {string} separator - Separator as string - * @returns {boolean} true/false + * @param separator - Separator as string + * @returns true/false */ - public static isUblockHtml(separator: string): boolean { - return separator == CosmeticRuleSeparator.UboHTML || separator == CosmeticRuleSeparator.UboHTMLException; + public static isUboHtml(separator: string): boolean { + return separator == CosmeticRuleSeparator.UboHtml || separator == CosmeticRuleSeparator.UboHtmlException; } /** * Returns whether the specified separator is an AdGuard HTML separator. * - * @param {string} separator - Separator as string - * @returns {boolean} true/false + * @param separator - Separator as string + * @returns true/false */ - public static isAdGuardHtml(separator: string): boolean { + public static isAdgHtml(separator: string): boolean { return separator == CosmeticRuleSeparator.AdgHTML || separator == CosmeticRuleSeparator.AdgHTMLException; } /** * Returns whether the specified separator is a scriptlet separator. * - * @param {string} separator - Separator as string - * @returns {boolean} true/false + * @param separator - Separator as string + * @returns true/false */ public static isScriptlet(separator: string): boolean { return ( - CosmeticRuleSeparatorUtils.isAdblockPlusSnippet(separator) || - CosmeticRuleSeparatorUtils.isUblockScriptlet(separator) || - CosmeticRuleSeparatorUtils.isAdGuardScriptlet(separator) + CosmeticRuleSeparatorUtils.isAbpSnippet(separator) || + CosmeticRuleSeparatorUtils.isUboScriptlet(separator) || + CosmeticRuleSeparatorUtils.isAdgScriptlet(separator) ); } /** * Returns whether the specified separator is an exception. * - * @param {string} separator - Separator as string - * @returns {boolean} true/false + * @param separator - Separator as string + * @returns true/false */ public static isException(separator?: string): boolean { if (!separator) { @@ -224,8 +224,8 @@ export class CosmeticRuleSeparatorUtils { * Looks for the cosmetic rule separator in the rule. This is a simplified version that * masks the recursive function. * - * @param {string} rule - Raw rule - * @returns {CosmeticRuleSeparatorResult} Separator data + * @param rule - Raw rule + * @returns Separator data */ public static find(rule: string): CosmeticRuleSeparatorResult { return CosmeticRuleSeparatorUtils.findRecursively(rule); @@ -235,14 +235,14 @@ export class CosmeticRuleSeparatorUtils { * Recursively looks for the cosmetic rule separator in the rule. The basic idea is from TSUrlFilter, this is * a further optimized version. The method is very low level, but it is necessary for speed. * - * @param {string} rule - Raw rule. - * @param {number} hashMarkPos - Latest hashmark character (#) position (if none, then -1). - * @param {number} dollarSignPos - Latest dollar sign character ($) position (if none, then -1). - * @param {boolean} noHashMark - Once we have established that there is no hashmark character (#), it is + * @param rule - Raw rule. + * @param hashMarkPos - Latest hashmark character (#) position (if none, then -1). + * @param dollarSignPos - Latest dollar sign character ($) position (if none, then -1). + * @param noHashMark - Once we have established that there is no hashmark character (#), it is * unnecessary to search for it again in the next step of the recursion. - * @param {boolean} noDollarSign - once we have established that there is no dollar sign character ($), it is + * @param noDollarSign - once we have established that there is no dollar sign character ($), it is * unnecessary to search for it again in the next step of the recursion. - * @returns {CosmeticRuleSeparatorResult} Separator data + * @returns Separator data */ private static findRecursively( rule: string, @@ -269,7 +269,7 @@ export class CosmeticRuleSeparatorUtils { } // ##^ else if (rule[h + 2] == "^") { - return [h, h + 3, CosmeticRuleSeparator.UboHTML, false]; + return [h, h + 3, CosmeticRuleSeparator.UboHtml, false]; } // ## return [h, h + 2, CosmeticRuleSeparator.AbpCSSElemhide, false]; @@ -314,7 +314,7 @@ export class CosmeticRuleSeparatorUtils { } // #@#^ else if (rule[h + 3] == "^") { - return [h, h + 4, CosmeticRuleSeparator.UboHTMLException, true]; + return [h, h + 4, CosmeticRuleSeparator.UboHtmlException, true]; } return [h, h + 3, CosmeticRuleSeparator.AbpCSSElemhideException, true]; } diff --git a/src/utils/csstree.ts b/src/utils/csstree.ts index c6ac12ad..7e724333 100644 --- a/src/utils/csstree.ts +++ b/src/utils/csstree.ts @@ -31,7 +31,7 @@ import { } from "./constants"; import { CssTreeNodeType, CssTreeParserContext } from "./csstree-constants"; -export interface IExtendedCssNodes { +export interface ExtendedCssNodes { pseudos: PseudoClassSelector[]; attributes: AttributeSelector[]; } @@ -40,9 +40,9 @@ export class CssTree { /** * Helper function for parsing CSS parts. * - * @param {string} raw - Raw CSS input - * @param {string} context - CSSTree context - * @returns {CssNode} - CSSTree node (AST) + * @param raw - Raw CSS input + * @param context - CSSTree context + * @returns CSSTree node (AST) */ public static parse(raw: string, context: CssTreeParserContext): CssNode { return parse(raw, { @@ -60,9 +60,9 @@ export class CssTree { /** * Helper function for creating attribute selectors. * - * @param {string} attribute - Attribute name - * @param {string} value - Attribute value - * @returns {AttributeSelector} - Attribute selector AST + * @param attribute - Attribute name + * @param value - Attribute value + * @returns Attribute selector AST */ public static createAttributeSelector(attribute: string, value: string): AttributeSelector { return { @@ -82,10 +82,10 @@ export class CssTree { /** * - * @param {Selector} selectorAst - CSSTree Selector AST - * @returns {IExtendedCssNodes} Extended CSS Nodes + * @param selectorAst - CSSTree Selector AST + * @returns Extended CSS Nodes */ - public static getSelectorExtendedCssNodes(selectorAst: Selector): IExtendedCssNodes { + public static getSelectorExtendedCssNodes(selectorAst: Selector): ExtendedCssNodes { const pseudos: PseudoClassSelector[] = []; const attributes: AttributeSelector[] = []; @@ -115,8 +115,8 @@ export class CssTree { * Selector generation based on CSSTree's AST. This is necessary because CSSTree * only adds spaces in some edge cases. * - * @param {CssNode} ast - CSS Tree AST - * @returns {string} CSS selector as string + * @param ast - CSS Tree AST + * @returns CSS selector as string */ public static generateSelector(ast: Selector): string { let result = EMPTY; @@ -298,8 +298,8 @@ export class CssTree { /** * Block generation based on CSSTree's AST. This is necessary because CSSTree only adds spaces in some edge cases. * - * @param {CssNode} ast - CSS Tree AST - * @returns {string} CSS selector as string + * @param ast - CSS Tree AST + * @returns CSS selector as string */ public static generateBlock(ast: Block): string { let result = EMPTY; diff --git a/src/utils/string.ts b/src/utils/string.ts index 96c9230d..e842cd16 100644 --- a/src/utils/string.ts +++ b/src/utils/string.ts @@ -13,11 +13,11 @@ export class StringUtils { * Finds the first occurrence of a character that: * - isn't preceded by an escape character * - * @param {string} pattern - Source pattern - * @param {string} searchedCharacter - Searched character - * @param {number} start - Start index - * @param {string} escapeCharacter - Escape character, \ by default - * @returns {number} Index or -1 if the character not found + * @param pattern - Source pattern + * @param searchedCharacter - Searched character + * @param start - Start index + * @param escapeCharacter - Escape character, \ by default + * @returns Index or -1 if the character not found */ public static findNextUnescapedCharacter( pattern: string, @@ -38,10 +38,10 @@ export class StringUtils { * Finds the last occurrence of a character that: * - isn't preceded by an escape character * - * @param {string} pattern - Source pattern - * @param {string} searchedCharacter - Searched character - * @param {string} escapeCharacter - Escape character, \ by default - * @returns {number} Index or -1 if the character not found + * @param pattern - Source pattern + * @param searchedCharacter - Searched character + * @param escapeCharacter - Escape character, \ by default + * @returns Index or -1 if the character not found */ public static findLastUnescapedCharacter( pattern: string, @@ -62,12 +62,12 @@ export class StringUtils { * - isn't preceded by an escape character * - isn't followed by the specified character * - * @param {string} pattern - Source pattern - * @param {number} start - Start index - * @param {string} searchedCharacter - Searched character - * @param {string} notFollowedBy - Searched character not followed by this character - * @param {string} escapeCharacter - Escape character, \ by default - * @returns {number} Index or -1 if the character not found + * @param pattern - Source pattern + * @param start - Start index + * @param searchedCharacter - Searched character + * @param notFollowedBy - Searched character not followed by this character + * @param escapeCharacter - Escape character, \ by default + * @returns Index or -1 if the character not found */ public static findNextUnescapedCharacterThatNotFollowedBy( pattern: string, @@ -94,11 +94,11 @@ export class StringUtils { * - isn't preceded by an escape character * - isn't followed by the specified character * - * @param {string} pattern - Source pattern - * @param {string} searchedCharacter - Searched character - * @param {string} notFollowedBy - Searched character not followed by this character - * @param {string} escapeCharacter - Escape character, \ by default - * @returns {number} Index or -1 if the character not found + * @param pattern - Source pattern + * @param searchedCharacter - Searched character + * @param notFollowedBy - Searched character not followed by this character + * @param escapeCharacter - Escape character, \ by default + * @returns Index or -1 if the character not found */ public static findLastUnescapedCharacterThatNotFollowedBy( pattern: string, @@ -124,10 +124,10 @@ export class StringUtils { * - isn't part of any string literal ('literal' or "literal") * - isn't part of any RegExp expression (/regexp/) * - * @param {string} pattern - Source pattern - * @param {string} searchedCharacter - Searched character - * @param {number} start - Start index - * @returns {number} Index or -1 if the character not found + * @param pattern - Source pattern + * @param searchedCharacter - Searched character + * @param start - Start index + * @returns Index or -1 if the character not found */ public static findUnescapedNonStringNonRegexChar(pattern: string, searchedCharacter: string, start = 0) { let open: string | null = null; @@ -157,11 +157,11 @@ export class StringUtils { * - isn't part of any string literal ('literal' or "literal") * - isn't preceded by an escape character * - * @param {string} pattern - Source pattern - * @param {string} searchedCharacter - Searched character - * @param {number} start - Start index - * @param {string} escapeCharacter - Escape character, \ by default - * @returns {number} Index or -1 if the character not found + * @param pattern - Source pattern + * @param searchedCharacter - Searched character + * @param start - Start index + * @param escapeCharacter - Escape character, \ by default + * @returns Index or -1 if the character not found */ public static findNextUnquotedUnescapedCharacter( pattern: string, @@ -195,14 +195,14 @@ export class StringUtils { * - isn't "bracketed" * - isn't preceded by an escape character * - * @param {string} pattern - Source pattern - * @param {string} searchedCharacter - Searched character - * @param {number} start - Start index - * @param {string} escapeCharacter - Escape character, \ by default - * @param {string} openBracket - Open bracket, ( by default - * @param {string} closeBracket - Close bracket, ( by default + * @param pattern - Source pattern + * @param searchedCharacter - Searched character + * @param start - Start index + * @param escapeCharacter - Escape character, \ by default + * @param openBracket - Open bracket, ( by default + * @param closeBracket - Close bracket, ( by default * @throws If the opening and closing brackets are the same - * @returns {number} Index or -1 if the character not found + * @returns Index or -1 if the character not found */ public static findNextNotBracketedUnescapedCharacter( pattern: string, @@ -236,9 +236,9 @@ export class StringUtils { * - isn't part of any string literal ('literal' or "literal") * - isn't preceded by an escape character * - * @param {string} pattern - Source pattern - * @param {string} delimeterCharacter - Delimeter character - * @returns {string[]} Splitted string + * @param pattern - Source pattern + * @param delimeterCharacter - Delimeter character + * @returns Splitted string */ public static splitStringByUnquotedUnescapedCharacter(pattern: string, delimeterCharacter: string): string[] { const parts: string[] = []; @@ -265,9 +265,9 @@ export class StringUtils { * - isn't part of any RegExp expression (/regexp/) * - isn't preceded by an escape character * - * @param {string} pattern - Source pattern - * @param {string} delimeterCharacter - Delimeter character - * @returns {string[]} Splitted string + * @param pattern - Source pattern + * @param delimeterCharacter - Delimeter character + * @returns Splitted string */ public static splitStringByUnescapedNonStringNonRegexChar(pattern: string, delimeterCharacter: string): string[] { const parts: string[] = []; @@ -292,9 +292,9 @@ export class StringUtils { * Splits the source pattern along characters that: * - isn't preceded by an escape character * - * @param {string} pattern - Source pattern - * @param {string} delimeterCharacter - Delimeter character - * @returns {string[]} Splitted string + * @param pattern - Source pattern + * @param delimeterCharacter - Delimeter character + * @returns Splitted string */ public static splitStringByUnescapedCharacter(pattern: string, delimeterCharacter: string): string[] { const parts: string[] = []; @@ -314,8 +314,8 @@ export class StringUtils { /** *Checks if a character is whitespace. * - * @param {string} character - Character to check - * @returns {boolean} true/false + * @param character - Character to check + * @returns true/false */ public static isWhitespace(character: string): boolean { return character == SPACE || character == TAB; @@ -324,9 +324,9 @@ export class StringUtils { /** * Searches for the first non-whitespace character in the source pattern. * - * @param {string} pattern - Source pattern - * @param {number} start - Start index - * @returns {number} Index or -1 if the character not found + * @param pattern - Source pattern + * @param start - Start index + * @returns Index or -1 if the character not found */ public static findFirstNonWhitespaceCharacter(pattern: string, start = 0): number { for (let i = start; i < pattern.length; i++) { @@ -340,8 +340,8 @@ export class StringUtils { /** * Searches for the last non-whitespace character in the source pattern. * - * @param {string} pattern - Source pattern - * @returns {number} Index or -1 if the character not found + * @param pattern - Source pattern + * @returns Index or -1 if the character not found */ public static findLastNonWhitespaceCharacter(pattern: string): number { for (let i = pattern.length - 1; i >= 0; i--) { @@ -355,8 +355,8 @@ export class StringUtils { /** * Checks whether a string is a RegExp pattern. * - * @param {string} pattern - Pattern to check - * @returns {boolean} true/false + * @param pattern - Pattern to check + * @returns true/false */ public static isRegexPattern(pattern: string): boolean { const trimmedPattern = pattern.trim(); @@ -371,10 +371,10 @@ export class StringUtils { /** * Escapes a specified character in the string. * - * @param {string} pattern - Input string - * @param {string} character - Character to escape - * @param {string} escapeCharacter - Escape character (optional) - * @returns {boolean} true/false + * @param pattern - Input string + * @param character - Character to escape + * @param escapeCharacter - Escape character (optional) + * @returns true/false */ public static escapeCharacter(pattern: string, character: string, escapeCharacter = ESCAPE_CHARACTER): string { let result = EMPTY; diff --git a/test/parser/comment/agent.test.ts b/test/parser/comment/agent.test.ts index e0f2a599..5fb76c0f 100644 --- a/test/parser/comment/agent.test.ts +++ b/test/parser/comment/agent.test.ts @@ -1,6 +1,6 @@ -import { AgentParser, IAgent } from "../../../src/parser/comment/agent"; +import { AgentParser, Agent } from "../../../src/parser/comment/agent"; import { CommentRuleType } from "../../../src/parser/comment/common"; -import { RuleCategories } from "../../../src/parser/common"; +import { RuleCategory } from "../../../src/parser/common"; import { AdblockSyntax } from "../../../src/utils/adblockers"; import { EMPTY, SPACE } from "../../../src/utils/constants"; @@ -32,64 +32,64 @@ describe("AgentParser", () => { expect(AgentParser.parse(SPACE)).toBeNull(); // Valid agents - expect(AgentParser.parse("[]")).toEqual({ - category: RuleCategories.Comment, + expect(AgentParser.parse("[]")).toEqual({ + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.Agent, agents: [], }); - expect(AgentParser.parse("[ ]")).toEqual({ - category: RuleCategories.Comment, + expect(AgentParser.parse("[ ]")).toEqual({ + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.Agent, agents: [], }); - expect(AgentParser.parse("[ ]")).toEqual({ - category: RuleCategories.Comment, + expect(AgentParser.parse("[ ]")).toEqual({ + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.Agent, agents: [], }); - expect(AgentParser.parse("[;]")).toEqual({ - category: RuleCategories.Comment, + expect(AgentParser.parse("[;]")).toEqual({ + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.Agent, agents: [], }); - expect(AgentParser.parse("[ ; ]")).toEqual({ - category: RuleCategories.Comment, + expect(AgentParser.parse("[ ; ]")).toEqual({ + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.Agent, agents: [], }); - expect(AgentParser.parse("[ ; ]")).toEqual({ - category: RuleCategories.Comment, + expect(AgentParser.parse("[ ; ]")).toEqual({ + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.Agent, agents: [], }); - expect(AgentParser.parse("[;;;]")).toEqual({ - category: RuleCategories.Comment, + expect(AgentParser.parse("[;;;]")).toEqual({ + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.Agent, agents: [], }); - expect(AgentParser.parse("[ ; ; ; ]")).toEqual({ - category: RuleCategories.Comment, + expect(AgentParser.parse("[ ; ; ; ]")).toEqual({ + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.Agent, agents: [], }); - expect(AgentParser.parse("[AdBlock]")).toEqual({ - category: RuleCategories.Comment, + expect(AgentParser.parse("[AdBlock]")).toEqual({ + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.Agent, agents: [ @@ -99,8 +99,8 @@ describe("AgentParser", () => { ], }); - expect(AgentParser.parse("[AdGuard]")).toEqual({ - category: RuleCategories.Comment, + expect(AgentParser.parse("[AdGuard]")).toEqual({ + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.Agent, agents: [ @@ -110,8 +110,8 @@ describe("AgentParser", () => { ], }); - expect(AgentParser.parse("[uBlock]")).toEqual({ - category: RuleCategories.Comment, + expect(AgentParser.parse("[uBlock]")).toEqual({ + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.Agent, agents: [ @@ -121,8 +121,8 @@ describe("AgentParser", () => { ], }); - expect(AgentParser.parse("[uBlock Origin]")).toEqual({ - category: RuleCategories.Comment, + expect(AgentParser.parse("[uBlock Origin]")).toEqual({ + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.Agent, agents: [ @@ -132,8 +132,8 @@ describe("AgentParser", () => { ], }); - expect(AgentParser.parse("[Adblock Plus 2.0]")).toEqual({ - category: RuleCategories.Comment, + expect(AgentParser.parse("[Adblock Plus 2.0]")).toEqual({ + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.Agent, agents: [ @@ -144,8 +144,8 @@ describe("AgentParser", () => { ], }); - expect(AgentParser.parse("[uBlock Origin 1.0.0]")).toEqual({ - category: RuleCategories.Comment, + expect(AgentParser.parse("[uBlock Origin 1.0.0]")).toEqual({ + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.Agent, agents: [ @@ -156,8 +156,8 @@ describe("AgentParser", () => { ], }); - expect(AgentParser.parse("[Adblock Plus 2.0; AdGuard]")).toEqual({ - category: RuleCategories.Comment, + expect(AgentParser.parse("[Adblock Plus 2.0; AdGuard]")).toEqual({ + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.Agent, agents: [ @@ -171,8 +171,8 @@ describe("AgentParser", () => { ], }); - expect(AgentParser.parse("[Adblock Plus 2.0; AdGuard 1.0.1.10]")).toEqual({ - category: RuleCategories.Comment, + expect(AgentParser.parse("[Adblock Plus 2.0; AdGuard 1.0.1.10]")).toEqual({ + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.Agent, agents: [ @@ -187,8 +187,8 @@ describe("AgentParser", () => { ], }); - expect(AgentParser.parse("[Adblock Plus 3.1; AdGuard 1.4; uBlock Origin 1.0.15.0]")).toEqual({ - category: RuleCategories.Comment, + expect(AgentParser.parse("[Adblock Plus 3.1; AdGuard 1.4; uBlock Origin 1.0.15.0]")).toEqual({ + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.Agent, agents: [ @@ -207,8 +207,8 @@ describe("AgentParser", () => { ], }); - expect(AgentParser.parse("![Adblock Plus 3.1]")).toEqual({ - category: RuleCategories.Comment, + expect(AgentParser.parse("![Adblock Plus 3.1]")).toEqual({ + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.Agent, agents: [ @@ -219,8 +219,8 @@ describe("AgentParser", () => { ], }); - expect(AgentParser.parse("! [Adblock Plus 3.1]")).toEqual({ - category: RuleCategories.Comment, + expect(AgentParser.parse("! [Adblock Plus 3.1]")).toEqual({ + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.Agent, agents: [ @@ -231,8 +231,8 @@ describe("AgentParser", () => { ], }); - expect(AgentParser.parse("#[Adblock Plus 3.1]")).toEqual({ - category: RuleCategories.Comment, + expect(AgentParser.parse("#[Adblock Plus 3.1]")).toEqual({ + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.Agent, agents: [ @@ -243,8 +243,8 @@ describe("AgentParser", () => { ], }); - expect(AgentParser.parse("# [Adblock Plus 3.1]")).toEqual({ - category: RuleCategories.Comment, + expect(AgentParser.parse("# [Adblock Plus 3.1]")).toEqual({ + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.Agent, agents: [ @@ -255,8 +255,8 @@ describe("AgentParser", () => { ], }); - expect(AgentParser.parse("# [Adblock Plus 3.1 beta]")).toEqual({ - category: RuleCategories.Comment, + expect(AgentParser.parse("# [Adblock Plus 3.1 beta]")).toEqual({ + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.Agent, agents: [ @@ -268,8 +268,8 @@ describe("AgentParser", () => { }); // Syntactically this is fine: - expect(AgentParser.parse("[2.0]")).toEqual({ - category: RuleCategories.Comment, + expect(AgentParser.parse("[2.0]")).toEqual({ + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.Agent, agents: [ diff --git a/test/parser/comment/comment.test.ts b/test/parser/comment/comment.test.ts index 50659433..b0fabc8b 100644 --- a/test/parser/comment/comment.test.ts +++ b/test/parser/comment/comment.test.ts @@ -1,7 +1,7 @@ import { CommentParser } from "../../../src/parser/comment/comment"; import { CommentRuleType } from "../../../src/parser/comment/common"; -import { IMetadata } from "../../../src/parser/comment/metadata"; -import { RuleCategories } from "../../../src/parser/common"; +import { Metadata } from "../../../src/parser/comment/metadata"; +import { RuleCategory } from "../../../src/parser/common"; import { AdblockSyntax } from "../../../src/utils/adblockers"; import { EMPTY, SPACE } from "../../../src/utils/constants"; @@ -61,14 +61,14 @@ describe("CommentParser", () => { // Agents expect(CommentParser.parse("[]")).toEqual({ - category: RuleCategories.Comment, + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.Agent, agents: [], }); expect(CommentParser.parse("[Adblock Plus 2.0]")).toEqual({ - category: RuleCategories.Comment, + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.Agent, agents: [ @@ -80,7 +80,7 @@ describe("CommentParser", () => { }); expect(CommentParser.parse("[AdGuard]")).toEqual({ - category: RuleCategories.Comment, + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.Agent, agents: [ @@ -92,8 +92,8 @@ describe("CommentParser", () => { // Hints expect(CommentParser.parse("!+ NOT_OPTIMIZED")).toEqual({ - category: RuleCategories.Comment, - syntax: AdblockSyntax.AdGuard, + category: RuleCategory.Comment, + syntax: AdblockSyntax.Adg, type: CommentRuleType.Hint, hints: [ { @@ -104,8 +104,8 @@ describe("CommentParser", () => { }); expect(CommentParser.parse("!+NOT_OPTIMIZED")).toEqual({ - category: RuleCategories.Comment, - syntax: AdblockSyntax.AdGuard, + category: RuleCategory.Comment, + syntax: AdblockSyntax.Adg, type: CommentRuleType.Hint, hints: [ { @@ -116,8 +116,8 @@ describe("CommentParser", () => { }); expect(CommentParser.parse("!+ NOT_OPTIMIZED PLATFORM(windows, mac) NOT_PLATFORM(android, ios)")).toEqual({ - category: RuleCategories.Comment, - syntax: AdblockSyntax.AdGuard, + category: RuleCategory.Comment, + syntax: AdblockSyntax.Adg, type: CommentRuleType.Hint, hints: [ { @@ -137,7 +137,7 @@ describe("CommentParser", () => { // Pre processors expect(CommentParser.parse("!#if (adguard)")).toEqual({ - category: RuleCategories.Comment, + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.PreProcessor, name: "if", @@ -145,7 +145,7 @@ describe("CommentParser", () => { }); expect(CommentParser.parse("!#if (adguard && !adguard_ext_safari)")).toEqual({ - category: RuleCategories.Comment, + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.PreProcessor, name: "if", @@ -153,7 +153,7 @@ describe("CommentParser", () => { }); expect(CommentParser.parse("!#include https://example.org/path/includedfile.txt")).toEqual({ - category: RuleCategories.Comment, + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.PreProcessor, name: "include", @@ -161,8 +161,8 @@ describe("CommentParser", () => { }); // Metadata - expect(CommentParser.parse("! Title: Filter")).toEqual({ - category: RuleCategories.Comment, + expect(CommentParser.parse("! Title: Filter")).toEqual({ + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.Metadata, marker: "!", @@ -170,8 +170,8 @@ describe("CommentParser", () => { value: "Filter", }); - expect(CommentParser.parse("! Homepage: https://github.com/AdguardTeam/some-repo/wiki")).toEqual({ - category: RuleCategories.Comment, + expect(CommentParser.parse("! Homepage: https://github.com/AdguardTeam/some-repo/wiki")).toEqual({ + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.Metadata, marker: "!", @@ -179,8 +179,8 @@ describe("CommentParser", () => { value: "https://github.com/AdguardTeam/some-repo/wiki", }); - expect(CommentParser.parse("# Homepage: https://github.com/AdguardTeam/some-repo/wiki")).toEqual({ - category: RuleCategories.Comment, + expect(CommentParser.parse("# Homepage: https://github.com/AdguardTeam/some-repo/wiki")).toEqual({ + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.Metadata, marker: "#", @@ -190,7 +190,7 @@ describe("CommentParser", () => { // Config comments expect(CommentParser.parse("! aglint-disable rule1, rule2")).toEqual({ - category: RuleCategories.Comment, + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.ConfigComment, marker: "!", @@ -199,7 +199,7 @@ describe("CommentParser", () => { }); expect(CommentParser.parse("! aglint-enable rule1, rule2")).toEqual({ - category: RuleCategories.Comment, + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.ConfigComment, marker: "!", @@ -208,7 +208,7 @@ describe("CommentParser", () => { }); expect(CommentParser.parse("# aglint-disable rule1, rule2")).toEqual({ - category: RuleCategories.Comment, + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.ConfigComment, marker: "#", @@ -217,7 +217,7 @@ describe("CommentParser", () => { }); expect(CommentParser.parse("# aglint-enable rule1, rule2")).toEqual({ - category: RuleCategories.Comment, + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.ConfigComment, marker: "#", @@ -226,7 +226,7 @@ describe("CommentParser", () => { }); expect(CommentParser.parse('! aglint rule1: "off", rule2: ["a", "b"] -- this is a comment')).toEqual({ - category: RuleCategories.Comment, + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.ConfigComment, marker: "!", @@ -239,7 +239,7 @@ describe("CommentParser", () => { }); expect(CommentParser.parse('# aglint rule1: "off", rule2: ["a", "b"] -- this is a comment')).toEqual({ - category: RuleCategories.Comment, + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.ConfigComment, marker: "#", @@ -253,7 +253,7 @@ describe("CommentParser", () => { // Comments expect(CommentParser.parse("! This is just a comment")).toEqual({ - category: RuleCategories.Comment, + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.Comment, marker: "!", @@ -261,7 +261,7 @@ describe("CommentParser", () => { }); expect(CommentParser.parse("# This is just a comment")).toEqual({ - category: RuleCategories.Comment, + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.Comment, marker: "#", @@ -269,7 +269,7 @@ describe("CommentParser", () => { }); expect(CommentParser.parse("!#########################")).toEqual({ - category: RuleCategories.Comment, + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.Comment, marker: "!", @@ -277,7 +277,7 @@ describe("CommentParser", () => { }); expect(CommentParser.parse("##########################")).toEqual({ - category: RuleCategories.Comment, + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.Comment, marker: "#", diff --git a/test/parser/comment/hint.test.ts b/test/parser/comment/hint.test.ts index 018dcbbe..9aca7c4a 100644 --- a/test/parser/comment/hint.test.ts +++ b/test/parser/comment/hint.test.ts @@ -1,6 +1,6 @@ import { CommentRuleType } from "../../../src/parser/comment/common"; -import { HintParser, IHint } from "../../../src/parser/comment/hint"; -import { RuleCategories } from "../../../src/parser/common"; +import { HintParser, Hint } from "../../../src/parser/comment/hint"; +import { RuleCategory } from "../../../src/parser/common"; import { AdblockSyntax } from "../../../src/utils/adblockers"; import { EMPTY, SPACE } from "../../../src/utils/constants"; @@ -20,24 +20,24 @@ describe("HintParser", () => { test("parse", () => { // Empty - expect(HintParser.parse("!+")).toEqual({ - category: RuleCategories.Comment, - syntax: AdblockSyntax.AdGuard, + expect(HintParser.parse("!+")).toEqual({ + category: RuleCategory.Comment, + syntax: AdblockSyntax.Adg, type: CommentRuleType.Hint, hints: [], }); - expect(HintParser.parse("!+ ")).toEqual({ - category: RuleCategories.Comment, - syntax: AdblockSyntax.AdGuard, + expect(HintParser.parse("!+ ")).toEqual({ + category: RuleCategory.Comment, + syntax: AdblockSyntax.Adg, type: CommentRuleType.Hint, hints: [], }); // Without parameters - expect(HintParser.parse("!+NOT_OPTIMIZED")).toEqual({ - category: RuleCategories.Comment, - syntax: AdblockSyntax.AdGuard, + expect(HintParser.parse("!+NOT_OPTIMIZED")).toEqual({ + category: RuleCategory.Comment, + syntax: AdblockSyntax.Adg, type: CommentRuleType.Hint, hints: [ { @@ -47,9 +47,9 @@ describe("HintParser", () => { ], }); - expect(HintParser.parse("!+ NOT_OPTIMIZED")).toEqual({ - category: RuleCategories.Comment, - syntax: AdblockSyntax.AdGuard, + expect(HintParser.parse("!+ NOT_OPTIMIZED")).toEqual({ + category: RuleCategory.Comment, + syntax: AdblockSyntax.Adg, type: CommentRuleType.Hint, hints: [ { @@ -60,9 +60,9 @@ describe("HintParser", () => { }); // Multiple, without parameters - expect(HintParser.parse("!+ HINT_NAME1 HINT_NAME2")).toEqual({ - category: RuleCategories.Comment, - syntax: AdblockSyntax.AdGuard, + expect(HintParser.parse("!+ HINT_NAME1 HINT_NAME2")).toEqual({ + category: RuleCategory.Comment, + syntax: AdblockSyntax.Adg, type: CommentRuleType.Hint, hints: [ { @@ -77,9 +77,9 @@ describe("HintParser", () => { }); // Without parameters, but with empty parameter list () - expect(HintParser.parse("!+ HINT_NAME1()")).toEqual({ - category: RuleCategories.Comment, - syntax: AdblockSyntax.AdGuard, + expect(HintParser.parse("!+ HINT_NAME1()")).toEqual({ + category: RuleCategory.Comment, + syntax: AdblockSyntax.Adg, type: CommentRuleType.Hint, hints: [ { @@ -89,9 +89,9 @@ describe("HintParser", () => { ], }); - expect(HintParser.parse("!+ HINT_NAME1( )")).toEqual({ - category: RuleCategories.Comment, - syntax: AdblockSyntax.AdGuard, + expect(HintParser.parse("!+ HINT_NAME1( )")).toEqual({ + category: RuleCategory.Comment, + syntax: AdblockSyntax.Adg, type: CommentRuleType.Hint, hints: [ { @@ -101,9 +101,9 @@ describe("HintParser", () => { ], }); - expect(HintParser.parse("!+ HINT_NAME1() HINT_NAME2()")).toEqual({ - category: RuleCategories.Comment, - syntax: AdblockSyntax.AdGuard, + expect(HintParser.parse("!+ HINT_NAME1() HINT_NAME2()")).toEqual({ + category: RuleCategory.Comment, + syntax: AdblockSyntax.Adg, type: CommentRuleType.Hint, hints: [ { @@ -118,9 +118,9 @@ describe("HintParser", () => { }); // Variadic - expect(HintParser.parse("!+ HINT_NAME1(param0, param1) HINT_NAME2()")).toEqual({ - category: RuleCategories.Comment, - syntax: AdblockSyntax.AdGuard, + expect(HintParser.parse("!+ HINT_NAME1(param0, param1) HINT_NAME2()")).toEqual({ + category: RuleCategory.Comment, + syntax: AdblockSyntax.Adg, type: CommentRuleType.Hint, hints: [ { @@ -136,9 +136,9 @@ describe("HintParser", () => { expect(HintParser.parse("!+ HINT_NAME1(param0, param1) HINT_NAME2(param0)")).toEqual( // eslint-disable-next-line prettier/prettier - { - category: RuleCategories.Comment, - syntax: AdblockSyntax.AdGuard, + { + category: RuleCategory.Comment, + syntax: AdblockSyntax.Adg, type: CommentRuleType.Hint, hints: [ { @@ -156,9 +156,9 @@ describe("HintParser", () => { // Skipped parameters expect(HintParser.parse("!+ HINT_NAME(param0, , param1)")).toEqual( // eslint-disable-next-line prettier/prettier - { - category: RuleCategories.Comment, - syntax: AdblockSyntax.AdGuard, + { + category: RuleCategory.Comment, + syntax: AdblockSyntax.Adg, type: CommentRuleType.Hint, hints: [ { @@ -171,9 +171,9 @@ describe("HintParser", () => { expect(HintParser.parse("!+ HINT_NAME(param0, , , )")).toEqual( // eslint-disable-next-line prettier/prettier - { - category: RuleCategories.Comment, - syntax: AdblockSyntax.AdGuard, + { + category: RuleCategory.Comment, + syntax: AdblockSyntax.Adg, type: CommentRuleType.Hint, hints: [ { @@ -186,9 +186,9 @@ describe("HintParser", () => { expect(HintParser.parse("!+ HINT_NAME( , , , )")).toEqual( // eslint-disable-next-line prettier/prettier - { - category: RuleCategories.Comment, - syntax: AdblockSyntax.AdGuard, + { + category: RuleCategory.Comment, + syntax: AdblockSyntax.Adg, type: CommentRuleType.Hint, hints: [ { @@ -201,9 +201,9 @@ describe("HintParser", () => { expect(HintParser.parse("!+ HINT_NAME(,,,)")).toEqual( // eslint-disable-next-line prettier/prettier - { - category: RuleCategories.Comment, - syntax: AdblockSyntax.AdGuard, + { + category: RuleCategory.Comment, + syntax: AdblockSyntax.Adg, type: CommentRuleType.Hint, hints: [ { @@ -217,9 +217,9 @@ describe("HintParser", () => { // Spaces expect(HintParser.parse("!+ HINT_NAME( p0 , p1 , p2 , p3)")).toEqual( // eslint-disable-next-line prettier/prettier - { - category: RuleCategories.Comment, - syntax: AdblockSyntax.AdGuard, + { + category: RuleCategory.Comment, + syntax: AdblockSyntax.Adg, type: CommentRuleType.Hint, hints: [ { @@ -232,9 +232,9 @@ describe("HintParser", () => { expect(HintParser.parse("!+ HINT_NAME(hello world, hello world)")).toEqual( // eslint-disable-next-line prettier/prettier - { - category: RuleCategories.Comment, - syntax: AdblockSyntax.AdGuard, + { + category: RuleCategory.Comment, + syntax: AdblockSyntax.Adg, type: CommentRuleType.Hint, hints: [ { @@ -247,9 +247,9 @@ describe("HintParser", () => { expect(HintParser.parse("!+ hint_name(hello world, hello world)")).toEqual( // eslint-disable-next-line prettier/prettier - { - category: RuleCategories.Comment, - syntax: AdblockSyntax.AdGuard, + { + category: RuleCategory.Comment, + syntax: AdblockSyntax.Adg, type: CommentRuleType.Hint, hints: [ { diff --git a/test/parser/comment/inline-config.test.ts b/test/parser/comment/inline-config.test.ts index cd1a407b..f25be1a3 100644 --- a/test/parser/comment/inline-config.test.ts +++ b/test/parser/comment/inline-config.test.ts @@ -1,7 +1,7 @@ /* eslint-disable max-len */ import { CommentMarker, CommentRuleType } from "../../../src/parser/comment/common"; -import { IConfigComment, ConfigCommentParser } from "../../../src/parser/comment/inline-config"; -import { RuleCategories } from "../../../src/parser/common"; +import { ConfigComment, ConfigCommentParser } from "../../../src/parser/comment/inline-config"; +import { RuleCategory } from "../../../src/parser/common"; import { AdblockSyntax } from "../../../src/utils/adblockers"; import { EMPTY, SPACE } from "../../../src/utils/constants"; @@ -85,16 +85,16 @@ describe("PreProcessorParser", () => { test("parse", () => { // ! - expect(ConfigCommentParser.parse("! aglint-disable")).toEqual({ - category: RuleCategories.Comment, + expect(ConfigCommentParser.parse("! aglint-disable")).toEqual({ + category: RuleCategory.Comment, type: CommentRuleType.ConfigComment, syntax: AdblockSyntax.Unknown, marker: CommentMarker.Regular, command: "aglint-disable", }); - expect(ConfigCommentParser.parse("!aglint-disable")).toEqual({ - category: RuleCategories.Comment, + expect(ConfigCommentParser.parse("!aglint-disable")).toEqual({ + category: RuleCategory.Comment, type: CommentRuleType.ConfigComment, syntax: AdblockSyntax.Unknown, marker: CommentMarker.Regular, @@ -102,16 +102,16 @@ describe("PreProcessorParser", () => { }); // # - expect(ConfigCommentParser.parse("# aglint-disable")).toEqual({ - category: RuleCategories.Comment, + expect(ConfigCommentParser.parse("# aglint-disable")).toEqual({ + category: RuleCategory.Comment, type: CommentRuleType.ConfigComment, syntax: AdblockSyntax.Unknown, marker: CommentMarker.Hashmark, command: "aglint-disable", }); - expect(ConfigCommentParser.parse("#aglint-disable")).toEqual({ - category: RuleCategories.Comment, + expect(ConfigCommentParser.parse("#aglint-disable")).toEqual({ + category: RuleCategory.Comment, type: CommentRuleType.ConfigComment, syntax: AdblockSyntax.Unknown, marker: CommentMarker.Hashmark, @@ -119,16 +119,16 @@ describe("PreProcessorParser", () => { }); // Different command - expect(ConfigCommentParser.parse("! aglint-enable")).toEqual({ - category: RuleCategories.Comment, + expect(ConfigCommentParser.parse("! aglint-enable")).toEqual({ + category: RuleCategory.Comment, type: CommentRuleType.ConfigComment, syntax: AdblockSyntax.Unknown, marker: CommentMarker.Regular, command: "aglint-enable", }); - expect(ConfigCommentParser.parse("! aglint-enable rule1")).toEqual({ - category: RuleCategories.Comment, + expect(ConfigCommentParser.parse("! aglint-enable rule1")).toEqual({ + category: RuleCategory.Comment, type: CommentRuleType.ConfigComment, syntax: AdblockSyntax.Unknown, marker: CommentMarker.Regular, @@ -136,8 +136,8 @@ describe("PreProcessorParser", () => { params: ["rule1"], }); - expect(ConfigCommentParser.parse("! aglint-enable rule1,rule2")).toEqual({ - category: RuleCategories.Comment, + expect(ConfigCommentParser.parse("! aglint-enable rule1,rule2")).toEqual({ + category: RuleCategory.Comment, type: CommentRuleType.ConfigComment, syntax: AdblockSyntax.Unknown, marker: CommentMarker.Regular, @@ -145,8 +145,8 @@ describe("PreProcessorParser", () => { params: ["rule1", "rule2"], }); - expect(ConfigCommentParser.parse("! aglint-enable rule1, rule2")).toEqual({ - category: RuleCategories.Comment, + expect(ConfigCommentParser.parse("! aglint-enable rule1, rule2")).toEqual({ + category: RuleCategory.Comment, type: CommentRuleType.ConfigComment, syntax: AdblockSyntax.Unknown, marker: CommentMarker.Regular, @@ -155,8 +155,8 @@ describe("PreProcessorParser", () => { }); // Ignore comment - expect(ConfigCommentParser.parse("! aglint-enable rule1, rule2 -- comment")).toEqual({ - category: RuleCategories.Comment, + expect(ConfigCommentParser.parse("! aglint-enable rule1, rule2 -- comment")).toEqual({ + category: RuleCategory.Comment, type: CommentRuleType.ConfigComment, syntax: AdblockSyntax.Unknown, marker: CommentMarker.Regular, @@ -165,8 +165,8 @@ describe("PreProcessorParser", () => { comment: "comment", }); - expect(ConfigCommentParser.parse('! aglint rule1: "off"')).toEqual({ - category: RuleCategories.Comment, + expect(ConfigCommentParser.parse('! aglint rule1: "off"')).toEqual({ + category: RuleCategory.Comment, type: CommentRuleType.ConfigComment, syntax: AdblockSyntax.Unknown, marker: CommentMarker.Regular, @@ -176,8 +176,8 @@ describe("PreProcessorParser", () => { }, }); - expect(ConfigCommentParser.parse("! aglint rule1: 1")).toEqual({ - category: RuleCategories.Comment, + expect(ConfigCommentParser.parse("! aglint rule1: 1")).toEqual({ + category: RuleCategory.Comment, type: CommentRuleType.ConfigComment, syntax: AdblockSyntax.Unknown, marker: CommentMarker.Regular, @@ -187,8 +187,8 @@ describe("PreProcessorParser", () => { }, }); - expect(ConfigCommentParser.parse('! aglint rule1: ["error", "double"]')).toEqual({ - category: RuleCategories.Comment, + expect(ConfigCommentParser.parse('! aglint rule1: ["error", "double"]')).toEqual({ + category: RuleCategory.Comment, type: CommentRuleType.ConfigComment, syntax: AdblockSyntax.Unknown, marker: CommentMarker.Regular, @@ -203,8 +203,8 @@ describe("PreProcessorParser", () => { ConfigCommentParser.parse( '! aglint rule1: "off", rule2: [1, 2], rule3: ["error", { "max": 100 }] -- this is a comment -- this doesn\'t matter' ) - ).toEqual({ - category: RuleCategories.Comment, + ).toEqual({ + category: RuleCategory.Comment, type: CommentRuleType.ConfigComment, syntax: AdblockSyntax.Unknown, marker: CommentMarker.Regular, diff --git a/test/parser/comment/metadata.test.ts b/test/parser/comment/metadata.test.ts index 75001c03..f879fe71 100644 --- a/test/parser/comment/metadata.test.ts +++ b/test/parser/comment/metadata.test.ts @@ -1,6 +1,6 @@ import { CommentRuleType } from "../../../src/parser/comment/common"; -import { IMetadata, MetadataParser } from "../../../src/parser/comment/metadata"; -import { RuleCategories } from "../../../src/parser/common"; +import { Metadata, MetadataParser } from "../../../src/parser/comment/metadata"; +import { RuleCategory } from "../../../src/parser/common"; import { AdblockSyntax } from "../../../src/utils/adblockers"; import { EMPTY, SPACE } from "../../../src/utils/constants"; @@ -19,8 +19,8 @@ describe("MetadataParser", () => { expect(MetadataParser.parse("!:::")).toBe(null); expect(MetadataParser.parse("! : : :")).toBe(null); - expect(MetadataParser.parse("! Title: Filter")).toEqual({ - category: RuleCategories.Comment, + expect(MetadataParser.parse("! Title: Filter")).toEqual({ + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.Metadata, marker: "!", @@ -28,8 +28,8 @@ describe("MetadataParser", () => { value: "Filter", }); - expect(MetadataParser.parse("# Title: Filter")).toEqual({ - category: RuleCategories.Comment, + expect(MetadataParser.parse("# Title: Filter")).toEqual({ + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.Metadata, marker: "#", @@ -37,8 +37,8 @@ describe("MetadataParser", () => { value: "Filter", }); - expect(MetadataParser.parse("! title: Filter")).toEqual({ - category: RuleCategories.Comment, + expect(MetadataParser.parse("! title: Filter")).toEqual({ + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.Metadata, marker: "!", @@ -46,8 +46,8 @@ describe("MetadataParser", () => { value: "Filter", }); - expect(MetadataParser.parse("! title: Filter ")).toEqual({ - category: RuleCategories.Comment, + expect(MetadataParser.parse("! title: Filter ")).toEqual({ + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.Metadata, marker: "!", @@ -55,8 +55,8 @@ describe("MetadataParser", () => { value: "Filter", }); - expect(MetadataParser.parse("! Homepage: https://github.com/AdguardTeam/some-repo/wiki")).toEqual({ - category: RuleCategories.Comment, + expect(MetadataParser.parse("! Homepage: https://github.com/AdguardTeam/some-repo/wiki")).toEqual({ + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.Metadata, marker: "!", diff --git a/test/parser/comment/preprocessor.test.ts b/test/parser/comment/preprocessor.test.ts index 51653b06..10b9949c 100644 --- a/test/parser/comment/preprocessor.test.ts +++ b/test/parser/comment/preprocessor.test.ts @@ -1,6 +1,6 @@ import { CommentRuleType } from "../../../src/parser/comment/common"; -import { IPreProcessor, PreProcessorParser } from "../../../src/parser/comment/preprocessor"; -import { RuleCategories } from "../../../src/parser/common"; +import { PreProcessor, PreProcessorParser } from "../../../src/parser/comment/preprocessor"; +import { RuleCategory } from "../../../src/parser/common"; import { AdblockSyntax } from "../../../src/utils/adblockers"; import { EMPTY, SPACE } from "../../../src/utils/constants"; @@ -17,48 +17,48 @@ describe("PreProcessorParser", () => { test("parse", () => { // Valid pre-processors - expect(PreProcessorParser.parse("!#endif")).toEqual({ - category: RuleCategories.Comment, + expect(PreProcessorParser.parse("!#endif")).toEqual({ + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.PreProcessor, name: "endif", params: undefined, }); - expect(PreProcessorParser.parse("!#include ../sections/ads.txt")).toEqual({ - category: RuleCategories.Comment, + expect(PreProcessorParser.parse("!#include ../sections/ads.txt")).toEqual({ + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.PreProcessor, name: "include", params: "../sections/ads.txt", }); - expect(PreProcessorParser.parse("!#include ")).toEqual({ - category: RuleCategories.Comment, + expect(PreProcessorParser.parse("!#include ")).toEqual({ + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.PreProcessor, name: "include", params: undefined, }); - expect(PreProcessorParser.parse("!#if (conditions)")).toEqual({ - category: RuleCategories.Comment, + expect(PreProcessorParser.parse("!#if (conditions)")).toEqual({ + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.PreProcessor, name: "if", params: "(conditions)", }); - expect(PreProcessorParser.parse("!#if (conditions)")).toEqual({ - category: RuleCategories.Comment, + expect(PreProcessorParser.parse("!#if (conditions)")).toEqual({ + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.PreProcessor, name: "if", params: "(conditions)", }); - expect(PreProcessorParser.parse("!#safari_cb_affinity(content_blockers)")).toEqual({ - category: RuleCategories.Comment, + expect(PreProcessorParser.parse("!#safari_cb_affinity(content_blockers)")).toEqual({ + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.PreProcessor, // !#if is an operator, safari_cb_affinity is more of a "command" here @@ -67,8 +67,8 @@ describe("PreProcessorParser", () => { }); // If the parenthesis is open, do not split it in half along the space: - expect(PreProcessorParser.parse("!#aaa(bbb ccc)")).toEqual({ - category: RuleCategories.Comment, + expect(PreProcessorParser.parse("!#aaa(bbb ccc)")).toEqual({ + category: RuleCategory.Comment, syntax: AdblockSyntax.Unknown, type: CommentRuleType.PreProcessor, name: "aaa(bbb ccc)", diff --git a/test/parser/common/domain-list.test.ts b/test/parser/common/domain-list.test.ts index cbeca32d..78dd6ff8 100644 --- a/test/parser/common/domain-list.test.ts +++ b/test/parser/common/domain-list.test.ts @@ -2,32 +2,32 @@ import { DomainListParser, DomainListSeparator, DOMAIN_LIST_TYPE, - IDomainList, + DomainList, } from "../../../src/parser/common/domain-list"; -import { EMPTY } from "../../../src/utils/constants"; +import { COMMA, EMPTY } from "../../../src/utils/constants"; describe("DomainListParser", () => { test("parse", () => { // Single domain - expect(DomainListParser.parse("example.com")).toEqual({ + expect(DomainListParser.parse("example.com")).toEqual({ type: DOMAIN_LIST_TYPE, - separator: ",", + separator: COMMA, domains: [{ domain: "example.com", exception: false }], }); // Multiple domains - expect(DomainListParser.parse("example.com,example.net")).toEqual({ + expect(DomainListParser.parse("example.com,example.net")).toEqual({ type: DOMAIN_LIST_TYPE, - separator: ",", + separator: COMMA, domains: [ { domain: "example.com", exception: false }, { domain: "example.net", exception: false }, ], }); - expect(DomainListParser.parse("example.com,example.net,example.org")).toEqual({ + expect(DomainListParser.parse("example.com,example.net,example.org")).toEqual({ type: DOMAIN_LIST_TYPE, - separator: ",", + separator: COMMA, domains: [ { domain: "example.com", exception: false }, { domain: "example.net", exception: false }, @@ -36,25 +36,25 @@ describe("DomainListParser", () => { }); // Exception - single domain - expect(DomainListParser.parse("~example.com")).toEqual({ + expect(DomainListParser.parse("~example.com")).toEqual({ type: DOMAIN_LIST_TYPE, - separator: ",", + separator: COMMA, domains: [{ domain: "example.com", exception: true }], }); // Exception - multiple domains - expect(DomainListParser.parse("~example.com,~example.net")).toEqual({ + expect(DomainListParser.parse("~example.com,~example.net")).toEqual({ type: DOMAIN_LIST_TYPE, - separator: ",", + separator: COMMA, domains: [ { domain: "example.com", exception: true }, { domain: "example.net", exception: true }, ], }); - expect(DomainListParser.parse("~example.com,~example.net,~example.org")).toEqual({ + expect(DomainListParser.parse("~example.com,~example.net,~example.org")).toEqual({ type: DOMAIN_LIST_TYPE, - separator: ",", + separator: COMMA, domains: [ { domain: "example.com", exception: true }, { domain: "example.net", exception: true }, @@ -63,9 +63,9 @@ describe("DomainListParser", () => { }); // Mixed - multiple domains - expect(DomainListParser.parse("~example.com,~example.net,example.eu,~example.org")).toEqual({ + expect(DomainListParser.parse("~example.com,~example.net,example.eu,~example.org")).toEqual({ type: DOMAIN_LIST_TYPE, - separator: ",", + separator: COMMA, domains: [ { domain: "example.com", exception: true }, { domain: "example.net", exception: true }, @@ -76,10 +76,10 @@ describe("DomainListParser", () => { // Mixed - spaces (trim) expect(DomainListParser.parse("~example.com, example.net , example.eu , ~example.org")).toEqual(< - IDomainList + DomainList >{ type: DOMAIN_LIST_TYPE, - separator: ",", + separator: COMMA, domains: [ { domain: "example.com", exception: true }, { domain: "example.net", exception: false }, @@ -90,7 +90,7 @@ describe("DomainListParser", () => { expect( DomainListParser.parse("~example.com| example.net | example.eu | ~example.org", "|") - ).toEqual({ + ).toEqual({ type: DOMAIN_LIST_TYPE, separator: "|", domains: [ @@ -122,7 +122,7 @@ describe("DomainListParser", () => { }); test("generate", () => { - const parseAndGenerate = (raw: string, separator: DomainListSeparator = ",") => { + const parseAndGenerate = (raw: string, separator: DomainListSeparator = COMMA) => { const ast = DomainListParser.parse(raw, separator); if (ast) { diff --git a/test/parser/common/modifier-list.test.ts b/test/parser/common/modifier-list.test.ts index 3d7b240a..37f5b6a2 100644 --- a/test/parser/common/modifier-list.test.ts +++ b/test/parser/common/modifier-list.test.ts @@ -1,55 +1,55 @@ -import { IModifierList, ModifierListParser, MODIFIER_LIST_TYPE } from "../../../src/parser/common/modifier-list"; +import { ModifierList, ModifierListParser, MODIFIER_LIST_TYPE } from "../../../src/parser/common/modifier-list"; import { EMPTY, SPACE } from "../../../src/utils/constants"; describe("ModifierListParser", () => { test("parse", () => { // Empty modifiers - expect(ModifierListParser.parse(EMPTY)).toEqual({ + expect(ModifierListParser.parse(EMPTY)).toEqual({ type: MODIFIER_LIST_TYPE, modifiers: [], }); - expect(ModifierListParser.parse(SPACE)).toEqual({ + expect(ModifierListParser.parse(SPACE)).toEqual({ type: MODIFIER_LIST_TYPE, modifiers: [], }); // Valid modifiers - expect(ModifierListParser.parse("modifier1")).toEqual({ + expect(ModifierListParser.parse("modifier1")).toEqual({ type: MODIFIER_LIST_TYPE, modifiers: [{ modifier: "modifier1" }], }); - expect(ModifierListParser.parse("modifier1,modifier2")).toEqual({ + expect(ModifierListParser.parse("modifier1,modifier2")).toEqual({ type: MODIFIER_LIST_TYPE, modifiers: [{ modifier: "modifier1" }, { modifier: "modifier2" }], }); - expect(ModifierListParser.parse("modifier1, modifier2")).toEqual({ + expect(ModifierListParser.parse("modifier1, modifier2")).toEqual({ type: MODIFIER_LIST_TYPE, modifiers: [{ modifier: "modifier1" }, { modifier: "modifier2" }], }); - expect(ModifierListParser.parse("modifier1=value1")).toEqual({ + expect(ModifierListParser.parse("modifier1=value1")).toEqual({ type: MODIFIER_LIST_TYPE, modifiers: [{ modifier: "modifier1", value: "value1" }], }); - expect(ModifierListParser.parse("modifier1 = value1")).toEqual({ + expect(ModifierListParser.parse("modifier1 = value1")).toEqual({ type: MODIFIER_LIST_TYPE, modifiers: [{ modifier: "modifier1", value: "value1" }], }); - expect(ModifierListParser.parse(" modifier1 = value1 ")).toEqual({ + expect(ModifierListParser.parse(" modifier1 = value1 ")).toEqual({ type: MODIFIER_LIST_TYPE, modifiers: [{ modifier: "modifier1", value: "value1" }], }); - expect(ModifierListParser.parse("modifier1,modifier2=value2")).toEqual({ + expect(ModifierListParser.parse("modifier1,modifier2=value2")).toEqual({ type: MODIFIER_LIST_TYPE, modifiers: [{ modifier: "modifier1" }, { modifier: "modifier2", value: "value2" }], }); - expect(ModifierListParser.parse("modifier1=value1,modifier2=value2")).toEqual({ + expect(ModifierListParser.parse("modifier1=value1,modifier2=value2")).toEqual({ type: MODIFIER_LIST_TYPE, modifiers: [ { modifier: "modifier1", value: "value1" }, @@ -59,7 +59,7 @@ describe("ModifierListParser", () => { // Escaped separator comma // eslint-disable-next-line prettier/prettier - expect(ModifierListParser.parse("modifier1=a\\,b\\,c,modifier2=value2")).toEqual({ + expect(ModifierListParser.parse("modifier1=a\\,b\\,c,modifier2=value2")).toEqual({ type: MODIFIER_LIST_TYPE, modifiers: [ { modifier: "modifier1", value: "a\\,b\\,c" }, @@ -71,7 +71,7 @@ describe("ModifierListParser", () => { ModifierListParser.parse( "path=/\\/(sub1|sub2)\\/page\\.html/,replace=/()[\\s\\S]*<\\/VAST>/\\$1<\\/VAST>/i" ) - ).toEqual({ + ).toEqual({ type: MODIFIER_LIST_TYPE, modifiers: [ { diff --git a/test/parser/cosmetic/body/css.test.ts b/test/parser/cosmetic/body/css.test.ts index f0eebe88..509b08c1 100644 --- a/test/parser/cosmetic/body/css.test.ts +++ b/test/parser/cosmetic/body/css.test.ts @@ -1,74 +1,74 @@ /* eslint-disable max-len */ import { Selector } from "css-tree"; -import { CssInjectionBodyParser, ICssRuleBody, REMOVE_BLOCK_TYPE } from "../../../../src/parser/cosmetic/body/css"; +import { CssInjectionBodyParser, CssRuleBody, REMOVE_BLOCK_TYPE } from "../../../../src/parser/cosmetic/body/css"; import { AdblockSyntax } from "../../../../src/utils/adblockers"; import { EMPTY, SPACE } from "../../../../src/utils/constants"; import { CssTree } from "../../../../src/utils/csstree"; import { CssTreeParserContext } from "../../../../src/utils/csstree-constants"; describe("CssInjectionBodyParser", () => { - test("isUblockCssInjection", () => { - expect(CssInjectionBodyParser.isUblockCssInjection(EMPTY)).toBe(false); - expect(CssInjectionBodyParser.isUblockCssInjection(SPACE)).toBe(false); + test("isUboCssInjection", () => { + expect(CssInjectionBodyParser.isUboCssInjection(EMPTY)).toBe(false); + expect(CssInjectionBodyParser.isUboCssInjection(SPACE)).toBe(false); - expect(CssInjectionBodyParser.isUblockCssInjection(".ad")).toBe(false); + expect(CssInjectionBodyParser.isUboCssInjection(".ad")).toBe(false); - expect(CssInjectionBodyParser.isUblockCssInjection("body {}")).toBe(false); + expect(CssInjectionBodyParser.isUboCssInjection("body {}")).toBe(false); - expect(CssInjectionBodyParser.isUblockCssInjection("body { padding-top: 0 !important; }")).toBe(false); + expect(CssInjectionBodyParser.isUboCssInjection("body { padding-top: 0 !important; }")).toBe(false); expect( - CssInjectionBodyParser.isUblockCssInjection( + CssInjectionBodyParser.isUboCssInjection( "@media (min-width: 1024px) { body { padding-top: 0 !important; } }" ) ).toBe(false); // Empty - expect(CssInjectionBodyParser.isUblockCssInjection("body:style()")).toBe(false); + expect(CssInjectionBodyParser.isUboCssInjection("body:style()")).toBe(false); - expect(CssInjectionBodyParser.isUblockCssInjection("body:style(padding-top: 0 !important;)")).toBe(true); + expect(CssInjectionBodyParser.isUboCssInjection("body:style(padding-top: 0 !important;)")).toBe(true); - expect(CssInjectionBodyParser.isUblockCssInjection("body:ad-component:remove()")).toBe(true); + expect(CssInjectionBodyParser.isUboCssInjection("body:ad-component:remove()")).toBe(true); }); - test("isAdGuardCssInjection", () => { - expect(CssInjectionBodyParser.isAdGuardCssInjection(EMPTY)).toBe(false); - expect(CssInjectionBodyParser.isAdGuardCssInjection(SPACE)).toBe(false); + test("isAdgCssInjection", () => { + expect(CssInjectionBodyParser.isAdgCssInjection(EMPTY)).toBe(false); + expect(CssInjectionBodyParser.isAdgCssInjection(SPACE)).toBe(false); - expect(CssInjectionBodyParser.isAdGuardCssInjection(".ad")).toBe(false); + expect(CssInjectionBodyParser.isAdgCssInjection(".ad")).toBe(false); // Empty - expect(CssInjectionBodyParser.isAdGuardCssInjection("body {}")).toBe(false); + expect(CssInjectionBodyParser.isAdgCssInjection("body {}")).toBe(false); - expect(CssInjectionBodyParser.isAdGuardCssInjection("body { padding-top: 0 !important; }")).toBe(true); + expect(CssInjectionBodyParser.isAdgCssInjection("body { padding-top: 0 !important; }")).toBe(true); expect( - CssInjectionBodyParser.isAdGuardCssInjection( + CssInjectionBodyParser.isAdgCssInjection( "@media (min-width: 1024px) { body { padding-top: 0 !important; } }" ) ).toBe(true); - expect(CssInjectionBodyParser.isAdGuardCssInjection("body:style()")).toBe(false); + expect(CssInjectionBodyParser.isAdgCssInjection("body:style()")).toBe(false); - expect(CssInjectionBodyParser.isAdGuardCssInjection("body:style(padding-top: 0 !important;)")).toBe(false); + expect(CssInjectionBodyParser.isAdgCssInjection("body:style(padding-top: 0 !important;)")).toBe(false); - expect(CssInjectionBodyParser.isAdGuardCssInjection("body:ad-component:remove()")).toBe(false); + expect(CssInjectionBodyParser.isAdgCssInjection("body:ad-component:remove()")).toBe(false); }); - test("parseAdGuardCssInjection", () => { - expect(CssInjectionBodyParser.parseAdGuardCssInjection("body { padding-top: 0 !important; }")).toEqual(< - ICssRuleBody + test("parseAdgCssInjection", () => { + expect(CssInjectionBodyParser.parseAdgCssInjection("body { padding-top: 0 !important; }")).toEqual(< + CssRuleBody >{ selectors: [CssTree.parse("body", CssTreeParserContext.selector)], block: CssTree.parse("{ padding-top: 0 !important; }", CssTreeParserContext.block), }); expect( - CssInjectionBodyParser.parseAdGuardCssInjection( + CssInjectionBodyParser.parseAdgCssInjection( // eslint-disable-next-line max-len "body, section:has(.something) { padding-top: 0 !important; padding-bottom: 0 !important; color: red !important; }" ) - ).toEqual({ + ).toEqual({ selectors: [ CssTree.parse("body", CssTreeParserContext.selector), CssTree.parse("section:has(.something)", CssTreeParserContext.selector), @@ -81,11 +81,11 @@ describe("CssInjectionBodyParser", () => { // Complicated case: Media query, ExtCss selector expect( - CssInjectionBodyParser.parseAdGuardCssInjection( + CssInjectionBodyParser.parseAdgCssInjection( // eslint-disable-next-line max-len "@media (min-width: 1000px) and (max-width: 2000px) { body, section:has(.something) { padding-top: 0 !important; padding-bottom: 0 !important; color: red !important; } }" ) - ).toEqual({ + ).toEqual({ mediaQueryList: CssTree.parse( "(min-width: 1000px) and (max-width: 2000px)", CssTreeParserContext.mediaQueryList @@ -101,8 +101,8 @@ describe("CssInjectionBodyParser", () => { }); // Remove - expect(CssInjectionBodyParser.parseAdGuardCssInjection("body > section[ad-source] { remove: true; }")).toEqual(< - ICssRuleBody + expect(CssInjectionBodyParser.parseAdgCssInjection("body > section[ad-source] { remove: true; }")).toEqual(< + CssRuleBody >{ selectors: [CssTree.parse("body > section[ad-source]", CssTreeParserContext.selector)], block: REMOVE_BLOCK_TYPE, @@ -110,48 +110,46 @@ describe("CssInjectionBodyParser", () => { // Invalid cases expect(() => - CssInjectionBodyParser.parseAdGuardCssInjection("body > section[ad-source] { remove: true; remove: true; }") + CssInjectionBodyParser.parseAdgCssInjection("body > section[ad-source] { remove: true; remove: true; }") ).toThrowError(/^Multiple remove property found in the following CSS injection body:/); expect(() => - CssInjectionBodyParser.parseAdGuardCssInjection("body > section[ad-source] { remove: true; padding: 0; }") + CssInjectionBodyParser.parseAdgCssInjection("body > section[ad-source] { remove: true; padding: 0; }") ).toThrowError( /^In addition to the remove property, the following CSS injection body also uses other properties:/ ); expect(() => - CssInjectionBodyParser.parseAdGuardCssInjection("body > section[ad-source] { padding: 0; remove: true; }") + CssInjectionBodyParser.parseAdgCssInjection("body > section[ad-source] { padding: 0; remove: true; }") ).toThrowError( /^In addition to the remove property, the following CSS injection body also uses other properties:/ ); expect(() => - CssInjectionBodyParser.parseAdGuardCssInjection( + CssInjectionBodyParser.parseAdgCssInjection( "body > section[ad-source] { margin: 0; remove: true; padding: 0; }" ) ).toThrowError( /^In addition to the remove property, the following CSS injection body also uses other properties:/ ); - expect(() => - CssInjectionBodyParser.parseAdGuardCssInjection("body > section[ad-source] { asd }") - ).toThrowError(); + expect(() => CssInjectionBodyParser.parseAdgCssInjection("body > section[ad-source] { asd }")).toThrowError(); }); - test("parseUblockCssInjection", () => { - expect(CssInjectionBodyParser.parseUblockCssInjection("body:style(padding-top: 0 !important;)")).toEqual(< - ICssRuleBody + test("parseUboCssInjection", () => { + expect(CssInjectionBodyParser.parseUboCssInjection("body:style(padding-top: 0 !important;)")).toEqual(< + CssRuleBody >{ selectors: [CssTree.parse("body", CssTreeParserContext.selector)], block: CssTree.parse("{ padding-top: 0 !important; }", CssTreeParserContext.block), }); expect( - CssInjectionBodyParser.parseUblockCssInjection( + CssInjectionBodyParser.parseUboCssInjection( // eslint-disable-next-line max-len "body, section:has(.something):style(padding-top: 0 !important; padding-bottom: 0 !important; color: red !important;)" ) - ).toEqual({ + ).toEqual({ selectors: [ CssTree.parse("body", CssTreeParserContext.selector), CssTree.parse("section:has(.something)", CssTreeParserContext.selector), @@ -163,15 +161,13 @@ describe("CssInjectionBodyParser", () => { }); // Remove - expect(CssInjectionBodyParser.parseUblockCssInjection("body > section[ad-source]:remove()")).toEqual(< - ICssRuleBody - >{ + expect(CssInjectionBodyParser.parseUboCssInjection("body > section[ad-source]:remove()")).toEqual({ selectors: [CssTree.parse("body > section[ad-source]", CssTreeParserContext.selector)], block: REMOVE_BLOCK_TYPE, }); // Invalid - expect(CssInjectionBodyParser.parseUblockCssInjection("body { padding: 0; }")).toBe(null); + expect(CssInjectionBodyParser.parseUboCssInjection("body { padding: 0; }")).toBe(null); }); test("parse", () => { @@ -180,7 +176,7 @@ describe("CssInjectionBodyParser", () => { // eslint-disable-next-line max-len "body, section:has(.something):style(padding-top: 0 !important; padding-bottom: 0 !important; color: red !important;)" ) - ).toEqual({ + ).toEqual({ selectors: [ CssTree.parse("body", CssTreeParserContext.selector), CssTree.parse("section:has(.something)", CssTreeParserContext.selector), @@ -191,7 +187,7 @@ describe("CssInjectionBodyParser", () => { ), }); - expect(CssInjectionBodyParser.parse("body, section:has(.something):remove()")).toEqual({ + expect(CssInjectionBodyParser.parse("body, section:has(.something):remove()")).toEqual({ selectors: [ CssTree.parse("body", CssTreeParserContext.selector), CssTree.parse("section:has(.something)", CssTreeParserContext.selector), @@ -204,7 +200,7 @@ describe("CssInjectionBodyParser", () => { // eslint-disable-next-line max-len "@media (min-width: 1000px) and (max-width: 2000px) { body, section:has(.something) { padding-top: 0 !important; padding-bottom: 0 !important; color: red !important; } }" ) - ).toEqual({ + ).toEqual({ mediaQueryList: CssTree.parse( "(min-width: 1000px) and (max-width: 2000px)", CssTreeParserContext.mediaQueryList @@ -223,7 +219,7 @@ describe("CssInjectionBodyParser", () => { CssInjectionBodyParser.parse( "@media (min-width: 1000px) and (max-width: 2000px) { body, section:has(.something) { remove: true; } }" ) - ).toEqual({ + ).toEqual({ mediaQueryList: CssTree.parse( "(min-width: 1000px) and (max-width: 2000px)", CssTreeParserContext.mediaQueryList @@ -247,17 +243,17 @@ describe("CssInjectionBodyParser", () => { return null; }; - expect(parseAndGenerate("body { padding: 0!important; }", AdblockSyntax.AdGuard)).toEqual( + expect(parseAndGenerate("body { padding: 0!important; }", AdblockSyntax.Adg)).toEqual( "body { padding: 0 !important; }" ); - expect(parseAndGenerate("body > .ad:remove()", AdblockSyntax.uBlockOrigin)).toEqual("body > .ad:remove()"); + expect(parseAndGenerate("body > .ad:remove()", AdblockSyntax.Ubo)).toEqual("body > .ad:remove()"); expect( parseAndGenerate( // eslint-disable-next-line max-len "@media (min-width: 1000px) and (max-width: 2000px) { body, section:has(.something) { remove: true; } }", - AdblockSyntax.AdGuard + AdblockSyntax.Adg ) ).toEqual( "@media (min-width:1000px) and (max-width:2000px) { body, section:has(.something) { remove: true; } }" @@ -268,7 +264,7 @@ describe("CssInjectionBodyParser", () => { { selectors: [CssTree.parse(".test", CssTreeParserContext.selector)], }, - AdblockSyntax.AdGuard + AdblockSyntax.Adg ) ).toEqual(".test { }"); @@ -277,7 +273,7 @@ describe("CssInjectionBodyParser", () => { { selectors: [CssTree.parse(".test", CssTreeParserContext.selector)], }, - AdblockSyntax.uBlockOrigin + AdblockSyntax.Ubo ) ).toEqual(".test:style()"); @@ -286,14 +282,14 @@ describe("CssInjectionBodyParser", () => { { selectors: [CssTree.parse(".test", CssTreeParserContext.selector)], }, - AdblockSyntax.AdblockPlus + AdblockSyntax.Abp ) ).toThrowError(/^Unsupported syntax:/); expect(() => parseAndGenerate( "@media (min-width: 1000px) and (max-width: 2000px) { body, section:has(.something) { remove: true; } }", - AdblockSyntax.uBlockOrigin + AdblockSyntax.Ubo ) ).toThrowError("uBlock doesn't support media queries"); }); diff --git a/test/parser/cosmetic/body/html.test.ts b/test/parser/cosmetic/body/html.test.ts index bcee223e..6d0be331 100644 --- a/test/parser/cosmetic/body/html.test.ts +++ b/test/parser/cosmetic/body/html.test.ts @@ -48,13 +48,11 @@ describe("HtmlBodyParser", () => { return null; }; - expect(parseAndGenerate('[tag-content="""a"""]', AdblockSyntax.AdGuard)).toEqual('[tag-content="""a"""]'); + expect(parseAndGenerate('[tag-content="""a"""]', AdblockSyntax.Adg)).toEqual('[tag-content="""a"""]'); - expect(parseAndGenerate('[tag-content="""""a"""""]', AdblockSyntax.AdGuard)).toEqual( - '[tag-content="""""a"""""]' - ); + expect(parseAndGenerate('[tag-content="""""a"""""]', AdblockSyntax.Adg)).toEqual('[tag-content="""""a"""""]'); - expect(parseAndGenerate("script:has-text(a), script:has-text(b)", AdblockSyntax.AdGuard)).toEqual( + expect(parseAndGenerate("script:has-text(a), script:has-text(b)", AdblockSyntax.Adg)).toEqual( "script:has-text(a), script:has-text(b)" ); }); diff --git a/test/parser/cosmetic/body/scriptlet.test.ts b/test/parser/cosmetic/body/scriptlet.test.ts index 21f98637..a1e2e4d2 100644 --- a/test/parser/cosmetic/body/scriptlet.test.ts +++ b/test/parser/cosmetic/body/scriptlet.test.ts @@ -677,7 +677,7 @@ describe("ScriptletBodyParser", () => { return null; }; - expect(parseAndGenerate("()", AdblockSyntax.AdGuard)).toEqual([]); + expect(parseAndGenerate("()", AdblockSyntax.Adg)).toEqual([]); expect( ScriptletBodyParser.generate( @@ -691,31 +691,33 @@ describe("ScriptletBodyParser", () => { }, ], }, - AdblockSyntax.AdGuard + AdblockSyntax.Adg ) ).toEqual(["(scriptlet1)"]); - expect(parseAndGenerate("(scriptlet0, arg0, /a;b/, 'a;b', \"a;b\")", AdblockSyntax.AdGuard)).toEqual([ + expect(parseAndGenerate("(scriptlet0, arg0, /a;b/, 'a;b', \"a;b\")", AdblockSyntax.Adg)).toEqual([ "(scriptlet0, arg0, /a;b/, 'a;b', \"a;b\")", ]); expect( parseAndGenerate( "scriptlet0 arg0 /a;b/ 'a;b' \"a;b\"; scriptlet-1; scriptlet2 'arg0' arg1\\ something;", - AdblockSyntax.AdblockPlus + AdblockSyntax.Abp ) ).toEqual(["scriptlet0 arg0 /a;b/ 'a;b' \"a;b\"", "scriptlet-1", "scriptlet2 'arg0' arg1\\ something"]); - expect( - parseAndGenerate("scriptlet0 arg0 arg1; scriptlet1; scriptlet2 arg0", AdblockSyntax.AdblockPlus) - ).toEqual(["scriptlet0 arg0 arg1", "scriptlet1", "scriptlet2 arg0"]); + expect(parseAndGenerate("scriptlet0 arg0 arg1; scriptlet1; scriptlet2 arg0", AdblockSyntax.Abp)).toEqual([ + "scriptlet0 arg0 arg1", + "scriptlet1", + "scriptlet2 arg0", + ]); // Complicated "real world" example // Source: https://github.com/abp-filters/abp-filters-anti-cv/blob/4474f3aafcdb87bb7dd4053f1950068f7e3906ef/fb_non-graph.txt#L2 expect( parseAndGenerate( `race start; hide-if-contains-visible-text /[Sponsred]{9}|[Gesponrtd]{10}|[Sponrisé]{10}|[Comandité]{9}|[Publicda]{10}|[Sponsrwae]{12}|[Patrocind]{11}|[Sponsrizat]{13}/ 'div[role=feed] div[role=article]' a[href="#"][role="link"]; hide-if-contains-visible-text /[Sponsred]{9}|[Gesponrtd]{10}|[Sponrisé]{10}|[Comandité]{9}|[Publicda]{10}|[Sponsrwae]{12}|[Patrocind]{11}|[Sponsrizat]{13}/ 'div[role=feed] div[role=article]' a[href^="?__cft__"]; hide-if-contains-visible-text /[Sponsred]{9}|[Gesponrtd]{10}|[Sponrisé]{10}|[Comandité]{9}|[Publicda]{10}|[Sponsrwae]{12}|[Patrocind]{11}|[Sponsrizat]{13}/ 'div[role=feed] div[role=article]' a[href="#"][role="link"]>span>span>b; hide-if-matches-xpath './/div[@role="feed"]//div[@role="article"]//a[@aria-label[.="Patrocinado" or .="Sponsa" or .="Bersponsor" or .="Commandité" or .="Ditaja" or .="Gesponsert" or .="Gesponsord" or .="Sponsrad" or .="Publicidad" or .="Sponsoreret" or .="Sponset" or .="Sponsored" or .="Sponsorisé" or .="Sponsorizat" or .="Sponsorizzato" or .="Sponsorlu" or .="Sponsorowane" or .="Реклама" or .="ממומן" or .="تمويل شوي" or .="دارای پشتیبانی مالی" or .="سپانسرڈ" or .="مُموَّل" or .="प्रायोजित" or .="সৌজন্যে" or .="ได้รับการสนับสนุน" or .="内容" or .="贊助" or .="Sponsoroitu" or .="May Sponsor" or .="Được tài trợ"]]/ancestor::div[@role="article"]'; race stop;`, - AdblockSyntax.AdblockPlus + AdblockSyntax.Abp ) ).toEqual([ "race start", diff --git a/test/parser/cosmetic/cosmetic.test.ts b/test/parser/cosmetic/cosmetic.test.ts index 99f552b3..966d4b34 100644 --- a/test/parser/cosmetic/cosmetic.test.ts +++ b/test/parser/cosmetic/cosmetic.test.ts @@ -6,8 +6,8 @@ import { HtmlBodyParser } from "../../../src/parser/cosmetic/body/html"; import { ScriptletBodyParser } from "../../../src/parser/cosmetic/body/scriptlet"; import { CosmeticRuleType } from "../../../src/parser/cosmetic/common"; import { CosmeticRuleParser } from "../../../src/parser/cosmetic/cosmetic"; -import { AdGuardModifierListParser } from "../../../src/parser/cosmetic/specific/adg-options"; -import { UBlockModifierListParser } from "../../../src/parser/cosmetic/specific/ubo-options"; +import { AdgModifierListParser } from "../../../src/parser/cosmetic/specific/adg-modifiers"; +import { UboModifierListParser } from "../../../src/parser/cosmetic/specific/ubo-modifiers"; import { AdblockSyntax } from "../../../src/utils/adblockers"; import { EMPTY, SPACE } from "../../../src/utils/constants"; @@ -124,7 +124,7 @@ describe("CosmeticRuleParser", () => { // Valid CSS inject (AdGuard) expect(CosmeticRuleParser.parse("#$#body { padding: 0; }")).toMatchObject({ type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: false, modifiers: [], domains: [], @@ -134,7 +134,7 @@ describe("CosmeticRuleParser", () => { expect(CosmeticRuleParser.parse("example.com,~example.net#$#body { padding: 0; }")).toMatchObject({ type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: false, modifiers: [], domains: DomainListParser.parse("example.com,~example.net").domains, @@ -144,7 +144,7 @@ describe("CosmeticRuleParser", () => { expect(CosmeticRuleParser.parse("#@$#body { padding: 0; }")).toMatchObject({ type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: true, modifiers: [], domains: [], @@ -154,7 +154,7 @@ describe("CosmeticRuleParser", () => { expect(CosmeticRuleParser.parse("example.com,~example.net#@$#body { padding: 0; }")).toMatchObject({ type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: true, modifiers: [], domains: DomainListParser.parse("example.com,~example.net").domains, @@ -167,7 +167,7 @@ describe("CosmeticRuleParser", () => { CosmeticRuleParser.parse("#$#@media (min-height: 1024px) and (max-height: 1920px) { body { padding: 0; } }") ).toMatchObject({ type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: false, modifiers: [], domains: [], @@ -181,7 +181,7 @@ describe("CosmeticRuleParser", () => { CosmeticRuleParser.parse("#$#@media(min-height: 1024px) and (max-height: 1920px) { body { padding: 0; } }") ).toMatchObject({ type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: false, modifiers: [], domains: [], @@ -197,7 +197,7 @@ describe("CosmeticRuleParser", () => { ) ).toMatchObject({ type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: false, modifiers: [], domains: DomainListParser.parse("example.com,~example.net").domains, @@ -210,7 +210,7 @@ describe("CosmeticRuleParser", () => { // Valid ExtendedCSS inject (AdGuard) expect(CosmeticRuleParser.parse("#$?#body:-abp-has(.ad) { padding: 0; }")).toMatchObject({ type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: false, modifiers: [], domains: [], @@ -222,7 +222,7 @@ describe("CosmeticRuleParser", () => { CosmeticRuleParser.parse("example.com,~example.net#$?#body:-abp-has(.ad) { padding: 0; }") ).toMatchObject({ type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: false, modifiers: [], domains: DomainListParser.parse("example.com,~example.net").domains, @@ -232,7 +232,7 @@ describe("CosmeticRuleParser", () => { expect(CosmeticRuleParser.parse("#@$?#body:-abp-has(.ad) { padding: 0; }")).toMatchObject({ type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: true, modifiers: [], domains: [], @@ -244,7 +244,7 @@ describe("CosmeticRuleParser", () => { CosmeticRuleParser.parse("example.com,~example.net#@$?#body:-abp-has(.ad) { padding: 0; }") ).toMatchObject({ type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: true, modifiers: [], domains: DomainListParser.parse("example.com,~example.net").domains, @@ -255,7 +255,7 @@ describe("CosmeticRuleParser", () => { // Valid CSS inject (uBlock) expect(CosmeticRuleParser.parse("##body:style(padding: 0;)")).toMatchObject({ type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: false, modifiers: [], domains: [], @@ -265,7 +265,7 @@ describe("CosmeticRuleParser", () => { expect(CosmeticRuleParser.parse("example.com,~example.net##body:style(padding: 0;)")).toMatchObject({ type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: false, modifiers: [], domains: DomainListParser.parse("example.com,~example.net").domains, @@ -275,7 +275,7 @@ describe("CosmeticRuleParser", () => { expect(CosmeticRuleParser.parse("#@#body:style(padding: 0;)")).toMatchObject({ type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: true, modifiers: [], domains: [], @@ -285,7 +285,7 @@ describe("CosmeticRuleParser", () => { expect(CosmeticRuleParser.parse("example.com,~example.net#@#body:style(padding: 0;)")).toMatchObject({ type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: true, modifiers: [], domains: DomainListParser.parse("example.com,~example.net").domains, @@ -296,7 +296,7 @@ describe("CosmeticRuleParser", () => { // Valid ExtendedCSS inject (uBlock) expect(CosmeticRuleParser.parse("##body:has(.ad):style(padding: 0;)")).toMatchObject({ type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: false, modifiers: [], domains: [], @@ -306,7 +306,7 @@ describe("CosmeticRuleParser", () => { expect(CosmeticRuleParser.parse("example.com,~example.net##body:has(.ad):style(padding: 0;)")).toMatchObject({ type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: false, modifiers: [], domains: DomainListParser.parse("example.com,~example.net").domains, @@ -316,7 +316,7 @@ describe("CosmeticRuleParser", () => { expect(CosmeticRuleParser.parse("#@#body:has(.ad):style(padding: 0;)")).toMatchObject({ type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: true, modifiers: [], domains: [], @@ -326,7 +326,7 @@ describe("CosmeticRuleParser", () => { expect(CosmeticRuleParser.parse("example.com,~example.net#@#body:has(.ad):style(padding: 0;)")).toMatchObject({ type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: true, modifiers: [], domains: DomainListParser.parse("example.com,~example.net").domains, @@ -337,7 +337,7 @@ describe("CosmeticRuleParser", () => { // Valid scriptlet inject (AdGuard) expect(CosmeticRuleParser.parse("#%#//scriptlet('scriptlet0', 'arg0', 'arg1')")).toMatchObject({ type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: false, modifiers: [], domains: [], @@ -349,7 +349,7 @@ describe("CosmeticRuleParser", () => { CosmeticRuleParser.parse("example.com,~example.net#%#//scriptlet('scriptlet0', 'arg0', 'arg1')") ).toMatchObject({ type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: false, modifiers: [], domains: DomainListParser.parse("example.com,~example.net").domains, @@ -359,7 +359,7 @@ describe("CosmeticRuleParser", () => { expect(CosmeticRuleParser.parse("#@%#//scriptlet('scriptlet0', 'arg0', 'arg1')")).toMatchObject({ type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: true, modifiers: [], domains: [], @@ -371,7 +371,7 @@ describe("CosmeticRuleParser", () => { CosmeticRuleParser.parse("example.com,~example.net#@%#//scriptlet('scriptlet0', 'arg0', 'arg1')") ).toMatchObject({ type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: true, modifiers: [], domains: DomainListParser.parse("example.com,~example.net").domains, @@ -382,7 +382,7 @@ describe("CosmeticRuleParser", () => { // Valid scriptlet inject (uBlock) expect(CosmeticRuleParser.parse("##+js(scriptlet0, arg0, arg1)")).toMatchObject({ type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: false, modifiers: [], domains: [], @@ -392,7 +392,7 @@ describe("CosmeticRuleParser", () => { expect(CosmeticRuleParser.parse("example.com,~example.net##+js(scriptlet0, arg0, arg1)")).toMatchObject({ type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: false, modifiers: [], domains: DomainListParser.parse("example.com,~example.net").domains, @@ -402,7 +402,7 @@ describe("CosmeticRuleParser", () => { expect(CosmeticRuleParser.parse("#@#+js(scriptlet0, arg0, arg1)")).toMatchObject({ type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: true, modifiers: [], domains: [], @@ -412,7 +412,7 @@ describe("CosmeticRuleParser", () => { expect(CosmeticRuleParser.parse("example.com,~example.net#@#+js(scriptlet0, arg0, arg1)")).toMatchObject({ type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: true, modifiers: [], domains: DomainListParser.parse("example.com,~example.net").domains, @@ -423,7 +423,7 @@ describe("CosmeticRuleParser", () => { // Valid scriptlet inject (ABP) expect(CosmeticRuleParser.parse("#$#scriptlet0 arg0 arg1")).toMatchObject({ type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.AdblockPlus, + syntax: AdblockSyntax.Abp, exception: false, modifiers: [], domains: [], @@ -433,7 +433,7 @@ describe("CosmeticRuleParser", () => { expect(CosmeticRuleParser.parse("example.com,~example.net#$#scriptlet0 arg0 arg1")).toMatchObject({ type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.AdblockPlus, + syntax: AdblockSyntax.Abp, exception: false, modifiers: [], domains: DomainListParser.parse("example.com,~example.net").domains, @@ -443,7 +443,7 @@ describe("CosmeticRuleParser", () => { expect(CosmeticRuleParser.parse("#@$#scriptlet0 arg0 arg1")).toMatchObject({ type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.AdblockPlus, + syntax: AdblockSyntax.Abp, exception: true, modifiers: [], domains: [], @@ -453,7 +453,7 @@ describe("CosmeticRuleParser", () => { expect(CosmeticRuleParser.parse("example.com,~example.net#@$#scriptlet0 arg0 arg1")).toMatchObject({ type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.AdblockPlus, + syntax: AdblockSyntax.Abp, exception: true, modifiers: [], domains: DomainListParser.parse("example.com,~example.net").domains, @@ -463,7 +463,7 @@ describe("CosmeticRuleParser", () => { expect(CosmeticRuleParser.parse("#$#scriptlet0 arg00 arg01; scriptlet1 arg10 arg11")).toMatchObject({ type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.AdblockPlus, + syntax: AdblockSyntax.Abp, exception: false, modifiers: [], domains: [], @@ -475,7 +475,7 @@ describe("CosmeticRuleParser", () => { CosmeticRuleParser.parse("example.com,~example.net#$#scriptlet0 arg00 arg01; scriptlet1 arg10 arg11") ).toMatchObject({ type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.AdblockPlus, + syntax: AdblockSyntax.Abp, exception: false, modifiers: [], domains: DomainListParser.parse("example.com,~example.net").domains, @@ -485,7 +485,7 @@ describe("CosmeticRuleParser", () => { expect(CosmeticRuleParser.parse("#@$#scriptlet0 arg00 arg01; scriptlet1 arg10 arg11")).toMatchObject({ type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.AdblockPlus, + syntax: AdblockSyntax.Abp, exception: true, modifiers: [], domains: [], @@ -497,7 +497,7 @@ describe("CosmeticRuleParser", () => { CosmeticRuleParser.parse("example.com,~example.net#@$#scriptlet0 arg00 arg01; scriptlet1 arg10 arg11;") ).toMatchObject({ type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.AdblockPlus, + syntax: AdblockSyntax.Abp, exception: true, modifiers: [], domains: DomainListParser.parse("example.com,~example.net").domains, @@ -507,7 +507,7 @@ describe("CosmeticRuleParser", () => { expect(CosmeticRuleParser.parse("#$#scriptlet0 arg00 arg01; scriptlet1 arg10 arg11;")).toMatchObject({ type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.AdblockPlus, + syntax: AdblockSyntax.Abp, exception: false, modifiers: [], domains: [], @@ -519,7 +519,7 @@ describe("CosmeticRuleParser", () => { CosmeticRuleParser.parse("example.com,~example.net#$#scriptlet0 arg00 arg01; scriptlet1 arg10 arg11;") ).toMatchObject({ type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.AdblockPlus, + syntax: AdblockSyntax.Abp, exception: false, modifiers: [], domains: DomainListParser.parse("example.com,~example.net").domains, @@ -529,7 +529,7 @@ describe("CosmeticRuleParser", () => { expect(CosmeticRuleParser.parse("#@$#scriptlet0 arg00 arg01; scriptlet1 arg10 arg11;")).toMatchObject({ type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.AdblockPlus, + syntax: AdblockSyntax.Abp, exception: true, modifiers: [], domains: [], @@ -541,7 +541,7 @@ describe("CosmeticRuleParser", () => { CosmeticRuleParser.parse("example.com,~example.net#@$#scriptlet0 arg00 arg01; scriptlet1 arg10 arg11;") ).toMatchObject({ type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.AdblockPlus, + syntax: AdblockSyntax.Abp, exception: true, modifiers: [], domains: DomainListParser.parse("example.com,~example.net").domains, @@ -552,7 +552,7 @@ describe("CosmeticRuleParser", () => { // Valid HTML filters (AdGuard) expect(CosmeticRuleParser.parse('$$script[tag-content="adblock"]')).toMatchObject({ type: CosmeticRuleType.HtmlRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: false, modifiers: [], domains: [], @@ -562,7 +562,7 @@ describe("CosmeticRuleParser", () => { expect(CosmeticRuleParser.parse('example.com,~example.net$$script[tag-content="adblock"]')).toMatchObject({ type: CosmeticRuleType.HtmlRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: false, modifiers: [], domains: DomainListParser.parse("example.com,~example.net").domains, @@ -572,7 +572,7 @@ describe("CosmeticRuleParser", () => { expect(CosmeticRuleParser.parse('$@$script[tag-content="adblock"]')).toMatchObject({ type: CosmeticRuleType.HtmlRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: true, modifiers: [], domains: [], @@ -582,7 +582,7 @@ describe("CosmeticRuleParser", () => { expect(CosmeticRuleParser.parse('example.com,~example.net$@$script[tag-content="adblock"]')).toMatchObject({ type: CosmeticRuleType.HtmlRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: true, modifiers: [], domains: DomainListParser.parse("example.com,~example.net").domains, @@ -593,7 +593,7 @@ describe("CosmeticRuleParser", () => { // Valid HTML filters (uBlock) expect(CosmeticRuleParser.parse("##^script:has-text(adblock)")).toMatchObject({ type: CosmeticRuleType.HtmlRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: false, modifiers: [], domains: [], @@ -603,7 +603,7 @@ describe("CosmeticRuleParser", () => { expect(CosmeticRuleParser.parse("example.com,~example.net##^script:has-text(adblock)")).toMatchObject({ type: CosmeticRuleType.HtmlRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: false, modifiers: [], domains: DomainListParser.parse("example.com,~example.net").domains, @@ -613,7 +613,7 @@ describe("CosmeticRuleParser", () => { expect(CosmeticRuleParser.parse("#@#^script:has-text(adblock)")).toMatchObject({ type: CosmeticRuleType.HtmlRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: true, modifiers: [], domains: [], @@ -623,7 +623,7 @@ describe("CosmeticRuleParser", () => { expect(CosmeticRuleParser.parse("example.com,~example.net#@#^script:has-text(adblock)")).toMatchObject({ type: CosmeticRuleType.HtmlRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: true, modifiers: [], domains: DomainListParser.parse("example.com,~example.net").domains, @@ -633,7 +633,7 @@ describe("CosmeticRuleParser", () => { expect(CosmeticRuleParser.parse("##^script:has-text(adblock), script:has-text(detector)")).toMatchObject({ type: CosmeticRuleType.HtmlRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: false, modifiers: [], domains: [], @@ -645,7 +645,7 @@ describe("CosmeticRuleParser", () => { CosmeticRuleParser.parse("example.com,~example.net##^script:has-text(adblock), script:has-text(detector)") ).toMatchObject({ type: CosmeticRuleType.HtmlRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: false, modifiers: [], domains: DomainListParser.parse("example.com,~example.net").domains, @@ -655,7 +655,7 @@ describe("CosmeticRuleParser", () => { expect(CosmeticRuleParser.parse("#@#^script:has-text(adblock), script:has-text(detector)")).toMatchObject({ type: CosmeticRuleType.HtmlRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: true, modifiers: [], domains: [], @@ -667,7 +667,7 @@ describe("CosmeticRuleParser", () => { CosmeticRuleParser.parse("example.com,~example.net#@#^script:has-text(adblock), script:has-text(detector)") ).toMatchObject({ type: CosmeticRuleType.HtmlRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: true, modifiers: [], domains: DomainListParser.parse("example.com,~example.net").domains, @@ -678,7 +678,7 @@ describe("CosmeticRuleParser", () => { // Valid JS injections (AdGuard) expect(CosmeticRuleParser.parse("#%#const a = 2;")).toMatchObject({ type: CosmeticRuleType.JsRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: false, modifiers: [], domains: [], @@ -688,7 +688,7 @@ describe("CosmeticRuleParser", () => { expect(CosmeticRuleParser.parse("example.com,~example.net#%#const a = 2;")).toMatchObject({ type: CosmeticRuleType.JsRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: false, modifiers: [], domains: DomainListParser.parse("example.com,~example.net").domains, @@ -698,7 +698,7 @@ describe("CosmeticRuleParser", () => { expect(CosmeticRuleParser.parse("#@%#const a = 2;")).toMatchObject({ type: CosmeticRuleType.JsRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: true, modifiers: [], domains: [], @@ -708,7 +708,7 @@ describe("CosmeticRuleParser", () => { expect(CosmeticRuleParser.parse("example.com,~example.net#@%#const a = 2;")).toMatchObject({ type: CosmeticRuleType.JsRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: true, modifiers: [], domains: DomainListParser.parse("example.com,~example.net").domains, @@ -720,9 +720,9 @@ describe("CosmeticRuleParser", () => { // ! At this point, we aren't yet testing the conflict between the domain modifier and the classic domain list expect(CosmeticRuleParser.parse("[$app=com.something]#%#const a = 2;")).toMatchObject({ type: CosmeticRuleType.JsRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: false, - modifiers: AdGuardModifierListParser.parse("[$app=com.something]").modifiers, + modifiers: AdgModifierListParser.parse("[$app=com.something]").modifiers, domains: [], separator: "#%#", body: "const a = 2;", @@ -730,9 +730,9 @@ describe("CosmeticRuleParser", () => { expect(CosmeticRuleParser.parse("[$app=com.something,anything=123]#%#const a = 2;")).toMatchObject({ type: CosmeticRuleType.JsRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: false, - modifiers: AdGuardModifierListParser.parse("[$app=com.something,anything=123]").modifiers, + modifiers: AdgModifierListParser.parse("[$app=com.something,anything=123]").modifiers, domains: [], separator: "#%#", body: "const a = 2;", @@ -740,9 +740,9 @@ describe("CosmeticRuleParser", () => { expect(CosmeticRuleParser.parse("[$app=com.something]example.com,~example.net#%#const a = 2;")).toMatchObject({ type: CosmeticRuleType.JsRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: false, - modifiers: AdGuardModifierListParser.parse("[$app=com.something]").modifiers, + modifiers: AdgModifierListParser.parse("[$app=com.something]").modifiers, domains: DomainListParser.parse("example.com,~example.net").domains, separator: "#%#", body: "const a = 2;", @@ -752,9 +752,9 @@ describe("CosmeticRuleParser", () => { CosmeticRuleParser.parse("[$app=com.something,anything=123]example.com,~example.net#%#const a = 2;") ).toMatchObject({ type: CosmeticRuleType.JsRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: false, - modifiers: AdGuardModifierListParser.parse("[$app=com.something,anything=123]").modifiers, + modifiers: AdgModifierListParser.parse("[$app=com.something,anything=123]").modifiers, domains: DomainListParser.parse("example.com,~example.net").domains, separator: "#%#", body: "const a = 2;", @@ -763,9 +763,9 @@ describe("CosmeticRuleParser", () => { // uBlock modifiers/options expect(CosmeticRuleParser.parse("##:matches-path(/path) .ad")).toMatchObject({ type: CosmeticRuleType.ElementHidingRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: false, - modifiers: UBlockModifierListParser.parse(":matches-path(/path)").modifiers, + modifiers: UboModifierListParser.parse(":matches-path(/path)").modifiers, domains: [], separator: "##", body: ElementHidingBodyParser.parse(".ad"), @@ -773,9 +773,9 @@ describe("CosmeticRuleParser", () => { expect(CosmeticRuleParser.parse("example.com,~example.net##:matches-path(/path) .ad")).toMatchObject({ type: CosmeticRuleType.ElementHidingRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: false, - modifiers: UBlockModifierListParser.parse(":matches-path(/path)").modifiers, + modifiers: UboModifierListParser.parse(":matches-path(/path)").modifiers, domains: DomainListParser.parse("example.com,~example.net").domains, separator: "##", body: ElementHidingBodyParser.parse(".ad"), @@ -785,9 +785,9 @@ describe("CosmeticRuleParser", () => { CosmeticRuleParser.parse("example.com,~example.net##^:matches-path(/path) script:has-text(detect)") ).toMatchObject({ type: CosmeticRuleType.HtmlRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: false, - modifiers: UBlockModifierListParser.parse(":matches-path(/path)").modifiers, + modifiers: UboModifierListParser.parse(":matches-path(/path)").modifiers, domains: DomainListParser.parse("example.com,~example.net").domains, separator: "##^", body: HtmlBodyParser.parse("script:has-text(detect)"), @@ -808,15 +808,15 @@ describe("CosmeticRuleParser", () => { expect(CosmeticRuleParser.parse("example.com,~example.net##^responseheader(a)")).toBeNull(); expect(() => CosmeticRuleParser.parse("[$a=b]##:matches-path(/c) .ad")).toThrowError( - "Cannot use AdGuard modifier list with uBO options" + "Cannot use AdGuard modifier list with uBO modifiers" ); expect(() => CosmeticRuleParser.parse("$$:matches-path(/c).ad")).toThrowError( - "Cannot use uBO options with ADG HTML filtering" + "Cannot use uBO modifiers with ADG HTML filtering" ); expect(() => CosmeticRuleParser.parse("[$a=b]##^:matches-path(/c) .ad")).toThrowError( - "Cannot use AdGuard modifier list with uBO options" + "Cannot use AdGuard modifier list with uBO modifiers" ); expect(() => CosmeticRuleParser.parse("[$a=b]##body:style(padding:0)")).toThrowError( diff --git a/test/parser/cosmetic/specific/adg-options.test.ts b/test/parser/cosmetic/specific/adg-modifiers.test.ts similarity index 67% rename from test/parser/cosmetic/specific/adg-options.test.ts rename to test/parser/cosmetic/specific/adg-modifiers.test.ts index 362281ec..0638f47b 100644 --- a/test/parser/cosmetic/specific/adg-options.test.ts +++ b/test/parser/cosmetic/specific/adg-modifiers.test.ts @@ -1,21 +1,18 @@ /* eslint-disable max-len */ -import { - AdGuardModifierListParser, - ADG_MODIFIER_LIST_TYPE, -} from "../../../../src/parser/cosmetic/specific/adg-options"; +import { AdgModifierListParser, ADG_MODIFIER_LIST_TYPE } from "../../../../src/parser/cosmetic/specific/adg-modifiers"; import { EMPTY } from "../../../../src/utils/constants"; -describe("AdGuardModifierListParser", () => { +describe("AdgModifierListParser", () => { test("parse", async () => { // Empty - expect(AdGuardModifierListParser.parse(EMPTY)).toEqual({ + expect(AdgModifierListParser.parse(EMPTY)).toEqual({ type: ADG_MODIFIER_LIST_TYPE, modifiers: [], rest: EMPTY, }); // Valid cases (please note that modifier parser are tested in another test file) - expect(AdGuardModifierListParser.parse("[$m1]example.com")).toEqual({ + expect(AdgModifierListParser.parse("[$m1]example.com")).toEqual({ type: ADG_MODIFIER_LIST_TYPE, modifiers: [ { @@ -25,7 +22,7 @@ describe("AdGuardModifierListParser", () => { rest: "example.com", }); - expect(AdGuardModifierListParser.parse("[$m1,m2=v2]example.com")).toEqual({ + expect(AdgModifierListParser.parse("[$m1,m2=v2]example.com")).toEqual({ type: ADG_MODIFIER_LIST_TYPE, modifiers: [ { @@ -39,7 +36,7 @@ describe("AdGuardModifierListParser", () => { rest: "example.com", }); - expect(AdGuardModifierListParser.parse("[$m1,m2=v2]")).toEqual({ + expect(AdgModifierListParser.parse("[$m1,m2=v2]")).toEqual({ type: ADG_MODIFIER_LIST_TYPE, modifiers: [ { @@ -54,7 +51,7 @@ describe("AdGuardModifierListParser", () => { }); // Spaces - expect(AdGuardModifierListParser.parse("[$ path = /test ]")).toEqual({ + expect(AdgModifierListParser.parse("[$ path = /test ]")).toEqual({ type: ADG_MODIFIER_LIST_TYPE, modifiers: [ { @@ -67,7 +64,7 @@ describe("AdGuardModifierListParser", () => { // Complicated case expect( - AdGuardModifierListParser.parse( + AdgModifierListParser.parse( `[$path=/test,domain=/(^|.+\\.)example\\.(com|org)\\$/,modifier1]example.com,example.org` ) ).toEqual({ @@ -89,32 +86,30 @@ describe("AdGuardModifierListParser", () => { }); // Invalid cases - expect(() => AdGuardModifierListParser.parse("[")).toThrowError(/^Missing modifier marker "\$" at pattern/); + expect(() => AdgModifierListParser.parse("[")).toThrowError(/^Missing modifier marker "\$" at pattern/); - expect(() => AdGuardModifierListParser.parse("[ ")).toThrowError(/^Missing modifier marker "\$" at pattern/); + expect(() => AdgModifierListParser.parse("[ ")).toThrowError(/^Missing modifier marker "\$" at pattern/); - expect(() => AdGuardModifierListParser.parse("[m1]example.com")).toThrowError( + expect(() => AdgModifierListParser.parse("[m1]example.com")).toThrowError( /^Missing modifier marker "\$" at pattern/ ); - expect(() => AdGuardModifierListParser.parse("[$m1example.com")).toThrowError( + expect(() => AdgModifierListParser.parse("[$m1example.com")).toThrowError( /^Missing closing bracket "]" at pattern/ ); - expect(() => AdGuardModifierListParser.parse("[$]example.com")).toThrowError( - /^No modifiers specified at pattern/ - ); - expect(() => AdGuardModifierListParser.parse("[$ ]example.com")).toThrowError( + expect(() => AdgModifierListParser.parse("[$]example.com")).toThrowError(/^No modifiers specified at pattern/); + expect(() => AdgModifierListParser.parse("[$ ]example.com")).toThrowError( /^No modifiers specified at pattern/ ); }); test("generate", async () => { const parseAndGenerate = (raw: string) => { - const ast = AdGuardModifierListParser.parse(raw); + const ast = AdgModifierListParser.parse(raw); if (ast) { - return AdGuardModifierListParser.generate(ast); + return AdgModifierListParser.generate(ast); } return null; diff --git a/test/parser/cosmetic/specific/ubo-options.test.ts b/test/parser/cosmetic/specific/ubo-modifiers.test.ts similarity index 75% rename from test/parser/cosmetic/specific/ubo-options.test.ts rename to test/parser/cosmetic/specific/ubo-modifiers.test.ts index 1c04e7fa..c90b2963 100644 --- a/test/parser/cosmetic/specific/ubo-options.test.ts +++ b/test/parser/cosmetic/specific/ubo-modifiers.test.ts @@ -1,21 +1,21 @@ /* eslint-disable max-len */ -import { UBlockModifierListParser, UBO_MODIFIER_LIST_TYPE } from "../../../../src/parser/cosmetic/specific/ubo-options"; +import { UboModifierListParser, UBO_MODIFIER_LIST_TYPE } from "../../../../src/parser/cosmetic/specific/ubo-modifiers"; import { EMPTY } from "../../../../src/utils/constants"; -describe("UBlockModifierListParser", () => { - test("hasUblockModifierIndicators", async () => { - expect(UBlockModifierListParser.hasUblockModifierIndicators(":matches-path(/path).ad")).toBe(true); - expect( - UBlockModifierListParser.hasUblockModifierIndicators(":matches-path(/path).ad:matches-path(/path)") - ).toBe(true); +describe("UboModifierListParser", () => { + test("hasUboModifierIndicators", async () => { + expect(UboModifierListParser.hasUboModifierIndicators(":matches-path(/path).ad")).toBe(true); + expect(UboModifierListParser.hasUboModifierIndicators(":matches-path(/path).ad:matches-path(/path)")).toBe( + true + ); - expect(UBlockModifierListParser.hasUblockModifierIndicators(":matches-path2(/path).ad")).toBe(false); + expect(UboModifierListParser.hasUboModifierIndicators(":matches-path2(/path).ad")).toBe(false); - expect(UBlockModifierListParser.hasUblockModifierIndicators(".ad")).toBe(false); + expect(UboModifierListParser.hasUboModifierIndicators(".ad")).toBe(false); }); test("parse", async () => { - expect(UBlockModifierListParser.parse(":matches-path(/path).ad")).toEqual({ + expect(UboModifierListParser.parse(":matches-path(/path).ad")).toEqual({ type: UBO_MODIFIER_LIST_TYPE, modifiers: [ { @@ -28,7 +28,7 @@ describe("UBlockModifierListParser", () => { }); // trim - expect(UBlockModifierListParser.parse(":matches-path(/path) .ad")).toEqual({ + expect(UboModifierListParser.parse(":matches-path(/path) .ad")).toEqual({ type: UBO_MODIFIER_LIST_TYPE, modifiers: [ { @@ -40,7 +40,7 @@ describe("UBlockModifierListParser", () => { rest: ".ad", }); - expect(UBlockModifierListParser.parse(":matches-path() .ad")).toEqual({ + expect(UboModifierListParser.parse(":matches-path() .ad")).toEqual({ type: UBO_MODIFIER_LIST_TYPE, modifiers: [ { @@ -52,7 +52,7 @@ describe("UBlockModifierListParser", () => { rest: ".ad", }); - expect(UBlockModifierListParser.parse(":matches-path() .ad")).toEqual({ + expect(UboModifierListParser.parse(":matches-path() .ad")).toEqual({ type: UBO_MODIFIER_LIST_TYPE, modifiers: [ { @@ -65,7 +65,7 @@ describe("UBlockModifierListParser", () => { }); expect( - UBlockModifierListParser.parse( + UboModifierListParser.parse( ":matches-path(a) .ad:matches-path(b):matches-path(c) > .something:matches-path(d) > .advert" ) ).toEqual({ @@ -96,7 +96,7 @@ describe("UBlockModifierListParser", () => { }); // not - expect(UBlockModifierListParser.parse(":not(:matches-path(/path)) .ad")).toEqual({ + expect(UboModifierListParser.parse(":not(:matches-path(/path)) .ad")).toEqual({ type: UBO_MODIFIER_LIST_TYPE, modifiers: [ { @@ -109,7 +109,7 @@ describe("UBlockModifierListParser", () => { }); expect( - UBlockModifierListParser.parse( + UboModifierListParser.parse( ":not(:matches-path(a)) .ad:matches-path(b):not(:matches-path(c)) > .something:matches-path(d) > .advert" ) ).toEqual({ @@ -140,20 +140,20 @@ describe("UBlockModifierListParser", () => { }); // No uBO modifier - expect(UBlockModifierListParser.parse(".ad:-abp-has(.something)")).toEqual({ + expect(UboModifierListParser.parse(".ad:-abp-has(.something)")).toEqual({ type: UBO_MODIFIER_LIST_TYPE, modifiers: [], rest: ".ad:-abp-has(.something)", }); - expect(UBlockModifierListParser.parse(EMPTY)).toEqual({ + expect(UboModifierListParser.parse(EMPTY)).toEqual({ type: UBO_MODIFIER_LIST_TYPE, modifiers: [], rest: EMPTY, }); // trim - expect(UBlockModifierListParser.parse(" ")).toEqual({ + expect(UboModifierListParser.parse(" ")).toEqual({ type: UBO_MODIFIER_LIST_TYPE, modifiers: [], rest: EMPTY, @@ -162,10 +162,10 @@ describe("UBlockModifierListParser", () => { test("generate", async () => { const parseAndGenerate = (raw: string) => { - const ast = UBlockModifierListParser.parse(raw); + const ast = UboModifierListParser.parse(raw); if (ast) { - return UBlockModifierListParser.generate(ast); + return UboModifierListParser.generate(ast); } return null; diff --git a/test/parser/network/network.test.ts b/test/parser/network/network.test.ts index d145cca9..f499d0e7 100644 --- a/test/parser/network/network.test.ts +++ b/test/parser/network/network.test.ts @@ -1,7 +1,7 @@ -import { RuleCategories } from "../../../src/parser/common"; +import { RuleCategory } from "../../../src/parser/common"; import { NetworkRuleType } from "../../../src/parser/network/common"; import { - IRemoveHeaderNetworkRule, + RemoveHeaderNetworkRule, NetworkRuleParser, UBO_RESPONSEHEADER_INDICATOR, } from "../../../src/parser/network/network"; @@ -11,7 +11,7 @@ import { CLOSE_PARENTHESIS } from "../../../src/utils/constants"; describe("NetworkRuleParser", () => { test("parse", () => { expect(NetworkRuleParser.parse("||example.com")).toEqual({ - category: RuleCategories.Network, + category: RuleCategory.Network, type: NetworkRuleType.BasicNetworkRule, syntax: AdblockSyntax.Unknown, exception: false, @@ -20,7 +20,7 @@ describe("NetworkRuleParser", () => { }); expect(NetworkRuleParser.parse("@@||example.com")).toEqual({ - category: RuleCategories.Network, + category: RuleCategory.Network, type: NetworkRuleType.BasicNetworkRule, syntax: AdblockSyntax.Unknown, exception: true, @@ -29,7 +29,7 @@ describe("NetworkRuleParser", () => { }); expect(NetworkRuleParser.parse("@@||example.com$m1,m2=v2")).toEqual({ - category: RuleCategories.Network, + category: RuleCategory.Network, type: NetworkRuleType.BasicNetworkRule, syntax: AdblockSyntax.Unknown, exception: true, @@ -46,7 +46,7 @@ describe("NetworkRuleParser", () => { }); expect(NetworkRuleParser.parse("/example/")).toEqual({ - category: RuleCategories.Network, + category: RuleCategory.Network, type: NetworkRuleType.BasicNetworkRule, syntax: AdblockSyntax.Unknown, exception: false, @@ -55,7 +55,7 @@ describe("NetworkRuleParser", () => { }); expect(NetworkRuleParser.parse("@@/example/")).toEqual({ - category: RuleCategories.Network, + category: RuleCategory.Network, type: NetworkRuleType.BasicNetworkRule, syntax: AdblockSyntax.Unknown, exception: true, @@ -64,7 +64,7 @@ describe("NetworkRuleParser", () => { }); expect(NetworkRuleParser.parse("@@/example/$m1,m2=v2")).toEqual({ - category: RuleCategories.Network, + category: RuleCategory.Network, type: NetworkRuleType.BasicNetworkRule, syntax: AdblockSyntax.Unknown, exception: true, @@ -82,7 +82,7 @@ describe("NetworkRuleParser", () => { // Last $ in regex pattern expect(NetworkRuleParser.parse("@@/example/$m1,m2=v2,m3=/^r3$/")).toEqual({ - category: RuleCategories.Network, + category: RuleCategory.Network, type: NetworkRuleType.BasicNetworkRule, syntax: AdblockSyntax.Unknown, exception: true, @@ -104,7 +104,7 @@ describe("NetworkRuleParser", () => { // Escaped $ in regex expect(NetworkRuleParser.parse("@@/example/$m1,m2=v2,m3=/^r3\\$/")).toEqual({ - category: RuleCategories.Network, + category: RuleCategory.Network, type: NetworkRuleType.BasicNetworkRule, syntax: AdblockSyntax.Unknown, exception: true, @@ -126,7 +126,7 @@ describe("NetworkRuleParser", () => { // Escaped separator expect(NetworkRuleParser.parse("example.com\\$m1")).toEqual({ - category: RuleCategories.Network, + category: RuleCategory.Network, type: NetworkRuleType.BasicNetworkRule, syntax: AdblockSyntax.Unknown, exception: false, @@ -136,7 +136,7 @@ describe("NetworkRuleParser", () => { // Multiple separators expect(NetworkRuleParser.parse("example.com$m1$m2$m3$m4$m5")).toEqual({ - category: RuleCategories.Network, + category: RuleCategory.Network, type: NetworkRuleType.BasicNetworkRule, syntax: AdblockSyntax.Unknown, exception: false, @@ -149,7 +149,7 @@ describe("NetworkRuleParser", () => { }); expect(NetworkRuleParser.parse("example.com$m1=v1$m2$m3=v3$m4$m5=v5")).toEqual({ - category: RuleCategories.Network, + category: RuleCategory.Network, type: NetworkRuleType.BasicNetworkRule, syntax: AdblockSyntax.Unknown, exception: false, @@ -164,7 +164,7 @@ describe("NetworkRuleParser", () => { // Starts with "/" expect(NetworkRuleParser.parse("/ad.js$m1=v1")).toEqual({ - category: RuleCategories.Network, + category: RuleCategory.Network, type: NetworkRuleType.BasicNetworkRule, syntax: AdblockSyntax.Unknown, exception: false, @@ -179,7 +179,7 @@ describe("NetworkRuleParser", () => { // Pattern starts with / like regex patterns expect(NetworkRuleParser.parse("/ad.js^$m1=v1")).toEqual({ - category: RuleCategories.Network, + category: RuleCategory.Network, type: NetworkRuleType.BasicNetworkRule, syntax: AdblockSyntax.Unknown, exception: false, @@ -193,7 +193,7 @@ describe("NetworkRuleParser", () => { }); expect(NetworkRuleParser.parse("/ad.js^$m1=/^v1$/")).toEqual({ - category: RuleCategories.Network, + category: RuleCategory.Network, type: NetworkRuleType.BasicNetworkRule, syntax: AdblockSyntax.Unknown, exception: false, @@ -208,7 +208,7 @@ describe("NetworkRuleParser", () => { // Pattern contains an odd number of "/" characters expect(NetworkRuleParser.parse("example.com/a/b/c$m1=v1")).toEqual({ - category: RuleCategories.Network, + category: RuleCategory.Network, type: NetworkRuleType.BasicNetworkRule, syntax: AdblockSyntax.Unknown, exception: false, @@ -222,7 +222,7 @@ describe("NetworkRuleParser", () => { }); expect(NetworkRuleParser.parse("example.com$m1,m2=/^regex$/")).toEqual({ - category: RuleCategories.Network, + category: RuleCategory.Network, type: NetworkRuleType.BasicNetworkRule, syntax: AdblockSyntax.Unknown, exception: false, @@ -242,7 +242,7 @@ describe("NetworkRuleParser", () => { expect( NetworkRuleParser.parse("@@/example/scripts/ad.js$m1,m2=v2,m3=/^r3\\$/,m4=/r4\\/r4$/,m5=/^r5\\$/") ).toEqual({ - category: RuleCategories.Network, + category: RuleCategory.Network, type: NetworkRuleType.BasicNetworkRule, syntax: AdblockSyntax.Unknown, exception: true, @@ -273,7 +273,7 @@ describe("NetworkRuleParser", () => { expect( NetworkRuleParser.parse(`@@||example.org^$replace=/()[\\s\\S]*<\\/VAST>/v\\$1<\\/VAST>/i`) ).toEqual({ - category: RuleCategories.Network, + category: RuleCategory.Network, type: NetworkRuleType.BasicNetworkRule, syntax: AdblockSyntax.Unknown, exception: true, @@ -287,41 +287,41 @@ describe("NetworkRuleParser", () => { }); // ADG removeheader - expect(NetworkRuleParser.parse(`||example.org^$removeheader=header-name`)).toEqual({ - category: RuleCategories.Network, + expect(NetworkRuleParser.parse(`||example.org^$removeheader=header-name`)).toEqual({ + category: RuleCategory.Network, type: NetworkRuleType.RemoveHeaderNetworkRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: false, pattern: "||example.org^", header: "header-name", }); - expect(NetworkRuleParser.parse(`@@||example.org^$removeheader=header-name`)).toEqual({ - category: RuleCategories.Network, + expect(NetworkRuleParser.parse(`@@||example.org^$removeheader=header-name`)).toEqual({ + category: RuleCategory.Network, type: NetworkRuleType.RemoveHeaderNetworkRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: true, pattern: "||example.org^", header: "header-name", }); expect(NetworkRuleParser.parse(`||example.org^$removeheader=request:header-name`)).toEqual(< - IRemoveHeaderNetworkRule + RemoveHeaderNetworkRule >{ - category: RuleCategories.Network, + category: RuleCategory.Network, type: NetworkRuleType.RemoveHeaderNetworkRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: false, pattern: "||example.org^", header: "request:header-name", }); expect(NetworkRuleParser.parse(`@@||example.org^$removeheader=request:header-name`)).toEqual(< - IRemoveHeaderNetworkRule + RemoveHeaderNetworkRule >{ - category: RuleCategories.Network, + category: RuleCategory.Network, type: NetworkRuleType.RemoveHeaderNetworkRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: true, pattern: "||example.org^", header: "request:header-name", @@ -332,21 +332,19 @@ describe("NetworkRuleParser", () => { ); // uBO responseheader - expect(NetworkRuleParser.parse(`example.org##^responseheader(header-name)`)).toEqual({ - category: RuleCategories.Network, + expect(NetworkRuleParser.parse(`example.org##^responseheader(header-name)`)).toEqual({ + category: RuleCategory.Network, type: NetworkRuleType.RemoveHeaderNetworkRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: false, pattern: "example.org", header: "header-name", }); - expect(NetworkRuleParser.parse(`example.org#@#^responseheader(header-name)`)).toEqual(< - IRemoveHeaderNetworkRule - >{ - category: RuleCategories.Network, + expect(NetworkRuleParser.parse(`example.org#@#^responseheader(header-name)`)).toEqual({ + category: RuleCategory.Network, type: NetworkRuleType.RemoveHeaderNetworkRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: true, pattern: "example.org", header: "header-name", diff --git a/test/parser/rule.test.ts b/test/parser/rule.test.ts index c1679a7b..dd05cdc3 100644 --- a/test/parser/rule.test.ts +++ b/test/parser/rule.test.ts @@ -1,7 +1,7 @@ /* eslint-disable max-len */ import { CommentRuleType } from "../../src/parser/comment/common"; -import { RuleCategories } from "../../src/parser/common"; +import { RuleCategory } from "../../src/parser/common"; import { CosmeticRuleType } from "../../src/parser/cosmetic/common"; import { NetworkRuleType } from "../../src/parser/network/common"; import { RuleParser } from "../../src/parser/rule"; @@ -13,198 +13,198 @@ describe("RuleParser", () => { // Agents expect(RuleParser.parse("[Adblock Plus 2.0]")).toMatchObject({ - category: RuleCategories.Comment, + category: RuleCategory.Comment, type: CommentRuleType.Agent, }); expect(RuleParser.parse("[Adblock Plus]")).toMatchObject({ - category: RuleCategories.Comment, + category: RuleCategory.Comment, type: CommentRuleType.Agent, }); expect(RuleParser.parse("[AdGuard]")).toMatchObject({ - category: RuleCategories.Comment, + category: RuleCategory.Comment, type: CommentRuleType.Agent, }); // Hints expect(RuleParser.parse("!+NOT_OPTIMIZED")).toMatchObject({ - category: RuleCategories.Comment, + category: RuleCategory.Comment, type: CommentRuleType.Hint, }); expect(RuleParser.parse("!+ NOT_OPTIMIZED")).toMatchObject({ - category: RuleCategories.Comment, + category: RuleCategory.Comment, type: CommentRuleType.Hint, }); expect(RuleParser.parse("!+ NOT_OPTIMIZED PLATFORM(windows, mac) NOT_PLATFORM(android, ios)")).toMatchObject({ - category: RuleCategories.Comment, + category: RuleCategory.Comment, type: CommentRuleType.Hint, }); // Pre-processors expect(RuleParser.parse("!#if (adguard)")).toMatchObject({ - category: RuleCategories.Comment, + category: RuleCategory.Comment, type: CommentRuleType.PreProcessor, }); expect(RuleParser.parse("!#if (adguard && !adguard_ext_safari)")).toMatchObject({ - category: RuleCategories.Comment, + category: RuleCategory.Comment, type: CommentRuleType.PreProcessor, }); expect(RuleParser.parse("!#safari_cb_affinity")).toMatchObject({ - category: RuleCategories.Comment, + category: RuleCategory.Comment, type: CommentRuleType.PreProcessor, }); expect(RuleParser.parse("!#safari_cb_affinity(content_blockers)")).toMatchObject({ - category: RuleCategories.Comment, + category: RuleCategory.Comment, type: CommentRuleType.PreProcessor, }); // Inline linter configurations expect(RuleParser.parse("! aglint-enable")).toMatchObject({ - category: RuleCategories.Comment, + category: RuleCategory.Comment, type: CommentRuleType.ConfigComment, }); expect(RuleParser.parse("! aglint-disable")).toMatchObject({ - category: RuleCategories.Comment, + category: RuleCategory.Comment, type: CommentRuleType.ConfigComment, }); expect(RuleParser.parse('! aglint rule1: "off"')).toMatchObject({ - category: RuleCategories.Comment, + category: RuleCategory.Comment, type: CommentRuleType.ConfigComment, }); // Metadata comments expect(RuleParser.parse("! Title: My List")).toMatchObject({ - category: RuleCategories.Comment, + category: RuleCategory.Comment, type: CommentRuleType.Metadata, }); expect(RuleParser.parse("! Version: 2.0.150")).toMatchObject({ - category: RuleCategories.Comment, + category: RuleCategory.Comment, type: CommentRuleType.Metadata, }); // Simple comments expect(RuleParser.parse("! This is just a comment")).toMatchObject({ - category: RuleCategories.Comment, + category: RuleCategory.Comment, type: CommentRuleType.Comment, }); expect(RuleParser.parse("# This is just a comment")).toMatchObject({ - category: RuleCategories.Comment, + category: RuleCategory.Comment, type: CommentRuleType.Comment, }); expect(RuleParser.parse("! https://github.com/AdguardTeam/AdguardFilters/issues/134623")).toMatchObject({ - category: RuleCategories.Comment, + category: RuleCategory.Comment, type: CommentRuleType.Comment, }); // Element hiding rules expect(RuleParser.parse("##.ad")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.ElementHidingRule, exception: false, }); expect(RuleParser.parse("example.com,~example.net##.ad")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.ElementHidingRule, exception: false, }); expect(RuleParser.parse("#@#.ad")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.ElementHidingRule, exception: true, }); expect(RuleParser.parse("example.com,~example.net#@#.ad")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.ElementHidingRule, exception: true, }); expect(RuleParser.parse("##.ad:-abp-has(.ad)")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.ElementHidingRule, exception: false, }); expect(RuleParser.parse("example.com,~example.net##.ad:-abp-has(.ad)")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.ElementHidingRule, exception: false, }); expect(RuleParser.parse("#@#.ad:-abp-has(.ad)")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.ElementHidingRule, exception: true, }); expect(RuleParser.parse("example.com,~example.net#@#.ad:-abp-has(.ad)")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.ElementHidingRule, exception: true, }); // CSS injections (AdGuard) expect(RuleParser.parse("#$#body { padding: 0; }")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: false, }); expect(RuleParser.parse("example.com,~example.net#$#body { padding: 0; }")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: false, }); expect(RuleParser.parse("#$#.ads { remove: true; }")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: false, }); expect(RuleParser.parse("#@$#body { padding: 0; }")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: true, }); expect(RuleParser.parse("example.com,~example.net#@$#body { padding: 0; }")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: true, }); expect(RuleParser.parse("#@$#.ads { remove: true; }")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: true, }); expect( RuleParser.parse("#$#@media (min-height: 1024px) and (max-height: 1920px) { body { padding: 0; } }") ).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: false, }); @@ -213,18 +213,18 @@ describe("RuleParser", () => { "example.com,~example.net#$#@media (min-height: 1024px) and (max-height: 1920px) { body { padding: 0; } }" ) ).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: false, }); expect( RuleParser.parse("#@$#@media (min-height: 1024px) and (max-height: 1920px) { body { padding: 0; } }") ).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: true, }); @@ -233,37 +233,37 @@ describe("RuleParser", () => { "example.com,~example.net#@$#@media (min-height: 1024px) and (max-height: 1920px) { body { padding: 0; } }" ) ).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: true, }); expect(RuleParser.parse("#$?#body:has(.ads) { padding: 0; }")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: false, }); expect(RuleParser.parse("example.com,~example.net#$?#body:has(.ads) { padding: 0; }")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: false, }); expect(RuleParser.parse("#@$?#body:has(.ads) { padding: 0; }")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: true, }); expect(RuleParser.parse("example.com,~example.net#@$?#body:has(.ads) { padding: 0; }")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: true, }); @@ -272,9 +272,9 @@ describe("RuleParser", () => { "#$?#@media (min-height: 1024px) and (max-height: 1920px) { body:has(.ads) { padding: 0; } }" ) ).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: false, }); @@ -283,9 +283,9 @@ describe("RuleParser", () => { "example.com,~example.net#$?#@media (min-height: 1024px) and (max-height: 1920px) { body:has(.ads) { padding: 0; } }" ) ).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: false, }); @@ -294,9 +294,9 @@ describe("RuleParser", () => { "#@$?#@media (min-height: 1024px) and (max-height: 1920px) { body:has(.ads) { padding: 0; } }" ) ).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: true, }); @@ -305,502 +305,502 @@ describe("RuleParser", () => { "example.com,~example.net#@$?#@media (min-height: 1024px) and (max-height: 1920px) { body:has(.ads) { padding: 0; } }" ) ).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: true, }); // CSS injections (uBlock) expect(RuleParser.parse("##body:style(padding: 0;)")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: false, }); expect(RuleParser.parse("example.com,~example.net##body:style(padding: 0;)")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: false, }); expect(RuleParser.parse("##.ads:remove()")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: false, }); expect(RuleParser.parse("#@#body:style(padding: 0;)")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: true, }); expect(RuleParser.parse("example.com,~example.net#@#body:style(padding: 0;)")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: true, }); expect(RuleParser.parse("#@#.ads:remove()")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: true, }); expect(RuleParser.parse("##body:has(.ads):style(padding: 0;)")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: false, }); expect(RuleParser.parse("example.com,~example.net##body:has(.ads):style(padding: 0;)")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: false, }); expect(RuleParser.parse("#@#body:has(.ads):style(padding: 0;)")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: true, }); expect(RuleParser.parse("example.com,~example.net#@#body:has(.ads):style(padding: 0;)")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.CssRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: true, }); // Scriptlets (AdGuard) expect(RuleParser.parse("#%#//scriptlet('scriptlet0', 'arg0', 'arg1')")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: false, }); expect(RuleParser.parse("example.com,~example.net#%#//scriptlet('scriptlet0', 'arg0', 'arg1')")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: false, }); expect(RuleParser.parse("#@%#//scriptlet('scriptlet0', 'arg0', 'arg1')")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: true, }); expect(RuleParser.parse("example.com,~example.net#@%#//scriptlet('scriptlet0', 'arg0', 'arg1')")).toMatchObject( { - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: true, } ); // Scriptlets (uBlock) expect(RuleParser.parse("##+js(scriptlet0, arg0, arg1)")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: false, }); expect(RuleParser.parse("example.com,~example.net##+js(scriptlet0, arg0, arg1)")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: false, }); expect(RuleParser.parse("#@#+js(scriptlet0, arg0, arg1)")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: true, }); expect(RuleParser.parse("example.com,~example.net#@#+js(scriptlet0, arg0, arg1)")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: true, }); // Snippets (Adblock Plus) expect(RuleParser.parse("#$#scriptlet0 arg0 arg1")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.AdblockPlus, + syntax: AdblockSyntax.Abp, exception: false, }); expect(RuleParser.parse("example.com,~example.net#$#scriptlet0 arg0 arg1")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.AdblockPlus, + syntax: AdblockSyntax.Abp, exception: false, }); expect(RuleParser.parse("#@$#scriptlet0 arg0 arg1")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.AdblockPlus, + syntax: AdblockSyntax.Abp, exception: true, }); expect(RuleParser.parse("example.com,~example.net#@$#scriptlet0 arg0 arg1")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.AdblockPlus, + syntax: AdblockSyntax.Abp, exception: true, }); // ; at end expect(RuleParser.parse("#$#scriptlet0 arg0 arg1;")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.AdblockPlus, + syntax: AdblockSyntax.Abp, exception: false, }); expect(RuleParser.parse("example.com,~example.net#$#scriptlet0 arg0 arg1;")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.AdblockPlus, + syntax: AdblockSyntax.Abp, exception: false, }); expect(RuleParser.parse("#@$#scriptlet0 arg0 arg1;")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.AdblockPlus, + syntax: AdblockSyntax.Abp, exception: true, }); expect(RuleParser.parse("example.com,~example.net#@$#scriptlet0 arg0 arg1;")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.AdblockPlus, + syntax: AdblockSyntax.Abp, exception: true, }); // Multiple snippets (Adblock Plus) expect(RuleParser.parse("#$#scriptlet0 arg00 arg01; scriptlet1 arg10 arg11")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.AdblockPlus, + syntax: AdblockSyntax.Abp, exception: false, }); expect( RuleParser.parse("example.com,~example.net#$#scriptlet0 arg00 arg01; scriptlet1 arg10 arg11") ).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.AdblockPlus, + syntax: AdblockSyntax.Abp, exception: false, }); expect(RuleParser.parse("#@$#scriptlet0 arg00 arg01; scriptlet1 arg10 arg11")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.AdblockPlus, + syntax: AdblockSyntax.Abp, exception: true, }); expect( RuleParser.parse("example.com,~example.net#@$#scriptlet0 arg00 arg01; scriptlet1 arg10 arg11") ).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.AdblockPlus, + syntax: AdblockSyntax.Abp, exception: true, }); // ; at end expect(RuleParser.parse("#$#scriptlet0 arg00 arg01; scriptlet1 arg10 arg11;")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.AdblockPlus, + syntax: AdblockSyntax.Abp, }); expect( RuleParser.parse("example.com,~example.net#$#scriptlet0 arg00 arg01; scriptlet1 arg10 arg11;") ).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.AdblockPlus, + syntax: AdblockSyntax.Abp, exception: false, }); expect(RuleParser.parse("#@$#scriptlet0 arg00 arg01; scriptlet1 arg10 arg11;")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.AdblockPlus, + syntax: AdblockSyntax.Abp, exception: true, }); expect( RuleParser.parse("example.com,~example.net#@$#scriptlet0 arg00 arg01; scriptlet1 arg10 arg11;") ).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.ScriptletRule, - syntax: AdblockSyntax.AdblockPlus, + syntax: AdblockSyntax.Abp, exception: true, }); // HTML filters (AdGuard) expect(RuleParser.parse('$$script[tag-content="adblock"]')).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.HtmlRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: false, }); expect(RuleParser.parse('example.com,~example.net$$script[tag-content="adblock"]')).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.HtmlRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: false, }); expect(RuleParser.parse('$@$script[tag-content="adblock"]')).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.HtmlRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: true, }); expect(RuleParser.parse('example.com,~example.net$@$script[tag-content="adblock"]')).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.HtmlRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: true, }); // HTML filters (uBlock) expect(RuleParser.parse("##^script:has-text(adblock)")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.HtmlRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: false, }); expect(RuleParser.parse("example.com,~example.net##^script:has-text(adblock)")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.HtmlRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: false, }); expect(RuleParser.parse("#@#^script:has-text(adblock)")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.HtmlRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: true, }); expect(RuleParser.parse("example.com,~example.net#@#^script:has-text(adblock)")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.HtmlRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: true, }); expect(RuleParser.parse("##^script:has-text(adblock), script:has-text(detector)")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.HtmlRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: false, }); expect( RuleParser.parse("example.com,~example.net##^script:has-text(adblock), script:has-text(detector)") ).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.HtmlRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: false, }); expect(RuleParser.parse("#@#^script:has-text(adblock), script:has-text(detector)")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.HtmlRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: true, }); expect( RuleParser.parse("example.com,~example.net#@#^script:has-text(adblock), script:has-text(detector)") ).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.HtmlRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: true, }); // JS inject (AdGuard) expect(RuleParser.parse("#%#const a = 2;")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.JsRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: false, }); expect(RuleParser.parse("example.com,~example.net#%#const a = 2;")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.JsRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: false, }); expect(RuleParser.parse("#@%#const a = 2;")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.JsRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: true, }); expect(RuleParser.parse("example.com,~example.net#@%#const a = 2;")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.JsRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: true, }); // AdGuard modifiers/options expect(RuleParser.parse("[$app=com.something]#%#const a = 2;")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.JsRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: false, }); expect(RuleParser.parse("[$app=com.something]example.com,~example.net#%#const a = 2;")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.JsRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: false, }); expect(RuleParser.parse("[$app=com.something]#@%#const a = 2;")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.JsRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: true, }); expect(RuleParser.parse("[$app=com.something]example.com,~example.net#@%#const a = 2;")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.JsRule, - syntax: AdblockSyntax.AdGuard, + syntax: AdblockSyntax.Adg, exception: true, }); // uBlock modifiers/options expect(RuleParser.parse("##:matches-path(/path) .ad")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.ElementHidingRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: false, }); expect(RuleParser.parse("example.com,~example.net##:matches-path(/path) .ad")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.ElementHidingRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: false, }); expect(RuleParser.parse("#@#:matches-path(/path) .ad")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.ElementHidingRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: true, }); expect(RuleParser.parse("example.com,~example.net#@#:matches-path(/path) .ad")).toMatchObject({ - category: RuleCategories.Cosmetic, + category: RuleCategory.Cosmetic, type: CosmeticRuleType.ElementHidingRule, - syntax: AdblockSyntax.uBlockOrigin, + syntax: AdblockSyntax.Ubo, exception: true, }); // Network rules expect(RuleParser.parse("||example.com")).toMatchObject({ - category: RuleCategories.Network, + category: RuleCategory.Network, type: NetworkRuleType.BasicNetworkRule, syntax: AdblockSyntax.Unknown, exception: false, }); expect(RuleParser.parse("@@||example.com")).toMatchObject({ - category: RuleCategories.Network, + category: RuleCategory.Network, type: NetworkRuleType.BasicNetworkRule, syntax: AdblockSyntax.Unknown, exception: true, }); expect(RuleParser.parse("/regex/")).toMatchObject({ - category: RuleCategories.Network, + category: RuleCategory.Network, type: NetworkRuleType.BasicNetworkRule, syntax: AdblockSyntax.Unknown, exception: false, }); expect(RuleParser.parse("@@/regex/")).toMatchObject({ - category: RuleCategories.Network, + category: RuleCategory.Network, type: NetworkRuleType.BasicNetworkRule, syntax: AdblockSyntax.Unknown, exception: true, }); expect(RuleParser.parse("/regex/$m1,m2=v2")).toMatchObject({ - category: RuleCategories.Network, + category: RuleCategory.Network, type: NetworkRuleType.BasicNetworkRule, syntax: AdblockSyntax.Unknown, exception: false, }); expect(RuleParser.parse("@@/regex/$m1,m2=v2")).toMatchObject({ - category: RuleCategories.Network, + category: RuleCategory.Network, type: NetworkRuleType.BasicNetworkRule, syntax: AdblockSyntax.Unknown, exception: true, }); expect(RuleParser.parse("/example/$m1,m2=v2,m3=/^r3$/")).toMatchObject({ - category: RuleCategories.Network, + category: RuleCategory.Network, type: NetworkRuleType.BasicNetworkRule, syntax: AdblockSyntax.Unknown, exception: false, }); expect(RuleParser.parse("@@/example/$m1,m2=v2,m3=/^r3$/")).toMatchObject({ - category: RuleCategories.Network, + category: RuleCategory.Network, type: NetworkRuleType.BasicNetworkRule, syntax: AdblockSyntax.Unknown, exception: true, }); expect(RuleParser.parse("/ad.js^$m1=/^v1$/")).toMatchObject({ - category: RuleCategories.Network, + category: RuleCategory.Network, type: NetworkRuleType.BasicNetworkRule, syntax: AdblockSyntax.Unknown, exception: false, @@ -809,7 +809,7 @@ describe("RuleParser", () => { expect( RuleParser.parse("@@/example/scripts/ad.js$m1,m2=v2,m3=/^r3\\$/,m4=/r4\\/r4$/,m5=/^r5\\$/") ).toMatchObject({ - category: RuleCategories.Network, + category: RuleCategory.Network, type: NetworkRuleType.BasicNetworkRule, syntax: AdblockSyntax.Unknown, exception: true, @@ -818,7 +818,7 @@ describe("RuleParser", () => { expect( RuleParser.parse("@@||example.org^$replace=/()[\\s\\S]*<\\/VAST>/v\\$1<\\/VAST>/i") ).toMatchObject({ - category: RuleCategories.Network, + category: RuleCategory.Network, type: NetworkRuleType.BasicNetworkRule, syntax: AdblockSyntax.Unknown, exception: true, diff --git a/test/utils/cosmetic-rule-separator.test.ts b/test/utils/cosmetic-rule-separator.test.ts index cc6235ba..c6751390 100644 --- a/test/utils/cosmetic-rule-separator.test.ts +++ b/test/utils/cosmetic-rule-separator.test.ts @@ -9,47 +9,47 @@ describe("CosmeticRuleSeparator", () => { expect(CosmeticRuleSeparatorUtils.isElementHiding("#@?#")).toBe(true); }); - test("isAdGuardCss", () => { - expect(CosmeticRuleSeparatorUtils.isAdGuardCss("#$#")).toBe(true); - expect(CosmeticRuleSeparatorUtils.isAdGuardCss("#@$#")).toBe(true); - expect(CosmeticRuleSeparatorUtils.isAdGuardCss("#$?#")).toBe(true); - expect(CosmeticRuleSeparatorUtils.isAdGuardCss("#@$?#")).toBe(true); + test("isAdgCss", () => { + expect(CosmeticRuleSeparatorUtils.isAdgCss("#$#")).toBe(true); + expect(CosmeticRuleSeparatorUtils.isAdgCss("#@$#")).toBe(true); + expect(CosmeticRuleSeparatorUtils.isAdgCss("#$?#")).toBe(true); + expect(CosmeticRuleSeparatorUtils.isAdgCss("#@$?#")).toBe(true); }); - test("isAdblockPlusSnippet", () => { - expect(CosmeticRuleSeparatorUtils.isAdblockPlusSnippet("#$#")).toBe(true); - expect(CosmeticRuleSeparatorUtils.isAdblockPlusSnippet("#@$#")).toBe(true); + test("isAbpSnippet", () => { + expect(CosmeticRuleSeparatorUtils.isAbpSnippet("#$#")).toBe(true); + expect(CosmeticRuleSeparatorUtils.isAbpSnippet("#@$#")).toBe(true); }); - test("isUblockScriptlet", () => { - expect(CosmeticRuleSeparatorUtils.isUblockScriptlet("##+js")).toBe(true); - expect(CosmeticRuleSeparatorUtils.isUblockScriptlet("#@#+js")).toBe(true); + test("isUboScriptlet", () => { + expect(CosmeticRuleSeparatorUtils.isUboScriptlet("##+js")).toBe(true); + expect(CosmeticRuleSeparatorUtils.isUboScriptlet("#@#+js")).toBe(true); - expect(CosmeticRuleSeparatorUtils.isUblockScriptlet("#?#+js")).toBe(false); - expect(CosmeticRuleSeparatorUtils.isUblockScriptlet("#@?#+js")).toBe(false); + expect(CosmeticRuleSeparatorUtils.isUboScriptlet("#?#+js")).toBe(false); + expect(CosmeticRuleSeparatorUtils.isUboScriptlet("#@?#+js")).toBe(false); }); - test("isAdGuardScriptlet", () => { - expect(CosmeticRuleSeparatorUtils.isAdGuardScriptlet("#%#//scriptlet")).toBe(true); - expect(CosmeticRuleSeparatorUtils.isAdGuardScriptlet("#@%#//scriptlet")).toBe(true); + test("isAdgScriptlet", () => { + expect(CosmeticRuleSeparatorUtils.isAdgScriptlet("#%#//scriptlet")).toBe(true); + expect(CosmeticRuleSeparatorUtils.isAdgScriptlet("#@%#//scriptlet")).toBe(true); - expect(CosmeticRuleSeparatorUtils.isAdGuardScriptlet("#?%#//scriptlet")).toBe(false); - expect(CosmeticRuleSeparatorUtils.isAdGuardScriptlet("#@?%#//scriptlet")).toBe(false); + expect(CosmeticRuleSeparatorUtils.isAdgScriptlet("#?%#//scriptlet")).toBe(false); + expect(CosmeticRuleSeparatorUtils.isAdgScriptlet("#@?%#//scriptlet")).toBe(false); }); - test("isAdGuardJs", () => { - expect(CosmeticRuleSeparatorUtils.isAdGuardJs("#%#")).toBe(true); - expect(CosmeticRuleSeparatorUtils.isAdGuardJs("#@%#")).toBe(true); + test("isAdgJs", () => { + expect(CosmeticRuleSeparatorUtils.isAdgJs("#%#")).toBe(true); + expect(CosmeticRuleSeparatorUtils.isAdgJs("#@%#")).toBe(true); }); - test("isUblockHtml", () => { - expect(CosmeticRuleSeparatorUtils.isUblockHtml("##^")).toBe(true); - expect(CosmeticRuleSeparatorUtils.isUblockHtml("#@#^")).toBe(true); + test("isUboHtml", () => { + expect(CosmeticRuleSeparatorUtils.isUboHtml("##^")).toBe(true); + expect(CosmeticRuleSeparatorUtils.isUboHtml("#@#^")).toBe(true); }); - test("isAdGuardHtml", () => { - expect(CosmeticRuleSeparatorUtils.isAdGuardHtml("$$")).toBe(true); - expect(CosmeticRuleSeparatorUtils.isAdGuardHtml("$@$")).toBe(true); + test("isAdgHtml", () => { + expect(CosmeticRuleSeparatorUtils.isAdgHtml("$$")).toBe(true); + expect(CosmeticRuleSeparatorUtils.isAdgHtml("$@$")).toBe(true); }); test("isScriptlet", () => { diff --git a/tsconfig.json b/tsconfig.json index 6d72ac1d..a06e2faf 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -14,8 +14,8 @@ "esModuleInterop": true, "forceConsistentCasingInFileNames": true, "strict": true, - "skipLibCheck": true + "skipLibCheck": true, }, "include": ["./src/"], - "exclude": ["node_modules", "dist"] + "exclude": ["./node_modules", "./dist"] }