@adguard/agtree 1.1.5 → 1.1.7

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/dist/agtree.d.ts CHANGED
@@ -1,10 +1,10 @@
1
1
  /*
2
- * AGTree v1.1.5 (build date: Tue, 05 Sep 2023 15:10:53 GMT)
2
+ * AGTree v1.1.7 (build date: Tue, 07 Nov 2023 14:17:46 GMT)
3
3
  * (c) 2023 AdGuard Software Ltd.
4
4
  * Released under the MIT license
5
5
  * https://github.com/AdguardTeam/tsurlfilter/tree/master/packages/agtree#readme
6
6
  */
7
- import { MediaQueryListPlain, SelectorListPlain, DeclarationListPlain, FunctionNodePlain, CssNode, CssNodePlain, SelectorList, DeclarationList, MediaQueryList, MediaQuery, Selector, AttributeSelector, PseudoClassSelectorPlain, PseudoClassSelector, FunctionNode } from '@adguard/ecss-tree';
7
+ import { MediaQueryListPlain, SelectorListPlain, DeclarationListPlain, FunctionNodePlain, CssNode, CssNodePlain, SelectorList, DeclarationList, MediaQueryList, MediaQuery, Selector, SelectorPlain, AttributeSelector, PseudoClassSelectorPlain, PseudoClassSelector, FunctionNode } from '@adguard/ecss-tree';
8
8
  import * as ecssTree from '@adguard/ecss-tree';
9
9
  export { ecssTree as ECSSTree };
10
10
 
@@ -2066,12 +2066,38 @@ declare class ModifierValidator {
2066
2066
  }
2067
2067
  declare const modifierValidator: ModifierValidator;
2068
2068
 
2069
+ /**
2070
+ * @file Conversion result interface and helper functions
2071
+ */
2072
+
2073
+ /**
2074
+ * Common conversion result interface
2075
+ *
2076
+ * @template T Type of the item to convert
2077
+ * @template U Type of the conversion result (defaults to `T`, but can be `T[]` as well)
2078
+ */
2079
+ interface ConversionResult<T, U extends T | T[] = T> {
2080
+ /**
2081
+ * Conversion result
2082
+ */
2083
+ result: U;
2084
+ /**
2085
+ * Indicates whether the input item was converted
2086
+ */
2087
+ isConverted: boolean;
2088
+ }
2089
+ /**
2090
+ * Adblock rule node conversion result interface, where the conversion result is an array of rules
2091
+ */
2092
+ type NodeConversionResult<T extends Node> = ConversionResult<T, T[]>;
2093
+
2069
2094
  /**
2070
2095
  * @file Base class for converters
2071
2096
  *
2072
2097
  * TS doesn't support abstract static methods, so we should use
2073
2098
  * a workaround and extend this class instead of implementing it
2074
2099
  */
2100
+
2075
2101
  /**
2076
2102
  * Basic class for rule converters
2077
2103
  */
