Skip to content
Permalink
Browse files Browse the repository at this point in the history
Ensure parseXml/parseHtml input is string or buffer (#594)
  • Loading branch information
rchipka committed Mar 30, 2022
1 parent d6e4cf3 commit 2501807
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -12,7 +12,7 @@
"package_name": "{node_abi}-{platform}-{arch}.tar.gz"
},
"description": "libxml bindings for v8 javascript engine",
"version": "0.19.7",
"version": "0.19.8",
"scripts": {
"install": "node-pre-gyp install --fallback-to-build --loglevel http",
"test": "node --expose_gc ./node_modules/.bin/nodeunit test"
Expand All @@ -38,4 +38,4 @@
"nodeunit": "~0.11.2",
"semver": "~5.5.0"
}
}
}
12 changes: 8 additions & 4 deletions src/xml_document.cc
Expand Up @@ -375,16 +375,18 @@ NAN_METHOD(XmlDocument::FromHtml)
opts |= HTML_PARSE_NOIMPLIED | HTML_PARSE_NODEFDTD;

htmlDocPtr doc;
if (!node::Buffer::HasInstance(info[0])) {
if (info[0]->IsString()) {
// Parse a string
Nan::Utf8String str(Nan::To<v8::String>(info[0]).ToLocalChecked());
doc = htmlReadMemory(*str, str.length(), baseUrl, encoding, opts);
}
else {
else if (node::Buffer::HasInstance(info[0])) {
// Parse a buffer
v8::Local<v8::Object> buf = Nan::To<v8::Object>(info[0]).ToLocalChecked();
doc = htmlReadMemory(node::Buffer::Data(buf), node::Buffer::Length(buf),
baseUrl, encoding, opts);
} else {
return Nan::ThrowError("XML must be a string or buffer");
}

xmlSetStructuredErrorFunc(NULL, NULL);
Expand Down Expand Up @@ -439,16 +441,18 @@ NAN_METHOD(XmlDocument::FromXml)

int opts = (int) getParserOptions(options);
xmlDocPtr doc;
if (!node::Buffer::HasInstance(info[0])) {
if (info[0]->IsString()) {
// Parse a string
Nan::Utf8String str(Nan::To<v8::String>(info[0]).ToLocalChecked());
doc = xmlReadMemory(*str, str.length(), baseUrl, "UTF-8", opts);
}
else {
else if (node::Buffer::HasInstance(info[0])) {
// Parse a buffer
v8::Local<v8::Object> buf = Nan::To<v8::Object>(info[0]).ToLocalChecked();
doc = xmlReadMemory(node::Buffer::Data(buf), node::Buffer::Length(buf),
baseUrl, encoding, opts);
} else {
return Nan::ThrowError("XML must be a string or buffer");
}

xmlSetStructuredErrorFunc(NULL, NULL);
Expand Down
11 changes: 11 additions & 0 deletions test/html_parser.js
Expand Up @@ -32,6 +32,17 @@ module.exports.parse = function(assert) {
assert.done();
};

module.exports.invalid_input = function(assert) {
try {
libxml.parseHtml({object: true});
assert.ok(false);
} catch(err) {
assert.ok(true)
}

assert.done();
};

// Although libxml defaults to a utf-8 encoding, if not specifically specified
// it will guess the encoding based on meta http-equiv tags available
// This test shows that the "guessed" encoding can be overridden
Expand Down
11 changes: 11 additions & 0 deletions test/xml_parser.js
Expand Up @@ -21,6 +21,17 @@ module.exports.parse = function(assert) {
assert.done();
};

module.exports.invalid_input = function(assert) {
try {
libxml.parseXml({object: true});
assert.ok(false);
} catch(err) {
assert.ok(true)
}

assert.done();
};

module.exports.parse_buffer = function(assert) {
var filename = __dirname + '/fixtures/parser-utf16.xml';
var buf = fs.readFileSync(filename);
Expand Down

0 comments on commit 2501807

Please sign in to comment.