Closed Bug 6917 Opened 25 years ago Closed 25 years ago

[block] elements created via document.createElement() should have parentNode == null

Categories

(Core :: DOM: Core & HTML, defect, P3)

defect

Tracking

()

VERIFIED INVALID

People

(Reporter: waterson, Assigned: vidur)

References

Details

This is a problem with all HTML and XML elements, but is implemented correctly in XUL. See Sec 1.2 of Core DOM Level 1 spec; specifically, p. 28, "parentNode The parent of this node. All nodes, except Document [p.22], DocumentFragment[p.21], and Attr[p.37] may have a parent. However, if a node has just been created and not yet added to the tree, or if it has been removed from the tree, this is null." Our implementation (in nsGenericElement.cpp) returns the document as the parent.
Summary: elements created via document.createElement() should have parentNode == null → [block] elements created via document.createElement() should have parentNode == null
Target Milestone: M6
I need this to fix bug 6895. The change is trivial. See diffs. Index: nsGenericElement.cpp =================================================================== RCS file: /cvsroot/mozilla/layout/base/src/nsGenericElement.cpp,v retrieving revision 3.33 diff -c -r3.33 nsGenericElement.cpp *** nsGenericElement.cpp 1999/05/04 20:51:42 3.33 --- nsGenericElement.cpp 1999/05/22 00:11:55 *************** *** 259,273 **** res = mParent->QueryInterface(kIDOMNodeIID, (void**)aParentNode); NS_ASSERTION(NS_OK == res, "Must be a DOM Node"); } ! else if (nsnull == mDocument) { *aParentNode = nsnull; } - else { - // If we don't have a parent, but we're in the document, we must - // be the root node of the document. The DOM says that the root - // is the document. - res = mDocument->QueryInterface(kIDOMNodeIID, (void**)aParentNode); - } return res; } --- 259,267 ---- res = mParent->QueryInterface(kIDOMNodeIID, (void**)aParentNode); NS_ASSERTION(NS_OK == res, "Must be a DOM Node"); } ! else { *aParentNode = nsnull; } return res; }
QA Contact: 4015 → 3847
Status: NEW → RESOLVED
Closed: 25 years ago
Resolution: --- → INVALID
Ummm...I don't see this problem. An element created via document.createElement() does not have its document set and does return null as its parent. If it does have its document set, it's in the tree and must be the root node (in which case it's parent is the document). Are you doing something different for XUL nodes?
Status: RESOLVED → REOPENED
But see the ownerDocument field. Again, from the spec: "ownerDocument The Document object associated with this node. This is also the Document object used to create new nodes. When this node is a Document this is null. " As I read it, this implies that an element created via document.createElement() _always_ has an ownerDocument; specifically, the owner document that created the element. _Only_ in the case that the node actually _is_ a document is this field null. You seem to imply that an element _only_ has an ownerDocument when that element is _in_the_document_. According to the spec, you are wrong (see the second paragraph under "Interface Document"): "Since elements, text nodes, comments, processing instructions, etc. cannot exist outside the context of a Document, the Document interface also contains the factory methods needed to create these objects. The Node objects created have a ownerDocument attribute which associates them with the Document within whose context they were created." FWIW, I am looking at http://www.w3.org/TR/1998/REC-DOM-Level-1-19981001/DOM.txt. Is this out-of-date?
Status: REOPENED → RESOLVED
Closed: 25 years ago25 years ago
I just talked to Chris about this over the phone. The mDocument instance variable is no the same as ownerDocument. In fact, ownerDocument is not implemented correctly according to the spec (I have issues with this aspect of the spec and have brough them up in the DOM Working Group). The mDocument instance variable should only be set when the element is part of the document tree. Code in various other places depends on this assumption (see SetDocument in nsGenericElement for an example).
Blocks: 6895
Status: RESOLVED → VERIFIED
Verified.
Component: DOM → DOM: Core & HTML
You need to log in before you can comment on or make changes to this bug.