fix: fix escaping algo for P selectors (#10474)

This commit is contained in:
jrandolf 2023-06-29 13:00:57 +02:00 committed by GitHub
parent 0a7bad6d6a
commit 84a956f56b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 12 deletions

View File

@ -33,19 +33,15 @@ TOKENS['combinator'] = /\s*(>>>>?|[\s>+~])\s*/g;
const ESCAPE_REGEXP = /\\[\s\S]/g; const ESCAPE_REGEXP = /\\[\s\S]/g;
const unquote = (text: string): string => { const unquote = (text: string): string => {
if (text.length > 1) { if (text.length <= 1) {
for (const char of ['"', "'"]) {
if (!text.startsWith(char) || !text.endsWith(char)) {
continue;
}
return text
.slice(char.length, -char.length)
.replace(ESCAPE_REGEXP, match => {
return match.slice(1);
});
}
}
return text; return text;
}
if ((text[0] === '"' || text[0] === "'") && text.endsWith(text[0])) {
text = text.slice(1, -1);
}
return text.replace(ESCAPE_REGEXP, match => {
return match[1] as string;
});
}; };
export function parsePSelectors( export function parsePSelectors(

View File

@ -2,6 +2,7 @@
<span id="f"></span> <span id="f"></span>
<div id="c"></div> <div id="c"></div>
</div> </div>
<a>My name is Jun (pronounced like "June")</a>
<script> <script>
const topShadow = document.querySelector('#c'); const topShadow = document.querySelector('#c');

View File

@ -637,5 +637,26 @@ describe('Query handler tests', function () {
const elements = await page.$$('::-p-text(world), button'); const elements = await page.$$('::-p-text(world), button');
expect(elements).toHaveLength(1); expect(elements).toHaveLength(1);
}); });
it('should handle escapes', async () => {
const {server, page} = await getTestState();
await page.goto(`${server.PREFIX}/p-selectors.html`);
let element = await page.$(
':scope >>> ::-p-text(My name is Jun \\(pronounced like "June"\\))'
);
expect(element).toBeTruthy();
element = await page.$(
':scope >>> ::-p-text("My name is Jun (pronounced like \\"June\\")")'
);
expect(element).toBeTruthy();
element = await page.$(
':scope >>> ::-p-text(My name is Jun \\(pronounced like "June"\\)")'
);
expect(element).toBeFalsy();
element = await page.$(
':scope >>> ::-p-text("My name is Jun \\(pronounced like "June"\\))'
);
expect(element).toBeFalsy();
});
}); });
}); });