Node.js
3.67 KB
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
//https://developer.mozilla.org/en-US/docs/Web/API/Element
function Node(cfg) {
this.namespace = cfg.namespace || null;
this.text = cfg.text;
this._selfCloseTag = cfg.selfCloseTag;
Object.defineProperties(this, {
nodeType: {
value: cfg.nodeType
},
nodeName: {
value: cfg.nodeType == 1 ? cfg.nodeName : '#text'
},
childNodes: {
value: cfg.childNodes
},
firstChild: {
get: function(){
return this.childNodes[0] || null;
}
},
lastChild: {
get: function(){
return this.childNodes[this.childNodes.length-1] || null;
}
},
parentNode: {
value: cfg.parentNode || null
},
attributes: {
value: cfg.attributes || []
},
innerHTML: {
get: function(){
var
result = '',
cNode;
for (var i = 0, l = this.childNodes.length; i < l; i++) {
cNode = this.childNodes[i];
result += cNode.nodeType === 3 ? cNode.text : cNode.outerHTML;
}
return result;
}
},
outerHTML: {
get: function(){
if (this.nodeType != 3){
var
str,
attrs = (this.attributes.map(function(elem){
return elem.name + (elem.value ? '=' + '"'+ elem.value +'"' : '');
}) || []).join(' '),
childs = '';
str = '<' + this.nodeName + (attrs ? ' ' + attrs : '') + (this._selfCloseTag ? '/' : '') + '>';
if (!this._selfCloseTag){
childs = (this._selfCloseTag ? '' : this.childNodes.map(function(child){
return child.outerHTML;
}) || []).join('');
str += childs;
str += '</' + this.nodeName + '>';
}
}
else{
str = this.textContent;
}
return str;
}
},
textContent: {
get: function(){
if (this.nodeType == Node.TEXT_NODE){
return this.text;
}
else{
return this.childNodes.map(function(node){
return node.textContent;
}).join('').replace(/\x20+/g, ' ');
}
}
}
});
}
Node.prototype.getAttribute = function (attributeName) {
for (var i = 0, l = this.attributes.length; i < l; i++) {
if (this.attributes[i].name == attributeName) {
return this.attributes[i].value;
}
}
return null;
};
function searchElements(root, conditionFn, onlyFirst){
var result = [];
onlyFirst = !!onlyFirst;
if (root.nodeType !== 3) {
for (var i = 0, l = root.childNodes.length; i < l; i++) {
if (root.childNodes[i].nodeType !== 3 && conditionFn(root.childNodes[i])) {
result.push(root.childNodes[i]);
if (onlyFirst){
break;
}
}
result = result.concat(searchElements(root.childNodes[i], conditionFn));
}
}
return onlyFirst ? result[0] : result;
}
Node.prototype.getElementsByTagName = function (tagName) {
return searchElements(this, function(elem){
return elem.nodeName == tagName;
})
};
Node.prototype.getElementsByClassName = function (className) {
var expr = new RegExp('^(.*?\\s)?' + className + '(\\s.*?)?$');
return searchElements(this, function(elem){
return elem.attributes.length && expr.test(elem.getAttribute('class'));
})
};
Node.prototype.getElementById = function (id) {
return searchElements(this, function(elem){
return elem.attributes.length && elem.getAttribute('id') == id;
}, true)
};
Node.prototype.getElementsByName = function (name) {
return searchElements(this, function(elem){
return elem.attributes.length && elem.getAttribute('name') == name;
})
};
Node.ELEMENT_NODE = 1;
Node.TEXT_NODE = 3;
module.exports = Node;