-
Notifications
You must be signed in to change notification settings - Fork 0
/
e5x.js
197 lines (173 loc) · 5.02 KB
/
e5x.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
"use strict";
/**
* <title>Simple XML and HTML Processing with ECMAScript Edition 5</title>
*/
/**
* @namespace
*/
jsx.dom.e5x = (/** @constructor */ function () {
var _Attribute = (
/**
* @constructor
*/
function jsx_dom_e5x_Attribute (attribute) {
this.name = attribute.name;
this.target = attribute;
}
).extend(null, {
/**
* Returns the value of this attribute
*
* @memberOf jsx.dom.e5x.Attribute.prototype
* @return {string}
*/
toString: function () {
return this.target.nodeValue;
},
toXML: function () {
return this.target.name + '="'
+ this.toString()
.replace(/&/g, "&").replace(/</g, "<")
.replace(/"/g, """)
+ '"';
}
});
var _Text = (
/**
* @constructor
*/
function jsx_dom_e5x_Text (text) {
this.target = text;
}
).extend(null, {
/**
* @memberOf jsx.dom.e5x.Text.prototype
* @return {string}
*/
toString: function () {
return this.target.data || this.target.nodeValue;
},
toXML: function () {
return this.toString().replace(/&/g, "&").replace(/</g, "<");
}
});
return {
Attribute: _Attribute,
/**
* @memberOf jsx.dom.e5x
* @param {Element} element
*/
Element: (
function jsx_dom_e5x_Element (element) {
this.nodeName = element.nodeName;
this.attributes = [];
this.childNodes = [];
this.childElements = [];
var attributes = element.attributes;
for (var i = 0, len = attributes.length; i < len; ++i)
{
var attribute = attributes[i];
attribute = new _Attribute(attribute);
var attributes_by_name = this.attributes[attribute.name];
if (typeof attributes_by_name == "undefined")
{
this.attributes[attribute.name] = attribute;
attribute[0] = attribute;
attribute.length = 1;
}
else if (attributes_by_name instanceof _Attribute)
{
attributes_by_name[attributes_by_name.length] = attribute;
++attributes_by_name.length;
}
this.attributes.push(attribute);
}
var childNodes = element.childNodes;
for (i = 0, len = childNodes.length; i < len; ++i)
{
var child = childNodes[i];
var child_nodes = this.childNodes;
if (child.nodeType == 1)
{
child = new jsx_dom_e5x_Element(child);
var node_name = child.nodeName;
var child_nodes_by_node_name = this[node_name];
if (typeof child_nodes_by_node_name == "undefined")
{
this[node_name] = child;
child[0] = child;
child.length = 1;
}
else if (child_nodes_by_node_name instanceof jsx_dom_e5x_Element)
{
child_nodes_by_node_name[child_nodes_by_node_name.length] =
child;
++child_nodes_by_node_name.length;
}
var lower_name = node_name.toLowerCase();
var child_nodes_by_lower_name = this[lower_name];
if (typeof child_nodes_by_lower_name == "undefined")
{
this[lower_name] = child;
child.length = 1;
}
else if (child_nodes_by_lower_name instanceof jsx_dom_e5x_Element)
{
child_nodes_by_lower_name[child_nodes_by_lower_name.length] =
child;
++child_nodes_by_lower_name.length;
}
this.childElements.push(child);
}
else if (child.nodeType == 3)
{
child = new _Text(child);
}
child_nodes.push(child);
}
jsx.object.defineProperty(this, "textContent", {
get: function () {
return this.getTextContent();
}
});
this.target = element;
}
).extend(null, {
/**
* Returns the text content of this element
*
* @memberOf jsx.dom.e5x.Element.prototype
* @return {string}
*/
getTextContent: function () {
var text_content = this.target.textContent;
return (typeof text_content != "undefined"
? text_content
: [].join.call(this.childNodes));
},
/**
* Returns the text content of this element
*
* @return {string}
*/
toString: function () {
return this.textContent;
},
toXML: function () {
function _toXML (o)
{
return o.toXML();
}
var node_name = this.target.nodeName;
var attributes = this.attributes.map(_toXML).join(" ");
var content = this.childNodes.map(_toXML).join("");
return "<" + node_name
+ (attributes ? " " + attributes : "")
+ (content ? "" : " /")
+ ">"
+ (content ? content + "</" + node_name + ">" : "");
}
}),
Text: _Text
};
}());