@@ -2080,26 +2106,121 @@ declare class ConverterBase {
2080
2106
  * Converts some data to AdGuard format
2081
2107
  *
2082
2108
  * @param data Data to convert
2083
- * @returns Converted data
2109
+ * @returns An object which follows the {@link ConversionResult} interface. Its `result` property contains
2110
+ * the converted node, and its `isConverted` flag indicates whether the original node was converted.
2111
+ * If the node was not converted, the result will contain the original node with the same object reference
2084
2112
  * @throws If the data is invalid or incompatible
2085
2113
  */
2086
- static convertToAdg(data: unknown): unknown;
2114
+ static convertToAdg(data: unknown): ConversionResult<unknown>;
2087
2115
  /**
2088
2116
  * Converts some data to Adblock Plus format
2089
2117
  *
2090
2118
  * @param data Data to convert
2091
- * @returns Converted data
2119
+ * @returns An object which follows the {@link ConversionResult} interface. Its `result` property contains
2120
+ * the converted node, and its `isConverted` flag indicates whether the original node was converted.
2121
+ * If the node was not converted, the result will contain the original node with the same object reference
2092
2122
  * @throws If the data is invalid or incompatible
2093
2123
  */
2094
- static convertToAbp(data: unknown): unknown;
2124
+ static convertToAbp(data: unknown): ConversionResult<unknown>;
2095
2125
  /**
2096
2126
  * Converts some data to uBlock Origin format
2097
2127
  *
2098
2128
  * @param data Data to convert
2099
- * @returns Converted data
2129
+ * @returns An object which follows the {@link ConversionResult} interface. Its `result` property contains
2130
+ * the converted node, and its `isConverted` flag indicates whether the original node was converted.
2131
+ * If the node was not converted, the result will contain the original node with the same object reference
2100
2132
  * @throws If the data is invalid or incompatible
2101
2133
  */
2102
- static convertToUbo(data: unknown): unknown;
2134
+ static convertToUbo(data: unknown): ConversionResult<unknown>;
2135
+ }
2136
+
2137
+ /**
2138
+ * @file Adblock filter list converter
2139
+ */
2140
+
2141
+ /**
2142
+ * Adblock filter list converter class
2143
+ *
2144
+ * This class just provides an extra layer on top of the {@link RuleConverter}
2145
+ * and can be used to convert entire filter lists.
2146
+ *
2147
+ * @todo Implement `convertToUbo` and `convertToAbp`
2148
+ * @todo Implement tolerant mode, which will allow to convert a filter list
2149
+ * even if some of its rules are invalid
2150
+ */
2151
+ declare class FilterListConverter extends ConverterBase {
2152
+ /**
2153
+ * Converts an adblock filter list to AdGuard format, if possible.
2154
+ *
2155
+ * @param filterListNode Filter list node to convert
2156
+ * @param tolerant Indicates whether the converter should be tolerant to invalid rules. If enabled and a rule is
2157
+ * invalid, it will be left as is. If disabled and a rule is invalid, the whole filter list will be failed.
2158
+ * Defaults to `true`.
2159
+ * @returns An object which follows the {@link ConversionResult} interface. Its `result` property contains
2160
+ * the converted node, and its `isConverted` flag indicates whether the original node was converted.
2161
+ * If the node was not converted, the result will contain the original node with the same object reference
2162
+ * @throws If the filter list is invalid or cannot be converted (if the tolerant mode is disabled)
2163
+ */
2164
+ static convertToAdg(filterListNode: FilterList, tolerant?: boolean): ConversionResult<FilterList>;
2165
+ }
2166
+
2167
+ /**
2168
+ * @file Filter list converter for raw filter lists
2169
+ *
2170
+ * Technically, this is a wrapper around `FilterListConverter` that works with nodes instead of strings.
2171
+ */
2172
+
2173
+ /**
2174
+ * Adblock filter list converter class.
2175
+ *
2176
+ * You can use this class to convert string-based filter lists, since most of the converters work with nodes.
2177
+ * This class just provides an extra layer on top of the {@link FilterListConverter} and calls the parser/serializer
2178
+ * before/after the conversion internally.
2179
+ *
2180
+ * @todo Implement `convertToUbo` and `convertToAbp`
2181
+ */
2182
+ declare class RawFilterListConverter extends ConverterBase {
2183
+ /**
2184
+ * Converts an adblock filter list text to AdGuard format, if possible.
2185
+ *
2186
+ * @param rawFilterList Raw filter list text to convert
2187
+ * @param tolerant Indicates whether the converter should be tolerant to invalid rules. If enabled and a rule is
2188
+ * invalid, it will be left as is. If disabled and a rule is invalid, the whole filter list will be failed.
2189
+ * Defaults to `true`.
2190
+ * @returns An object which follows the {@link ConversionResult} interface. Its `result` property contains
2191
+ * the array of converted filter list text, and its `isConverted` flag indicates whether the original rule was
2192
+ * converted. If the rule was not converted, the original filter list text will be returned
2193
+ * @throws If the filter list is invalid or cannot be converted (if the tolerant mode is disabled)
2194
+ */
2195
+ static convertToAdg(rawFilterList: string, tolerant?: boolean): ConversionResult<string>;
2196
+ }
2197
+
2198
+ /**
2199
+ * @file Rule converter for raw rules
2200
+ *
2201
+ * Technically, this is a wrapper around `RuleConverter` that works with nodes instead of strings.
2202
+ */
2203
+
2204
+ /**
2205
+ * Adblock filtering rule converter class.
2206
+ *
2207
+ * You can use this class to convert string-based adblock rules, since most of the converters work with nodes.
2208
+ * This class just provides an extra layer on top of the {@link RuleConverter} and calls the parser/serializer
2209
+ * before/after the conversion internally.
2210
+ *
2211
+ * @todo Implement `convertToUbo` and `convertToAbp`
2212
+ */
2213
+ declare class RawRuleConverter extends ConverterBase {
2214
+ /**
2215
+ * Converts an adblock filtering rule to AdGuard format, if possible.
2216
+ *
2217
+ * @param rawRule Raw rule text to convert
2218
+ * @returns An object which follows the {@link ConversionResult} interface. Its `result` property contains
2219
+ * the array of converted rule texts, and its `isConverted` flag indicates whether the original rule was converted.
2220
+ * If the rule was not converted, the original rule text will be returned
2221
+ * @throws If the rule is invalid or cannot be converted
2222
+ */
2223
+ static convertToAdg(rawRule: string): ConversionResult<string, string[]>;
2103
2224
  }
