UNPKG

@adguard/agtree

Version:
71 lines (68 loc) 2.4 kB
/* * AGTree v4.1.1 (build date: Thu, 23 Apr 2026 09:15:37 GMT) * (c) 2026 Adguard Software Ltd. * Released under the MIT license * https://github.com/AdguardTeam/tsurlfilter/tree/master/packages/agtree#readme */ import { BaseParser } from '../../base-parser.js'; import { defaultParserOptions } from '../../options.js'; import { SelectorListParser } from '../selector/selector-list-parser.js'; /** * Class responsible for parsing HTML filtering rule body. * * Please note that the parser will parse any HTML filtering rule body if it is syntactically correct. * For example, it will parse this:. * ```adblock * span[special-attr="Example"] * div:special-pseudo(Example) * ``` * * But it didn't check if the pseudo selector `special-pseudo` or if * the attribute selector `special-attr` actually supported by any adblocker.. * * @see {@link https://www.w3.org/TR/selectors-4} * @see {@link https://adguard.com/kb/general/ad-filtering/create-own-filters/#html-filtering-rules} * @see {@link https://github.com/gorhill/uBlock/wiki/Static-filter-syntax#html-filters} */ class HtmlFilteringBodyParser extends BaseParser { /** * Parses a HTML filtering rule body. * * @param raw Raw input to parse. * @param options Global parser options. * @param baseOffset Starting offset of the input. Node locations are calculated relative to this offset. * * @returns Node of the parsed HTML filtering rule body. * * @throws If the body is syntactically incorrect. * * @example * ``` * span[tag-content="Example"] * div:has-text(Example) * ``` */ static parse(raw, options = defaultParserOptions, baseOffset = 0) { // If HTML filtering rules parsing is disabled, return raw value node let result; if (options.parseHtmlFilteringRuleBodies) { result = { type: 'HtmlFilteringRuleBody', selectorList: SelectorListParser.parse(raw, options, baseOffset), }; } else { result = { type: 'Value', value: raw, }; } // Include body locations if needed if (options.isLocIncluded) { result.start = baseOffset; result.end = baseOffset + raw.length; } return result; } } export { HtmlFilteringBodyParser };