Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: typecheck source param before parsing #113

Merged
merged 1 commit into from Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/dom-parser.js
Expand Up @@ -21,7 +21,7 @@ DOMParser.prototype.parseFromString = function(source,mimeType){
defaultNSMap['']= 'http://www.w3.org/1999/xhtml';
}
defaultNSMap.xml = defaultNSMap.xml || 'http://www.w3.org/XML/1998/namespace';
if(source){
if(source && typeof source === 'string'){
sax.parse(source,defaultNSMap,entityMap);
}else{
sax.errorHandler.error("invalid doc source");
Expand Down
34 changes: 33 additions & 1 deletion test/parse/simple.vows.js
Expand Up @@ -46,7 +46,39 @@ wows.describe('parse').addBatch({
var dom = parser.parseFromString(xmlLineError, "text/xml");
var node = dom.documentElement.firstChild.nextSibling
assert(node.lineNumber, 7);
}
},
'invalid input - falsy string': runParserWith(''),
'invalid input - not a string': runParserWith({}),
'invalid input - number': runParserWith(12345),
'invalid input - null': runParserWith(null)
}).export(module);

function runParserWith (testValue) {
return function () {
var parser = new DOMParser(rethrowErrorHandler());

try {
parser.parseFromString(testValue);
// If the above line doesn't throw then fail the test
assert.isTrue(false);
} catch (e) {
assert.isTrue(isInvalidDocSource(e));
}
}
}

function rethrowErrorHandler () {
return {
errorHandler: {
error: function (errorMessage) {
throw errorMessage;
}
}
}
}

function isInvalidDocSource (errorMessage) {
// Errors that are thrown are embedded within a string containing locator data. Infer the original
// error message via regex
return /^\[xmldom error\][\s]*invalid doc source[\s\S]*$/.test(errorMessage);
}