16.10. Are two DOM trees identical?
Topics
This question is in
16. DOM: finding elementsIs the markup a component rendered the same as last time? Did a sanitizer change anything? Does a server-rendered page match what the client would render? All of these ask whether two DOM trees are identical: the same kinds of nodes, with the same names, attributes and text, in the same order, all the way down. Comparing outerHTML strings looks like an answer but is not: it treats attributes in a different order as different, and an SVG <title> and an HTML <title> as the same.
Write isSameTree(a, b) in same-tree.js. It returns the same answer as a.isEqualNode(b), without calling it.
Signature
function isSameTree(a, b) // returns true or falsea,b: the roots of two trees (elements, text nodes, comments or document fragments), ornull.- Returns
truewhen the trees are identical. Always a boolean.
Requirements
- A tree is identical to its clone (
cloneNode(true)) and to itself. - Two nodes must be of the same type: an element, a text node and a comment are never identical to each other, even with the same text.
- Elements must have the same name in the same namespace: an SVG
<title>is not an HTML<title>. - Elements must have the same set of attributes (same namespace, name and exact value). The order of attributes does not matter; the text of a value does, exactly (
class="a b"is notclass="b a"). - Only attributes count, not properties: an input the user typed into still matches its clone.
- Text nodes and comments must have the same text.
- Both nodes must have the same number of child nodes, and each pair of children, in order, must be identical. Whitespace text nodes count, and one text node
"ab"is not two text nodes"a","b". - A document fragment is compared by its children only.
nullis never identical to a node;isSameTree(null, null)istrue.- It works 2,000 levels deep, and when
Node.prototype.isEqualNodedoes not exist.
Examples
const a = document.createElement("div");
a.innerHTML = '<img src="cat.png" alt="Cat"><p>Hi <b>there</b></p>';
const b = a.cloneNode(true);
isSameTree(a, b); // true
b.querySelector("img").setAttribute("alt", "Dog");
isSameTree(a, b); // false// attribute order does not matter
// <img src="x" alt="y"> and <img alt="y" src="x"> → true
// whitespace does
// <ul><li>a</li></ul> and <ul> <li>a</li> </ul> → falseconst input = document.createElement("input");
const copy = input.cloneNode(true);
copy.value = "typed"; // a property, not an attribute
isSameTree(input, copy); // trueEnvironment
- Plain JavaScript in a browser page; the tests call the global
isSameTreefromsame-tree.js. isEqualNodeandisSameNodeare not allowed: one test removes them while it runs.- Other node types (doctypes, processing instructions) are out of scope.
Hints
- Comparing two trees is comparing two roots, then comparing pairs of smaller trees. What does that suggest about the shape of the function?
- How can you find "the same attribute" in the other element without depending on the order of
attributes?
The explanation comes with the course
A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.
- Every line explained
- The mistakes each test catches
- Links to the lessons behind it