2104
2225
 
2105
2226
  /**
@@ -2117,26 +2238,32 @@ declare class RuleConverterBase extends ConverterBase {
2117
2238
  * Converts an adblock filtering rule to AdGuard format, if possible.
2118
2239
  *
2119
2240
  * @param rule Rule node to convert
2120
- * @returns Array of converted rule nodes
2241
+ * @returns An object which follows the {@link NodeConversionResult} interface. Its `result` property contains
2242
+ * the array of converted rule nodes, and its `isConverted` flag indicates whether the original rule was converted.
2243
+ * If the rule was not converted, the result array will contain the original node with the same object reference
2121
2244
  * @throws If the rule is invalid or cannot be converted
2122
2245
  */
2123
- static convertToAdg(rule: Node): Node[];
2246
+ static convertToAdg(rule: Node): NodeConversionResult<Node>;
2124
2247
  /**
2125
2248
  * Converts an adblock filtering rule to Adblock Plus format, if possible.
2126
2249
  *
2127
2250
  * @param rule Rule node to convert
2128
- * @returns Array of converted rule nodes
2251
+ * @returns An object which follows the {@link NodeConversionResult} interface. Its `result` property contains
2252
+ * the array of converted rule nodes, and its `isConverted` flag indicates whether the original rule was converted.
2253
+ * If the rule was not converted, the result array will contain the original node with the same object reference
2129
2254
  * @throws If the rule is invalid or cannot be converted
2130
2255
  */
2131
- static convertToAbp(rule: Node): Node[];
2256
+ static convertToAbp(rule: Node): NodeConversionResult<Node>;
2132
2257
  /**
2133
2258
  * Converts an adblock filtering rule to uBlock Origin format, if possible.
2134
2259
  *
2135
2260
  * @param rule Rule node to convert
2136
- * @returns Array of converted rule nodes
2261
+ * @returns An object which follows the {@link NodeConversionResult} interface. Its `result` property contains
2262
+ * the array of converted rule nodes, and its `isConverted` flag indicates whether the original rule was converted.
2263
+ * If the rule was not converted, the result array will contain the original node with the same object reference
2137
2264
  * @throws If the rule is invalid or cannot be converted
2138
2265
  */
2139
- static convertToUbo(rule: Node): Node[];
2266
+ static convertToUbo(rule: Node): NodeConversionResult<Node>;
2140
2267
  }
2141
2268
 
2142
2269
  /**
@@ -2157,35 +2284,12 @@ declare class RuleConverter extends RuleConverterBase {
2157
2284
  * Converts an adblock filtering rule to AdGuard format, if possible.
2158
2285
  *
2159
2286
  * @param rule Rule node to convert
2160
- * @returns Array of converted rule nodes
2287
+ * @returns An object which follows the {@link NodeConversionResult} interface. Its `result` property contains
2288
+ * the array of converted rule nodes, and its `isConverted` flag indicates whether the original rule was converted.
2289
+ * If the rule was not converted, the result array will contain the original node with the same object reference
2161
2290
  * @throws If the rule is invalid or cannot be converted
2162
2291
  */
