Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81147 views
1
// This object will be used as the prototype for Nodes when creating a
2
// DOM-Level-1-compliant structure.
3
var NodePrototype = module.exports = {
4
get firstChild() {
5
var children = this.children;
6
return children && children[0] || null;
7
},
8
get lastChild() {
9
var children = this.children;
10
return children && children[children.length - 1] || null;
11
},
12
get nodeType() {
13
return nodeTypes[this.type] || nodeTypes.element;
14
}
15
};
16
17
var domLvl1 = {
18
tagName: "name",
19
childNodes: "children",
20
parentNode: "parent",
21
previousSibling: "prev",
22
nextSibling: "next",
23
nodeValue: "data"
24
};
25
26
var nodeTypes = {
27
element: 1,
28
text: 3,
29
cdata: 4,
30
comment: 8
31
};
32
33
Object.keys(domLvl1).forEach(function(key) {
34
var shorthand = domLvl1[key];
35
Object.defineProperty(NodePrototype, key, {
36
get: function() {
37
return this[shorthand] || null;
38
},
39
set: function(val) {
40
this[shorthand] = val;
41
return val;
42
}
43
});
44
});
45
46