Skip to content

Commit

Permalink
Expose .type value on NodeTest objects (restore previous functionality)
Browse files Browse the repository at this point in the history
  • Loading branch information
JLRishe committed Oct 20, 2020
1 parent 435dfa7 commit 152fcb8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 27 deletions.
24 changes: 24 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
60 changes: 33 additions & 27 deletions xpath.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 152fcb8

Please sign in to comment.