Skip to content

Commit

Permalink
feat: filtration common pseudo-selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
ala-n committed Mar 9, 2021
1 parent af9d08d commit 8b4d80e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type CollectionProcessor = (els: Element[], sel: string) => Element[];
* - ::parent and ::child pseudo-selectors
* - ::find pseudo-selector
* - ::first, ::last and :nth(#) limitation pseudo-selectors
* - ::filter, ::not filtration pseudo-selectors
*
* @example "#id .class [attr]" - find by CSS selector in a current document
* @example "" - get current base element
Expand All @@ -24,6 +25,8 @@ type CollectionProcessor = (els: Element[], sel: string) => Element[];
* @example "::parent(#id .class [attr])" - find the closest parent matching passed selector
* @example "::child(#id .class [attr])" - find direct child element(s) that match passed selector
* @example "::find(#id .class [attr])" - find child element(s) that match passed selector
* @example "::find(buttons, a)::not([hidden])" - find all buttons and anchors that are not have hidden attribute
* @example "::find(buttons, a)::filter(:first-child)" - find all buttons and anchors that are first child in container
* @example "::parent::child(some-tag)" - find direct child element(s) that match tag 'some-tag' in the parent
* @example "#id .class [attr]::parent" - find parent of element matching selector '#id .class [attr]' in document
* @example "::find(.row)::last::parent" - find parent of the last element matching selector '.row' from the base element subtree
Expand All @@ -42,7 +45,9 @@ export class TraversingQuery {
'::nth': (list: Element[], sel?: string) => {
const index = sel ? +sel : NaN;
return wrap(list[index - 1]);
}
},
'::not': (list: Element[], sel?: string) => list.filter((el) => !el.matches(sel || '')),
'::filter': (list: Element[], sel?: string) => list.filter((el) => el.matches(sel || ''))
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ describe('Traversing Query tests', () => {
['::find(.btn)::nth(8)', root, []],
['::find(.btn)::nth(bla bla)', root, []],

// Filters
['::find(.btn)::filter(.un-existing)', root, []],
['::find(.btn)::not(button)', root, []],
['::find(.btn)::filter([data-test])', root, [btn2, btn5, btn6]],
['::find(.btn)::not([data-test])', root, [btn1, btn3, btn4]],
['::find(.btn)::filter(:first-child)', root, [btn1]],
['::find(.btn)::not(:first-child)', root, [btn2, btn3, btn4, btn5, btn6]],

// Complex query test
['::parent::child', root, [root]],
['::parent::find(.btn)', btn5, [btn1, btn2, btn3, btn4, btn5]],
Expand Down

0 comments on commit 8b4d80e

Please sign in to comment.