diff --git a/test.js b/test.js index 9a8ad00..746f10f 100644 --- a/test.js +++ b/test.js @@ -976,6 +976,30 @@ describe('xpath', () => { }); }); + describe('.parse()', () => { + it('should correctly set types on path steps', () => { + const parsed = xpath.parse('./my:*/my:name'); + + const steps = parsed.expression.expression.locationPath.steps; + + const step0 = steps[0]; + + assert.strictEqual(xpath.NodeTest.NODE, step0.nodeTest.type); + + const step1 = steps[1]; + + assert.strictEqual(xpath.NodeTest.NAMETESTPREFIXANY, step1.nodeTest.type); + assert.strictEqual('my', step1.nodeTest.prefix); + + const step2 = steps[2]; + + assert.strictEqual(xpath.NodeTest.NAMETESTQNAME, step2.nodeTest.type); + assert.strictEqual('my', step2.nodeTest.prefix); + assert.strictEqual('name', step2.nodeTest.localName); + assert.strictEqual('my:name', step2.nodeTest.name); + }); + }) + describe('miscellaneous', () => { it('should create XPathExceptions that act like Errors', () => { try { diff --git a/xpath.js b/xpath.js index 4882a27..d58a4b8 100644 --- a/xpath.js +++ b/xpath.js @@ -2319,12 +2319,10 @@ var xpath = (typeof exports === 'undefined') ? {} : exports; NodeTest.makeNodeTestType = function (type, members, ctor) { var newType = ctor || function () { }; - newType.prototype = new NodeTest(members.type); - newType.prototype.constructor = type; + newType.prototype = new NodeTest(type); + newType.prototype.constructor = newType; - for (var key in members) { - newType.prototype[key] = members[key]; - } + assign(newType.prototype, members); return newType; }; @@ -2364,32 +2362,40 @@ var xpath = (typeof exports === 'undefined') ? {} : exports; : localName === nLocalName; }; - NodeTest.NameTestPrefixAny = NodeTest.makeNodeTestType(NodeTest.NAMETESTPREFIXANY, { - matches: function (n, xpc) { - return NodeTest.isElementOrAttribute(n) && - NodeTest.nameSpaceMatches(this.prefix, xpc, n); + NodeTest.NameTestPrefixAny = NodeTest.makeNodeTestType( + NodeTest.NAMETESTPREFIXANY, + { + matches: function (n, xpc) { + return NodeTest.isElementOrAttribute(n) && + NodeTest.nameSpaceMatches(this.prefix, xpc, n); + }, + toString: function () { + return this.prefix + ":*"; + } }, - toString: function () { - return this.prefix + ":*"; - } - }, function (prefix) { this.prefix = prefix; }); + function NameTestPrefixAny(prefix) { this.prefix = prefix; } + ); - NodeTest.NameTestQName = NodeTest.makeNodeTestType(NodeTest.NAMETESTQNAME, { - matches: function (n, xpc) { - return NodeTest.isNodeType([1, 2, XPathNamespace.XPATH_NAMESPACE_NODE])(n) && - NodeTest.nameSpaceMatches(this.prefix, xpc, n) && - NodeTest.localNameMatches(this.localName, xpc, n); + NodeTest.NameTestQName = NodeTest.makeNodeTestType( + NodeTest.NAMETESTQNAME, + { + matches: function (n, xpc) { + return NodeTest.isNodeType([1, 2, XPathNamespace.XPATH_NAMESPACE_NODE])(n) && + NodeTest.nameSpaceMatches(this.prefix, xpc, n) && + NodeTest.localNameMatches(this.localName, xpc, n); + }, + toString: function () { + return this.name; + } }, - toString: function () { - return this.name; - } - }, function (name) { - var nameParts = name.split(':'); + function NameTestQName(name) { + var nameParts = name.split(':'); - this.name = name; - this.prefix = nameParts.length > 1 ? nameParts[0] : null; - this.localName = nameParts[nameParts.length > 1 ? 1 : 0]; - }); + this.name = name; + this.prefix = nameParts.length > 1 ? nameParts[0] : null; + this.localName = nameParts[nameParts.length > 1 ? 1 : 0]; + } + ); NodeTest.PITest = NodeTest.makeNodeTestType(NodeTest.PI, { matches: function (n, xpc) {