Skip to content

Commit

Permalink
fix: fix escaping algo for P selectors (#10474)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrandolf committed Jun 29, 2023
1 parent 0a7bad6 commit 84a956f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
20 changes: 8 additions & 12 deletions packages/puppeteer-core/src/injected/PSelectorParser.ts
Expand Up @@ -33,19 +33,15 @@ TOKENS['combinator'] = /\s*(>>>>?|[\s>+~])\s*/g;

const ESCAPE_REGEXP = /\\[\s\S]/g;
const unquote = (text: string): string => {
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);
});
}
if (text.length <= 1) {
return text;
}
if ((text[0] === '"' || text[0] === "'") && text.endsWith(text[0])) {
text = text.slice(1, -1);
}
return text;
return text.replace(ESCAPE_REGEXP, match => {
return match[1] as string;
});
};

export function parsePSelectors(
Expand Down
1 change: 1 addition & 0 deletions test/assets/p-selectors.html
Expand Up @@ -2,6 +2,7 @@
<span id="f"></span>
<div id="c"></div>
</div>
<a>My name is Jun (pronounced like "June")</a>

<script>
const topShadow = document.querySelector('#c');
Expand Down
21 changes: 21 additions & 0 deletions test/src/queryhandler.spec.ts
Expand Up @@ -637,5 +637,26 @@ describe('Query handler tests', function () {
const elements = await page.$$('::-p-text(world), button');
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();
});
});
});

0 comments on commit 84a956f

Please sign in to comment.