support non-latin characters in matches-path

This commit is contained in:
Anton Lazarev
2025-05-22 21:49:40 -07:00
parent 5d03c4ae86
commit eded7f1027
3 changed files with 21 additions and 6 deletions
@@ -2827,10 +2827,14 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, ProceduralFilterMatchesPath) {
"p.odd:matches-path(cosmetic_filtering.html)\n"
"a.com##:matches-path(/c[aeiou]smetic\\_[a-z]{9}/) section "
".positive-regex-case p.odd\n"
"a.com##section .negative-case:matches-path(/some-other-page.html)");
"a.com##section .negative-case:matches-path(/some-other-page.html)\n"
"a.com##:matches-path(😎) section .positive-unicode-case p.odd");
GURL tab_url =
embedded_test_server()->GetURL("a.com", "/cosmetic_filtering.html");
GURL::Replacements replacements;
replacements.SetQueryStr("😎");
GURL tab_url = embedded_test_server()
->GetURL("a.com", "/cosmetic_filtering.html")
.ReplaceComponents(replacements);
NavigateToURL(tab_url);
content::WebContents* contents = web_contents();
@@ -123,6 +123,7 @@ const _extractKeyMatchRuleFromStr = (text: string): [TextMatchRule, number] => {
}
const _extractValueMatchRuleFromStr = (text: string,
uriEncode = false,
needlePosition = 0): TextMatchRule => {
const isQuotedCase = text[needlePosition] === '"'
let endIndex
@@ -140,7 +141,10 @@ const _extractValueMatchRuleFromStr = (text: string,
endIndex = text.length
}
const testCaseStr = text.slice(needlePosition, endIndex)
let testCaseStr = text.slice(needlePosition, endIndex)
if (uriEncode) {
testCaseStr = testCaseStr.replace(/\P{ASCII}/gu, c => encodeURIComponent(c))
}
const testCaseFunc = _testMatches.bind(undefined, testCaseStr)
return testCaseFunc
}
@@ -159,7 +163,7 @@ const _extractValueMatchRuleFromStr = (text: string,
// }
const _parseKeyValueMatchRules = (arg: string): KeyValueMatchRules => {
const [keyMatchRule, needlePos] = _extractKeyMatchRuleFromStr(arg)
const valueMatchRule = _extractValueMatchRuleFromStr(arg, needlePos)
const valueMatchRule = _extractValueMatchRuleFromStr(arg, false, needlePos)
return [keyMatchRule, valueMatchRule]
}
@@ -387,7 +391,7 @@ const operatorMatchesMedia = (instruction: string,
const operatorMatchesPath = (instruction: string,
element: HTMLElement): OperatorResult => {
const pathAndQuery = W.location.pathname + W.location.search
const matchRule = _extractValueMatchRuleFromStr(instruction)
const matchRule = _extractValueMatchRuleFromStr(instruction, true)
return matchRule(pathAndQuery) ? [element] : []
}
+7
View File
@@ -288,6 +288,13 @@ const classes = (expectedClasses) =>
<p data-expect="visible">Should not be hidden</p>
</div>
<div class="positive-unicode-case">
<h2><code>:matches-path</code>: positive unicode case</h2>
<p class="odd" data-expect="hidden">Should be hidden</p>
<p class="even" data-expect="visible">Should not be hidden</p>
<p data-expect="visible">Should not be hidden</p>
</div>
<div class="negative-case">
<h2><code>:matches-path</code>: negative case</h2>
<p class="odd" data-expect="visible">Should not be hidden</p>