Skip to content

Commit

Permalink
allow selectors concatenation
Browse files Browse the repository at this point in the history
  • Loading branch information
ro0gr committed Jan 20, 2024
1 parent 607f9c4 commit 2d190c6
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
22 changes: 17 additions & 5 deletions addon/src/-private/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,26 @@ export class Query {
? fullPath.length - 1
: fullPath.findLastIndex((i) => typeof i.resetScope === 'boolean');

const allLocators = (
const locators = (
resetScopeIndex === -1 ? fullPath : fullPath.slice(resetScopeIndex)
)
.map((p) => p.scope)
.filter(Boolean);

this._locators = allLocators.length
? allLocators
.filter(Boolean)
.reduce((acc, locator) => {
if (acc.length === 0) {
return [locator];
}

const lastLocator = acc[acc.length - 1];
if (lastLocator.canConcat(locator)) {
return [...acc.slice(0, -1), lastLocator.concat(locator)];
}

return [...acc, locator];
}, []);

this._locators = locators.length
? locators
: [new Locator(':first-child', { at: 0 })];
}

Expand Down
12 changes: 12 additions & 0 deletions addon/src/-private/query/locator.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ export default class Locator {

return `${selector}:${modifiers.join(':')}`;
}

canConcat(locator) {
if (this.filters) {
return false;
}

return this.selector.canConcat(locator.selector);
}

concat(locator) {
return new Locator(this.selector.concat(locator.selector), locator.filters);
}
}

function getDefaultQuerySelectorClass() {
Expand Down
9 changes: 9 additions & 0 deletions addon/src/-private/query/selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ export class QuerySelector {
toString() {
return this.selector.toString();
}

// eslint-disable-next-line no-unused-vars
canConcat(/* selector */) {
return false;
}

concat(/* selector */) {
throw new Error('`concat` method is not implemented');
}
}

export function isQuerySelector(selector) {
Expand Down
8 changes: 8 additions & 0 deletions addon/src/-private/query/selectors/jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ export default class JQueryQuerySelector extends QuerySelector {

return $(selector, containerElement).toArray();
}

canConcat(selector) {
return selector instanceof JQueryQuerySelector;
}

concat(selector) {
return new JQueryQuerySelector(`${this.selector} ${selector.selector}`);
}
}

function validate(selector) {
Expand Down

0 comments on commit 2d190c6

Please sign in to comment.