JackWM
JackWM

Reputation: 10535

How does namespace affect the DOM tree building?

As I know, the XML supports namespace to resolve the conflicts between element's names from different specifications.

Question 1: Does HMTL support namespace?

Question 2: How does namespace affect the DOM tree building in terms of the tree structure?

A simple example is the best.

Upvotes: 0

Views: 137

Answers (1)

Alohci
Alohci

Reputation: 82976

The below applies to HTML5 parsing. It does not necessarily apply to legacy standalone HTML parsers.

Question 1: Does HTML support namespace?

In the text/html syntax, you have no control. Elements are placed in different namespaces based on their name and on their ancestor elements as they are added to the DOM by the parser.

You can control the namespace of new elements added via javascript using document.createElementNS.

Namespaces can be used in the application/xhtml+xml syntax, but there are no new HTML5-conforming elements available than are not available in the text/html syntax.

Question 2: How does namespace affect the DOM tree building? A simple example is the best.

In the text/html syntax

<div>
  <svg>
    <script ...></script>
  </svg>
  <script ...></script>
</div>  

The div element is known to be an HTML element so that is placed in the http://www.w3.org/1999/xhtml namespace.

The svg element is known to be an SVG element so that is placed in the http://www.w3.org/2000/svg namespace.

The first script element is inside the svg element, so it is placed in the http://www.w3.org/2000/svg namespace.

The second script element is not inside any special element so it is placed in the http://www.w3.org/1999/xhtml namespace

Tags that are parsed into elements in the SVG or MathML namespaces may use self-closing syntax for all such elements in the manner of XML. Self closing syntax has no effect for tags that form HTML namespaced elements. Using /> to end the tags of void HTML elements is permissible but has no effect different from using > to end the tag.

Upvotes: 2

Related Questions