Skip to content

Commit

Permalink
chore: bump css-selector-parser to v3
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach committed Oct 14, 2023
1 parent f070232 commit 4050a32
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 27 deletions.
63 changes: 37 additions & 26 deletions lib/css-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,44 +240,55 @@ class CssConverter {
`Only child combinator (>) and descendant combinator are supported.`);
}

let uiAutomatorSelector = 'new UiSelector()';
// @ts-ignore the attribute should exist
const tagName = cssRule.tag?.name;
/** @type {string[]} */
const uiAutomatorSelector = ['new UiSelector()'];
/** @type {import('css-selector-parser').AstClassName[]} */
// @ts-ignore This should work
const astClassNames = cssRule.items.filter(({type}) => type === 'ClassName');
const classNames = astClassNames.map(({name}) => name);
/** @type {import('css-selector-parser').AstTagName|undefined} */
// @ts-ignore This should work
const astTag = cssRule.items.find(({type}) => type === 'TagName');
const tagName = astTag?.name;
if (tagName && tagName !== '*') {
let androidClass = [tagName];
if (cssRule.classNames) {
for (const cssClassNames of cssRule.classNames) {
const androidClass = [tagName];
if (classNames.length) {
for (const cssClassNames of classNames) {
androidClass.push(cssClassNames);
}
uiAutomatorSelector += `.className("${androidClass.join('.')}")`;
uiAutomatorSelector.push(`.className("${androidClass.join('.')}")`);
} else {
uiAutomatorSelector += `.classNameMatches("${tagName}")`;
uiAutomatorSelector.push(`.classNameMatches("${tagName}")`);
}
} else if (!_.isEmpty(cssRule.classNames)) {
// @ts-ignore the attribute should exist
uiAutomatorSelector += `.classNameMatches("${cssRule.classNames.join('\\.')}")`;
} else if (classNames.length) {
uiAutomatorSelector.push(`.classNameMatches("${classNames.join('\\.')}")`);
}
if (!_.isEmpty(cssRule.ids)) {
// @ts-ignore The attribute should exist
uiAutomatorSelector += `.resourceId("${this.formatIdLocator(cssRule.ids[0])}")`;
/** @type {import('css-selector-parser').AstId[]} */
// @ts-ignore This should work
const astIds = cssRule.items.filter(({type}) => type === 'Id');
const ids = astIds.map(({name}) => name);
if (ids.length) {
uiAutomatorSelector.push(`.resourceId("${this.formatIdLocator(ids[0])}")`);
}
if (cssRule.attributes) {
for (const attr of cssRule.attributes) {
uiAutomatorSelector += this.parseAttr(attr);
}
/** @type {import('css-selector-parser').AstAttribute[]} */
// @ts-ignore This should work
const attributes = cssRule.items.filter(({type}) => type === 'Attribute');
for (const attr of attributes) {
uiAutomatorSelector.push(this.parseAttr(attr));
}
if (cssRule.pseudoClasses) {
for (const pseudo of cssRule.pseudoClasses) {
const sel = this.parsePseudo(pseudo);
if (sel) {
uiAutomatorSelector += sel;
}
/** @type {import('css-selector-parser').AstPseudoClass[]} */
// @ts-ignore This should work
const pseudoClasses = cssRule.items.filter(({type}) => type === 'PseudoClass');
for (const pseudo of pseudoClasses) {
const sel = this.parsePseudo(pseudo);
if (sel) {
uiAutomatorSelector.push(sel);
}
}
if (cssRule.nestedRule) {
uiAutomatorSelector += `.childSelector(${this.parseCssRule(cssRule.nestedRule)})`;
uiAutomatorSelector.push(`.childSelector(${this.parseCssRule(cssRule.nestedRule)})`);
}
return uiAutomatorSelector;
return uiAutomatorSelector.join('');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"asyncbox": "^2.3.1",
"axios": "^1.x",
"bluebird": "^3.5.1",
"css-selector-parser": "^2.2.3",
"css-selector-parser": "^3.0.0",
"lodash": "^4.17.4",
"portscanner": "^2.2.0",
"source-map-support": "^0.x",
Expand Down

0 comments on commit 4050a32

Please sign in to comment.