forked from 1N50MN14/html-element
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
330 lines (268 loc) · 8.1 KB
/
index.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
global.Document = Document
global.Node = Node
global.Element = Element
global.Comment = Comment
global.Text = Text
global.document = new Document()
var ClassList = require('class-list')
function Document() {}
Document.prototype.createTextNode = function(v) {
var n = new Text();
n.textContent = v;
n.nodeName = '#text'
n.nodeType = 3
return n;
}
Document.prototype.createElement = function(nodeName) {
var el = new Element();
el.nodeName = nodeName;
return el;
}
Document.prototype.createComment = function(data) {
var el = new Comment()
el.data = data
return el;
}
function Node () {}
Text.prototype = new Node()
Element.prototype = new Node()
Comment.prototype = new Node()
function Style (el) {
this.el = el;
this.styles = [];
}
Style.prototype.setProperty = function (n,v) {
this.el._setProperty(this.styles, {name: n, value:v});
}
Style.prototype.getProperty = function(n) {
return this.el._getProperty(this.styles, n);
}
Style.prototype.__defineGetter__('cssText', function () {
var stylified = '';
this.styles.forEach(function(s){
stylified+=s.name+':'+s.value+';';
})
return stylified;
})
Style.prototype.__defineSetter__('cssText', function (v) {
this.styles.length = 0
// parse cssText and set style attributes
v.split(';').forEach(function(part){
var splitPoint = part.indexOf(':')
if (splitPoint){
var key = part.slice(0, splitPoint).trim()
var value = part.slice(splitPoint+1).trim()
this.setProperty(key, value)
}
}, this)
})
function Attribute(name, value){
if (name) {
this.name = name;
this.value = value ? value : '';
}
}
function Element() {
var self = this;
this.style = new Style(this)
this.classList = ClassList(this);
this.childNodes = [];
this.attributes = [];
this.dataset = {};
this.className = '';
this._setProperty = function(arr, obj, key, val) {
var p = self._getProperty(arr, key);
if (p) {
p.value = val;
return;
}
arr.push('function' === typeof obj ? new obj(key.toLowerCase(),val) : obj);
}
this._getProperty = function(arr, key) {
if (!key) return;
key = key.toLowerCase();
for (var i=0;i<arr.length;i++) {
if (key == arr[i].name) return arr[i];
}
}
}
Element.prototype.nodeType = 1;
Element.prototype.appendChild = function(child) {
child.parentElement = this;
this.childNodes.push(child);
return child;
}
Element.prototype.setAttribute = function (n, v) {
if (n == 'style'){
this.style.cssText = v
} else {
this._setProperty(this.attributes, Attribute, n, v);
}
}
Element.prototype.getAttribute = function(n) {
if (n == 'style'){
return this.style.cssText
} else {
return this._getProperty(this.attributes, n);
}
}
Element.prototype.replaceChild = function(newChild, oldChild) {
var self = this;
var replaced = false;
this.childNodes.forEach(function(child, index){
if (child === oldChild) {
self.childNodes[index] = newChild;
replaced = true;
}
});
if (replaced) return oldChild;
}
Element.prototype.removeChild = function(rChild) {
var self = this;
var removed = true;
this.childNodes.forEach(function(child, index){
if (child === rChild) {
delete self.childNodes[index];
removed = true;
}
})
if (removed) return rChild;
}
Element.prototype.insertBefore = function(newChild, existingChild) {
var self = this;
this.childNodes.forEach(function(child, index){
if (child === existingChild) {
index === 0 ? self.childNodes.unshift(newChild)
: self.childNodes.splice(index, 0, newChild);
}
})
return newChild;
}
Element.prototype.addEventListener = function(type, listener, useCapture, wantsUntrusted) {
// https://developer.mozilla.org/en-US/docs/Web/API/EventTarget.addEventListener
// There is an implementation there but probably not worth it.
}
Element.prototype.removeEventListener = function(type, listener, useCapture) {
// https://developer.mozilla.org/en/docs/Web/API/EventTarget.removeEventListener
// There is an implementation there but probably not worth it.
}
Element.prototype.insertAdjacentHTML = function(position, text) {
// https://developer.mozilla.org/en-US/docs/Web/API/Element.insertAdjacentHTML
// Not too much work to implement similar to innerHTML below.
}
Element.prototype.__defineGetter__('innerHTML', function () {
// regurgitate set innerHTML
var s = this.childNodes.html || ''
this.childNodes.forEach(function (e) {
s += (e.outerHTML || e.textContent)
})
return s
})
Element.prototype.__defineSetter__('innerHTML', function (v) {
//only handle this simple case that doesn't need parsing
//this case is useful... parsing is hard and will need added deps!
this.childNodes.length = 0
// hack to preserve set innerHTML - no parsing just regurgitation
this.childNodes.html = v
})
Element.prototype.__defineGetter__('outerHTML', function () {
var a = [], self = this;
var VOID_ELEMENTS = {
AREA: true,
BASE: true,
BR: true,
COL: true,
EMBED: true,
HR: true,
IMG: true,
INPUT: true,
KEYGEN: true,
LINK: true,
META: true,
PARAM: true,
SOURCE: true,
TRACK: true,
WBR: true
};
function _stringify(arr) {
var attr = [], value;
arr.forEach(function(a){
value = ('style' != a.name) ? a.value : self.style.cssText;
attr.push(a.name+'='+'\"'+escapeAttribute(value)+'\"');
})
return attr.length ? ' '+attr.join(" ") : '';
}
function _dataify(data) {
var attr = [], value;
Object.keys(data).forEach(function(name){
attr.push('data-'+name+'='+'\"'+escapeAttribute(data[name])+'\"');
})
return attr.length ? ' '+attr.join(" ") : '';
}
function _propertify() {
var props = [];
for (var key in self) {
_isProperty(key) && props.push({name: key, value:self[key]});
}
// special className case, if className property is define while 'class' attribute is not then
// include class attribute in output
self.className.length && !self.getAttribute('class') && props.push({name:'class', value: self.className})
return props ? _stringify(props) : '';
}
function _isProperty(key) {
var types = ['string','boolean','number']
for (var i=0; i<=types.length;i++) {
if (self.hasOwnProperty(key) &&
types[i] === typeof self[key] &&
key !== 'nodeName' &&
key !== 'nodeType' &&
key !== 'className'
) return true;
}
}
var attrs = this.style.cssText ? this.attributes.concat([{name: 'style'}]) : this.attributes;
a.push('<'+this.nodeName + _propertify() + _stringify(attrs) + _dataify(this.dataset) +'>')
if (!VOID_ELEMENTS[this.nodeName.toUpperCase()]){
a.push(this.innerHTML)
a.push('</'+this.nodeName+'>')
}
return a.join('')
})
Element.prototype.__defineGetter__('textContent', function () {
var s = ''
this.childNodes.forEach(function (e) {
s += e.textContent
})
return s
})
Element.prototype.addEventListener = function(t, l) {}
function escapeHTML(s) {
return String(s)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>');
}
function escapeAttribute(s) {
return escapeHTML(s).replace(/"/g, '"')
}
function Text(){}
Text.prototype.nodeType = 3;
Text.prototype.nodeName = '#text';
Text.prototype.__defineGetter__('textContent', function() {
return escapeHTML(this.value || '');
})
Text.prototype.__defineSetter__('textContent', function(v) {
this.value = v
})
function Comment(){}
Comment.prototype.nodeType = 8;
Comment.prototype.nodeName = '#comment';
Comment.prototype.__defineGetter__('data', function() {
return this.value
})
Comment.prototype.__defineSetter__('data', function(v) {
this.value = v
})
Comment.prototype.__defineGetter__('outerHTML', function() {
return '<!--' + escapeHTML(this.value || '') + '-->'
})