1 |
function SimpleXMLSerializer () {
|
2 |
}
|
3 |
|
4 |
SimpleXMLSerializer.prototype.writeToString = function (nodeArg) {
|
5 |
var ELEMENT_NODE = 1;
|
6 |
var ATTR_NODE = 2;
|
7 |
var TEXT_NODE = 3;
|
8 |
var CDATA_SECTION_NODE = 4;
|
9 |
var PI_NODE = 7;
|
10 |
var COMMENT_NODE = 8;
|
11 |
var DOCUMENT_NODE = 9;
|
12 |
var isHTMLDocument = false;
|
13 |
var rootElement;
|
14 |
if (nodeArg.ownerDocument) rootElement = nodeArg.ownerDocument.documentElement;
|
15 |
if (!rootElement) { // WinIE 6
|
16 |
rootElement = nodeArg;
|
17 |
while (rootElement.parentNode) {
|
18 |
rootElement = rootElement.parentNode;
|
19 |
}
|
20 |
rootElement = rootElement.documentElement;
|
21 |
}
|
22 |
if (!rootElement.localName && !rootElement.namespaceURI &&
|
23 |
rootElement.nodeName == 'HTML') {
|
24 |
isHTMLDocument = true;
|
25 |
}
|
26 |
var srcs = new Array (nodeArg);
|
27 |
var nsbind = [{xml: 'http://www.w3.org/XML/1998/namespace',
|
28 |
xmlns: 'http://suika.fam.cx/~wakaba/-temp/2003/09/27/undef'}];
|
29 |
nsbind[0][''] = '';
|
30 |
var xescape = function (s) {
|
31 |
return s.replace (/&/g, '&')
|
32 |
.replace (/</g, '<')
|
33 |
.replace (/>/g, '>')
|
34 |
.replace (/"/g, '"');
|
35 |
};
|
36 |
var copynsbind = function (nsbind) {
|
37 |
var newbind = {};
|
38 |
for (var ns in nsbind) {
|
39 |
newbind[ns] = nsbind[ns];
|
40 |
}
|
41 |
return newbind;
|
42 |
};
|
43 |
var copychildren = function (pNode) {
|
44 |
var children = pNode.childNodes;
|
45 |
var childrenLength = children ? children.length : 0;
|
46 |
if (childrenLength == 0 && pNode.nodeType == ATTR_NODE) {
|
47 |
// For WinIE 6 and Opera 8
|
48 |
if (pNode.value) {
|
49 |
return [pNode.value];
|
50 |
} else {
|
51 |
return [];
|
52 |
}
|
53 |
}
|
54 |
var snapshot = [];
|
55 |
for (var i = 0; i < childrenLength; i++) {
|
56 |
snapshot.push (children[i]);
|
57 |
}
|
58 |
return snapshot;
|
59 |
};
|
60 |
var copychildrento = function (pNode, ary) {
|
61 |
var children = pNode.childNodes;
|
62 |
var childrenLength = children.length;
|
63 |
for (var i = 0; i < childrenLength; i++) {
|
64 |
ary.push (children[i]);
|
65 |
}
|
66 |
return ary;
|
67 |
};
|
68 |
var r = '';
|
69 |
while (true) {
|
70 |
var src = srcs.shift ();
|
71 |
if (!src) break;
|
72 |
if (src instanceof Array) {
|
73 |
nsbind.pop (); // End tag
|
74 |
} else if (src instanceof String || typeof (src) == 'string') {
|
75 |
r += src;
|
76 |
} else { // Node
|
77 |
if (src.nodeType == ELEMENT_NODE) {
|
78 |
var csrc = [];
|
79 |
var etag;
|
80 |
var ns = copynsbind (nsbind[nsbind.length - 1]);
|
81 |
nsbind.push (ns);
|
82 |
var attrr = {};
|
83 |
|
84 |
var defpfx = {};
|
85 |
var ansao = {};
|
86 |
var nodeAttrs = src.attributes;
|
87 |
var nodeAttrsLength = nodeAttrs.length;
|
88 |
for (var i = 0; i < nodeAttrsLength; i++) {
|
89 |
var attr = nodeAttrs[i];
|
90 |
if (attr.localName == null) {
|
91 |
// Non-namespace attribute
|
92 |
if (attr.nodeValue) {
|
93 |
if (isHTMLDocument) {
|
94 |
attrr[attr.nodeName.toLowerCase ()] = copychildren (attr);
|
95 |
} else {
|
96 |
attrr[attr.nodeName] = copychildren (attr);
|
97 |
}
|
98 |
}
|
99 |
} else if (attr.namespaceURI &&
|
100 |
attr.namespaceURI == 'http://www.w3.org/2000/xmlns/') {
|
101 |
// Namespace attribute
|
102 |
var nsuri = attr.value;
|
103 |
if (attr.localName == 'xmlns') {
|
104 |
// Default namespace
|
105 |
ns[''] = nsuri;
|
106 |
attrr['xmlns'] = copychildren (attr);
|
107 |
} else {
|
108 |
// Prefixed namespace
|
109 |
if (nsuri.length > 0) {
|
110 |
ns[attr.localName] = nsuri;
|
111 |
} else {
|
112 |
ns[attr.localName]
|
113 |
= 'http://suika.fam.cx/~wakaba/-temp/2003/09/27/undef';
|
114 |
}
|
115 |
attrr['xmlns:' + attr.localName] = copychildren (attr);
|
116 |
}
|
117 |
} else if (attr.namespaceURI) {
|
118 |
// Global partition attribute
|
119 |
var pfx;
|
120 |
var ans = attr.namespaceURI;
|
121 |
if (!(defpfx[ans] != null)) defpfx[ans] = null;
|
122 |
PFX: {
|
123 |
if (attr.prefix) {
|
124 |
if (ns[attr.prefix]) {
|
125 |
if (ns[attr.prefix] == ans) {
|
126 |
// The namespace is already defined
|
127 |
pfx = attr.prefix;
|
128 |
if (!defpfx[ans]) defpfx[ans] = pfx;
|
129 |
break PFX;
|
130 |
}
|
131 |
} else {
|
132 |
// The namespace prefix is not defined yet
|
133 |
pfx = attr.prefix;
|
134 |
if (!defpfx[ans]) defpfx[ans] = pfx;
|
135 |
ns[pfx] = ans;
|
136 |
attrr['xmlns:' + pfx] = [xescape (ans)];
|
137 |
break PFX;
|
138 |
}
|
139 |
}
|
140 |
if (defpfx[ans] != null) {
|
141 |
pfx = defpfx[ans];
|
142 |
break PFX;
|
143 |
}
|
144 |
} // PFX
|
145 |
if (!ansao[ans]) ansao[ans] = [];
|
146 |
ansao[ans].push ([pfx, attr]);
|
147 |
} else {
|
148 |
// Per-element type partition attribute
|
149 |
attrr[attr.localName] = copychildren (attr);
|
150 |
}
|
151 |
} // Attributes
|
152 |
|
153 |
// Prefix for global attributes
|
154 |
PFX: for (var ans in defpfx) {
|
155 |
if (defpfx[ans] != null) continue PFX;
|
156 |
|
157 |
// No prefix available from the attribute nodes
|
158 |
|
159 |
// Available from already defined namespaces?
|
160 |
P: for (var pfx in ns) {
|
161 |
if (ns[pfx] != ans) continue P;
|
162 |
if (pfx.length > 0) {
|
163 |
defpfx[ans] = pfx;
|
164 |
continue PFX;
|
165 |
}
|
166 |
}
|
167 |
|
168 |
// Available from the element itself?
|
169 |
if ((src.namespaceURI != null) &&
|
170 |
(src.namespaceURI == ans) &&
|
171 |
(src.prefix != null)) {
|
172 |
if (ns[src.prefix] == ans) {
|
173 |
// The namespace is already defined
|
174 |
defpfx[ans] = src.prefix;
|
175 |
continue PFX;
|
176 |
} else {
|
177 |
// The namespace is not defined yet
|
178 |
defpfx[ans] = src.prefix;
|
179 |
ns[defpfx[ans]] = ans;
|
180 |
attrr['xmlns:' + defpfx[ans]] = [xescape (ans)];
|
181 |
continue PFX;
|
182 |
}
|
183 |
}
|
184 |
|
185 |
// No prefix is defined anywhere
|
186 |
var i = 1;
|
187 |
while (ns['ns' + i] != null) {i++}
|
188 |
defpfx = 'ns' + i;
|
189 |
ns[defpfx[ans]] = ans;
|
190 |
attrr['xmlns:ns' + i] = [xescape (ans)];
|
191 |
} // PFX
|
192 |
|
193 |
for (var ans in ansao) {
|
194 |
for (var ansn in ansao[ans]) {
|
195 |
var pfx = ansao[ans][ansn][0] ? ansao[ans][ansn][0] : defpfx[ans];
|
196 |
attrr[pfx + ':' + ansao[ans][ansn][1].localName]
|
197 |
= copychildren (ansao[ans][ansn][1]);
|
198 |
}
|
199 |
}
|
200 |
|
201 |
// Element type name
|
202 |
if (src.localName != null) {
|
203 |
if (src.namespaceURI != null) {
|
204 |
if (src.prefix != null &&
|
205 |
ns[src.prefix] != null &&
|
206 |
ns[src.prefix] == src.namespaceURI) {
|
207 |
// Non-null namespace and its prefix is defined
|
208 |
r += '<' + src.prefix + ':' + src.localName;
|
209 |
etag = '</' + src.prefix + ':' + src.localName + '>';
|
210 |
} else if (src.prefix != null &&
|
211 |
ns[src.prefix] == null) {
|
212 |
attrr['xmlns:' + src.prefix] = [xescape (src.namespaceURI)];
|
213 |
ns[src.prefix] = src.namespaceURI;
|
214 |
r += '<' + src.prefix + ':' + src.localName;
|
215 |
etag = '</' + src.prefix + ':' + src.localName + '>';
|
216 |
} else {
|
217 |
PFX0: {
|
218 |
// Non-null namespace and its prefix is not defined
|
219 |
// but is already declared as a namespace attribute
|
220 |
P0: for (var pfx in ns) {
|
221 |
if (ns[pfx] != src.namespaceURI) continue P0;
|
222 |
if (pfx.length > 0) {
|
223 |
r += '<' + pfx + ':' + src.localName;
|
224 |
etag = '</' + pfx + ':' + src.localName + '>';
|
225 |
} else {
|
226 |
r += '<' + src.localName;
|
227 |
etag = '</' + src.localName + '>';
|
228 |
}
|
229 |
break PFX0;
|
230 |
}
|
231 |
|
232 |
// Non-null namespace and its prefix is not defined anywhere
|
233 |
var i = 1;
|
234 |
while (ns['ns' + i] != null) i++;
|
235 |
ns['ns' + i] = src.namespaceURI;
|
236 |
attrr['xmlns:ns' + i] = [xescape (src.namespaceURI)];
|
237 |
r += '<ns' + i + ':' + src.localName;
|
238 |
etag = '</ns' + i + ':' + src.localName + '>';
|
239 |
} // PFX0
|
240 |
}
|
241 |
} else {
|
242 |
// Null-namespace
|
243 |
if (ns[''] != '') {
|
244 |
// The default namespace is not the null-namespace
|
245 |
ns[''] = '';
|
246 |
attrr['xmlns'] = [''];
|
247 |
}
|
248 |
r += '<' + src.localName;
|
249 |
etag = '</' + src.localName + '>';
|
250 |
}
|
251 |
} else {
|
252 |
// Non-namespace node
|
253 |
if (isHTMLDocument) {
|
254 |
r += '<' + src.nodeName.toLowerCase ();
|
255 |
etag = '</' + src.nodeName.toLowerCase () + '>';
|
256 |
} else {
|
257 |
r += '<' + src.nodeName;
|
258 |
etag = '</' + src.nodeName + '>';
|
259 |
}
|
260 |
}
|
261 |
|
262 |
// The attribute specifications
|
263 |
for (var an in attrr) {
|
264 |
csrc.push (' ' + an + '="');
|
265 |
for (var i = 0; i < attrr[an].length; i++) {
|
266 |
csrc.push (attrr[an][i]);
|
267 |
}
|
268 |
csrc.push ('"');
|
269 |
}
|
270 |
|
271 |
// The child nodes
|
272 |
if (src.hasChildNodes ()) {
|
273 |
csrc.push ('>');
|
274 |
copychildrento (src, csrc);
|
275 |
csrc.push (etag, []);
|
276 |
} else if (this.UseEmptyElemTag[src.namespaceURI] &&
|
277 |
this.UseEmptyElemTag[src.namespaceURI]
|
278 |
[src.localName ? src.localName : src.nodeName]) {
|
279 |
csrc.push (' />');
|
280 |
nsbind.shift ();
|
281 |
} else {
|
282 |
csrc.push ('>' + etag, []);
|
283 |
}
|
284 |
for (var i = csrc.length - 1; i >= 0; i--) {
|
285 |
srcs.unshift (csrc[i]);
|
286 |
}
|
287 |
csrc = [];
|
288 |
} else if (src.nodeType == TEXT_NODE) {;
|
289 |
r += xescape (src.data);
|
290 |
} else if (src.nodeType == CDATA_SECTION_NODE) {
|
291 |
r += '<![CDATA[' + src.data.replace (/]]>/g, ']]]]>><![CDATA[') + ']]>';
|
292 |
} else if (src.nodeType == PI_NODE) {
|
293 |
r += '<?' + src.target;
|
294 |
if (src.data != null && src.data.length > 0) {
|
295 |
r += ' ' + src.data.replace (/\?>/g, '?>');
|
296 |
}
|
297 |
r += '?>';
|
298 |
} else if (src.nodeType == COMMENT_NODE) {
|
299 |
r += '<!--' + src.data.replace (/--/g, '- - ') + '-->';
|
300 |
} else if (src.nodeType == DOCUMENT_NODE) {
|
301 |
var children = src.childNodes;
|
302 |
var childrenLength = children.length;
|
303 |
for (var i = 0; i < childrenLength; i++) {
|
304 |
srcs.unshift (children[i]);
|
305 |
srcs.unshift ("\n");
|
306 |
}
|
307 |
} // nodeType
|
308 |
}
|
309 |
}
|
310 |
return r;
|
311 |
};
|
312 |
SimpleXMLSerializer.prototype.UseEmptyElemTag = {};
|
313 |
SimpleXMLSerializer.prototype.UseEmptyElemTag['http://www.w3.org/1999/xhtml'] = {
|
314 |
base: true,
|
315 |
basefont: true,
|
316 |
bgsound: true,
|
317 |
br: true,
|
318 |
frame: true,
|
319 |
img: true,
|
320 |
input: true,
|
321 |
isindex: true,
|
322 |
link: true,
|
323 |
meta: true,
|
324 |
nextid: true,
|
325 |
wbr: true
|
326 |
};
|
327 |
|
328 |
/* Revision: $Date: 2005/04/27 01:33:25 $ */
|
329 |
|
330 |
/* ***** BEGIN LICENSE BLOCK *****
|
331 |
* Copyright 2005 Wakaba <w@suika.fam.cx>. All rights reserved.
|
332 |
*
|
333 |
* This program is free software; you can redistribute it and/or
|
334 |
* modify it under the same terms as Perl itself.
|
335 |
*
|
336 |
* Alternatively, the contents of this file may be used
|
337 |
* under the following terms (the "MPL/GPL/LGPL"),
|
338 |
* in which case the provisions of the MPL/GPL/LGPL are applicable instead
|
339 |
* of those above. If you wish to allow use of your version of this file only
|
340 |
* under the terms of the MPL/GPL/LGPL, and not to allow others to
|
341 |
* use your version of this file under the terms of the Perl, indicate your
|
342 |
* decision by deleting the provisions above and replace them with the notice
|
343 |
* and other provisions required by the MPL/GPL/LGPL. If you do not delete
|
344 |
* the provisions above, a recipient may use your version of this file under
|
345 |
* the terms of any one of the Perl or the MPL/GPL/LGPL.
|
346 |
*
|
347 |
* "MPL/GPL/LGPL":
|
348 |
*
|
349 |
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
350 |
*
|
351 |
* The contents of this file are subject to the Mozilla Public License Version
|
352 |
* 1.1 (the "License"); you may not use this file except in compliance with
|
353 |
* the License. You may obtain a copy of the License at
|
354 |
* <http://www.mozilla.org/MPL/>
|
355 |
*
|
356 |
* Software distributed under the License is distributed on an "AS IS" basis,
|
357 |
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
358 |
* for the specific language governing rights and limitations under the
|
359 |
* License.
|
360 |
*
|
361 |
* The Original Code is manakai SimpleXMLSerializer code.
|
362 |
*
|
363 |
* The Initial Developer of the Original Code is Wakaba.
|
364 |
* Portions created by the Initial Developer are Copyright (C) 2005
|
365 |
* the Initial Developer. All Rights Reserved.
|
366 |
*
|
367 |
* Contributor(s):
|
368 |
* Wakaba <w@suika.fam.cx>
|
369 |
*
|
370 |
* Alternatively, the contents of this file may be used under the terms of
|
371 |
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
372 |
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
373 |
* in which case the provisions of the GPL or the LGPL are applicable instead
|
374 |
* of those above. If you wish to allow use of your version of this file only
|
375 |
* under the terms of either the GPL or the LGPL, and not to allow others to
|
376 |
* use your version of this file under the terms of the MPL, indicate your
|
377 |
* decision by deleting the provisions above and replace them with the notice
|
378 |
* and other provisions required by the LGPL or the GPL. If you do not delete
|
379 |
* the provisions above, a recipient may use your version of this file under
|
380 |
* the terms of any one of the MPL, the GPL or the LGPL.
|
381 |
*
|
382 |
* ***** END LICENSE BLOCK ***** */
|
383 |
|