-
Notifications
You must be signed in to change notification settings - Fork 12
/
node-gumbo.js
40 lines (32 loc) · 1.09 KB
/
node-gumbo.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
var extend = require("util")._extend;
var bind = require("./build/Release/binding");
module.exports = function Gumbo(text, config) {
config = extend(config || {});
// Work for old API;
if(config.fragment === true) {
config.fragmentContext = "body";
config.fragmentNamespace = "html";
delete config.fragment;
}
if(config.fragmentContext && !config.fragmentNamespace) {
config.fragmentNamespace = "html";
} else if (config.fragmentNamespace) {
config.fragmentNamespace = config.fragmentNamespace.toLowerCase();
}
// Sanity check on namespace
if("fragmentNamespace" in config &&
["svg", "html", "mathml"].indexOf(config.fragmentNamespace) === -1) {
throw new Error("Invalid namespace: Valid namespaces are 'svg', 'html', 'mathml'");
}
var parsedDocument = bind.gumbo.call(this, text, config);
if(config.fragmentContext) {
return parsedDocument.root;
}
// extract the root tag from document resp
try {
parsedDocument.root = parsedDocument.document.childNodes[0];
} catch(e) {
// TODO: Say smth smart here
}
return parsedDocument;
};