@adguard/agtree 2.2.0 → 3.0.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/ast-utils/clone.js +61 -0
- package/dist/ast-utils/modifiers.js +53 -0
- package/dist/ast-utils/network-rules.js +41 -0
- package/dist/ast-utils/scriptlets.js +87 -0
- package/dist/common/abp-snippet-injection-body-common.js +23 -0
- package/dist/common/agent-common.js +56 -0
- package/dist/common/ubo-selector-common.js +17 -0
- package/dist/compatibility-tables/base.js +262 -0
- package/dist/compatibility-tables/compatibility-table-data.js +11 -0
- package/dist/compatibility-tables/modifiers.js +55 -0
- package/dist/compatibility-tables/platforms.js +76 -0
- package/dist/compatibility-tables/redirects.js +100 -0
- package/dist/compatibility-tables/schemas/base.js +111 -0
- package/dist/compatibility-tables/schemas/modifier.js +129 -0
- package/dist/compatibility-tables/schemas/platform.js +69 -0
- package/dist/compatibility-tables/schemas/redirect.js +29 -0
- package/dist/compatibility-tables/schemas/resource-type.js +36 -0
- package/dist/compatibility-tables/schemas/scriptlet.js +79 -0
- package/dist/compatibility-tables/scriptlets.js +28 -0
- package/dist/compatibility-tables/utils/platform-helpers.js +105 -0
- package/dist/compatibility-tables/utils/resource-type-helpers.js +61 -0
- package/dist/compatibility-tables/utils/zod-camelcase.js +25 -0
- package/dist/converter/base-interfaces/base-converter.js +59 -0
- package/dist/converter/base-interfaces/conversion-result.js +38 -0
- package/dist/converter/base-interfaces/rule-converter-base.js +60 -0
- package/dist/converter/comment/index.js +54 -0
- package/dist/converter/cosmetic/css.js +69 -0
- package/dist/converter/cosmetic/element-hiding.js +62 -0
- package/dist/converter/cosmetic/header-removal.js +123 -0
- package/dist/converter/cosmetic/html.js +379 -0
- package/dist/converter/cosmetic/index.js +118 -0
- package/dist/converter/cosmetic/rule-modifiers/adg.js +79 -0
- package/dist/converter/cosmetic/scriptlet.js +354 -0
- package/dist/converter/css/index.js +188 -0
- package/dist/converter/data/css.js +126 -0
- package/dist/converter/filter-list.js +88 -0
- package/dist/converter/index.js +8 -0
- package/dist/converter/misc/network-rule-modifier.js +311 -0
- package/dist/converter/network/index.js +99 -0
- package/dist/converter/raw-filter-list.js +56 -0
- package/dist/converter/rule.js +81 -0
- package/dist/deserializer/base-deserializer.js +29 -0
- package/dist/deserializer/comment/agent-comment-deserializer.js +79 -0
- package/dist/deserializer/comment/agent-deserializer.js +65 -0
- package/dist/deserializer/comment/comment-rule-deserializer.js +54 -0
- package/dist/deserializer/comment/config-comment-deserializer.js +111 -0
- package/dist/deserializer/comment/hint-comment-deserializer.js +72 -0
- package/dist/deserializer/comment/hint-deserializer.js +85 -0
- package/dist/deserializer/comment/metadata-comment-deserializer.js +69 -0
- package/dist/deserializer/comment/pre-processor-comment-deserializer.js +112 -0
- package/dist/deserializer/comment/simple-comment-deserializer.js +65 -0
- package/dist/deserializer/cosmetic/cosmetic-rule-deserializer.js +131 -0
- package/dist/deserializer/cosmetic/css-injection-body-deserializer.js +56 -0
- package/dist/deserializer/cosmetic/element-hiding-body-deserializer.js +48 -0
- package/dist/deserializer/cosmetic/scriptlet-body/abp-snippet-injection-body-deserializer.js +39 -0
- package/dist/deserializer/cosmetic/scriptlet-body/adg-scriptlet-injection-body-deserializer.js +40 -0
- package/dist/deserializer/cosmetic/scriptlet-body/scriptlet-body-deserializer.js +54 -0
- package/dist/deserializer/cosmetic/scriptlet-body/ubo-scriptlet-injection-body-deserializer.js +38 -0
- package/dist/deserializer/empty-rule-deserializer.js +48 -0
- package/dist/deserializer/filterlist-deserializer.js +85 -0
- package/dist/deserializer/index.js +8 -0
- package/dist/deserializer/invalid-rule-deserializer.js +50 -0
- package/dist/deserializer/invalid-rule-error-node-deserializer.js +50 -0
- package/dist/deserializer/misc/domain-list-deserializer.js +64 -0
- package/dist/deserializer/misc/list-item-deserializer.js +69 -0
- package/dist/deserializer/misc/list-items-deserializer.js +30 -0
- package/dist/deserializer/misc/logical-expression-deserializer.js +196 -0
- package/dist/deserializer/misc/modifier-deserializer.js +87 -0
- package/dist/deserializer/misc/modifier-list-deserializer.js +61 -0
- package/dist/deserializer/misc/parameter-list-deserializer.js +64 -0
- package/dist/deserializer/misc/value-deserializer.js +50 -0
- package/dist/deserializer/network/host-rule-deserializer.js +67 -0
- package/dist/deserializer/network/hostname-list-deserializer.js +56 -0
- package/dist/deserializer/network/network-rule-deserializer.js +65 -0
- package/dist/deserializer/rule-deserializer.js +65 -0
- package/dist/deserializer/syntax-deserialization-map.js +21 -0
- package/dist/errors/adblock-syntax-error.js +39 -0
- package/dist/errors/binary-schema-mismatch-error.js +37 -0
- package/dist/errors/not-implemented-error.js +31 -0
- package/dist/errors/rule-conversion-error.js +26 -0
- package/dist/generator/base-generator.js +27 -0
- package/dist/generator/comment/agent-comment-generator.js +31 -0
- package/dist/generator/comment/agent-generator.js +36 -0
- package/dist/generator/comment/comment-rule-generator.js +47 -0
- package/dist/generator/comment/config-comment-generator.js +45 -0
- package/dist/generator/comment/hint-comment-generator.js +28 -0
- package/dist/generator/comment/hint-generator.js +33 -0
- package/dist/generator/comment/metadata-comment-generator.js +33 -0
- package/dist/generator/comment/pre-processor-comment-generator.js +48 -0
- package/dist/generator/comment/simple-comment-generator.js +29 -0
- package/dist/generator/cosmetic/body/abp-snippet-injection-body-generator.js +38 -0
- package/dist/generator/cosmetic/body/adg-scriptlet-injection-body-generator.js +43 -0
- package/dist/generator/cosmetic/body/ubo-scriptlet-injection-body-generator.js +45 -0
- package/dist/generator/cosmetic/cosmetic-rule-body-generator.js +94 -0
- package/dist/generator/cosmetic/cosmetic-rule-generator.js +54 -0
- package/dist/generator/cosmetic/cosmetic-rule-pattern-generator.js +42 -0
- package/dist/generator/css/adg-css-injection-generator.js +42 -0
- package/dist/generator/filterlist-generator.js +67 -0
- package/dist/generator/index.js +9 -0
- package/dist/generator/misc/domain-list-generator.js +26 -0
- package/dist/generator/misc/list-items-generator.js +39 -0
- package/dist/generator/misc/logical-expression-generator.js +48 -0
- package/dist/generator/misc/modifier-generator.js +34 -0
- package/dist/generator/misc/modifier-list-generator.js +29 -0
- package/dist/generator/misc/parameter-list-generator.js +41 -0
- package/dist/generator/misc/value-generator.js +24 -0
- package/dist/generator/network/host-rule-generator.js +39 -0
- package/dist/generator/network/network-rule-generator.js +38 -0
- package/dist/generator/rule-generator.js +65 -0
- package/dist/index.js +70 -0
- package/dist/marshalling-utils/comment/agent-comment-common.js +21 -0
- package/dist/marshalling-utils/comment/agent-common.js +40 -0
- package/dist/marshalling-utils/comment/config-comment-common.js +54 -0
- package/dist/marshalling-utils/comment/hint-comment-common.js +22 -0
- package/dist/marshalling-utils/comment/hint-common.js +56 -0
- package/dist/marshalling-utils/comment/metadata-comment-common.js +41 -0
- package/dist/marshalling-utils/comment/pre-processor-comment-common.js +59 -0
- package/dist/marshalling-utils/comment/simple-comment-common.js +22 -0
- package/dist/marshalling-utils/cosmetic/body/abp-snippet-injection-body-common.js +61 -0
- package/dist/marshalling-utils/cosmetic/body/adg-scriptlet-injection-body-common.js +66 -0
- package/dist/marshalling-utils/cosmetic/body/css-injection-body-common.js +24 -0
- package/dist/marshalling-utils/cosmetic/body/element-hiding-body-common.js +21 -0
- package/dist/marshalling-utils/cosmetic/body/ubo-scriptlet-injection-body-common.js +114 -0
- package/dist/marshalling-utils/cosmetic/cosmetic-rule-common.js +48 -0
- package/dist/marshalling-utils/empty-rule-common.js +20 -0
- package/dist/marshalling-utils/filter-list-common.js +21 -0
- package/dist/marshalling-utils/invalid-rule-common.js +21 -0
- package/dist/marshalling-utils/invalid-rule-error-node-common.js +22 -0
- package/dist/marshalling-utils/misc/binary-type-common.js +66 -0
- package/dist/marshalling-utils/misc/domain-list-common.js +36 -0
- package/dist/marshalling-utils/misc/host-rule-common.js +25 -0
- package/dist/marshalling-utils/misc/hostname-list-common.js +21 -0
- package/dist/marshalling-utils/misc/list-item-common.js +22 -0
- package/dist/marshalling-utils/misc/logical-expression-common.js +83 -0
- package/dist/marshalling-utils/misc/modifier-common.js +231 -0
- package/dist/marshalling-utils/misc/modifier-list-common.js +21 -0
- package/dist/marshalling-utils/misc/parameter-list-common.js +21 -0
- package/dist/marshalling-utils/misc/value-common.js +22 -0
- package/dist/marshalling-utils/network/network-rule-common.js +25 -0
- package/dist/marshalling-utils/syntax-serialization-map.js +30 -0
- package/dist/nodes/index.js +168 -0
- package/dist/package.json.js +9 -0
- package/dist/parser/base-parser.js +30 -0
- package/dist/parser/comment/agent-comment-parser.js +119 -0
- package/dist/parser/comment/agent-parser.js +110 -0
- package/dist/parser/comment/comment-parser.js +108 -0
- package/dist/parser/comment/config-comment-parser.js +137 -0
- package/dist/parser/comment/hint-comment-parser.js +117 -0
- package/dist/parser/comment/hint-parser.js +129 -0
- package/dist/parser/comment/metadata-comment-parser.js +129 -0
- package/dist/parser/comment/preprocessor-parser.js +199 -0
- package/dist/parser/comment/simple-comment-parser.js +106 -0
- package/dist/parser/cosmetic/body/abp-snippet-injection-body-parser.js +79 -0
- package/dist/parser/cosmetic/body/adg-scriptlet-injection-body-parser.js +149 -0
- package/dist/parser/cosmetic/body/ubo-scriptlet-injection-body-parser.js +110 -0
- package/dist/parser/cosmetic/cosmetic-rule-parser.js +453 -0
- package/dist/parser/css/adg-css-injection-parser.js +208 -0
- package/dist/parser/css/balancing.js +94 -0
- package/dist/parser/css/constants.js +18 -0
- package/dist/parser/css/css-token-stream.js +343 -0
- package/dist/parser/css/ubo-selector-parser.js +369 -0
- package/dist/parser/filterlist-parser.js +102 -0
- package/dist/parser/index.js +12 -0
- package/dist/parser/misc/app-list-parser.js +45 -0
- package/dist/parser/misc/domain-list-parser.js +52 -0
- package/dist/parser/misc/list-items-parser.js +116 -0
- package/dist/parser/misc/logical-expression-parser.js +279 -0
- package/dist/parser/misc/method-list-parser.js +46 -0
- package/dist/parser/misc/modifier-list.js +70 -0
- package/dist/parser/misc/modifier-parser.js +89 -0
- package/dist/parser/misc/parameter-list-parser.js +75 -0
- package/dist/parser/misc/stealth-option-list-parser.js +46 -0
- package/dist/parser/misc/ubo-parameter-list-parser.js +149 -0
- package/dist/parser/misc/value-parser.js +37 -0
- package/dist/parser/network/host-rule-parser.js +130 -0
- package/dist/parser/network/network-rule-parser.js +117 -0
- package/dist/parser/options.js +23 -0
- package/dist/parser/rule-parser.js +192 -0
- package/dist/serializer/base-serializer.js +29 -0
- package/dist/serializer/comment/agent-comment-serializer.js +74 -0
- package/dist/serializer/comment/agent-serializer.js +59 -0
- package/dist/serializer/comment/comment-rule-serializer.js +105 -0
- package/dist/serializer/comment/config-comment-serializer.js +82 -0
- package/dist/serializer/comment/hint-comment-serializer.js +65 -0
- package/dist/serializer/comment/hint-serializer.js +54 -0
- package/dist/serializer/comment/metadata-comment-serializer.js +73 -0
- package/dist/serializer/comment/pre-processor-comment-serializer.js +71 -0
- package/dist/serializer/comment/simple-comment-serializer.js +52 -0
- package/dist/serializer/cosmetic/body/abp-snippet-injection-body-serializer.js +36 -0
- package/dist/serializer/cosmetic/body/adg-scriptlet-injection-body-serializer.js +36 -0
- package/dist/serializer/cosmetic/body/scriptlet-body-serializer.js +50 -0
- package/dist/serializer/cosmetic/body/ubo-scriptlet-injection-body-serializer.js +36 -0
- package/dist/serializer/cosmetic/cosmetic-rule-serializer.js +120 -0
- package/dist/serializer/cosmetic/css-injection-body-serializer.js +51 -0
- package/dist/serializer/cosmetic/element-hiding-body-serializer.js +40 -0
- package/dist/serializer/empty-rule-serializer.js +37 -0
- package/dist/serializer/filterlist-serializer.js +45 -0
- package/dist/serializer/index.js +7 -0
- package/dist/serializer/invalid-rule-error-node-serializer.js +41 -0
- package/dist/serializer/invalid-rule-serializer.js +40 -0
- package/dist/serializer/misc/domain-list-serializer.js +64 -0
- package/dist/serializer/misc/list-item-serializer.js +58 -0
- package/dist/serializer/misc/list-items-serializer.js +29 -0
- package/dist/serializer/misc/logical-expression-serializer.js +136 -0
- package/dist/serializer/misc/modifier-list-serializer.js +58 -0
- package/dist/serializer/misc/modifier-serializer.js +49 -0
- package/dist/serializer/misc/parameter-list-serializer.js +52 -0
- package/dist/serializer/misc/value-serializer.js +50 -0
- package/dist/serializer/network/host-rule-serializer.js +70 -0
- package/dist/serializer/network/hostname-list-serializer.js +53 -0
- package/dist/serializer/network/network-rule-serializer.js +54 -0
- package/dist/serializer/rule-serializer.js +61 -0
- package/dist/types/ast-utils/clone.d.ts +26 -0
- package/dist/types/ast-utils/filter-list.d.ts +11 -0
- package/dist/types/ast-utils/modifiers.d.ts +20 -0
- package/dist/types/ast-utils/network-rules.d.ts +15 -0
- package/dist/types/ast-utils/scriptlets.d.ts +52 -0
- package/dist/types/common/abp-snippet-injection-body-common.d.ts +15 -0
- package/dist/types/common/agent-common.d.ts +10 -0
- package/dist/types/common/ubo-selector-common.d.ts +10 -0
- package/dist/types/compatibility-tables/base.d.ts +157 -0
- package/dist/types/compatibility-tables/compatibility-table-data.d.ts +68 -0
- package/dist/types/compatibility-tables/index.d.ts +13 -0
- package/dist/types/compatibility-tables/modifiers.d.ts +22 -0
- package/dist/types/compatibility-tables/platforms.d.ts +49 -0
- package/dist/types/compatibility-tables/redirects.d.ts +33 -0
- package/dist/types/compatibility-tables/schemas/base.d.ts +124 -0
- package/dist/types/compatibility-tables/schemas/index.d.ts +8 -0
- package/dist/types/compatibility-tables/schemas/modifier.d.ts +36 -0
- package/dist/types/compatibility-tables/schemas/platform.d.ts +16 -0
- package/dist/types/compatibility-tables/schemas/redirect.d.ts +25 -0
- package/dist/types/compatibility-tables/schemas/resource-type.d.ts +41 -0
- package/dist/types/compatibility-tables/schemas/scriptlet.d.ts +31 -0
- package/dist/types/compatibility-tables/scriptlets.d.ts +15 -0
- package/dist/types/compatibility-tables/types.d.ts +34 -0
- package/dist/types/compatibility-tables/utils/platform-helpers.d.ts +44 -0
- package/dist/types/compatibility-tables/utils/resource-type-helpers.d.ts +20 -0
- package/dist/types/compatibility-tables/utils/zod-camelcase.d.ts +15 -0
- package/dist/types/converter/base-interfaces/base-converter.d.ts +42 -0
- package/dist/types/converter/base-interfaces/conversion-result.d.ts +43 -0
- package/dist/types/converter/base-interfaces/rule-converter-base.d.ts +44 -0
- package/dist/types/converter/comment/index.d.ts +23 -0
- package/dist/types/converter/cosmetic/css.d.ts +23 -0
- package/dist/types/converter/cosmetic/element-hiding.d.ts +23 -0
- package/dist/types/converter/cosmetic/header-removal.d.ts +37 -0
- package/dist/types/converter/cosmetic/html.d.ts +54 -0
- package/dist/types/converter/cosmetic/index.d.ts +33 -0
- package/dist/types/converter/cosmetic/rule-modifiers/adg.d.ts +21 -0
- package/dist/types/converter/cosmetic/scriptlet.d.ts +33 -0
- package/dist/types/converter/css/index.d.ts +40 -0
- package/dist/types/converter/data/css.d.ts +48 -0
- package/dist/types/converter/filter-list.d.ts +31 -0
- package/dist/types/converter/index.d.ts +5 -0
- package/dist/types/converter/misc/network-rule-modifier.d.ts +35 -0
- package/dist/types/converter/network/index.d.ts +33 -0
- package/dist/types/converter/raw-filter-list.d.ts +31 -0
- package/dist/types/converter/raw-rule.d.ts +28 -0
- package/dist/types/converter/rule.d.ts +37 -0
- package/dist/types/deserializer/base-deserializer.d.ts +15 -0
- package/dist/types/deserializer/comment/agent-comment-deserializer.d.ts +34 -0
- package/dist/types/deserializer/comment/agent-deserializer.d.ts +25 -0
- package/dist/types/deserializer/comment/comment-rule-deserializer.d.ts +16 -0
- package/dist/types/deserializer/comment/config-comment-deserializer.d.ts +27 -0
- package/dist/types/deserializer/comment/hint-comment-deserializer.d.ts +24 -0
- package/dist/types/deserializer/comment/hint-deserializer.d.ts +26 -0
- package/dist/types/deserializer/comment/metadata-comment-deserializer.d.ts +26 -0
- package/dist/types/deserializer/comment/pre-processor-comment-deserializer.d.ts +29 -0
- package/dist/types/deserializer/comment/simple-comment-deserializer.d.ts +26 -0
- package/dist/types/deserializer/cosmetic/cosmetic-rule-deserializer.d.ts +18 -0
- package/dist/types/deserializer/cosmetic/css-injection-body-deserializer.d.ts +15 -0
- package/dist/types/deserializer/cosmetic/element-hiding-body-deserializer.d.ts +16 -0
- package/dist/types/deserializer/cosmetic/scriptlet-body/abp-snippet-injection-body-deserializer.d.ts +17 -0
- package/dist/types/deserializer/cosmetic/scriptlet-body/adg-scriptlet-injection-body-deserializer.d.ts +18 -0
- package/dist/types/deserializer/cosmetic/scriptlet-body/scriptlet-body-deserializer.d.ts +19 -0
- package/dist/types/deserializer/cosmetic/scriptlet-body/ubo-scriptlet-injection-body-deserializer.d.ts +17 -0
- package/dist/types/deserializer/empty-rule-deserializer.d.ts +16 -0
- package/dist/types/deserializer/filterlist-deserializer.d.ts +34 -0
- package/dist/types/deserializer/index.d.ts +2 -0
- package/dist/types/deserializer/invalid-rule-deserializer.d.ts +16 -0
- package/dist/types/deserializer/invalid-rule-error-node-deserializer.d.ts +16 -0
- package/dist/types/deserializer/misc/domain-list-deserializer.d.ts +15 -0
- package/dist/types/deserializer/misc/list-item-deserializer.d.ts +19 -0
- package/dist/types/deserializer/misc/list-items-deserializer.d.ts +16 -0
- package/dist/types/deserializer/misc/logical-expression-deserializer.d.ts +55 -0
- package/dist/types/deserializer/misc/modifier-deserializer.d.ts +18 -0
- package/dist/types/deserializer/misc/modifier-list-deserializer.d.ts +20 -0
- package/dist/types/deserializer/misc/parameter-list-deserializer.d.ts +18 -0
- package/dist/types/deserializer/misc/value-deserializer.d.ts +17 -0
- package/dist/types/deserializer/network/host-rule-deserializer.d.ts +18 -0
- package/dist/types/deserializer/network/hostname-list-deserializer.d.ts +15 -0
- package/dist/types/deserializer/network/network-rule-deserializer.d.ts +18 -0
- package/dist/types/deserializer/rule-deserializer.d.ts +17 -0
- package/dist/types/deserializer/syntax-deserialization-map.d.ts +2 -0
- package/dist/types/errors/adblock-syntax-error.d.ts +25 -0
- package/dist/types/errors/binary-schema-mismatch-error.d.ts +23 -0
- package/dist/types/errors/not-implemented-error.d.ts +14 -0
- package/dist/types/errors/rule-conversion-error.d.ts +14 -0
- package/dist/types/generator/base-generator.d.ts +12 -0
- package/dist/types/generator/comment/agent-comment-generator.d.ts +14 -0
- package/dist/types/generator/comment/agent-generator.d.ts +15 -0
- package/dist/types/generator/comment/comment-rule-generator.d.ts +14 -0
- package/dist/types/generator/comment/config-comment-generator.d.ts +14 -0
- package/dist/types/generator/comment/hint-comment-generator.d.ts +14 -0
- package/dist/types/generator/comment/hint-generator.d.ts +14 -0
- package/dist/types/generator/comment/index.d.ts +1 -0
- package/dist/types/generator/comment/metadata-comment-generator.d.ts +14 -0
- package/dist/types/generator/comment/pre-processor-comment-generator.d.ts +14 -0
- package/dist/types/generator/comment/simple-comment-generator.d.ts +14 -0
- package/dist/types/generator/cosmetic/body/abp-snippet-injection-body-generator.d.ts +15 -0
- package/dist/types/generator/cosmetic/body/adg-scriptlet-injection-body-generator.d.ts +21 -0
- package/dist/types/generator/cosmetic/body/ubo-scriptlet-injection-body-generator.d.ts +21 -0
- package/dist/types/generator/cosmetic/cosmetic-rule-body-generator.d.ts +20 -0
- package/dist/types/generator/cosmetic/cosmetic-rule-generator.d.ts +28 -0
- package/dist/types/generator/cosmetic/cosmetic-rule-pattern-generator.d.ts +18 -0
- package/dist/types/generator/cosmetic/index.d.ts +1 -0
- package/dist/types/generator/css/adg-css-injection-generator.d.ts +15 -0
- package/dist/types/generator/css/ubo-selector-generator.d.ts +14 -0
- package/dist/types/generator/filterlist-generator.d.ts +18 -0
- package/dist/types/generator/index.d.ts +3 -0
- package/dist/types/generator/misc/domain-list-generator.d.ts +15 -0
- package/dist/types/generator/misc/list-items-generator.d.ts +25 -0
- package/dist/types/generator/misc/logical-expression-generator.d.ts +14 -0
- package/dist/types/generator/misc/modifier-generator.d.ts +14 -0
- package/dist/types/generator/misc/modifier-list-generator.d.ts +14 -0
- package/dist/types/generator/misc/parameter-list-generator.d.ts +15 -0
- package/dist/types/generator/misc/value-generator.d.ts +14 -0
- package/dist/types/generator/network/host-rule-generator.d.ts +14 -0
- package/dist/types/generator/network/index.d.ts +2 -0
- package/dist/types/generator/network/network-rule-generator.d.ts +14 -0
- package/dist/types/generator/rule-generator.d.ts +24 -0
- package/dist/types/index.d.ts +55 -0
- package/dist/types/marshalling-utils/comment/agent-comment-common.d.ts +14 -0
- package/dist/types/marshalling-utils/comment/agent-common.d.ts +20 -0
- package/dist/types/marshalling-utils/comment/config-comment-common.d.ts +42 -0
- package/dist/types/marshalling-utils/comment/hint-comment-common.d.ts +15 -0
- package/dist/types/marshalling-utils/comment/hint-common.d.ts +33 -0
- package/dist/types/marshalling-utils/comment/metadata-comment-common.d.ts +21 -0
- package/dist/types/marshalling-utils/comment/pre-processor-comment-common.d.ts +37 -0
- package/dist/types/marshalling-utils/comment/simple-comment-common.d.ts +15 -0
- package/dist/types/marshalling-utils/cosmetic/body/abp-snippet-injection-body-common.d.ts +23 -0
- package/dist/types/marshalling-utils/cosmetic/body/adg-scriptlet-injection-body-common.d.ts +9 -0
- package/dist/types/marshalling-utils/cosmetic/body/css-injection-body-common.d.ts +17 -0
- package/dist/types/marshalling-utils/cosmetic/body/element-hiding-body-common.d.ts +14 -0
- package/dist/types/marshalling-utils/cosmetic/body/ubo-scriptlet-injection-body-common.d.ts +9 -0
- package/dist/types/marshalling-utils/cosmetic/cosmetic-rule-common.d.ts +28 -0
- package/dist/types/marshalling-utils/empty-rule-common.d.ts +13 -0
- package/dist/types/marshalling-utils/filter-list-common.d.ts +14 -0
- package/dist/types/marshalling-utils/invalid-rule-common.d.ts +14 -0
- package/dist/types/marshalling-utils/invalid-rule-error-node-common.d.ts +15 -0
- package/dist/types/marshalling-utils/misc/binary-type-common.d.ts +57 -0
- package/dist/types/marshalling-utils/misc/domain-list-common.d.ts +24 -0
- package/dist/types/marshalling-utils/misc/host-rule-common.d.ts +18 -0
- package/dist/types/marshalling-utils/misc/hostname-list-common.d.ts +14 -0
- package/dist/types/marshalling-utils/misc/list-item-common.d.ts +15 -0
- package/dist/types/marshalling-utils/misc/logical-expression-common.d.ts +47 -0
- package/dist/types/marshalling-utils/misc/modifier-common.d.ts +41 -0
- package/dist/types/marshalling-utils/misc/modifier-list-common.d.ts +14 -0
- package/dist/types/marshalling-utils/misc/parameter-list-common.d.ts +14 -0
- package/dist/types/marshalling-utils/misc/value-common.d.ts +15 -0
- package/dist/types/marshalling-utils/network/network-rule-common.d.ts +18 -0
- package/dist/types/marshalling-utils/syntax-serialization-map.d.ts +2 -0
- package/dist/types/nodes/index.d.ts +1183 -0
- package/dist/types/parser/base-parser.d.ts +16 -0
- package/dist/types/parser/comment/agent-comment-parser.d.ts +42 -0
- package/dist/types/parser/comment/agent-parser.d.ts +33 -0
- package/dist/types/parser/comment/comment-parser.d.ts +76 -0
- package/dist/types/parser/comment/config-comment-parser.d.ts +31 -0
- package/dist/types/parser/comment/hint-comment-parser.d.ts +33 -0
- package/dist/types/parser/comment/hint-parser.d.ts +30 -0
- package/dist/types/parser/comment/metadata-comment-parser.d.ts +29 -0
- package/dist/types/parser/comment/preprocessor-parser.d.ts +43 -0
- package/dist/types/parser/comment/simple-comment-parser.d.ts +33 -0
- package/dist/types/parser/cosmetic/body/abp-snippet-injection-body-parser.d.ts +34 -0
- package/dist/types/parser/cosmetic/body/adg-scriptlet-injection-body-parser.d.ts +47 -0
- package/dist/types/parser/cosmetic/body/ubo-scriptlet-injection-body-parser.d.ts +44 -0
- package/dist/types/parser/cosmetic/cosmetic-rule-parser.d.ts +51 -0
- package/dist/types/parser/css/adg-css-injection-parser.d.ts +28 -0
- package/dist/types/parser/css/balancing.d.ts +36 -0
- package/dist/types/parser/css/constants.d.ts +10 -0
- package/dist/types/parser/css/css-token-stream.d.ts +210 -0
- package/dist/types/parser/css/ubo-selector-parser.d.ts +46 -0
- package/dist/types/parser/filterlist-parser.d.ts +29 -0
- package/dist/types/parser/index.d.ts +6 -0
- package/dist/types/parser/misc/app-list-parser.d.ts +22 -0
- package/dist/types/parser/misc/domain-list-parser.d.ts +26 -0
- package/dist/types/parser/misc/list-items-parser.d.ts +37 -0
- package/dist/types/parser/misc/logical-expression-parser.d.ts +42 -0
- package/dist/types/parser/misc/method-list-parser.d.ts +22 -0
- package/dist/types/parser/misc/modifier-list.d.ts +24 -0
- package/dist/types/parser/misc/modifier-parser.d.ts +21 -0
- package/dist/types/parser/misc/parameter-list-parser.d.ts +17 -0
- package/dist/types/parser/misc/stealth-option-list-parser.d.ts +22 -0
- package/dist/types/parser/misc/ubo-parameter-list-parser.d.ts +21 -0
- package/dist/types/parser/misc/value-parser.d.ts +18 -0
- package/dist/types/parser/network/host-rule-parser.d.ts +35 -0
- package/dist/types/parser/network/network-rule-parser.d.ts +31 -0
- package/dist/types/parser/options.d.ts +41 -0
- package/dist/types/parser/rule-parser.d.ts +78 -0
- package/dist/types/serializer/base-serializer.d.ts +15 -0
- package/dist/types/serializer/comment/agent-comment-serializer.d.ts +34 -0
- package/dist/types/serializer/comment/agent-serializer.d.ts +15 -0
- package/dist/types/serializer/comment/comment-rule-serializer.d.ts +68 -0
- package/dist/types/serializer/comment/config-comment-serializer.d.ts +25 -0
- package/dist/types/serializer/comment/hint-comment-serializer.d.ts +23 -0
- package/dist/types/serializer/comment/hint-serializer.d.ts +24 -0
- package/dist/types/serializer/comment/metadata-comment-serializer.d.ts +25 -0
- package/dist/types/serializer/comment/pre-processor-comment-serializer.d.ts +28 -0
- package/dist/types/serializer/comment/simple-comment-serializer.d.ts +24 -0
- package/dist/types/serializer/cosmetic/body/abp-snippet-injection-body-serializer.d.ts +25 -0
- package/dist/types/serializer/cosmetic/body/adg-scriptlet-injection-body-serializer.d.ts +25 -0
- package/dist/types/serializer/cosmetic/body/scriptlet-body-serializer.d.ts +17 -0
- package/dist/types/serializer/cosmetic/body/ubo-scriptlet-injection-body-serializer.d.ts +25 -0
- package/dist/types/serializer/cosmetic/cosmetic-rule-serializer.d.ts +19 -0
- package/dist/types/serializer/cosmetic/css-injection-body-serializer.d.ts +15 -0
- package/dist/types/serializer/cosmetic/element-hiding-body-serializer.d.ts +15 -0
- package/dist/types/serializer/empty-rule-serializer.d.ts +15 -0
- package/dist/types/serializer/filterlist-serializer.d.ts +15 -0
- package/dist/types/serializer/index.d.ts +1 -0
- package/dist/types/serializer/invalid-rule-error-node-serializer.d.ts +15 -0
- package/dist/types/serializer/invalid-rule-serializer.d.ts +15 -0
- package/dist/types/serializer/misc/domain-list-serializer.d.ts +21 -0
- package/dist/types/serializer/misc/list-item-serializer.d.ts +16 -0
- package/dist/types/serializer/misc/list-items-serializer.d.ts +15 -0
- package/dist/types/serializer/misc/logical-expression-serializer.d.ts +52 -0
- package/dist/types/serializer/misc/modifier-list-serializer.d.ts +20 -0
- package/dist/types/serializer/misc/modifier-serializer.d.ts +18 -0
- package/dist/types/serializer/misc/parameter-list-serializer.d.ts +17 -0
- package/dist/types/serializer/misc/value-serializer.d.ts +17 -0
- package/dist/types/serializer/network/host-rule-serializer.d.ts +30 -0
- package/dist/types/serializer/network/hostname-list-serializer.d.ts +15 -0
- package/dist/types/serializer/network/network-rule-serializer.d.ts +18 -0
- package/dist/types/serializer/rule-serializer.d.ts +17 -0
- package/dist/types/utils/adblockers.d.ts +49 -0
- package/dist/types/utils/binary-schema-version.d.ts +9 -0
- package/dist/types/utils/byte-buffer.d.ts +54 -0
- package/dist/types/utils/categorizer.d.ts +14 -0
- package/dist/types/utils/clone.d.ts +15 -0
- package/dist/types/utils/constants.d.ts +188 -0
- package/dist/types/utils/cosmetic-rule-separator.d.ts +45 -0
- package/dist/types/utils/deep-freeze.d.ts +13 -0
- package/dist/types/utils/domain.d.ts +29 -0
- package/dist/types/utils/error.d.ts +8 -0
- package/dist/types/utils/index.d.ts +15 -0
- package/dist/types/utils/input-byte-buffer.d.ts +146 -0
- package/dist/types/utils/is-chromium.d.ts +7 -0
- package/dist/types/utils/logical-expression.d.ts +45 -0
- package/dist/types/utils/multi-value-map.d.ts +16 -0
- package/dist/types/utils/output-byte-buffer.d.ts +132 -0
- package/dist/types/utils/position-provider.d.ts +46 -0
- package/dist/types/utils/quotes.d.ts +94 -0
- package/dist/types/utils/regexp.d.ts +54 -0
- package/dist/types/utils/storage-interface.d.ts +23 -0
- package/dist/types/utils/string.d.ts +280 -0
- package/dist/types/utils/text-decoder-polyfill.d.ts +14 -0
- package/dist/types/utils/text-encoder-polyfill.d.ts +18 -0
- package/dist/types/utils/type-guards.d.ts +47 -0
- package/dist/types/validator/constants.d.ts +89 -0
- package/dist/types/validator/helpers.d.ts +37 -0
- package/dist/types/validator/index.d.ts +38 -0
- package/dist/types/validator/value.d.ts +12 -0
- package/dist/types/version.d.ts +5 -0
- package/dist/utils/adblockers.js +56 -0
- package/dist/utils/binary-schema-version.js +17 -0
- package/dist/utils/byte-buffer.js +91 -0
- package/dist/utils/categorizer.js +44 -0
- package/dist/utils/clone.js +28 -0
- package/dist/utils/constants.js +249 -0
- package/dist/utils/cosmetic-rule-separator.js +127 -0
- package/dist/utils/deep-freeze.js +32 -0
- package/dist/utils/domain.js +59 -0
- package/dist/utils/error.js +49 -0
- package/dist/utils/index.js +21 -0
- package/dist/utils/input-byte-buffer.js +265 -0
- package/dist/utils/is-chromium.js +20 -0
- package/dist/utils/logical-expression.js +92 -0
- package/dist/utils/multi-value-map.js +33 -0
- package/dist/utils/output-byte-buffer.js +231 -0
- package/dist/utils/position-provider.js +73 -0
- package/dist/utils/quotes.js +214 -0
- package/dist/utils/regexp.js +176 -0
- package/dist/utils/string.js +559 -0
- package/dist/utils/text-decoder-polyfill.js +107 -0
- package/dist/utils/text-encoder-polyfill.js +78 -0
- package/dist/utils/type-guards.js +47 -0
- package/dist/validator/constants.js +210 -0
- package/dist/validator/helpers.js +45 -0
- package/dist/validator/index.js +174 -0
- package/dist/validator/value.js +568 -0
- package/dist/version.js +18 -0
- package/package.json +61 -39
- package/dist/agtree.d.ts +0 -4155
- package/dist/agtree.js +0 -15819
- package/dist/agtree.mjs +0 -15720
- package/dist/compatibility-table-data.js +0 -12782
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* AGTree v3.0.0-alpha.2 (build date: Tue, 17 Dec 2024 16:50:04 GMT)
|
|
3
|
+
* (c) 2024 Adguard Software Ltd.
|
|
4
|
+
* Released under the MIT license
|
|
5
|
+
* https://github.com/AdguardTeam/tsurlfilter/tree/master/packages/agtree#readme
|
|
6
|
+
*/
|
|
7
|
+
import { HINT_MARKER, OPEN_PARENTHESIS, BACKSLASH, CLOSE_PARENTHESIS, HINT_MARKER_LEN } from '../../utils/constants.js';
|
|
8
|
+
import { StringUtils } from '../../utils/string.js';
|
|
9
|
+
import { CommentRuleType, RuleCategory } from '../../nodes/index.js';
|
|
10
|
+
import { HintParser } from './hint-parser.js';
|
|
11
|
+
import { AdblockSyntax } from '../../utils/adblockers.js';
|
|
12
|
+
import { AdblockSyntaxError } from '../../errors/adblock-syntax-error.js';
|
|
13
|
+
import { defaultParserOptions } from '../options.js';
|
|
14
|
+
import { BaseParser } from '../base-parser.js';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* `HintRuleParser` is responsible for parsing AdGuard hint rules.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* The following hint rule
|
|
21
|
+
* ```adblock
|
|
22
|
+
* !+ NOT_OPTIMIZED PLATFORM(windows)
|
|
23
|
+
* ```
|
|
24
|
+
* contains two hints: `NOT_OPTIMIZED` and `PLATFORM`.
|
|
25
|
+
* @see {@link https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#hints}
|
|
26
|
+
*/
|
|
27
|
+
class HintCommentParser extends BaseParser {
|
|
28
|
+
/**
|
|
29
|
+
* Checks if the raw rule is a hint rule.
|
|
30
|
+
*
|
|
31
|
+
* @param raw Raw rule
|
|
32
|
+
* @returns `true` if the rule is a hint rule, `false` otherwise
|
|
33
|
+
*/
|
|
34
|
+
static isHintRule(raw) {
|
|
35
|
+
return raw.trim().startsWith(HINT_MARKER);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Parses a raw rule as a hint comment.
|
|
39
|
+
*
|
|
40
|
+
* @param raw Raw input to parse.
|
|
41
|
+
* @param options Global parser options.
|
|
42
|
+
* @param baseOffset Starting offset of the input. Node locations are calculated relative to this offset.
|
|
43
|
+
* @returns Hint AST or null (if the raw rule cannot be parsed as a hint comment)
|
|
44
|
+
* @throws If the input matches the HINT pattern but syntactically invalid
|
|
45
|
+
* @see {@link https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#hints-1}
|
|
46
|
+
*/
|
|
47
|
+
static parse(raw, options = defaultParserOptions, baseOffset = 0) {
|
|
48
|
+
// Ignore non-hint rules
|
|
49
|
+
if (!HintCommentParser.isHintRule(raw)) {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
let offset = 0;
|
|
53
|
+
// Skip whitespace characters before the rule
|
|
54
|
+
offset = StringUtils.skipWS(raw);
|
|
55
|
+
// Skip hint marker
|
|
56
|
+
offset += HINT_MARKER_LEN;
|
|
57
|
+
const hints = [];
|
|
58
|
+
// Collect hints. Each hint is a string, optionally followed by a parameter list,
|
|
59
|
+
// enclosed in parentheses. One rule can contain multiple hints.
|
|
60
|
+
while (offset < raw.length) {
|
|
61
|
+
// Split rule into raw hints (e.g. 'HINT_NAME' or 'HINT_NAME(PARAMS)')
|
|
62
|
+
// Hints are separated by whitespace characters, but we should ignore
|
|
63
|
+
// whitespace characters inside the parameter list
|
|
64
|
+
// Ignore whitespace characters before the hint
|
|
65
|
+
offset = StringUtils.skipWS(raw, offset);
|
|
66
|
+
// Save the start index of the hint
|
|
67
|
+
const hintStartIndex = offset;
|
|
68
|
+
// Find the end of the hint
|
|
69
|
+
let hintEndIndex = offset;
|
|
70
|
+
let balance = 0;
|
|
71
|
+
while (hintEndIndex < raw.length) {
|
|
72
|
+
if (raw[hintEndIndex] === OPEN_PARENTHESIS && raw[hintEndIndex - 1] !== BACKSLASH) {
|
|
73
|
+
balance += 1;
|
|
74
|
+
// Throw error for nesting
|
|
75
|
+
if (balance > 1) {
|
|
76
|
+
throw new AdblockSyntaxError('Invalid hint: nested parentheses are not allowed', baseOffset + hintStartIndex, baseOffset + hintEndIndex);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
else if (raw[hintEndIndex] === CLOSE_PARENTHESIS && raw[hintEndIndex - 1] !== BACKSLASH) {
|
|
80
|
+
balance -= 1;
|
|
81
|
+
}
|
|
82
|
+
else if (StringUtils.isWhitespace(raw[hintEndIndex]) && balance === 0) {
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
hintEndIndex += 1;
|
|
86
|
+
}
|
|
87
|
+
offset = hintEndIndex;
|
|
88
|
+
// Skip whitespace characters after the hint
|
|
89
|
+
offset = StringUtils.skipWS(raw, offset);
|
|
90
|
+
// Parse the hint
|
|
91
|
+
const hint = HintParser.parse(raw.slice(hintStartIndex, hintEndIndex), options, baseOffset + hintStartIndex);
|
|
92
|
+
hints.push(hint);
|
|
93
|
+
}
|
|
94
|
+
// Throw error if no hints were found
|
|
95
|
+
if (hints.length === 0) {
|
|
96
|
+
throw new AdblockSyntaxError('Empty hint rule', baseOffset, baseOffset + offset);
|
|
97
|
+
}
|
|
98
|
+
const result = {
|
|
99
|
+
type: CommentRuleType.HintCommentRule,
|
|
100
|
+
category: RuleCategory.Comment,
|
|
101
|
+
syntax: AdblockSyntax.Adg,
|
|
102
|
+
children: hints,
|
|
103
|
+
};
|
|
104
|
+
if (options.includeRaws) {
|
|
105
|
+
result.raws = {
|
|
106
|
+
text: raw,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
if (options.isLocIncluded) {
|
|
110
|
+
result.start = baseOffset;
|
|
111
|
+
result.end = baseOffset + offset;
|
|
112
|
+
}
|
|
113
|
+
return result;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export { HintCommentParser };
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* AGTree v3.0.0-alpha.2 (build date: Tue, 17 Dec 2024 16:50:04 GMT)
|
|
3
|
+
* (c) 2024 Adguard Software Ltd.
|
|
4
|
+
* Released under the MIT license
|
|
5
|
+
* https://github.com/AdguardTeam/tsurlfilter/tree/master/packages/agtree#readme
|
|
6
|
+
*/
|
|
7
|
+
import { OPEN_PARENTHESIS, SPACE, UNDERSCORE, EMPTY, CLOSE_PARENTHESIS, COMMA } from '../../utils/constants.js';
|
|
8
|
+
import { StringUtils } from '../../utils/string.js';
|
|
9
|
+
import { AdblockSyntaxError } from '../../errors/adblock-syntax-error.js';
|
|
10
|
+
import { ParameterListParser } from '../misc/parameter-list-parser.js';
|
|
11
|
+
import { defaultParserOptions } from '../options.js';
|
|
12
|
+
import { BaseParser } from '../base-parser.js';
|
|
13
|
+
import { ValueParser } from '../misc/value-parser.js';
|
|
14
|
+
|
|
15
|
+
/* eslint-disable no-param-reassign */
|
|
16
|
+
/**
|
|
17
|
+
* @file AdGuard Hints
|
|
18
|
+
* @see {@link https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#hints}
|
|
19
|
+
*/
|
|
20
|
+
/**
|
|
21
|
+
* `HintParser` is responsible for parsing AdGuard hints.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* If the hint rule is
|
|
25
|
+
* ```adblock
|
|
26
|
+
* !+ NOT_OPTIMIZED PLATFORM(windows)
|
|
27
|
+
* ```
|
|
28
|
+
* then the hints are `NOT_OPTIMIZED` and `PLATFORM(windows)`, and this
|
|
29
|
+
* class is responsible for parsing them. The rule itself is parsed by
|
|
30
|
+
* the `HintRuleParser`, which uses this class to parse single hints.
|
|
31
|
+
*/
|
|
32
|
+
class HintParser extends BaseParser {
|
|
33
|
+
/**
|
|
34
|
+
* Parses a raw rule as a hint.
|
|
35
|
+
*
|
|
36
|
+
* @param raw Raw input to parse.
|
|
37
|
+
* @param options Global parser options.
|
|
38
|
+
* @param baseOffset Starting offset of the input. Node locations are calculated relative to this offset.
|
|
39
|
+
* @returns Hint rule AST or null
|
|
40
|
+
* @throws If the syntax is invalid
|
|
41
|
+
*/
|
|
42
|
+
static parse(raw, options = defaultParserOptions, baseOffset = 0) {
|
|
43
|
+
let offset = 0;
|
|
44
|
+
// Skip whitespace characters before the hint
|
|
45
|
+
offset = StringUtils.skipWS(raw);
|
|
46
|
+
// Hint should start with the hint name in every case
|
|
47
|
+
// Save the start offset of the hint name
|
|
48
|
+
const nameStartIndex = offset;
|
|
49
|
+
// Parse the hint name
|
|
50
|
+
for (; offset < raw.length; offset += 1) {
|
|
51
|
+
const char = raw[offset];
|
|
52
|
+
// Abort consuming the hint name if we encounter a whitespace character
|
|
53
|
+
// or an opening parenthesis, which means 'HIT_NAME(' case
|
|
54
|
+
if (char === OPEN_PARENTHESIS || char === SPACE) {
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
// Hint name should only contain letters, digits, and underscores
|
|
58
|
+
if (!StringUtils.isAlphaNumeric(char) && char !== UNDERSCORE) {
|
|
59
|
+
throw new AdblockSyntaxError(`Invalid character "${char}" in hint name: "${char}"`, baseOffset + nameStartIndex, baseOffset + offset);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
// Save the end offset of the hint name
|
|
63
|
+
const nameEndIndex = offset;
|
|
64
|
+
// Save the hint name token
|
|
65
|
+
const name = raw.slice(nameStartIndex, nameEndIndex);
|
|
66
|
+
// Hint name cannot be empty
|
|
67
|
+
if (name === EMPTY) {
|
|
68
|
+
throw new AdblockSyntaxError('Empty hint name', baseOffset, baseOffset + nameEndIndex);
|
|
69
|
+
}
|
|
70
|
+
// Now we have two case:
|
|
71
|
+
// 1. We have HINT_NAME and should return it
|
|
72
|
+
// 2. We have HINT_NAME(PARAMS) and should continue parsing
|
|
73
|
+
// Skip whitespace characters after the hint name
|
|
74
|
+
offset = StringUtils.skipWS(raw, offset);
|
|
75
|
+
// Throw error for 'HINT_NAME (' case
|
|
76
|
+
if (offset > nameEndIndex && raw[offset] === OPEN_PARENTHESIS) {
|
|
77
|
+
throw new AdblockSyntaxError('Unexpected whitespace(s) between hint name and opening parenthesis', baseOffset + nameEndIndex, baseOffset + offset);
|
|
78
|
+
}
|
|
79
|
+
// Create the hint name node (we can reuse it in the 'HINT_NAME' case, if needed)
|
|
80
|
+
const nameNode = ValueParser.parse(name, options, baseOffset + nameStartIndex);
|
|
81
|
+
// Just return the hint name if we have 'HINT_NAME' case (no params)
|
|
82
|
+
if (raw[offset] !== OPEN_PARENTHESIS) {
|
|
83
|
+
const result = {
|
|
84
|
+
type: 'Hint',
|
|
85
|
+
name: nameNode,
|
|
86
|
+
};
|
|
87
|
+
if (options.isLocIncluded) {
|
|
88
|
+
result.start = baseOffset;
|
|
89
|
+
result.end = baseOffset + offset;
|
|
90
|
+
}
|
|
91
|
+
return result;
|
|
92
|
+
}
|
|
93
|
+
// Skip the opening parenthesis
|
|
94
|
+
offset += 1;
|
|
95
|
+
// Find closing parenthesis
|
|
96
|
+
const closeParenthesisIndex = raw.lastIndexOf(CLOSE_PARENTHESIS);
|
|
97
|
+
// Throw error if we don't have closing parenthesis
|
|
98
|
+
if (closeParenthesisIndex === -1) {
|
|
99
|
+
throw new AdblockSyntaxError(`Missing closing parenthesis for hint "${name}"`, baseOffset + nameStartIndex, baseOffset + raw.length);
|
|
100
|
+
}
|
|
101
|
+
// Save the start and end index of the params
|
|
102
|
+
const paramsStartIndex = offset;
|
|
103
|
+
const paramsEndIndex = closeParenthesisIndex;
|
|
104
|
+
// Parse the params
|
|
105
|
+
const params = ParameterListParser.parse(raw.slice(paramsStartIndex, paramsEndIndex), options, baseOffset + paramsStartIndex, COMMA);
|
|
106
|
+
offset = closeParenthesisIndex + 1;
|
|
107
|
+
// Skip whitespace characters after the closing parenthesis
|
|
108
|
+
offset = StringUtils.skipWS(raw, offset);
|
|
109
|
+
// Throw error if we don't reach the end of the input
|
|
110
|
+
if (offset !== raw.length) {
|
|
111
|
+
throw new AdblockSyntaxError(
|
|
112
|
+
// eslint-disable-next-line max-len
|
|
113
|
+
`Unexpected input after closing parenthesis for hint "${name}": "${raw.slice(closeParenthesisIndex + 1, offset + 1)}"`, baseOffset + closeParenthesisIndex + 1, baseOffset + offset + 1);
|
|
114
|
+
}
|
|
115
|
+
// Return the HINT_NAME(PARAMS) case AST
|
|
116
|
+
const result = {
|
|
117
|
+
type: 'Hint',
|
|
118
|
+
name: nameNode,
|
|
119
|
+
params,
|
|
120
|
+
};
|
|
121
|
+
if (options.isLocIncluded) {
|
|
122
|
+
result.start = baseOffset;
|
|
123
|
+
result.end = baseOffset + offset;
|
|
124
|
+
}
|
|
125
|
+
return result;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export { HintParser };
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* AGTree v3.0.0-alpha.2 (build date: Tue, 17 Dec 2024 16:50:04 GMT)
|
|
3
|
+
* (c) 2024 Adguard Software Ltd.
|
|
4
|
+
* Released under the MIT license
|
|
5
|
+
* https://github.com/AdguardTeam/tsurlfilter/tree/master/packages/agtree#readme
|
|
6
|
+
*/
|
|
7
|
+
import { StringUtils } from '../../utils/string.js';
|
|
8
|
+
import { AdblockSyntax } from '../../utils/adblockers.js';
|
|
9
|
+
import { COLON } from '../../utils/constants.js';
|
|
10
|
+
import { CommentMarker, CommentRuleType, RuleCategory } from '../../nodes/index.js';
|
|
11
|
+
import { defaultParserOptions } from '../options.js';
|
|
12
|
+
import { BaseParser } from '../base-parser.js';
|
|
13
|
+
import { ValueParser } from '../misc/value-parser.js';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @file Metadata comments
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* Set of known metadata headers. This helps to quickly identify and validate
|
|
20
|
+
* metadata headers in the comments.
|
|
21
|
+
*/
|
|
22
|
+
const KNOWN_METADATA_HEADERS = new Set([
|
|
23
|
+
'Checksum',
|
|
24
|
+
'Description',
|
|
25
|
+
'Expires',
|
|
26
|
+
'Homepage',
|
|
27
|
+
'Last Modified',
|
|
28
|
+
'LastModified',
|
|
29
|
+
'Licence',
|
|
30
|
+
'License',
|
|
31
|
+
'Time Updated',
|
|
32
|
+
'TimeUpdated',
|
|
33
|
+
'Version',
|
|
34
|
+
'Title',
|
|
35
|
+
]);
|
|
36
|
+
/**
|
|
37
|
+
* `MetadataParser` is responsible for parsing metadata comments.
|
|
38
|
+
* Metadata comments are special comments that specify some properties of the list.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* For example, in the case of
|
|
42
|
+
* ```adblock
|
|
43
|
+
* ! Title: My List
|
|
44
|
+
* ```
|
|
45
|
+
* the name of the header is `Title`, and the value is `My List`, which means that
|
|
46
|
+
* the list title is `My List`, and it can be used in the adblocker UI.
|
|
47
|
+
* @see {@link https://help.eyeo.com/adblockplus/how-to-write-filters#special-comments}
|
|
48
|
+
*/
|
|
49
|
+
class MetadataCommentParser extends BaseParser {
|
|
50
|
+
/**
|
|
51
|
+
* Parses a raw rule as a metadata comment.
|
|
52
|
+
*
|
|
53
|
+
* @param raw Raw input to parse.
|
|
54
|
+
* @param options Global parser options.
|
|
55
|
+
* @param baseOffset Starting offset of the input. Node locations are calculated relative to this offset.
|
|
56
|
+
* @returns Metadata comment AST or null (if the raw rule cannot be parsed as a metadata comment)
|
|
57
|
+
*/
|
|
58
|
+
static parse(raw, options = defaultParserOptions, baseOffset = 0) {
|
|
59
|
+
// Fast check to avoid unnecessary work
|
|
60
|
+
if (raw.indexOf(COLON) === -1) {
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
let offset = 0;
|
|
64
|
+
// Skip leading spaces before the comment marker
|
|
65
|
+
offset = StringUtils.skipWS(raw, offset);
|
|
66
|
+
// Check if the rule starts with a comment marker (first non-space sequence)
|
|
67
|
+
if (raw[offset] !== CommentMarker.Regular && raw[offset] !== CommentMarker.Hashmark) {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
// Consume the comment marker
|
|
71
|
+
const marker = ValueParser.parse(raw[offset], options, baseOffset + offset);
|
|
72
|
+
offset += 1;
|
|
73
|
+
// Skip spaces
|
|
74
|
+
offset = StringUtils.skipWS(raw, offset);
|
|
75
|
+
// Save header start position
|
|
76
|
+
const headerStart = offset;
|
|
77
|
+
// Check if the comment text starts with a known header
|
|
78
|
+
const text = raw.slice(offset);
|
|
79
|
+
for (const knownHeader of KNOWN_METADATA_HEADERS) {
|
|
80
|
+
// Check if the comment text starts with the header (case-insensitive)
|
|
81
|
+
if (text.toLocaleLowerCase().startsWith(knownHeader.toLocaleLowerCase())) {
|
|
82
|
+
// Skip the header
|
|
83
|
+
offset += knownHeader.length;
|
|
84
|
+
// Save header
|
|
85
|
+
const header = ValueParser.parse(raw.slice(headerStart, offset), options, baseOffset + headerStart);
|
|
86
|
+
// Skip spaces after the header
|
|
87
|
+
offset = StringUtils.skipWS(raw, offset);
|
|
88
|
+
// Check if the rule contains a separator after the header
|
|
89
|
+
if (raw[offset] !== COLON) {
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
// Skip the separator
|
|
93
|
+
offset += 1;
|
|
94
|
+
// Skip spaces after the separator
|
|
95
|
+
offset = StringUtils.skipWS(raw, offset);
|
|
96
|
+
// Save the value start position
|
|
97
|
+
const valueStart = offset;
|
|
98
|
+
// Check if the rule contains a value
|
|
99
|
+
if (offset >= raw.length) {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
const valueEnd = StringUtils.skipWSBack(raw, raw.length - 1) + 1;
|
|
103
|
+
// Save the value
|
|
104
|
+
const value = ValueParser.parse(raw.slice(valueStart, valueEnd), options, baseOffset + valueStart);
|
|
105
|
+
const result = {
|
|
106
|
+
type: CommentRuleType.MetadataCommentRule,
|
|
107
|
+
category: RuleCategory.Comment,
|
|
108
|
+
syntax: AdblockSyntax.Common,
|
|
109
|
+
marker,
|
|
110
|
+
header,
|
|
111
|
+
value,
|
|
112
|
+
};
|
|
113
|
+
if (options.includeRaws) {
|
|
114
|
+
result.raws = {
|
|
115
|
+
text: raw,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
if (options.isLocIncluded) {
|
|
119
|
+
result.start = baseOffset;
|
|
120
|
+
result.end = baseOffset + raw.length;
|
|
121
|
+
}
|
|
122
|
+
return result;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export { MetadataCommentParser };
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* AGTree v3.0.0-alpha.2 (build date: Tue, 17 Dec 2024 16:50:04 GMT)
|
|
3
|
+
* (c) 2024 Adguard Software Ltd.
|
|
4
|
+
* Released under the MIT license
|
|
5
|
+
* https://github.com/AdguardTeam/tsurlfilter/tree/master/packages/agtree#readme
|
|
6
|
+
*/
|
|
7
|
+
import { AdblockSyntax } from '../../utils/adblockers.js';
|
|
8
|
+
import { PREPROCESSOR_MARKER, PREPROCESSOR_MARKER_LEN, HASHMARK, PREPROCESSOR_SEPARATOR, OPEN_PARENTHESIS, SAFARI_CB_AFFINITY, CLOSE_PARENTHESIS, COMMA, IF, INCLUDE } from '../../utils/constants.js';
|
|
9
|
+
import { StringUtils } from '../../utils/string.js';
|
|
10
|
+
import { CommentRuleType, RuleCategory } from '../../nodes/index.js';
|
|
11
|
+
import { LogicalExpressionParser } from '../misc/logical-expression-parser.js';
|
|
12
|
+
import { AdblockSyntaxError } from '../../errors/adblock-syntax-error.js';
|
|
13
|
+
import { ParameterListParser } from '../misc/parameter-list-parser.js';
|
|
14
|
+
import { defaultParserOptions } from '../options.js';
|
|
15
|
+
import { BaseParser } from '../base-parser.js';
|
|
16
|
+
import { ValueParser } from '../misc/value-parser.js';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Pre-processor directives
|
|
20
|
+
*
|
|
21
|
+
* @see {@link https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#pre-processor-directives}
|
|
22
|
+
* @see {@link https://github.com/gorhill/uBlock/wiki/Static-filter-syntax#pre-parsing-directives}
|
|
23
|
+
*/
|
|
24
|
+
/**
|
|
25
|
+
* `PreProcessorParser` is responsible for parsing preprocessor rules.
|
|
26
|
+
* Pre-processor comments are special comments that are used to control the behavior of the filter list processor.
|
|
27
|
+
* Please note that this parser only handles general syntax for now, and does not validate the parameters at
|
|
28
|
+
* the parsing stage.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* If your rule is
|
|
32
|
+
* ```adblock
|
|
33
|
+
* !#if (adguard)
|
|
34
|
+
* ```
|
|
35
|
+
* then the directive's name is `if` and its value is `(adguard)`, but the parameter list
|
|
36
|
+
* is not parsed / validated further.
|
|
37
|
+
* @see {@link https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#pre-processor-directives}
|
|
38
|
+
* @see {@link https://github.com/gorhill/uBlock/wiki/Static-filter-syntax#pre-parsing-directives}
|
|
39
|
+
*/
|
|
40
|
+
class PreProcessorCommentParser extends BaseParser {
|
|
41
|
+
/**
|
|
42
|
+
* Determines whether the rule is a pre-processor rule.
|
|
43
|
+
*
|
|
44
|
+
* @param raw Raw rule
|
|
45
|
+
* @returns `true` if the rule is a pre-processor rule, `false` otherwise
|
|
46
|
+
*/
|
|
47
|
+
static isPreProcessorRule(raw) {
|
|
48
|
+
const trimmed = raw.trim();
|
|
49
|
+
// Avoid this case: !##... (commonly used in AdGuard filters)
|
|
50
|
+
return trimmed.startsWith(PREPROCESSOR_MARKER) && trimmed[PREPROCESSOR_MARKER_LEN] !== HASHMARK;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Parses a raw rule as a pre-processor comment.
|
|
54
|
+
*
|
|
55
|
+
* @param raw Raw input to parse.
|
|
56
|
+
* @param options Global parser options.
|
|
57
|
+
* @param baseOffset Starting offset of the input. Node locations are calculated relative to this offset.
|
|
58
|
+
* @returns
|
|
59
|
+
* Pre-processor comment AST or null (if the raw rule cannot be parsed as a pre-processor comment)
|
|
60
|
+
*/
|
|
61
|
+
static parse(raw, options = defaultParserOptions, baseOffset = 0) {
|
|
62
|
+
// Ignore non-pre-processor rules
|
|
63
|
+
if (!PreProcessorCommentParser.isPreProcessorRule(raw)) {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
let offset = 0;
|
|
67
|
+
// Ignore whitespace characters before the rule (if any)
|
|
68
|
+
offset = StringUtils.skipWS(raw, offset);
|
|
69
|
+
// Ignore the pre-processor marker
|
|
70
|
+
offset += PREPROCESSOR_MARKER_LEN;
|
|
71
|
+
// Ignore whitespace characters after the pre-processor marker (if any)
|
|
72
|
+
// Note: this is incorrect according to the spec, but we do it for tolerance
|
|
73
|
+
offset = StringUtils.skipWS(raw, offset);
|
|
74
|
+
// Directive name should start at this offset, so we save this offset now
|
|
75
|
+
const nameStart = offset;
|
|
76
|
+
// Consume directive name, so parse the sequence until the first
|
|
77
|
+
// whitespace / opening parenthesis / end of string
|
|
78
|
+
while (offset < raw.length) {
|
|
79
|
+
const ch = raw[offset];
|
|
80
|
+
if (ch === PREPROCESSOR_SEPARATOR || ch === OPEN_PARENTHESIS) {
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
offset += 1;
|
|
84
|
+
}
|
|
85
|
+
// Save name end offset
|
|
86
|
+
const nameEnd = offset;
|
|
87
|
+
// Create name node
|
|
88
|
+
const name = ValueParser.parse(raw.slice(nameStart, nameEnd), options, baseOffset + nameStart);
|
|
89
|
+
// Ignore whitespace characters after the directive name (if any)
|
|
90
|
+
// Note: this may incorrect according to the spec, but we do it for tolerance
|
|
91
|
+
offset = StringUtils.skipWS(raw, offset);
|
|
92
|
+
// If the directive name is "safari_cb_affinity", then we have a special case
|
|
93
|
+
if (name.value === SAFARI_CB_AFFINITY) {
|
|
94
|
+
// Throw error if there are spaces after the directive name
|
|
95
|
+
if (offset > nameEnd) {
|
|
96
|
+
throw new AdblockSyntaxError(`Unexpected whitespace after "${SAFARI_CB_AFFINITY}" directive name`, baseOffset + nameEnd, baseOffset + offset);
|
|
97
|
+
}
|
|
98
|
+
// safari_cb_affinity directive optionally accepts a parameter list
|
|
99
|
+
// So at this point we should check if there are parameters or not
|
|
100
|
+
// (cb_affinity directive followed by an opening parenthesis or if we
|
|
101
|
+
// skip the whitespace we reach the end of the string)
|
|
102
|
+
if (StringUtils.skipWS(raw, offset) !== raw.length) {
|
|
103
|
+
if (raw[offset] !== OPEN_PARENTHESIS) {
|
|
104
|
+
throw new AdblockSyntaxError(`Unexpected character '${raw[offset]}' after '${SAFARI_CB_AFFINITY}' directive name`, baseOffset + offset, baseOffset + offset + 1);
|
|
105
|
+
}
|
|
106
|
+
// If we have parameters, then we should parse them
|
|
107
|
+
// Note: we don't validate the parameters at this stage
|
|
108
|
+
// Ignore opening parenthesis
|
|
109
|
+
offset += 1;
|
|
110
|
+
// Save parameter list start offset
|
|
111
|
+
const parameterListStart = offset;
|
|
112
|
+
// Check for closing parenthesis
|
|
113
|
+
const closingParenthesesIndex = StringUtils.skipWSBack(raw);
|
|
114
|
+
if (closingParenthesesIndex === -1 || raw[closingParenthesesIndex] !== CLOSE_PARENTHESIS) {
|
|
115
|
+
throw new AdblockSyntaxError(`Missing closing parenthesis for '${SAFARI_CB_AFFINITY}' directive`, baseOffset + offset, baseOffset + raw.length);
|
|
116
|
+
}
|
|
117
|
+
// Save parameter list end offset
|
|
118
|
+
const parameterListEnd = closingParenthesesIndex;
|
|
119
|
+
// Parse parameters between the opening and closing parentheses
|
|
120
|
+
const result = {
|
|
121
|
+
type: CommentRuleType.PreProcessorCommentRule,
|
|
122
|
+
category: RuleCategory.Comment,
|
|
123
|
+
syntax: AdblockSyntax.Adg,
|
|
124
|
+
name,
|
|
125
|
+
// comma separated list of parameters
|
|
126
|
+
params: ParameterListParser.parse(raw.slice(parameterListStart, parameterListEnd), options, baseOffset + parameterListStart, COMMA),
|
|
127
|
+
};
|
|
128
|
+
if (options.includeRaws) {
|
|
129
|
+
result.raws = {
|
|
130
|
+
text: raw,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
if (options.isLocIncluded) {
|
|
134
|
+
result.start = baseOffset;
|
|
135
|
+
result.end = baseOffset + raw.length;
|
|
136
|
+
}
|
|
137
|
+
return result;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
// If we reached the end of the string, then we have a directive without parameters
|
|
141
|
+
// (e.g. "!#safari_cb_affinity" or "!#endif")
|
|
142
|
+
// No need to continue parsing in this case.
|
|
143
|
+
if (offset === raw.length) {
|
|
144
|
+
// Throw error if the directive name is "if" or "include", because these directives
|
|
145
|
+
// should have parameters
|
|
146
|
+
if (name.value === IF || name.value === INCLUDE) {
|
|
147
|
+
throw new AdblockSyntaxError(`Directive "${name.value}" requires parameters`, baseOffset, baseOffset + raw.length);
|
|
148
|
+
}
|
|
149
|
+
const result = {
|
|
150
|
+
type: CommentRuleType.PreProcessorCommentRule,
|
|
151
|
+
category: RuleCategory.Comment,
|
|
152
|
+
syntax: AdblockSyntax.Common,
|
|
153
|
+
name,
|
|
154
|
+
};
|
|
155
|
+
if (options.includeRaws) {
|
|
156
|
+
result.raws = {
|
|
157
|
+
text: raw,
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
if (options.isLocIncluded) {
|
|
161
|
+
result.start = baseOffset;
|
|
162
|
+
result.end = baseOffset + raw.length;
|
|
163
|
+
}
|
|
164
|
+
return result;
|
|
165
|
+
}
|
|
166
|
+
// Get start and end offsets of the directive parameters
|
|
167
|
+
const paramsStart = offset;
|
|
168
|
+
const paramsEnd = StringUtils.skipWSBack(raw) + 1;
|
|
169
|
+
// Prepare parameters node
|
|
170
|
+
let params;
|
|
171
|
+
// Parse parameters. Handle "if" and "safari_cb_affinity" directives
|
|
172
|
+
// separately.
|
|
173
|
+
if (name.value === IF) {
|
|
174
|
+
params = LogicalExpressionParser.parse(raw.slice(paramsStart, paramsEnd), options, baseOffset + paramsStart);
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
params = ValueParser.parse(raw.slice(paramsStart, paramsEnd), options, baseOffset + paramsStart);
|
|
178
|
+
}
|
|
179
|
+
const result = {
|
|
180
|
+
type: CommentRuleType.PreProcessorCommentRule,
|
|
181
|
+
category: RuleCategory.Comment,
|
|
182
|
+
syntax: AdblockSyntax.Common,
|
|
183
|
+
name,
|
|
184
|
+
params,
|
|
185
|
+
};
|
|
186
|
+
if (options.includeRaws) {
|
|
187
|
+
result.raws = {
|
|
188
|
+
text: raw,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
if (options.isLocIncluded) {
|
|
192
|
+
result.start = baseOffset;
|
|
193
|
+
result.end = baseOffset + raw.length;
|
|
194
|
+
}
|
|
195
|
+
return result;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export { PreProcessorCommentParser };
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* AGTree v3.0.0-alpha.2 (build date: Tue, 17 Dec 2024 16:50:04 GMT)
|
|
3
|
+
* (c) 2024 Adguard Software Ltd.
|
|
4
|
+
* Released under the MIT license
|
|
5
|
+
* https://github.com/AdguardTeam/tsurlfilter/tree/master/packages/agtree#readme
|
|
6
|
+
*/
|
|
7
|
+
import { AdblockSyntax } from '../../utils/adblockers.js';
|
|
8
|
+
import { CosmeticRuleSeparatorUtils } from '../../utils/cosmetic-rule-separator.js';
|
|
9
|
+
import { StringUtils } from '../../utils/string.js';
|
|
10
|
+
import { CommentMarker, RuleCategory, CommentRuleType } from '../../nodes/index.js';
|
|
11
|
+
import { BaseParser } from '../base-parser.js';
|
|
12
|
+
import { ValueParser } from '../misc/value-parser.js';
|
|
13
|
+
import { defaultParserOptions } from '../options.js';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* `SimpleCommentParser` is responsible for parsing simple comments.
|
|
17
|
+
* Some comments have a special meaning in adblock syntax, like agent comments or hints,
|
|
18
|
+
* but this parser is only responsible for parsing regular comments,
|
|
19
|
+
* whose only purpose is to provide some human-readable information.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```adblock
|
|
23
|
+
* ! This is a simple comment
|
|
24
|
+
* # This is a simple comment, but in host-like syntax
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
class SimpleCommentParser extends BaseParser {
|
|
28
|
+
/**
|
|
29
|
+
* Checks if the raw rule is a simple comment.
|
|
30
|
+
*
|
|
31
|
+
* @param raw Raw input to check.
|
|
32
|
+
* @returns `true` if the input is a simple comment, `false` otherwise.
|
|
33
|
+
* @note This method does not check for adblock agent comments.
|
|
34
|
+
*/
|
|
35
|
+
static isSimpleComment(raw) {
|
|
36
|
+
const trimmed = raw.trim();
|
|
37
|
+
// Exclamation mark based comments
|
|
38
|
+
if (trimmed.startsWith(CommentMarker.Regular)) {
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
// Hashmark based comments
|
|
42
|
+
// Note: in this case, we must be sure that we do not mistakenly parse a cosmetic rule as a #-like comment,
|
|
43
|
+
// since most cosmetic rule separators also start with #
|
|
44
|
+
if (trimmed.startsWith(CommentMarker.Hashmark)) {
|
|
45
|
+
const result = CosmeticRuleSeparatorUtils.find(trimmed);
|
|
46
|
+
// If we cannot find a separator, it means that the rule is definitely a comment
|
|
47
|
+
if (result === null) {
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
// Otherwise, we must check if the separator is followed by a valid selector
|
|
51
|
+
const { end } = result;
|
|
52
|
+
// No valid selector
|
|
53
|
+
if (!trimmed[end + 1]
|
|
54
|
+
|| StringUtils.isWhitespace(trimmed[end + 1])
|
|
55
|
+
|| (trimmed[end + 1] === CommentMarker.Hashmark && trimmed[end + 2] === CommentMarker.Hashmark)) {
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Parses a raw rule as a simple comment.
|
|
63
|
+
*
|
|
64
|
+
* @param raw Raw input to parse.
|
|
65
|
+
* @param options Global parser options.
|
|
66
|
+
* @param baseOffset Starting offset of the input. Node locations are calculated relative to this offset.
|
|
67
|
+
* @returns Comment rule node or null (if the raw rule cannot be parsed as a simple comment).
|
|
68
|
+
*/
|
|
69
|
+
static parse(raw, options = defaultParserOptions, baseOffset = 0) {
|
|
70
|
+
// Ignore non-comment rules
|
|
71
|
+
if (!this.isSimpleComment(raw)) {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
// If we are here, it means that the rule is a regular comment
|
|
75
|
+
let offset = 0;
|
|
76
|
+
// Skip leading whitespace (if any)
|
|
77
|
+
offset = StringUtils.skipWS(raw, offset);
|
|
78
|
+
// Get comment marker
|
|
79
|
+
const marker = ValueParser.parse(raw[offset], options, baseOffset + offset);
|
|
80
|
+
// Skip marker
|
|
81
|
+
offset += 1;
|
|
82
|
+
// Get comment text
|
|
83
|
+
const text = ValueParser.parse(raw.slice(offset), options, baseOffset + offset);
|
|
84
|
+
// Regular comment rule
|
|
85
|
+
const result = {
|
|
86
|
+
category: RuleCategory.Comment,
|
|
87
|
+
type: CommentRuleType.CommentRule,
|
|
88
|
+
// TODO: Change syntax when hashmark is used
|
|
89
|
+
syntax: AdblockSyntax.Common,
|
|
90
|
+
marker,
|
|
91
|
+
text,
|
|
92
|
+
};
|
|
93
|
+
if (options.includeRaws) {
|
|
94
|
+
result.raws = {
|
|
95
|
+
text: raw,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
if (options.isLocIncluded) {
|
|
99
|
+
result.start = baseOffset;
|
|
100
|
+
result.end = baseOffset + raw.length;
|
|
101
|
+
}
|
|
102
|
+
return result;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export { SimpleCommentParser };
|