-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelement.js
More file actions
44 lines (41 loc) · 967 Bytes
/
Copy pathelement.js
File metadata and controls
44 lines (41 loc) · 967 Bytes
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
function Element (tagName, props, children){
if(!(this instanceof Element)){
return new Element(tagName, props, children);
}
this.tagName = tagName;
this.props = props;
this.children = children;
var count = 0;
foreach(children, function(child, i){
if(child instanceof Element){
count += child.count
} else {
children[i] = '' + child
}
count++;
})
this.count = count;
console.log(tagName + ':' +count)
}
Element.prototype.render = function(){
var el = document.createElement(this.tagName);
var props = this.props;
for(var propName in props){
var propValue = props[propName];
el.setAttribute(propName, propValue)
}
var children = this.children || [];
foreach(children, function(child, i){
var childEl = (child instanceof Element)
? child.render()
: document.createTextNode(child)
el.appendChild(childEl)
})
return el;
}
function foreach(arr, cb){
var i, l = arr.length;
for(i=0; i<l; i++){
cb && cb(arr[i], i)
}
}