Pathologic was missing because of a .git folder inside.
[yaffs-website] / node_modules / min-document / dom-element.js
1 var domWalk = require("dom-walk")
2 var dispatchEvent = require("./event/dispatch-event.js")
3 var addEventListener = require("./event/add-event-listener.js")
4 var removeEventListener = require("./event/remove-event-listener.js")
5 var serializeNode = require("./serialize.js")
6
7 var htmlns = "http://www.w3.org/1999/xhtml"
8
9 module.exports = DOMElement
10
11 function DOMElement(tagName, owner, namespace) {
12     if (!(this instanceof DOMElement)) {
13         return new DOMElement(tagName)
14     }
15
16     var ns = namespace === undefined ? htmlns : (namespace || null)
17
18     this.tagName = ns === htmlns ? String(tagName).toUpperCase() : tagName
19     this.nodeName = this.tagName
20     this.className = ""
21     this.dataset = {}
22     this.childNodes = []
23     this.parentNode = null
24     this.style = {}
25     this.ownerDocument = owner || null
26     this.namespaceURI = ns
27     this._attributes = {}
28
29     if (this.tagName === 'INPUT') {
30       this.type = 'text'
31     }
32 }
33
34 DOMElement.prototype.type = "DOMElement"
35 DOMElement.prototype.nodeType = 1
36
37 DOMElement.prototype.appendChild = function _Element_appendChild(child) {
38     if (child.parentNode) {
39         child.parentNode.removeChild(child)
40     }
41
42     this.childNodes.push(child)
43     child.parentNode = this
44
45     return child
46 }
47
48 DOMElement.prototype.replaceChild =
49     function _Element_replaceChild(elem, needle) {
50         // TODO: Throw NotFoundError if needle.parentNode !== this
51
52         if (elem.parentNode) {
53             elem.parentNode.removeChild(elem)
54         }
55
56         var index = this.childNodes.indexOf(needle)
57
58         needle.parentNode = null
59         this.childNodes[index] = elem
60         elem.parentNode = this
61
62         return needle
63     }
64
65 DOMElement.prototype.removeChild = function _Element_removeChild(elem) {
66     // TODO: Throw NotFoundError if elem.parentNode !== this
67
68     var index = this.childNodes.indexOf(elem)
69     this.childNodes.splice(index, 1)
70
71     elem.parentNode = null
72     return elem
73 }
74
75 DOMElement.prototype.insertBefore =
76     function _Element_insertBefore(elem, needle) {
77         // TODO: Throw NotFoundError if referenceElement is a dom node
78         // and parentNode !== this
79
80         if (elem.parentNode) {
81             elem.parentNode.removeChild(elem)
82         }
83
84         var index = needle === null || needle === undefined ?
85             -1 :
86             this.childNodes.indexOf(needle)
87
88         if (index > -1) {
89             this.childNodes.splice(index, 0, elem)
90         } else {
91             this.childNodes.push(elem)
92         }
93
94         elem.parentNode = this
95         return elem
96     }
97
98 DOMElement.prototype.setAttributeNS =
99     function _Element_setAttributeNS(namespace, name, value) {
100         var prefix = null
101         var localName = name
102         var colonPosition = name.indexOf(":")
103         if (colonPosition > -1) {
104             prefix = name.substr(0, colonPosition)
105             localName = name.substr(colonPosition + 1)
106         }
107         if (this.tagName === 'INPUT' && name === 'type') {
108           this.type = value;
109         }
110         else {
111           var attributes = this._attributes[namespace] || (this._attributes[namespace] = {})
112           attributes[localName] = {value: value, prefix: prefix}
113         }
114     }
115
116 DOMElement.prototype.getAttributeNS =
117     function _Element_getAttributeNS(namespace, name) {
118         var attributes = this._attributes[namespace];
119         var value = attributes && attributes[name] && attributes[name].value
120         if (this.tagName === 'INPUT' && name === 'type') {
121           return this.type;
122         }
123         if (typeof value !== "string") {
124             return null
125         }
126         return value
127     }
128
129 DOMElement.prototype.removeAttributeNS =
130     function _Element_removeAttributeNS(namespace, name) {
131         var attributes = this._attributes[namespace];
132         if (attributes) {
133             delete attributes[name]
134         }
135     }
136
137 DOMElement.prototype.hasAttributeNS =
138     function _Element_hasAttributeNS(namespace, name) {
139         var attributes = this._attributes[namespace]
140         return !!attributes && name in attributes;
141     }
142
143 DOMElement.prototype.setAttribute = function _Element_setAttribute(name, value) {
144     return this.setAttributeNS(null, name, value)
145 }
146
147 DOMElement.prototype.getAttribute = function _Element_getAttribute(name) {
148     return this.getAttributeNS(null, name)
149 }
150
151 DOMElement.prototype.removeAttribute = function _Element_removeAttribute(name) {
152     return this.removeAttributeNS(null, name)
153 }
154
155 DOMElement.prototype.hasAttribute = function _Element_hasAttribute(name) {
156     return this.hasAttributeNS(null, name)
157 }
158
159 DOMElement.prototype.removeEventListener = removeEventListener
160 DOMElement.prototype.addEventListener = addEventListener
161 DOMElement.prototype.dispatchEvent = dispatchEvent
162
163 // Un-implemented
164 DOMElement.prototype.focus = function _Element_focus() {
165     return void 0
166 }
167
168 DOMElement.prototype.toString = function _Element_toString() {
169     return serializeNode(this)
170 }
171
172 DOMElement.prototype.getElementsByClassName = function _Element_getElementsByClassName(classNames) {
173     var classes = classNames.split(" ");
174     var elems = []
175
176     domWalk(this, function (node) {
177         if (node.nodeType === 1) {
178             var nodeClassName = node.className || ""
179             var nodeClasses = nodeClassName.split(" ")
180
181             if (classes.every(function (item) {
182                 return nodeClasses.indexOf(item) !== -1
183             })) {
184                 elems.push(node)
185             }
186         }
187     })
188
189     return elems
190 }
191
192 DOMElement.prototype.getElementsByTagName = function _Element_getElementsByTagName(tagName) {
193     tagName = tagName.toLowerCase()
194     var elems = []
195
196     domWalk(this.childNodes, function (node) {
197         if (node.nodeType === 1 && (tagName === '*' || node.tagName.toLowerCase() === tagName)) {
198             elems.push(node)
199         }
200     })
201
202     return elems
203 }
204
205 DOMElement.prototype.contains = function _Element_contains(element) {
206     return domWalk(this, function (node) {
207         return element === node
208     }) || false
209 }