-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
207 lines (189 loc) · 6.26 KB
/
main.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
// libxmljs-easy
// =============
//
// libxmljs-easy is a Node.js module which simplifies XML traversing,
// similar to [E4X](http://en.wikipedia.org/wiki/ECMAScript_for_XML).
//
// Installation
// ------------
//
// npm install libxmljs-easy
//
var libxml = require("libxmljs");
var util = require("util");
var handlerMaker = require("./handler-maker");
// Usage
// -----
//
// Use module
//
// var easy = require("libxmljs-easy");
//
// Parse XML
//
// var xml = easy.parse('<books><book name="Lord of the Rings">' +
// '<author name="J. R. R. Tolkien" />' +
// '<language>English</language>' +
// '</book></books>');
//
// Select elements from collections explicitly
//
// assert.equal(xml.book[0].$name, "Lord of the Rings");
// assert.equal(xml.book[0].author[0].$name, "J. R. R. Tolkien");
//
// Use shorthands (works well for case when there is single child element with given name)
//
// assert.equal(xml.book.$name, "Lord of the Rings");
// assert.equal(xml.book.author.$name, "J. R. R. Tolkien");
//
// Basically the idea is that you construct a path from tag names,
// which can optionally end with attribute name prefixed with "$".
//
// When index is ommited – the array of elements is matched.
// When attribute is accessed on such array, its value is concatenated string
// of attribute values for each of elements in the array.
//
// There is also original DOM element available as "$" property
// of individual converted elements.
//
// assert.equal(xml.book.language[0].$.text(), "English");
//
//
exports.parse = function(str, options) {
var xml = libxml.parseXmlString(str, options);
return convertElement(xml.root());
}
function createProxy(converted, attrGetter, elemGetter, attrSetter, elemSetter) {
// Create default handler for object proxy
var handler = handlerMaker(converted);
// Getter for proxy properties
handler.get = function(target, name) {
if (typeof converted[name] != "undefined") {
return converted[name];
}
if (name == "$") {
return undefined;
}
if (name[0] == "$") {
var attrName = name.slice(1);
return attrGetter.call(target, attrName);
}
return enhanceResults(elemGetter.call(target, name));
};
// Setter for proxy properties
handler.set = function(target, name, value) {
if (typeof converted[name] != "undefined") {
// Default forwarding
converted[name] = value;
} else if (name[0] == "$") {
// Set attribute
var attrName = name.slice(1);
attrSetter.call(target, attrName, value);
} else {
// Child elements
elemSetter.call(target, name, value);
}
};
return Proxy.create(handler);
}
function enhanceResults(results) {
return createProxy(results,
function(attrName) {
return results.map(function(it) {
return it["$" + attrName];
}).join("");
},
function(name) {
return Array.prototype.concat.apply([],
results.map(function(it) {
return it[name];
})
);
},
function(attrName, value) {
results.forEach(function(it) {
it["$" + attrName] = value;
});
},
function(name, value) {
results.forEach(function(it) {
it[name] = value;
});
}
);
}
// Convert single DOM element into "easy" representation
function convertElement(elem) {
// Convert child elements recursively
var converted = elem.childNodes().map(function(it) {
if (it instanceof libxml.Element) {
return convertElement(it);
}
return it;
});
// Save DOM element object
Object.defineProperty(converted, "$", {value: elem});
// Create proxy with helper methods
return createProxy(converted,
function(attrName) {
return elem.attr(attrName) ? elem.attr(attrName).value() : "";
},
function(name) {
return converted.filter(function(it) {
return it.$.name() == name;
});
},
function(attrName, value) {
elem.attr(attrName, value);
},
function(name, value) {
// Child elements
if (value != null && value.constructor == String) {
var matchingElements = this[name];
if (matchingElements.length > 0) {
// Set text of existing elements
matchingElements.forEach(function(it) {
it.$.text(value);
});
} else {
// Create new element
converted.push(convertElement(elem.node(name, value)));
}
} else {
// Remove old elements
for (var i = 0; i < converted.length;) {
if (converted[i].$.name() == name) {
converted[i].$.remove();
converted.splice(i, 1);
} else {
i++;
}
}
if (value == null) {
return;
}
function addChildElement(obj) {
if (obj.$) {
converted.$.addChild(obj.$);
converted.push(convertElement(obj.$));
} else {
// Create new element
var child = convertElement(elem.node(name));
converted.push(child);
// Create child elements
Object.keys(obj).forEach(function(it) {
child[it] = obj[it];
});
}
}
if (!value.$ && value.constructor == Array) {
value.forEach(function(it) {
addChildElement(it);
});
} else {
addChildElement(value);
}
}
}
);
}