-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
dom.js
497 lines (458 loc) · 14.5 KB
/
dom.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*! litejs.com/MIT-LICENSE.txt */
var boolAttrs = {
async:1, autoplay:1, loop:1, checked:1, defer:1, disabled:1, muted:1, multiple:1, nomodule:1, playsinline:1, readonly:1, required:1, selected:1
}
, numAttrs = "height maxLength minLength size tabIndex width"
, strAttrs = "accept accesskey autocapitalize autofocus capture class contenteditable crossorigin dir for hidden href id integrity lang name nonce rel slot spellcheck src title type translate"
, defaultAttrs = {
"form method get":1, "input type text":1,
"script type text/javascript":1, "style type text/css":1
}
, voidElements = {
AREA:1, BASE:1, BR:1, COL:1, EMBED:1, HR:1, IMG:1, INPUT:1, KEYGEN:1, LINK:1, MENUITEM:1, META:1, PARAM:1, SOURCE:1, TRACK:1, WBR:1
}
, rawTextElements = { SCRIPT: /<(?=\/script)/i, STYLE: /<(?=\/style)/i }
, rawTextEscape = { SCRIPT: /<(?=\/script|!--)/ig, STYLE: /<(?=\/style|!--)/ig }
, hasOwn = voidElements.hasOwnProperty
, { CSSStyleDeclaration, CSSStyleSheet } = require("./css.js")
, selector = require("./selector.js")
, Node = {
ELEMENT_NODE: 1,
TEXT_NODE: 3,
PROCESSING_INSTRUCTION_NODE: 7,
COMMENT_NODE: 8,
DOCUMENT_NODE: 9,
DOCUMENT_TYPE_NODE: 10,
DOCUMENT_FRAGMENT_NODE: 11,
nodeName: null,
parentNode: null,
ownerDocument: null,
childNodes: null,
get nodeValue() {
return this.nodeType === 3 || this.nodeType === 8 ? this.data : null
},
set nodeValue(text) {
if (this.nodeType === 3 || this.nodeType === 8) this.data = text
},
get textContent() {
return this.nodeType === 3 || this.nodeType === 8 ? this.data : this.childNodes.map(node => node.textContent).join("")
},
set textContent(text) {
if (this.nodeType === 3 || this.nodeType === 8) this.data = text
else replaceChildren.call(this, this.ownerDocument.createTextNode(
rawTextEscape[this.tagName] ? text.replace(rawTextEscape[this.tagName], "<\\") : text
))
},
get firstChild() {
return this.childNodes && this.childNodes[0] || null
},
get lastChild() {
return this.childNodes && this.childNodes[ this.childNodes.length - 1 ] || null
},
get nextSibling() {
return getSibling(this, 1, 0)
},
get previousSibling() {
return getSibling(this, -1, 0)
},
// innerHTML and outerHTML should be extensions to the Element interface
get innerHTML() {
return Node.toString.call(this)
},
set innerHTML(html) {
var child, m, re, text
, node = this
, doc = node.ownerDocument || node
, tagRe = /<(!--([\s\S]*?)--!?|!\[CDATA\[([\s\S]*?)\]\]|[?!][\s\S]*?)>|<(\/?)([^ \/>]+)((?:("|')(?:\\\7|[\s\S])*?\7|[^>])*?)(\/?)>|[^<]+|</g
, attrRe = /([^=\s]+)(?:\s*=\s*(("|')((?:\\\3|[\s\S])*?)\3|[^\s"'`=<>]+)|)/g
, frag = doc.createDocumentFragment()
, tree = frag
for (; (m = tagRe.exec(html)); ) {
if (m[4]) {
tree = tree.parentNode || tree
} else if (m[5]) {
child = doc.contentType === "text/html" ? doc.createElement(m[5]) : doc.createElementNS(null, m[5])
if (m[6]) {
m[6].replace(attrRe, setAttr)
}
tree.appendChild(child)
if ((re = rawTextElements[child.tagName])) {
for (text = ""; (m = tagRe.exec(html)) && !re.test(m[0]); text += m[3] || m[0]);
child.textContent = text.replace(unescRe, unescFn)
if (!m) break
} else if (!voidElements[child.tagName] && !m[8]) tree = child
} else {
tree.appendChild(
m[2] ? doc.createComment(m[2].replace(unescRe, unescFn)) :
m[1] ? doc.createDocumentType(m[1]) :
doc.createTextNode(m[0].replace(unescRe, unescFn))
)
}
}
replaceChildren.call(node, frag)
function setAttr(_, name, value, q, qvalue) {
child.setAttribute(name, (q ? qvalue : value || "").replace(unescRe, unescFn))
}
},
get outerHTML() {
return this.toString()
},
set outerHTML(html) {
var frag = this.ownerDocument.createDocumentFragment()
frag.innerHTML = html
this.parentNode.replaceChild(frag, this)
},
get sheet() {
if (this.tagName === "STYLE" || this.tagName === "LINK" && this.rel === "stylesheet" && this.href) return new CSSStyleSheet({
href: this.href,
ownerNode: this
})
},
get style() {
return this._style || (this._style = CSSStyleDeclaration(this.getAttribute("style") || ""))
},
set style(value) {
this.style.cssText = value
},
contains(el) {
for (; el; el = el.parentNode) if (el === this) return true
return false
},
hasChildNodes() {
return !!this.firstChild
},
getElementById(id) {
return selector.find(this, "#" + id, 1)
},
appendChild(el) {
return this.insertBefore(el)
},
insertBefore(el, ref) {
var node = this
, childs = node.childNodes
if (el.nodeType === 11) {
for (; el.firstChild; ) node.insertBefore(el.firstChild, ref)
} else {
if (el.parentNode) el.parentNode.removeChild(el)
el.parentNode = node
// If ref is null, insert el at the end of the list of children.
childs.splice(ref ? childs.indexOf(ref) : childs.length, 0, el)
if (node.nodeType === 9 && el.nodeType === 1) {
node.documentElement = el
node.body = el.querySelector("body")
}
}
return el
},
removeChild(el) {
var node = this
, index = node.childNodes.indexOf(el)
if (index === -1) throw Error("NOT_FOUND_ERR")
node.childNodes.splice(index, 1)
el.parentNode = null
return el
},
replaceChild(el, ref) {
this.insertBefore(el, ref)
return this.removeChild(ref)
},
cloneNode(deep) {
var node = this
, clone = new node.constructor(node.tagName || node.data)
clone.ownerDocument = node.ownerDocument
if (node.attributes) {
node.attributes.names().forEach(attr => clone.setAttribute(attr, node.getAttribute(attr)))
}
if (deep && node.hasChildNodes()) {
node.childNodes.forEach(child => clone.appendChild(child.cloneNode(deep)))
}
return clone
},
querySelector(sel) {
return selector.find(this, sel, 1)
},
querySelectorAll(sel) {
return selector.find(this, sel)
},
toString(min) {
return rawTextElements[this.tagName] ? (
this.tagName === "STYLE" && (min === true || min && min.css) ? "\n" + this.sheet.toString(min.css || true) + "\n" :
this.textContent
) : this.childNodes.map(node => node.toString(min)).join("")
}
}
, Element = {
get firstElementChild() {
return getElement(this.childNodes, 0, 1, 1)
},
get lastElementChild() {
return getElement(this.childNodes, this.childNodes.length - 1, -1, 1)
},
get nextElementSibling() {
return getSibling(this, 1, 1)
},
get previousElementSibling() {
return getSibling(this, -1, 1)
},
replaceChildren: replaceChildren,
hasAttribute(name) {
return this.attributes.getNamedItem(name) != null
},
getAttribute(name) {
var attr = this.attributes.getNamedItem(name)
return attr ? attr.value : null
},
setAttribute(name, value) {
this.attributes.setNamedItem(new Attr(this, name, value))
},
removeAttribute(name) {
this.attributes.removeNamedItem(name)
},
getElementsByTagName(tag) {
return selector.find(this, tag)
},
getElementsByClassName(sel) {
return selector.find(this, "." + sel.replace(/\s+/g, "."))
}
}
, quotedAttrRe = /[\s"'`=<>]/
, escRe = /<|&(?=[a-z#])/gi
, escFn = chr => chr === "<" ? "<" : "&"
, unescRe = /&[a-z]{1,31};?|&#(x|)([\da-f]+);/ig
, unescFn = (ent, hex, num) => num ? String.fromCharCode(parseInt(num, hex === "" ? 10 : 16)) : entities[ent] || ent
, entities = {
"&": "&", "'": "'", "¢": "¢", "©": "©", "¤": "¤",
"°": "°", "€": "€", ">": ">", "<": "<", " ": " ",
"±": "±", "£": "£", """: "\"", "®": "®",
"§": "§", "²": "²", "³": "³", "¥": "¥"
}
Object.keys(boolAttrs).forEach(addGetter, { isBool: true, readonly: "readOnly" })
numAttrs.split(" ").forEach(addGetter, { isNum: true })
strAttrs.split(" ").forEach(addGetter, { "for": "htmlFor", "class": "className" })
function addGetter(key) {
var attr = key.toLowerCase()
Object.defineProperty(Element, this[key] || key, {
configurable: true,
enumerable: true,
get: (
this.isBool ? function() { return this.hasAttribute(attr) } :
this.isNum ? function() { return +this.getAttribute(attr) || 0 } :
function() { return this.getAttribute(attr) || "" }
),
set(value) {
this.setAttribute(attr, value)
}
})
}
;["hasAttribute", "getAttribute", "setAttribute", "removeAttribute"].forEach(function(name) {
Element[name + "NS"] = function(ns, a, b) {
return this[name].call(this, a, b)
}
})
function Attr(node, name, value) {
this.ownerElement = node
this.name = name.toLowerCase()
this.value = "" + value
}
function NamedNodeMap(node) {
Object.defineProperty(this, "length", { get() { return this.names().length } })
Object.defineProperty(this, "ownerElement", { value: node })
}
NamedNodeMap.prototype = {
names() {
this.getNamedItem("style")
return Object.keys(this)
},
getNamedItem(name) {
var loName = name.toLowerCase()
, attr = this[loName] || null
if (loName === "style" && this.ownerElement._style) {
if (attr === null) attr = this[loName] = new Attr(this.ownerElement, name, "")
attr.value = this.ownerElement._style.cssText
}
return attr
},
removeNamedItem(name) {
var loName = name.toLowerCase()
, attr = this[loName] || null
if (loName === "style") delete this.ownerElement._style
if (attr !== null) delete this[loName]
return attr
},
setNamedItem(attr) {
var oldAttr = this.getNamedItem(attr.name)
if (attr.name === "style") attr.value = CSSStyleDeclaration(attr.value).cssText
this[attr.name] = attr
return oldAttr
},
toString(minify) {
var map = this
, tagName = map.ownerElement.tagName
, isXml = map.ownerElement.ownerDocument.contentType === "application/xml"
return map.names().map(loName => {
var attr = map.getNamedItem(loName)
, name = attr.name
, value = attr.value.replace(escRe, escFn)
if (!isXml) {
if (hasOwn.call(boolAttrs, loName)) return name
if (minify) {
value = loName.slice(0, 2) === "on" ? value.replace(/^[\s\uFEFF\xA0;]+|[\s\uFEFF\xA0;]+$/g, "") : value.replace(/\s+/g, " ").trim()
if (hasOwn.call(defaultAttrs, (tagName + " " + name + " " + value).toLowerCase())) return
if (!quotedAttrRe.test(value)) return name + "=" + value
if (value.split("\"").length > value.split("'").length) return name + "='" + value.replace(/'/g, "'") + "'"
}
}
return name + "=\"" + value.replace(/"/g, """) + "\""
}).filter(Boolean).join(" ")
}
}
function DocumentFragment() {
this.childNodes = []
}
extendNode(DocumentFragment, Node, {
nodeType: 11,
nodeName: "#document-fragment"
})
function HTMLElement(tag) {
var el = this
el.attributes = new NamedNodeMap(el)
el.childNodes = []
el.localName = tag.toLowerCase()
el.nodeName = el.tagName = tag.toUpperCase()
}
extendNode(HTMLElement, Element, {
nodeType: 1,
matches(sel) {
return selector.matches(this, sel)
},
closest(sel) {
return selector.closest(this, sel)
},
namespaceURI: "http://www.w3.org/1999/xhtml",
localName: null,
tagName: null,
toString(minify) {
var attrs = this.attributes.toString(minify)
return "<" + this.localName + (attrs ? " " + attrs + (attrs.slice(-1) === "/" ? " >" : ">") : ">") +
(voidElements[this.tagName] ? "" : Node.toString.call(this, minify) + "</" + this.localName + ">")
}
})
function ElementNS(namespace, tag) {
var el = this
el.attributes = new NamedNodeMap(el)
el.childNodes = []
el.namespaceURI = namespace
el.nodeName = el.tagName = el.localName = tag
}
ElementNS.prototype = HTMLElement.prototype
function Text(data) {
this.data = data
}
extendNode(Text, {
nodeType: 3,
nodeName: "#text",
toString(minify) {
return (minify ? ("" + this.data).trim() : "" + this.data).replace(escRe, escFn)
}
})
function Comment(data) {
this.data = data
}
extendNode(Comment, {
nodeType: 8,
nodeName: "#comment",
toString(minify) {
return minify ? "" : "<!--" + this.data + "-->"
}
})
function DocumentType(data) {
this.data = data
}
extendNode(DocumentType, {
nodeType: 10,
toString() {
return "<" + this.data + ">"
// var node = document.doctype
// return "<!DOCTYPE " + node.name +
// (node.publicId ? ' PUBLIC "' + node.publicId + '"' : '') +
// (!node.publicId && node.systemId ? ' SYSTEM' : '') +
// (node.systemId ? ' "' + node.systemId + '"' : '') + '>'
}
})
function Document() {
this.childNodes = []
this
.appendChild(this.createElement("html"))
.appendChild(this.body = this.createElement("body"))
}
extendNode(Document, Element, {
get styleSheets() {
return selector.find(this, "style,link[rel=stylesheet][href]").map(el => el.sheet)
},
get title() {
var el = selector.find(this, "title", 1)
return el && el.textContent || ""
},
set title(text) {
var el = selector.find(this, "title", 1) || this.appendChild(this.createElement("title"))
el.textContent = text
},
nodeType: 9,
nodeName: "#document",
contentType: "text/html",
createElement: own(HTMLElement),
createElementNS: own(ElementNS),
createTextNode: own(Text),
createComment: own(Comment),
createDocumentType: own(DocumentType), //Should be document.implementation.createDocumentType(name, publicId, systemId)
createDocumentFragment: own(DocumentFragment)
})
function DOMParser() {}
function XMLSerializer() {}
DOMParser.prototype.parseFromString = (str, mime) => {
var doc = new Document()
doc.contentType = mime || "text/html"
doc.documentElement.outerHTML = str
return doc
}
XMLSerializer.prototype.serializeToString = doc => doc.toString()
function own(Class) {
return function($1, $2) {
var node = new Class($1, $2)
node.ownerDocument = this
return node
}
}
function extendNode(obj, extras) {
obj.prototype = Object.create(Node)
for (var descriptor, key, i = 1; (extras = arguments[i++]); ) {
for (key in extras) {
descriptor = Object.getOwnPropertyDescriptor(extras, key)
Object.defineProperty(obj.prototype, key, descriptor)
}
}
obj.prototype.constructor = obj
}
function replaceChildren() {
for (var arr = this.childNodes, i = 0, l = arr.length; i < l; ) arr[i++].parentNode = null
for (i = arr.length = 0, l = arguments.length; i < l; ) this.insertBefore(arguments[i++])
}
function getElement(childs, index, step, type) {
if (childs && index > -1) for (; childs[index]; index += step) {
if (childs[index].nodeType === type) return childs[index]
}
return null
}
function getSibling(node, step, type) {
var silbings = node.parentNode && node.parentNode.childNodes
, index = silbings ? silbings.indexOf(node) : -1
return type > 0 ? getElement(silbings, index + step, step, type) : silbings && silbings[index + step] || null
}
exports.document = new Document()
exports.entities = entities
exports.DOMParser = DOMParser
exports.Document = Document
exports.DocumentFragment = DocumentFragment
exports.HTMLElement = HTMLElement
exports.Node = Node
exports.XMLSerializer = XMLSerializer