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

TreeWalker nextSibling/previousSibling throw error when currentNode is set to another DOM tree #558

Closed
maxmilton opened this issue Aug 7, 2022 · 1 comment · Fixed by #559
Assignees
Labels
bug Something isn't working

Comments

@maxmilton
Copy link
Contributor

maxmilton commented Aug 7, 2022

When TreeWalker is walking a DOM tree it was not initialised with it throws an error when nextSibling/previousSibling are called and it reaches the (other DOM tree's) root.

Although the spec doesn't explicitly allow or deny it, it's actually possible to initialize a TreeWalker instance and then use the .currentNode setter to change to a completely different DOM tree and then walk it.

Given the following reproduction:

const { GlobalWindow } = require("happy-dom");

global.window = new GlobalWindow();
global.document = global.window.document;

const tree1 = document.createElement("div");
const tree2 = document.createElement("div");
const walker = document.createTreeWalker(tree1);

walker.currentNode = tree2;
walker.nextNode();

Running the code, this error is throw:

> happydom-tree-walker-repro@0.0.0 start
> node index.js

/home/max/Projects/_experiments/happydom-tree-walker-repro/node_modules/happy-dom/lib/tree-walker/TreeWalker.js:130
            const siblings = this.currentNode.parentNode.childNodes;
                                                         ^

TypeError: Cannot read properties of null (reading 'childNodes')
    at TreeWalker.nextSibling (/home/max/Projects/_experiments/happydom-tree-walker-repro/node_modules/happy-dom/lib/tree-walker/TreeWalker.js:130:58)
    at TreeWalker.nextNode (/home/max/Projects/_experiments/happydom-tree-walker-repro/node_modules/happy-dom/lib/tree-walker/TreeWalker.js:41:26)
    at Object.<anonymous> (/home/max/Projects/_experiments/happydom-tree-walker-repro/index.js:11:8)
    at Module._compile (node:internal/modules/cjs/loader:1120:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1174:10)
    at Module.load (node:internal/modules/cjs/loader:998:32)
    at Module._load (node:internal/modules/cjs/loader:839:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47

Node.js v18.7.0

I abuse use this behaviour in one of my libraries so I can reuse the same TreeWalker instance as a performance optimisation. Works as expected in browsers and jsdom, it's just happy-dom which throws.

maxmilton added a commit to maxmilton/happy-dom that referenced this issue Aug 7, 2022
capricorn86 added a commit that referenced this issue Oct 6, 2022
…nt-null

#558@patch: Additional TreeWalker checks for parentNode.
@capricorn86 capricorn86 added the bug Something isn't working label Oct 6, 2022
@capricorn86
Copy link
Owner

Thanks for contributing @maxmilton! 🙂

The fix you did has been released.

You can read more about the release here:
https://github.com/capricorn86/happy-dom/releases/tag/v7.0.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants