Skip to content

Commit

Permalink
fix: typecheck source param before parsing (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
jesse-y committed Aug 26, 2020
1 parent 3609c88 commit bd81fab
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
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);
}

0 comments on commit bd81fab

Please sign in to comment.