document.querySelectorAll returns stale results
See original GitHub issueBasic info:
- Node.js version: v8.11.4
- jsdom version: v13.2.0 (bug is also present in v11.12.0)
Minimal reproduction case
const jsdom = require('jsdom');
const {JSDOM} = jsdom;
function append(html) {
document.body.innerHTML = html;
}
function clean() {
document.body.innerHTML = '';
}
const dom = new JSDOM(`<!DOCTYPE html>`);
const document = dom.window.document;
const fixtures = {
a: '<a id="link-a" class="test">a link</a>',
b: '<a id="link-b" class="test"><img src="foo.jpg" /></a>'
};
append(fixtures.a);
console.log(document.body.innerHTML);
// should log "link-a"
document.querySelectorAll('a.test').forEach(node => console.log(node.id));
clean();
append(fixtures.b);
console.log(document.body.innerHTML);
// should log "link-b"
document.querySelectorAll('a.test').forEach(node => console.log(node.id));
clean();
append(fixtures.a);
console.log(document.body.innerHTML);
// should log "link-a", but logs "link-b"
document.querySelectorAll('a.test').forEach(node => console.log(node.id));
clean();
How does similar code behave in browsers?
https://jsbin.com/fobuxoceva/edit?js,console
Pretty much what the title says. document.querySelectorAll
returns Nodes which have been removed already. I suspect this is an issue of memoization of queries: If the last querySelectorAll('a.test')
is changed to querySelectorAll('.test')
(parameters are different from previous calls), it works as expected. AFAIK memoization for querySelectorAll
has been removed here: https://github.com/jsdom/jsdom/commit/bfb08434f540491591aace17b302816f146dd4ef
Context
We discovered this while move old jasmine tests over to jest
which uses jsdom. I guess adding and removing DOM nodes between unit tests isn’t anything exotic.
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Document.querySelectorAll() - Web APIs | MDN
The Document method querySelectorAll() returns a static (not live) NodeList representing a list of the document's elements that match the ...
Read more >javascript - QuerySelectorAll returns more than expected
You can use this selector: document.querySelectorAll(".main [class='sub']"). You can iterate over result items as follows:.
Read more >HTML DOM Document querySelectorAll() Method
The querySelectorAll() method returns all elements that matches a CSS selector(s). The querySelectorAll() method returns a NodeList.
Read more >Javascript: Manipulating DOM (HTML & CSS) - Mr B
Returns a variable containing a reference to the first matching location within your HTML document as determined by your selector_string .
Read more >HTML | DOM querySelectorAll() Method
The querySelectorAll() method in HTML is used to return a collection of an ... < body style = "text-align: center;" > ... var...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Some discussion going on in https://github.com/dperini/nwsapi/pull/23; hopefully that gets fixed in nwsapi as this kind of breaking change in a minor version is not really OK 😦.
@sscaetite This looks correct and unrelated to this issue. You shouldn’t trust console.log to display the contents of your
div
s. Try to log the actual HTML usingnode.outerHTML
.