Skip to content

Commit b9c9597

Browse files
authored
feat(selectors): optimize old->new conversion for css (#4672)
1 parent e97ab7e commit b9c9597

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

src/server/common/selectorParser.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,30 @@ export function parseSelector(selector: string, customNames: Set<string>): Parse
6363
wrapInLight = true;
6464
name = name.substring(0, name.indexOf(':'));
6565
}
66-
let simple: CSSSimpleSelector;
6766
if (name === 'css') {
6867
const parsed = parseCSS(part.body, customNames);
6968
parsed.names.forEach(name => names.add(name));
70-
simple = callWith('is', parsed.selector);
69+
if (wrapInLight || parsed.selector.length > 1) {
70+
let simple = callWith('is', parsed.selector);
71+
if (wrapInLight)
72+
simple = callWith('light', [simpleToComplex(simple)]);
73+
result.simples.push({ selector: simple, combinator: '' });
74+
} else {
75+
result.simples.push(...parsed.selector[0].simples);
76+
}
7177
} else if (name === 'text') {
72-
simple = textSelectorToSimple(part.body);
78+
let simple = textSelectorToSimple(part.body);
7379
if (result.simples.length)
7480
result.simples[result.simples.length - 1].combinator = '>=';
81+
if (wrapInLight)
82+
simple = callWith('light', [simpleToComplex(simple)]);
83+
result.simples.push({ selector: simple, combinator: '' });
7584
} else {
76-
simple = callWith(name, [part.body]);
85+
let simple = callWith(name, [part.body]);
86+
if (wrapInLight)
87+
simple = callWith('light', [simpleToComplex(simple)]);
88+
result.simples.push({ selector: simple, combinator: '' });
7789
}
78-
if (wrapInLight)
79-
simple = callWith('light', [simpleToComplex(simple)]);
80-
result.simples.push({ selector: simple, combinator: '' });
8190
}
8291
return result;
8392
};

test/selectors-css.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,20 @@ it('should keep dom order with comma separated list', async ({page}) => {
146146
expect(await page.$$eval(`css=section >> *css=div,span >> css=y`, els => els.map(e => e.nodeName).join(','))).toBe('SPAN,DIV');
147147
});
148148

149+
it('should work with comma separated list in various positions', async ({page}) => {
150+
await page.setContent(`<section><span><div><x></x><y></y></div></span></section>`);
151+
expect(await page.$$eval(`css=span,div >> css=x,y`, els => els.map(e => e.nodeName).join(','))).toBe('X,Y');
152+
expect(await page.$$eval(`css=span,div >> css=x`, els => els.map(e => e.nodeName).join(','))).toBe('X');
153+
expect(await page.$$eval(`css=div >> css=x,y`, els => els.map(e => e.nodeName).join(','))).toBe('X,Y');
154+
expect(await page.$$eval(`css=div >> css=x`, els => els.map(e => e.nodeName).join(','))).toBe('X');
155+
156+
expect(await page.$$eval(`css=section >> css=div >> css=x`, els => els.map(e => e.nodeName).join(','))).toBe('X');
157+
expect(await page.$$eval(`css=section >> css=span >> css=div >> css=y`, els => els.map(e => e.nodeName).join(','))).toBe('Y');
158+
expect(await page.$$eval(`css=section >> css=div >> css=x,y`, els => els.map(e => e.nodeName).join(','))).toBe('X,Y');
159+
expect(await page.$$eval(`css=section >> css=div,span >> css=x,y`, els => els.map(e => e.nodeName).join(','))).toBe('X,Y');
160+
expect(await page.$$eval(`css=section >> css=span >> css=x,y`, els => els.map(e => e.nodeName).join(','))).toBe('X,Y');
161+
});
162+
149163
it('should work with comma inside text', async ({page}) => {
150164
await page.setContent(`<span></span><div attr="hello,world!"></div>`);
151165
expect(await page.$eval(`css=div[attr="hello,world!"]`, e => e.outerHTML)).toBe('<div attr="hello,world!"></div>');

0 commit comments

Comments
 (0)