On Tue, Jan 13, 2015 at 06:42:29PM -0500, Karl Dahlke wrote: > > the function returns the created element to javascript > > which then inserts it into the DOM in the correct position, > > Yes, but that's just part of it. > Each tag/element is put in the tree in its correct position, > and also put in the list. > It is linked in two places. > That's how it works when produced by html. >
creates a form object, which is somewhere in the dom tree, > and *also* in the list of forms. > I guessed, not much more than a guess, that that is how it works > for elements created dynamically. > An element e is placed somewhere in dom, as directed by javascrtip, > and also put on the list of said elements, > that part perhaps happening automatically. > So I made the second part happen automatically, again being just a guess. > Maybe the only way to know if my guess is right is to write some javascrtip, > createElement, > and alert document.list.length, and run it in a commercial browser No, the w3c has a very complete DOM spec, and if you look on w3schools.com you'll find not just a complete DOM reference but also javascript examples, including non-standard extensions and some notes on different browsers. There's actually a *lot* of information on how this stuff *should* work and how different browsers do things, no need for massive amounts of guess work if we pick our resources correctly. Of course there'll be the odd browser-specific thing we may think about incorperating, but for the core DOM stuff and most of the stuff things like jquery expect, there're standards and code examples. I've even seen implementation notes and things about the guts of DOM. As for inserting elements into lists, when parsing html appending elements to the appropriate lists works because the element's place in the DOM is dictated by the document. However, in javascript, all document.createElement does is give you the correct object, it doesn't link the element into the document in any way. That's the job of the script creating the element. For example if I have the following html:
first div
second div
third div
And use javascript to add a div saying "test div" between the 2nd and 3rd divs, then I'd expect it to appear at index 2 in document.divs as if it was created by html. However, in our implementation it'd actually appear at index 3 in document.divs, but be at index 2 in the $kids$ array of the body node. Also, from reading the w3schools DOM reference, it appears that all DOM element nodes (i.e. html, head, body, div etc) should inherit from Element, and that we should have text nodes for text, attribute nodes for tag attributes and even comment nodes for comments. Cheers, Adam.