Skip to content

Commit

Permalink
fix: implement nested selector parsing (puppeteer#12587)
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN authored Jun 14, 2024
1 parent 80783fe commit 3874300
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
50 changes: 50 additions & 0 deletions packages/puppeteer-core/src/common/PSelectorParser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* @license
* Copyright 2024 Google Inc.
* SPDX-License-Identifier: Apache-2.0
*/

import {describe, it} from 'node:test';

import expect from 'expect';

import {parsePSelectors} from './PSelectorParser.js';

describe('PSelectorParser', () => {
describe('parsePSelectors', () => {
it('parses nested selectors', () => {
const [updatedSelector, isPureCSS, hasPseudoClasses] =
parsePSelectors('& > div');
expect(updatedSelector).toEqual([[['&>div']]]);
expect(isPureCSS).toBeTruthy();
expect(hasPseudoClasses).toBeFalsy();
});

it('parses nested selectors with p-selector syntax', () => {
const [updatedSelector, isPureCSS, hasPseudoClasses] =
parsePSelectors('& > div >>> button');
expect(updatedSelector).toEqual([[['&>div'], '>>>', ['button']]]);
expect(isPureCSS).toBeFalsy();
expect(hasPseudoClasses).toBeFalsy();
});

it('parses selectors with pseudo classes', () => {
const [updatedSelector, isPureCSS, hasPseudoClasses] =
parsePSelectors('div:focus');
expect(updatedSelector).toEqual([[['div:focus']]]);
expect(isPureCSS).toBeTruthy();
expect(hasPseudoClasses).toBeTruthy();
});

it('parses nested selectors with pseudo classes and p-selector syntax', () => {
const [updatedSelector, isPureCSS, hasPseudoClasses] = parsePSelectors(
'& > div:focus >>>> button:focus'
);
expect(updatedSelector).toEqual([
[['&>div:focus'], '>>>>', ['button:focus']],
]);
expect(isPureCSS).toBeFalsy();
expect(hasPseudoClasses).toBeTruthy();
});
});
});
3 changes: 2 additions & 1 deletion packages/puppeteer-core/src/common/PSelectorParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type {
} from '../injected/PQuerySelector.js';
import {PCombinator} from '../injected/PQuerySelector.js';

TOKENS['nesting'] = /&/g;
TOKENS['combinator'] = /\s*(>>>>?|[\s>+~])\s*/g;

const ESCAPE_REGEXP = /\\[\s\S]/g;
Expand Down Expand Up @@ -94,7 +95,7 @@ export function parsePSelectors(
continue;
case 'pseudo-class':
hasPseudoClasses = true;
continue;
break;
case 'comma':
if (storage.length) {
compoundSelector.push(stringify(storage));
Expand Down

0 comments on commit 3874300

Please sign in to comment.