2163
- static convertToAdg(rule: AnyRule): AnyRule[];
2164
- }
2165
-
2166
- /**
2167
- * @file Adblock filter list converter
2168
- */
2169
-
2170
- /**
2171
- * Adblock filter list converter class
2172
- *
2173
- * This class just provides an extra layer on top of the {@link RuleConverter}
2174
- * and can be used to convert entire filter lists.
2175
- *
2176
- * @todo Implement `convertToUbo` and `convertToAbp`
2177
- * @todo Implement tolerant mode, which will allow to convert a filter list
2178
- * even if some of its rules are invalid
2179
- */
2180
- declare class FilterListConverter extends ConverterBase {
2181
- /**
2182
- * Converts an adblock filter list to AdGuard format, if possible.
2183
- *
2184
- * @param filterListNode Filter list node to convert
2185
- * @returns Converted filter list node
2186
- * @throws If the filter list is invalid or cannot be converted
2187
- */
2188
- static convertToAdg(filterListNode: FilterList): FilterList;
2292
+ static convertToAdg(rule: AnyRule): NodeConversionResult<AnyRule>;
2189
2293
  }
2190
2294
 
2191
2295
  /**
@@ -2473,6 +2577,21 @@ declare class CssTree {
2473
2577
  * @returns CSS selector as string
2474
2578
  */
2475
2579
  static generateSelector(ast: Selector): string;
2580
+ /**
2581
+ * Generates string representation of the selector list.
2582
+ *
2583
+ * @param ast SelectorList AST
2584
+ * @returns String representation of the selector list
2585
+ */
2586
+ static generateSelectorListPlain(ast: SelectorListPlain): string;
2587
+ /**
2588
+ * Selector generation based on CSSTree's AST. This is necessary because CSSTree
2589
+ * only adds spaces in some edge cases.
2590
+ *
2591
+ * @param ast CSS Tree AST
2592
+ * @returns CSS selector as string
2593
+ */
2594
+ static generateSelectorPlain(ast: SelectorPlain): string;
2476
2595
  /**
2477
2596
  * Block generation based on CSSTree's AST. This is necessary because CSSTree only adds spaces in some edge cases.
2478
2597
  *
@@ -2550,6 +2669,14 @@ declare class CssTree {
2550
2669
  * @example `responseheader(name)` -> `name`
2551
2670
  */
2552
2671
  static generateFunctionValue(node: FunctionNode): string;
2672
+ /**
2673
+ * Helper function to generate a raw string from a function selector's children
2674
+ *
2675
+ * @param node Function node
2676
+ * @returns Generated function value
2677
+ * @example `responseheader(name)` -> `name`
2678
+ */
2679
+ static generateFunctionPlainValue(node: FunctionNodePlain): string;
2553
2680
  }
2554
2681
 
