chore: drop parentNode property #2113
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
node
) is a root node (XastRoot
).selector
) only needs to check children of the first parameter, and so selectors that navigate across nested nodes aren't used, such as>
.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:
Changes
XastNode#parentNode
from internal API.computeOwnStyle
,querySelectorAll
,querySelector
, andmatches
accept an additional optional argument which is a map of children to parent nodes.mapNodesToParents
is now also exported, to make it simple to create the optional parameter for the most common usage. By accepting aMap
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.css-select
Options
so that instead of referencing the legacyparentNode
, it now receives the map of children to parents.Improvements
I would prefer to invoke
#mapNodesToParents
lazily, only ifgetParent
is invoked andparents
is undefined. However, that would mean invoking#mapNodesToParents
incss-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.