What exactly does document.normalize do?

I read that document.normalize Removes empty text nodes and connects adjacent nodes .

What does it mean?

+6
source share
2 answers

The text node will be as follows:

 <p> <span>foo</span> bar </p> 

<p> is a node, <span> is a node, so what is a "bar" ? β†’ This is the text node.

Using the DOM API, you can create empty text or two adjacent text nodes:

 var wrapper = document.createElement("div"); wrapper.appendChild(document.createTextNode("Part 1")); wrapper.appendChild(document.createTextNode("Part 2")); 

In HTML, which will be just <div>Part 1Part2</div> , but for the DOM, these are two separate text nodes that are ... weird.

Node.normalize normalizes this to get rid of such ineffective anomalies; it will merge both text nodes into one and delete the text nodes that are completely empty.

+6
source

Given:

 p element text node containing "" p element text node containing "Hello, " text node containing "world" 

He converts it to

 p element p element text node containing "Hello, world" 

The text node inside the paragraph itself, without content, is completely deleted.

Two adjacent text nodes are combined into one text node with combined text.

+5
source

All Articles