Skip to content

chore: drop parentNode property #2113

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

Merged
merged 1 commit into from
May 1, 2025
Merged

Conversation

SethFalco
Copy link
Member

@SethFalco SethFalco commented May 1, 2025

The parentNode property was declared legacy and pending removal during v4, but was still used internally in a few places.

This officially drops XastNode#parentNode so that it does not exist in public nor internal APIs.

This may have an impact on consumers who created custom plugins and imported our style/xast helpers like:

  • computeOwnStyle
  • querySelectorAll
  • querySelector
  • matches

Previously, these functions had the context of the whole node tree, even if a child node was passed to it. It is no longer has that context by default.

Usage is unaffected in the following conditions:

  • The first parameter (node) is a root node (XastRoot).
  • The second parameter (selector) only needs to check children of the first parameter, and so selectors that navigate across nested nodes aren't used, such as or >.

In other cases, a helper has been provided called #mapNodesToParents which maps every node in a tree to its parent node.

For example, you can do:

// applies `selector` with context only of the node and its decendants
const nodes = querySelectorAll(node, selector);

// applies `selector` with the context of the entire node tree relative from `node`
const nodes = querySelectorAll(node, selector, mapNodesToParents(root));

// this usage has the same behavior as v3, as `node` is already the entire node tree 
const nodes = querySelectorAll(root, selector);

Changes

  • Drops XastNode#parentNode from internal API.
  • computeOwnStyle, querySelectorAll, querySelector, and matches accept an additional optional argument which is a map of children to parent nodes.
  • In relation to above, a helper function called mapNodesToParents is now also exported, to make it simple to create the optional parameter for the most common usage. By accepting a Map directly instead of the root, it also avoids the need to revisit every node in cases where they've already been visited by the caller.
  • Reorganizes css-select Options so that instead of referencing the legacy parentNode, it now receives the map of children to parents.

Improvements

I would prefer to invoke #mapNodesToParents lazily, only if getParent is invoked and parents is undefined. However, that would mean invoking #mapNodesToParents in css-select-adapter, which would introduce a circular dependency.

For now, I'll leave this as-is and in future I will investigate restructure to avoid that issue.

@SethFalco SethFalco merged commit 9eb3af5 into svg:main May 1, 2025
13 checks passed
@SethFalco SethFalco deleted the drop-parent-node branch May 1, 2025 14:59
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.

1 participant