Skip to curriculum
Jump to the editor

16.10. Are two DOM trees identical?

Medium
Topics

This question is in

16. DOM: finding elements
20–30 min

Is 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

JAVASCRIPT
function isSameTree(a, b) // returns true or false
  • a, b: the roots of two trees (elements, text nodes, comments or document fragments), or null.
  • Returns true when the trees are identical. Always a boolean.

Requirements

  1. A tree is identical to its clone (cloneNode(true)) and to itself.
  2. 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.
  3. Elements must have the same name in the same namespace: an SVG <title> is not an HTML <title>.
  4. 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 not class="b a").
  5. Only attributes count, not properties: an input the user typed into still matches its clone.
  6. Text nodes and comments must have the same text.
  7. 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".
  8. A document fragment is compared by its children only.
  9. null is never identical to a node; isSameTree(null, null) is true.
  10. It works 2,000 levels deep, and when Node.prototype.isEqualNode does not exist.

Examples

JAVASCRIPT
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
JAVASCRIPT
// 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>      → false
JAVASCRIPT
const input = document.createElement("input");
const copy = input.cloneNode(true);
copy.value = "typed";              // a property, not an attribute
isSameTree(input, copy);           // true

Environment

  • Plain JavaScript in a browser page; the tests call the global isSameTree from same-tree.js.
  • isEqualNode and isSameNode are not allowed: one test removes them while it runs.
  • Other node types (doctypes, processing instructions) are out of scope.

Hints

  1. Comparing two trees is comparing two roots, then comparing pairs of smaller trees. What does that suggest about the shape of the function?
  2. 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
Back to the question
Loading the editor…