2555
2682
  declare class DomainUtils {
@@ -2783,4 +2910,4 @@ declare const FORBIDDEN_CSS_FUNCTIONS: Set<string>;
2783
2910
  */
2784
2911
  declare const version: string;
2785
2912
 
2786
- export { ADBLOCK_URL_SEPARATOR, ADBLOCK_URL_SEPARATOR_REGEX, ADBLOCK_URL_START, ADBLOCK_URL_START_REGEX, ADBLOCK_WILDCARD, ADBLOCK_WILDCARD_REGEX, ADG_SCRIPTLET_MASK, AGLINT_COMMAND_PREFIX, AdblockSyntax, AdblockSyntaxError, Agent, AgentCommentRule, AgentCommentRuleParser, AgentParser, AnyCommentRule, AnyCosmeticRule, AnyExpressionNode, AnyOperator, AnyRule, AppListParser, COMMA_DOMAIN_LIST_SEPARATOR, CommentBase, CommentMarker, CommentRule, CommentRuleParser, CommentRuleType, ConfigCommentRule, ConfigCommentRuleParser, CosmeticRule, CosmeticRuleParser, CosmeticRuleSeparator, CosmeticRuleSeparatorFinderResult, CosmeticRuleSeparatorUtils, CosmeticRuleType, CssInjectionRule, CssInjectionRuleBody, CssTree, CssTreeNodeType, CssTreeParserContext, Domain, DomainList, DomainListParser, DomainListSeparator, DomainUtils, EXT_CSS_LEGACY_ATTRIBUTES, EXT_CSS_PSEUDO_CLASSES, ElementHidingRule, ElementHidingRuleBody, EmptyRule, ExpressionOperatorNode, ExpressionParenthesisNode, ExpressionVariableNode, FORBIDDEN_CSS_FUNCTIONS, FilterList, FilterListConverter, FilterListParser, HINT_MARKER, Hint, HintCommentRule, HintCommentRuleParser, HintParser, HtmlFilteringRule, HtmlFilteringRuleBody, IF, INCLUDE, JsInjectionRule, Location, LocationRange, LogicalExpressionParser, LogicalExpressionUtils, METADATA_HEADERS, MODIFIERS_SEPARATOR, MODIFIER_ASSIGN_OPERATOR, MetadataCommentRule, MetadataCommentRuleParser, MethodListParser, Modifier, ModifierList, ModifierListParser, ModifierParser, NEGATION_MARKER, NETWORK_RULE_EXCEPTION_MARKER, NETWORK_RULE_SEPARATOR, NetworkRule, NetworkRuleParser, Node, NotImplementedError, PIPE_MODIFIER_SEPARATOR, PREPROCESSOR_MARKER, Parameter, ParameterList, ParameterListParser, PreProcessorCommentRule, PreProcessorCommentRuleParser, QuoteType, QuoteUtils, RegExpUtils, RuleBase, RuleCategory, RuleConversionError, RuleConverter, RuleParser, SAFARI_CB_AFFINITY, SPECIAL_REGEX_SYMBOLS, ScriptletInjectionRule, ScriptletInjectionRuleBody, StealthOptionListParser, UBO_SCRIPTLET_MASK, Value, VariableTable, locRange, modifierValidator, shiftLoc, version };
2913
+ export { ADBLOCK_URL_SEPARATOR, ADBLOCK_URL_SEPARATOR_REGEX, ADBLOCK_URL_START, ADBLOCK_URL_START_REGEX, ADBLOCK_WILDCARD, ADBLOCK_WILDCARD_REGEX, ADG_SCRIPTLET_MASK, AGLINT_COMMAND_PREFIX, AdblockSyntax, AdblockSyntaxError, Agent, AgentCommentRule, AgentCommentRuleParser, AgentParser, AnyCommentRule, AnyCosmeticRule, AnyExpressionNode, AnyOperator, AnyRule, AppListParser, COMMA_DOMAIN_LIST_SEPARATOR, CommentBase, CommentMarker, CommentRule, CommentRuleParser, CommentRuleType, ConfigCommentRule, ConfigCommentRuleParser, CosmeticRule, CosmeticRuleParser, CosmeticRuleSeparator, CosmeticRuleSeparatorFinderResult, CosmeticRuleSeparatorUtils, CosmeticRuleType, CssInjectionRule, CssInjectionRuleBody, CssTree, CssTreeNodeType, CssTreeParserContext, Domain, DomainList, DomainListParser, DomainListSeparator, DomainUtils, EXT_CSS_LEGACY_ATTRIBUTES, EXT_CSS_PSEUDO_CLASSES, ElementHidingRule, ElementHidingRuleBody, EmptyRule, ExpressionOperatorNode, ExpressionParenthesisNode, ExpressionVariableNode, FORBIDDEN_CSS_FUNCTIONS, FilterList, FilterListConverter, FilterListParser, HINT_MARKER, Hint, HintCommentRule, HintCommentRuleParser, HintParser, HtmlFilteringRule, HtmlFilteringRuleBody, IF, INCLUDE, JsInjectionRule, Location, LocationRange, LogicalExpressionParser, LogicalExpressionUtils, METADATA_HEADERS, MODIFIERS_SEPARATOR, MODIFIER_ASSIGN_OPERATOR, MetadataCommentRule, MetadataCommentRuleParser, MethodListParser, Modifier, ModifierList, ModifierListParser, ModifierParser, NEGATION_MARKER, NETWORK_RULE_EXCEPTION_MARKER, NETWORK_RULE_SEPARATOR, NetworkRule, NetworkRuleParser, Node, NotImplementedError, PIPE_MODIFIER_SEPARATOR, PREPROCESSOR_MARKER, Parameter, ParameterList, ParameterListParser, PreProcessorCommentRule, PreProcessorCommentRuleParser, QuoteType, QuoteUtils, RawFilterListConverter, RawRuleConverter, RegExpUtils, RuleBase, RuleCategory, RuleConversionError, RuleConverter, RuleParser, SAFARI_CB_AFFINITY, SPECIAL_REGEX_SYMBOLS, ScriptletInjectionRule, ScriptletInjectionRuleBody, StealthOptionListParser, UBO_SCRIPTLET_MASK, Value, VariableTable, locRange, modifierValidator, shiftLoc, version };