Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
140 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,56 @@ | |||
var DOMText = require("./dom-text.js") | |||
var DOMElement = require("./dom-element.js") | |||
var DocumentFragment = require("./dom-fragment.js") | |||
var Event = require("./event.js") | |||
|
|||
module.exports = Document; | |||
|
|||
function Document() { | |||
if (!(this instanceof Document)) { | |||
return new Document(); | |||
} | |||
|
|||
this.body = this.createElement("body") | |||
this.documentElement = this.createElement("html") | |||
this.documentElement.appendChild(this.body) | |||
} | |||
|
|||
var proto = Document.prototype; | |||
proto.createTextNode = function createTextNode(value) { | |||
return new DOMText(value, this) | |||
} | |||
|
|||
proto.createElement = function createElement(tagName) { | |||
return new DOMElement(tagName, this) | |||
} | |||
|
|||
proto.createDocumentFragment = function createDocumentFragment() { | |||
return new DocumentFragment(this) | |||
} | |||
|
|||
proto.createEvent = function createEvent(family) { | |||
return new Event(family) | |||
} | |||
|
|||
proto.getElementById = function getElementById(id, parent) { | |||
if (!parent) { | |||
parent = this.body | |||
} | |||
|
|||
if (String(parent.id) === String(id)) { | |||
return parent | |||
} | |||
|
|||
var arr = parent.childNodes | |||
var result = null | |||
|
|||
if (!arr) { | |||
return result | |||
} | |||
|
|||
for (var i = 0, len = arr.length; !result && i < len; i++) { | |||
result = getElementById(id, arr[i]) | |||
} | |||
|
|||
return result | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,77 +1,3 @@ | |||
var DOMText = require("./dom-text.js") | var Document = require('./document.js'); | ||
var DOMElement = require("./dom-element.js") | |||
var DocumentFragment = require("./dom-fragment.js") | |||
var Event = require("./event.js") | |||
|
|
||
var body = createElement("body") | module.exports = new Document(); | ||
var documentElement = createElement("html") | |||
|
|||
documentElement.appendChild(body) | |||
|
|||
module.exports = Document() | |||
|
|||
function Document() { | |||
return { | |||
body: body, | |||
documentElement: documentElement, | |||
createTextNode: createTextNode, | |||
createElement: createElement, | |||
createDocumentFragment: createDocumentFragment, | |||
createEvent: createEvent, | |||
getElementById: getElementById, | |||
Document: Document, | |||
Text: DOMText, | |||
Element: DOMElement, | |||
DocumentFragment: DocumentFragment | |||
} | |||
} | |||
|
|||
function ownerDocument(that, node) { | |||
node.ownerDocument = that | |||
return node | |||
} | |||
|
|||
function createTextNode(value) { | |||
return ownerDocument(this, new DOMText(value)) | |||
} | |||
|
|||
function createElement(tagName) { | |||
return ownerDocument(this, new DOMElement(tagName)) | |||
} | |||
|
|||
function createDocumentFragment() { | |||
return ownerDocument(this, new DocumentFragment()) | |||
} | |||
|
|||
function createEvent(family) { | |||
return new Event(family) | |||
} | |||
|
|||
|
|||
/* | |||
* getElementById returns the Element whose ID is given by elementId. | |||
* If no such element exists, returns null. | |||
* Behavior is not defined if more than one element has this ID. | |||
*/ | |||
function getElementById(id, parent) { | |||
if (!parent) { | |||
parent = body | |||
} | |||
|
|||
if (String(parent.id) === String(id)) { | |||
return parent | |||
} | |||
|
|||
var arr = parent.childNodes | |||
var result = null | |||
|
|||
if (!arr) { | |||
return result | |||
} | |||
|
|||
for (var i = 0, len = arr.length; !result && i < len; i++) { | |||
result = getElementById(id, arr[i]) | |||
} | |||
|
|||
return result | |||
} |