Home > Net >  Why does Attr inherit from Node in the DOM?
Why does Attr inherit from Node in the DOM?

Time:05-06

according to the DOM Core,

Attr objects inherit the Node interface, but since they are not actually child nodes of the element they describe, the DOM does not consider them part of the document tree.

and also

The DOM takes the view that attributes are properties of elements rather than having a separate identity from the elements they are associated with

Given this, what is the advantage of Attr objects inheriting from Node?

The DOM Spec states:

Attr nodes participate in a tree for historical reasons; they never have a (non-null) parent or any children and are therefore alone in a tree.

which is a partial answer, but I'm wondering if anyone has any deeper explination of Attr's Node inheritance and tree participation than simply "historical reasons".

Sorry if this is a duplicate question, most questions I've found revolve around the behavior of Attr objects, rather than the rationale behind their structure.

CodePudding user response:

With a "why" question, you need to be very clear whether you are asking

  • what are the potential benefits of this decision?, or

  • historically, what arguments were put forward and why did individual committee members vote the way they did (if indeed there was a discussion and a vote)?

In most cases the second question is unanswerable, though in a few cases (such as the Annotated XML specification by Tim Bray) you can at least see the rationale put forward by one of the editors.

To me the decision feels like a compromise. Attributes have many properties in common with other nodes, so there's an advantage in having them implement the same interface; but there's baggage that you don't really want because it might add unnecessary cost, like having a link to the containing element.

Frankly, DOM is not a good piece of design, and it's very hard to say why it made so many design mistakes, other than that it was put together by a committee in a hurry. Later models such as JDOM and XOM are much better, but sadly not available for Javascript.

  • Related