Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refactor & proper type definition

  • Loading branch information...
commit cbec444f9f5d6a69a7ffe53182dc6327dc4b0429 1 parent b9ceef7
@Raynos authored
View
95 docs.mli
@@ -1,10 +1,16 @@
type DOMText := {
data: String,
type: "DOMTextNode",
+ length: Number,
nodeType: 3,
- constructor: (value: String) => DOMText,
- toString: () => String
+ toString: (this: DOMText) => String,
+ replaceChild: (
+ this: DOMText,
+ index: Number,
+ length: Number,
+ value: String
+ ) => void
}
type DOMNode := DOMText | DOMElement | DocumentFragment
@@ -19,13 +25,16 @@ type DOMElement := {
style: Object<String, String>,
type: "DOMElement",
nodeType: 1,
+ ownerDoucment: null | Document,
- constructor: (tagName: String) => DOMElement,
- appendChild: (child: DOMNode),
- removeChild: (child: DOMChild),
- replaceChild: (elem: DOMNode, needle: DOMChild),
+ appendChild: (this: DOMElement, child: DOMChild),
+ replaceChild:
+ (this: DOMElement, elem: DOMChild, needle: DOMChild),
+ removeChild: (this: DOMElement, child: DOMChild),
+ addEventListener: addEventListener,
+ dispatchEvent: dispatchEvent,
focus: () => void,
- toString: () => String
+ toString: (this: DOMElement) => String
}
type DocumentFragment := {
@@ -34,25 +43,71 @@ type DocumentFragment := {
type: "DocumentFragment",
nodeType: 11,
nodeName: "#document-fragment",
+ ownerDoucment: Document | null,
- constructor: () => DocumentFragment,
- appendChild
- replaceChild
- removeChild
- toString: () => String
+ appendChild: (this: DocumentFragment, child: DOMChild),
+ replaceChild:
+ (this: DocumentFragment, elem: DOMChild, needle: DOMChild),
+ removeChild: (this: DocumentFragment, child: DOMChild),
+ toString: (this: DocumentFragment) => String
}
type Document := {
body: DOMElement,
+ documentElement: DOMElement,
- Document: () => Document,
- Text: (value: String) => DOMText,
- Element: (tagName: String) => DOMElement,
- DocumentFragment: () => DocumentFragment,
- createTextNode: (value: String) => DOMText,
- createElement: (tagName: String) => Element,
- createDocumentFragment: () => DocumentFragment,
- getElementById: (id: String) => null | Element
+ createTextNode: (this: Document, value: String) => DOMText,
+ createElement: (this: Document, tagName: String) => DOMElement,
+ createDocumentFragment: (this: Document) => DocumentFragment,
+ createEvent: () => Event,
+ getElementById: (
+ this: Document,
+ id: String,
+ parent?: DOMElement
+ ) => null | DOMElement
}
+type Event := {
+ type: String,
+ bubbles: Boolean,
+ cancelable: Boolean,
+
+ initEvent: (
+ this: Event,
+ type: String,
+ bubbles: Boolean,
+ cancelable: Boolean
+ ) => void
+}
+
+type addEventListener := (
+ this: DOMElement,
+ type: String,
+ listener: Listener
+) => void
+
+type dispatchEvent := (
+ this: DOMElement,
+ ev: Event
+)
+
+min-document/event/add-event-listener := addEventListener
+
+min-document/event/dispatch-event := dispatchEvent
+
+min-document/document := () => Document
+
+min-document/dom-element :=
+ (tagName: String, owner?: Document) => DOMElement
+
+min-document/dom-fragment :=
+ (owner?: Document) => DocumentFragment
+
+min-document/dom-text :=
+ (value: String, owner?: Document) => DOMText
+
+min-document/event := () => Event
+
+min-document/serialize := (DOMElement) => String
+
min-document := Document
View
56 document.js
@@ -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
+}
View
3  dom-element.js
@@ -4,7 +4,7 @@ var serializeElement = require("./serialize.js")
module.exports = DOMElement
-function DOMElement(tagName) {
+function DOMElement(tagName, owner) {
if (!(this instanceof DOMElement)) {
return new DOMElement(tagName)
}
@@ -15,6 +15,7 @@ function DOMElement(tagName) {
this.childNodes = []
this.parentNode = null
this.style = {}
+ this.ownerDocument = owner || null;
}
DOMElement.prototype.type = "DOMElement"
View
3  dom-fragment.js
@@ -2,13 +2,14 @@ var DOMElement = require("./dom-element.js")
module.exports = DocumentFragment
-function DocumentFragment() {
+function DocumentFragment(owner) {
if (!(this instanceof DocumentFragment)) {
return new DocumentFragment()
}
this.childNodes = []
this.parentNode = null
+ this.ownerDocument = owner || null
}
DocumentFragment.prototype.type = "DocumentFragment"
View
5 dom-text.js
@@ -1,12 +1,13 @@
module.exports = DOMText
-function DOMText(value) {
+function DOMText(value, owner) {
if (!(this instanceof DOMText)) {
return new DOMText(value)
}
this.data = value || ""
this.length = this.data.length
+ this.ownerDocument = owner || null
}
DOMText.prototype.type = "DOMTextNode"
@@ -22,4 +23,4 @@ DOMText.prototype.replaceData = function replaceData(index, length, value) {
var right = current.substring(index + length, current.length)
this.data = left + value + right
this.length = this.data.length
-}
+}
View
78 index.js
@@ -1,77 +1,3 @@
-var DOMText = require("./dom-text.js")
-var DOMElement = require("./dom-element.js")
-var DocumentFragment = require("./dom-fragment.js")
-var Event = require("./event.js")
+var Document = require('./document.js');
-var body = createElement("body")
-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
-}
+module.exports = new Document();
Please sign in to comment.
Something went wrong with that request. Please try again.