Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #40] add parent and ancestor selectors #90

Merged
merged 1 commit into from
Oct 16, 2024

Conversation

Mertzenich
Copy link
Contributor

@Mertzenich Mertzenich commented Mar 23, 2024

Description

Implements parent and ancestor selectors, should close #40. These functions act as natural counterparts to both child and descendant:

  • child returns the node at the end of a direct chain of direct child relationships, parent returns the node at the start of a direct chain of direct child relationships.
  • descendant returns the node at the end of a chain of descendant relationships, ancestor returns the node at the start of a chain of descendant relationships.

Three additional functions were added: compose-unary, parent, and ancestor. The compose-unary function implements the functionality utilized by the latter two functions. I added tests for parent and ancestor.

compose-unary

Takes a unary selector function, such as has-child or has-descendant, and any number of selectors. Each of the selectors are composed together using and and the provided unary selector function. For example, running (compose-unary has-child (tag :div) (class :foo) (attr :disabled)) produces the equivalent of:

(and (tag :div)
     (has-child (and (class :foo)
                     (has-child (and (attr :disabled))))))

parent and ancestor

Given any number of selectors, applies compose-unary to the selectors using has-child and has-descendant respectively, that is:

(apply compose-unary has-child selectors)
(apply compose-unary has-descendant selectors)

Examples

These examples are those contained in the docstrings and tests.

parent

<div><span class="foo"><input disabled></input></span></div>
<div><span class="foo"><b><input disabled></input></b></span></div>

(parent (tag :div) (class :foo) (attr :disabled)) will select the div in the first but not the second.

ancestor

<div><span class="foo"><input disabled></input></span></div>
<div><span class="foo"><b><input disabled></input></b></span></div>

(ancestor (tag :div) (class :foo) (attr :disabled)) will select the div in both.

@slipset slipset changed the title feat: add parent and ancestor selectors [Fix #40] add parent and ancestor selectors Oct 16, 2024
@slipset slipset merged commit 27ee318 into clj-commons:master Oct 16, 2024
1 check passed
@Mertzenich Mertzenich deleted the parent-ancestor branch October 16, 2024 16:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Counterparts of child and descendant for first element in the chain
2 participants