fix: correctly traverse ancestor chain in Node.contains#931
fix: correctly traverse ancestor chain in Node.contains#931karfau merged 1 commit intoxmldom:masterfrom yoshi389111:fix_contains_infinite_loop
Conversation
…r descendant nodes
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a critical bug in the Node.contains() method where the function would enter an infinite loop when checking if a node contains a deeper descendant. The issue was caused by incorrectly resetting the parent pointer to the same node in each iteration instead of traversing up the ancestor chain.
- Fixed the infinite loop bug by correcting the parent traversal logic
- Added test coverage for the descendant containment scenario
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| lib/dom.js | Fixed the bug by changing parent = other.parentNode to parent = parent.parentNode to properly traverse the ancestor chain |
| test/dom/node.test.js | Added test case and setup to verify that contains() works correctly for deeper descendants |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #931 +/- ##
=======================================
Coverage 95.12% 95.12%
=======================================
Files 8 8
Lines 2196 2196
Branches 577 577
=======================================
Hits 2089 2089
Misses 107 107 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Awesome, thx for your finding and contribution! |
|
If you are up for it you can also provide a pull request with a backport for the 0.8 version, but I can also take care of it. |
|
Thanks for the suggestion! I think I’ll pass on the backport this time, but I appreciate the offer. |
|
FYI: The method |
Hello, and thank you for maintaining this project!
I found a small bug in
Node.contains()and would like to propose a fix.Description
Node.contains()currently does not traverse the ancestor chain correctly, and therefore does not work correctly when the reference node is a deeper descendant.Problem:
The current implementation resets parent to
other.parentNodein each loop iteration, so it only checks the immediate parent and in an infinite loop.Fix:
Assign
parent = parent.parentNodeinstead, so the loop correctly walks up the ancestor chain.Reproduction Example:
Thank you for reviewing this pull request! I hope this helps improve the library.