Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,10 @@ describe('xpath', () => {
assert.equal(first3Nodes.length, 6);

assert.equal(first3Nodes[5].textContent, '3章');

const allButFirst = xpath.parse('/*/book[1]/chapter[1]/following::chapter').select({ node: doc });

assert.equal(allButFirst.length, 7);
});

it('should respect reverse axes', () => {
Expand Down
127 changes: 75 additions & 52 deletions xpath.js
Original file line number Diff line number Diff line change
Expand Up @@ -1271,7 +1271,7 @@ var xpath = (typeof exports === 'undefined') ? {} : exports;
if (!s) {
throw new Error('XPath expression unspecified.');
}
if (typeof s !== 'string'){
if (typeof s !== 'string') {
throw new Error('XPath expression must be a string.');
}

Expand Down Expand Up @@ -1312,9 +1312,10 @@ var xpath = (typeof exports === 'undefined') ? {} : exports;
var rhs = [];
for (var i = 0; i < num; i++) {
tokenType.pop();
rhs.unshift(tokenValue.pop());
rhs.push(tokenValue.pop());
state.pop();
}
rhs.reverse();
var s_ = state[state.length - 1];
tokenType.push(XPathParser.productions[XPathParser.actionTableNumber[s].charCodeAt(a - 1) - 32][0]);
if (this.reduceActions[XPathParser.actionTableNumber[s].charCodeAt(a - 1) - 32] == undefined) {
Expand Down Expand Up @@ -1900,6 +1901,76 @@ var xpath = (typeof exports === 'undefined') ? {} : exports;
return null;
};


var applyFollowingStep = function (step, xpc, nodes) {
var newNodes = [];

// TODO: why?
if (xpc.contextNode === xpc.virtualRoot) {
return newNodes;
}

var st = [];
if (xpc.contextNode.firstChild != null) {
st.push(xpc.contextNode.firstChild);
}

for (var m = xpc.contextNode; m != null && m.nodeType !== NodeTypes.DOCUMENT_NODE && m !== xpc.virtualRoot; m = m.parentNode) {
st.push(m.nextSibling);
}
st.reverse();

do {
for (var m = st.pop(); m != null;) {
if (step.nodeTest.matches(m, xpc)) {
newNodes.push(m);
}
if (m.firstChild != null) {
st.push(m.nextSibling);
m = m.firstChild;
} else {
m = m.nextSibling;
}
}
} while (st.length > 0);

return newNodes;
};

var applyPrecedingStep = function (step, xpc) {
var newNodes = [];
var st;

// TODO: Use getRoot instead of this if-else?
if (xpc.virtualRoot != null) {
st = [xpc.virtualRoot];
} else {
// cannot rely on .ownerDocument because the node may be in a document fragment
st = [findRoot(xpc.contextNode)];
}

outer: while (st.length > 0) {
for (var m = st.pop(); m != null;) {
if (m == xpc.contextNode) {
break outer;
}
if (step.nodeTest.matches(m, xpc)) {
newNodes.push(m);
}
if (m.firstChild != null) {
st.push(m.nextSibling);
m = m.firstChild;
} else {
m = m.nextSibling;
}
}
}

newNodes.reverse();

return newNodes;
};

PathExpr.applyStep = function (step, xpc, node) {
if (!node) {
throw new Error('Context node not found when evaluating XPath step: ' + step);
Expand Down Expand Up @@ -2006,32 +2077,7 @@ var xpath = (typeof exports === 'undefined') ? {} : exports;
break;

case Step.FOLLOWING:
if (xpc.contextNode === xpc.virtualRoot) {
break;
}
var st = [];
if (xpc.contextNode.firstChild != null) {
st.unshift(xpc.contextNode.firstChild);
} else {
st.unshift(xpc.contextNode.nextSibling);
}
for (var m = xpc.contextNode.parentNode; m != null && m.nodeType != NodeTypes.DOCUMENT_NODE && m !== xpc.virtualRoot; m = m.parentNode) {
st.unshift(m.nextSibling);
}
do {
for (var m = st.pop(); m != null;) {
if (step.nodeTest.matches(m, xpc)) {
newNodes.push(m);
}
if (m.firstChild != null) {
st.push(m.nextSibling);
m = m.firstChild;
} else {
m = m.nextSibling;
}
}
} while (st.length > 0);
break;
return applyFollowingStep(step, xpc);

case Step.FOLLOWINGSIBLING:
if (xpc.contextNode === xpc.virtualRoot) {
Expand Down Expand Up @@ -2089,30 +2135,7 @@ var xpath = (typeof exports === 'undefined') ? {} : exports;
break;

case Step.PRECEDING:
var st;
if (xpc.virtualRoot != null) {
st = [xpc.virtualRoot];
} else {
// cannot rely on .ownerDocument because the node may be in a document fragment
st = [findRoot(xpc.contextNode)];
}
outer: while (st.length > 0) {
for (var m = st.pop(); m != null;) {
if (m == xpc.contextNode) {
break outer;
}
if (step.nodeTest.matches(m, xpc)) {
newNodes.unshift(m);
}
if (m.firstChild != null) {
st.push(m.nextSibling);
m = m.firstChild;
} else {
m = m.nextSibling;
}
}
}
break;
return applyPrecedingStep(step, xpc);

case Step.PRECEDINGSIBLING:
if (xpc.contextNode === xpc.virtualRoot) {
Expand Down