Skip to content

Commit

Permalink
Reject non-string expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
JLRishe committed Dec 16, 2023
1 parent 35dabde commit 62f02a1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
9 changes: 8 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1305,11 +1305,18 @@ describe('xpath', () => {
it('should reject unspecified expression', () => {
for (let expr of [null, undefined, '']) {
assert.throws(() => xpath.parse(expr), {
message: 'XPath expression unspecified',
message: 'XPath expression unspecified.',
});
}
});

it('should reject non-string expression', () => {
for (let expr of [{}, []]) {
assert.throws(() => xpath.parse(expr), {
message: 'XPath expression must be a string.',
});
}
});
it('should reject non-nodes', () => {
for (let node of ['<n />', 0, 45, true, false, [], {}]) {
assert.throws(() => xpath.parse('/*').select({ node }), {
Expand Down
5 changes: 4 additions & 1 deletion xpath.js
Original file line number Diff line number Diff line change
Expand Up @@ -1269,7 +1269,10 @@ var xpath = (typeof exports === 'undefined') ? {} : exports;

XPathParser.prototype.parse = function (s) {
if (!s) {
throw new Error('XPath expression unspecified');
throw new Error('XPath expression unspecified.');
}
if (typeof s !== 'string'){
throw new Error('XPath expression must be a string.');
}

var types;
Expand Down

0 comments on commit 62f02a1

Please sign in to comment.