fix(selectors): allow :scope with additional css (#11338)

This commit is contained in:
Dmitry Gozman 2022-01-11 18:40:29 -08:00 committed by GitHub
parent 4efb30999f
commit 359d523ec3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 0 deletions

View file

@ -202,6 +202,9 @@ export class SelectorEvaluatorImpl implements SelectorEvaluator {
let firstIndex = -1;
if (css !== undefined) {
elements = this._queryCSS(context, css);
const hasScopeClause = funcs.some(f => f.name === 'scope');
if (hasScopeClause && context.scope.nodeType === 1 /* Node.ELEMENT_NODE */)
elements.unshift(context.scope as Element);
} else {
firstIndex = funcs.findIndex(func => this._getEngine(func.name).query !== undefined);
if (firstIndex === -1)

View file

@ -383,6 +383,13 @@ it('should work with :scope', async ({ page, server }) => {
expect(await scope.$$eval(`css=* > :scope`, els => els.length)).toBe(0);
expect(await scope.$$eval(`css=* ~ :scope`, els => els.length)).toBe(0);
}
await page.setContent(`<article><div class=target>hello<span></span></div></article>`);
// 'scope' should allow additional native css modifiers
expect(await page.$eval(`div >> :scope.target`, e => e.textContent)).toBe('hello');
expect(await page.$eval(`div >> :scope:nth-child(1)`, e => e.textContent)).toBe('hello');
expect(await page.$eval(`div >> :scope.target:has(span)`, e => e.textContent)).toBe('hello');
expect(await page.$eval(`html:scope`, e => e.nodeName)).toBe('HTML');
});
it('should absolutize relative selectors', async ({ page, server }) => {