Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference in DOM levels, and how do they interrelate?

Tags:

I often hear about "DOM level 1", "DOM level 2", "DOM level 3" and "DOM level 4" and realized that I don't know the difference between any of them or how they relate to each other.

I know the very basics - DOM is Document Object Model, and is what provides access for scripting languages (particularly, but as far as I know, not limited to various versions of ECMAScript, such as ECMAScript 5.1) to access elements of an HTML document. (Some sites I read - such as the dom introduction on quirksmode - say that it's for any XML document, but HTML is a sufficient subset.)

The dates on w3c's DOM technical reports seem to imply that each subsequent DOM level supersedes the previous ones.

Sadly, the best reference I've found to provide clarification has been wikipedia, which seems to say the same - the Standardization section says subsequent levels "added" extra functionality, while not mentioning removing anything.

Now, for my questions, which may be rapid fire, but hopefully express the general state of my ignorance:

  • What's the relation of one DOM level to another?
  • Are lower level DOMs complete subsets of higher level DOMs? Has any functionality been removed as the DOM level advances? When I see statements like The level 1 DOM will work fine on an HTML document and In the Level 1 DOM, each object, whatever it may be exactly, is a Node (both from the quirksmode intro), does this imply that such statements are true for levels 2, 3 and 4? (These are all kind of the same question, just asked different ways)
  • Is citing DOM level really little more than a shorthand way of how modern a user agent must be for a particular function to work?

Obviously, I can study each specification off of the w3c's DOM technical reports, but was hoping to get answers from those with first-hand experience. Just by glancing at the changes section of the spec for DOM level 3, I see that most of the changes from 2 to 3 were additions, though some of the key implementations in the Node interface have changed. Did these changes break anything?

I'd like to do more than just nod sagely next time someone tells me, "Oh, that's DOM level 2, so it's ok," so would welcome any references I have missed or firsthand information that I didn't glean from my research.

like image 868
Scott Mermelstein Avatar asked Dec 02 '13 17:12

Scott Mermelstein


People also ask

What are the different DOM Levels?

DOM Levels are essentially versions. DOM Level 1 defines the core elements of the Document Object Model. DOM Level 2 extends those elements and adds events. DOM Level 3 extends DOM lvl 2 and adds more elements and events.

What are the 3 parts of DOM?

The DOM is separated into three parts: Core, HTML, and XML.

What is DOM and how does it relate to XML?

The XML Document Object Model (DOM) class is an in-memory representation of an XML document. The DOM allows you to programmatically read, manipulate, and modify an XML document. The XmlReader class also reads XML; however, it provides non-cached, forward-only, read-only access.

What is DOM explain in detail?

What is the DOM? The Document Object Model (DOM) is a programming interface for web documents. It represents the page so that programs can change the document structure, style, and content. The DOM represents the document as nodes and objects; that way, programming languages can interact with the page.


2 Answers

First, I'll relate a message from MDN's writeup of DOM levels (emphasis in original):

The DOM used to be written as a set of levels. That is no longer the case. These days it is maintained as the DOM Living Standard. This page provides an historical overview of the olden days.

This is confirmed in a W3C document called "W3C DOM4". We might take that to mean "DOM Level 4", and assume it adds an additional DOM level, but the text of the specification actually says:

This document is published as a snapshot of the DOM Living Specification.

So, this is a historical discussion, but still one worth having.

A "DOM Level" was a collection of specifications that described DOM objects, methods, and behaviors. Higher levels of the DOM specification built on the previous levels. Changes happened in two ways:

  1. The addition of a totally new specification category (e.g., Level 3 adds "Validation" and "Load and Save" specifications, which did not exist in Level 2)

  2. The modification of an existing specification category (e.g. updating the "Core" spec)

Obviously, the first type of change was purely additive, rather than subtractive. The second kind of change also seems to have been nearly exclusively additive, probably because the W3C was interested in preserving backward compatibility with previous versions.

Changes that are not backward-compatible tend to be rare and fairly minor. The Document.doctype change you cite, for example, was actually largely additive. Level 3 added the sentence:

For HTML documents, a DocumentType object may be returned, independently of the presence or absence of document type declaration in the HTML document.

This simply gave greater flexibility to allow DOM implementations to add in a doctype in HTML when the author omitted a <!DOCTYPE>. The only functionality this would break is the ability to programmatically detect the presence of an author-specified doctype, which doesn't seem to be particularly valuable.

Probably the reason you've heard someone say, "Oh, that's DOM level 2, so it's ok," is because DOM level 2 is more widely supported than DOM level 3. In some cases, this isn't even a question of old browser support: Firefox marked their lack of support for the DOM 3's "Load and Save" specification as WONTFIX. All Level 2 specifications, by contrast, are supported pretty well by modern browsers, and enjoy support from much older browsers (since Level 2 is four years older than level 3).

like image 112
apsillers Avatar answered Sep 19 '22 17:09

apsillers


Just a couple of notes on DOM4 to add to apsillers' answer:

... In the Level 1 DOM, each object, whatever it may be exactly, is a Node ..., does this imply that such statements are true for levels 2, 3 and 4?

That's a definite no. Attributes in DOM4 are not Nodes.

DOM4 makes a number of significant non-backward compatible changes. The attributes are not nodes change is a big one if you're not using javascript or a duck typed language. Also document.createElement() on a XML document will create the element in the http://www.w3.org/1999/xhtml namespace, where earlier levels create the element in no namespace. Browsers have long done this, but typical XML oriented DOM implementations have used the DOM3 and earlier way. That's a big shift if you migrate from a DOM3 implementation to a DOM4 one in a non-browser context.

like image 36
Alohci Avatar answered Sep 21 '22 17:09

Alohci