chore: do not carry selector next to parsed selector (#10004)

This commit is contained in:
Pavel Feldman 2021-11-03 07:35:01 -08:00 committed by GitHub
parent 9cebe60831
commit 2ae2136810
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 8 deletions

View file

@ -19,10 +19,10 @@ import { CSSComplexSelectorList, parseCSS } from './cssParser';
export type ParsedSelectorPart = { export type ParsedSelectorPart = {
name: string, name: string,
body: string | CSSComplexSelectorList, body: string | CSSComplexSelectorList,
source: string,
}; };
export type ParsedSelector = { export type ParsedSelector = {
selector: string,
parts: ParsedSelectorPart[], parts: ParsedSelectorPart[],
capture?: number, capture?: number,
}; };
@ -43,18 +43,22 @@ export function parseSelector(selector: string): ParsedSelector {
const parsedCSS = parseCSS(part.body, customCSSNames); const parsedCSS = parseCSS(part.body, customCSSNames);
return { return {
name: 'css', name: 'css',
body: parsedCSS.selector body: parsedCSS.selector,
source: part.body
}; };
} }
return part; return { ...part, source: part.body };
}); });
return { return {
selector,
capture: result.capture, capture: result.capture,
parts parts
}; };
} }
export function stringifySelector(selector: ParsedSelector): string {
return selector.parts.map((p, i) => `${i === selector.capture ? '*' : ''}${p.name}=${p.source}`).join(' >> ');
}
function parseSelectorString(selector: string): ParsedSelectorStrings { function parseSelectorString(selector: string): ParsedSelectorStrings {
let index = 0; let index = 0;
let quote: string | undefined; let quote: string | undefined;

View file

@ -18,7 +18,7 @@ import { SelectorEngine, SelectorRoot } from './selectorEngine';
import { XPathEngine } from './xpathSelectorEngine'; import { XPathEngine } from './xpathSelectorEngine';
import { ReactEngine } from './reactSelectorEngine'; import { ReactEngine } from './reactSelectorEngine';
import { VueEngine } from './vueSelectorEngine'; import { VueEngine } from './vueSelectorEngine';
import { ParsedSelector, ParsedSelectorPart, parseSelector } from '../common/selectorParser'; import { ParsedSelector, ParsedSelectorPart, parseSelector, stringifySelector } from '../common/selectorParser';
import { SelectorEvaluatorImpl, isVisible, parentElementOrShadowHost, elementMatchesText, TextMatcher, createRegexTextMatcher, createStrictTextMatcher, createLaxTextMatcher } from './selectorEvaluator'; import { SelectorEvaluatorImpl, isVisible, parentElementOrShadowHost, elementMatchesText, TextMatcher, createRegexTextMatcher, createStrictTextMatcher, createLaxTextMatcher } from './selectorEvaluator';
import { CSSComplexSelectorList } from '../common/cssParser'; import { CSSComplexSelectorList } from '../common/cssParser';
import { generateSelector } from './selectorGenerator'; import { generateSelector } from './selectorGenerator';
@ -757,7 +757,7 @@ export class InjectedScript {
const lines = infos.map((info, i) => `\n ${i + 1}) ${info.preview} aka playwright.$("${info.selector}")`); const lines = infos.map((info, i) => `\n ${i + 1}) ${info.preview} aka playwright.$("${info.selector}")`);
if (infos.length < matches.length) if (infos.length < matches.length)
lines.push('\n ...'); lines.push('\n ...');
return this.createStacklessError(`strict mode violation: "${selector.selector}" resolved to ${matches.length} elements:${lines.join('')}\n`); return this.createStacklessError(`strict mode violation: "${stringifySelector(selector)}" resolved to ${matches.length} elements:${lines.join('')}\n`);
} }
createStacklessError(message: string): Error { createStacklessError(message: string): Error {

View file

@ -24,7 +24,6 @@ import { createGuid } from '../utils/utils';
export type SelectorInfo = { export type SelectorInfo = {
parsed: ParsedSelector, parsed: ParsedSelector,
world: types.World, world: types.World,
selector: string,
strict: boolean, strict: boolean,
}; };
@ -141,7 +140,6 @@ export class Selectors {
} }
return { return {
parsed, parsed,
selector,
world: needsMainWorld ? 'main' : 'utility', world: needsMainWorld ? 'main' : 'utility',
strict, strict,
}; };