/
dom-js.js
180 lines (162 loc) · 4.12 KB
/
dom-js.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
var sax = require("sax");
var strict = true;
/**
* Really simple XML DOM implementation based on sax that works with Strings.
*
* If you have an XML string and want a DOM this utility is convenient.
*
* var domjs = new DomJS();
* domjs.parse(xmlString, function(err, dom) {
*
* });
*
* If you want to compile C there are versions based on libxml2
* and jsdom is full featured but complicated.
*
* This is "lightweight" meaning really simple and serves my purpose, it does not support namespaces or all
* of the features of XML 1.0 it just takes a string and returns a JavaScript object graph.
*
* There are only three types of object supported Element, Text and Comment.
*
* e.g.
*
* take <xml><elem att="val1"/><elem att="val1"/><elem att="val1"/></xml>
*
* return { name : "xml",
* attributes : {}
* children [
* { name : "elem", attributes : {att:'val1'}, children [] },
* { name : "elem", attributes : {att:'val1'}, children [] },
* { name : "elem", attributes : {att:'val1'}, children [] }
* ]
* }
*
* The object returned can be serialized back out with obj.toXml();
*
*
* @constructor DomJS
*/
DomJS = function() {
this.root = null;
this.stack = new Array();
this.currElement = null;
this.error = false;
};
DomJS.prototype.parse = function(string, cb) {
if (typeof string != 'string') {
cb(true, 'Data is not a string');
return;
}
var self = this;
parser = sax.parser(strict);
parser.onerror = function (err) {
this.error = true;
cb(true, err);
};
parser.ontext = function (text) {
if (self.currElement == null) {
// console.log("Content in the prolog " + text);
return;
}
textNode = new Text(text);
self.currElement.children.push(textNode);
};
parser.onopentag = function (node) {
var elem = new Element(node.name, node.attributes);
if (self.root == null) {
self.root = elem;
}
if (self.currElement != null) {
self.currElement.children.push(elem);
}
self.currElement = elem;
self.stack.push(self.currElement);
};
parser.onclosetag = function (node) {
self.stack.pop();
self.currElement = self.stack[self.stack.length - 1 ];// self.stack.peek();
};
parser.oncomment = function (comment) {
if (self.currElement == null) {
console.log("Comments in the prolog discarded " + comment);
return;
}
commentNode = new Comment(comment);
self.currElement.children.push(commentNode);
};
parser.onend = function () {
if ( self.error == false) {
cb(false, self.root);
}
};
parser.write(string).close();
};
DomJS.prototype.reset = function() {
this.root = null;
this.stack = new Array();
this.currElement = null;
this.error = false;
};
escape = function(string) {
return string.replace(/&/g, '&')
.replace(/>/g, '>')
.replace(/</g, '<')
.replace(/"/g, '"')
.replace(/'/g, ''');
};
Element = function(name, attributes, children ) {
this.name = name;
this.attributes = attributes || [];
this.children = children || [];
this.toXml = function(sb) {
if (typeof sb == 'undefined') {
sb = {buf:''}; // Strings are pass by value in JS it seems
}
sb.buf += '<' + this.name;
for (att in this.attributes) {
sb.buf += ' ' + att + '="' + escape(this.attributes[att]) + '"';
}
if (this.children.length != 0) {
sb.buf += '>';
for (var i = 0 ; i < this.children.length ; i++) {
this.children[i].toXml(sb);
}
sb.buf += '</' + this.name + '>';
}
else {
sb.buf += '/>';
}
return sb.buf;
};
this.firstChild = function() {
if ( this.children.length > 0) {
return this.children[0];
}
return null;
};
this.text = function() {
if ( this.children.length > 0) {
if (typeof this.children[0].text == 'string') {
return this.children[0].text;
};
}
return null;
};
};
Text = function(data){
this.text = data;
this.toXml = function(sb) {
sb.buf += escape(this.text);
};
};
Comment = function(comment) {
this.comment = comment;
this.toXml = function(sb) {
sb.buf += '<!--' + this.comment + '-->';
};
};
exports.Element = Element;
exports.Text = Text;
exports.Comment = Comment;
exports.DomJS = DomJS;
exports.escape = escape;