forked from znerol/node-xmlshim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (42 loc) · 1.9 KB
/
index.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
var jsdom = require("jsdom"),
xml = require("libxmljs"),
handlersForDocument = require("./domparser.js").handlersForDocument,
DOMWriter = require("./domwriter.js").DOMWriter;
var impl = jsdom.jsdom(undefined, { parsingMode: "xml" }).implementation;
exports.DOMParser = function() {
}
exports.DOMParser.prototype.parseFromString = function(str, mime) {
var doc = impl.createDocument("", "", null);
parser = new xml.SaxParser(handlersForDocument(doc));
parser.parseString(str);
return doc;
}
exports.XMLSerializer = function() {
}
exports.XMLSerializer.prototype.serializeToString = function(doc) {
var textwriter = new xml.TextWriter(),
domwriter = new DOMWriter(textwriter),
result;
// libxml xmlTextWriterStartDocument not only writes the document preamble
// but also sets up functions used for document encoding. Therefore we
// need to call startDocument in any case, also for document fragments and
// single nodes. By calling outputMemory immediately after starting the
// document, we get rid of the preamble.
textwriter.startDocument();
textwriter.outputMemory(); // flush buffer and discard preamble
if (doc.nodeType !== doc.DOCUMENT_NODE) {
// Wrap contents into an element in order to force libxml textwriter
// into element-mode. This will trigger proper entity escaping for text
// nodes.
textwriter.startElementNS(undefined, 'dummy');
textwriter.writeString(''); // close dummy start-tag
textwriter.outputMemory(); // flush buffer
}
domwriter.writeNode(doc); // write actual contents
result = textwriter.outputMemory(); // collect results
// let's play nice and get the textwriter into a proper state again
textwriter.endDocument();
textwriter.outputMemory(); // flush buffer
return result;
};
exports.implementation = impl;