DOM Implementation and Scriptability
From MDC
[edit] What is the DOM?
- API for access and manipulation of a markup document
- Started as DOM0, NS3 & IE3
- Evolved into W3C DOM Level 1 Core + HTML
- Kept going to Level 2 and Level 3...
[edit] What's standardized?
- The document object
- Elements, Text nodes, Comment, Style objects...
- HTML DOM
- Range, Traversal
- ...
[edit] What's not?
- The window object
- Proprietary properties
document.all
document.location
element.offset*
element.innerHTML
element.scroll*
...
- JS-isms
- Variable number and types of arguments
- Dynamic element lookup on the document object and form objects
- Scoping and lookup order
[edit] The Mozilla DOM
- Tries to be as W3C DOM compliant as possible
- Tries to be as backwards compatible as possible (except for document.layers)
- Mozilla DOM objects are XPCOM objects
- XPIDL interfaces, "nsIDOM" prefixed, some already frozen
- DOM objects are refcounted, DOM object wrapper lifetime is managed by the JS GC.
[edit] The Mozilla document tree
- Document classes
- nsDocument, nsHTMLDocument, and nsXULDocument
- Internal interface is nsIDocument
- Element classes
- nsGenericElement (nsGenericContainerElement) nsHTML*Element, nsXMLElement, nsXULElement
- Internal interface is nsIContent n' friends
- Data node classes
- nsGenericDOMDataNode
- nsTextNode, nsCommentNode, nsCDATASection, nsProcessingInstruction, nsDocumentType, ...
- Internal interface is nsITextContent (nsIContent)
- "Tearoff" classes
- 3Node and EventTarget on elements and data nodes
[edit] The Mozilla "window" object
- nsIDOMWindow, frozen official interface
- nsIDOMWindowInternal, internal interface, not frozen
- nsIDOMChromeWindow, chrome specific window interface
- nsIDOMJSWindow, JS specific window hooks
[edit] Scriptability in Mozilla
- Used to be done with static generated C/C++ code (IDLC) that hooked the DOM objects into the JS environment.
- Now done using XPConnect and additional JS specific support code.
[edit] XPConnect and the DOM
- XPConnect - the XPCOM/JS glue
- Deals with the refcount/GC memory management models
[edit] nsIClassInfo
Exposes:
- Class name
- Implemented interfaces
- Language specific helpers
- Class flags (threading, singleton, ...)
[edit] Scriptable helpers
- nsIXPCScriptable (XPConnect hooks, JS specific)
- Dynamic name resolution
- Property get/set/enum/... hooks
[edit] Scriptable helpers, wrapper parenting
- JS object parent chain to match DOM object parent chain
- DOM objects w/o a parent are parented at the global object
[edit] Scriptable helpers, wrapper prototype setup
- Initial DOM wrapper prototype chain (done by XPConnect)
XPC wrapper -> XPC wrapper proto -> Object
- DOM wrapper prototype chain done by the helper
XPC wrapper -> XPC wrapper proto -> [class proto]* -> Object
[edit] Security and the DOM
- Default security model is same-origin
- Security checks done on all objects
- Special security code for access to script globals
[edit] The Mozilla DOM vs. the IE DOM
- No
document.allbefore Gecko 1.7.5 (undetectabledocument.allwas implemented for quirks mode documents - bug 248549) - No
window.event, and different interface for event objects - IE exposes elements by id in the global namespace (we do that too since Gecko 1.7.5 for quirks mode documents - bug 256932)
- Different property lookup order (i.e. form.submit() and <input name="submit">)