Commit 15e19111 15e191112a1c09e31cedb73b40576bcbe4064137 by Dave Lee

refs #300 - Heed scope for find's using XPath expressions

This ensures that findOne() and findAll() observe the scope for XPath expressions,
not just when passed CSS selectors.
1 parent 1f1d0872
......@@ -262,7 +262,7 @@
try {
var pSelector = this.processSelector(selector);
if (pSelector.type === 'xpath') {
return this.getElementsByXPath(pSelector.path);
return this.getElementsByXPath(pSelector.path, scope);
} else {
return scope.querySelectorAll(pSelector.path);
}
......@@ -283,7 +283,7 @@
try {
var pSelector = this.processSelector(selector);
if (pSelector.type === 'xpath') {
return this.getElementByXPath(pSelector.path);
return this.getElementByXPath(pSelector.path, scope);
} else {
return scope.querySelector(pSelector.path);
}
......@@ -395,11 +395,13 @@
/**
* Retrieves a single DOM element matching a given XPath expression.
*
* @param String expression The XPath expression
* @param String expression The XPath expression
* @param HTMLElement|null scope Element to search child elements within
* @return HTMLElement or null
*/
this.getElementByXPath = function getElementByXPath(expression) {
var a = document.evaluate(expression, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
this.getElementByXPath = function getElementByXPath(expression, scope) {
scope = scope || this.options.scope;
var a = document.evaluate(expression, scope, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
if (a.snapshotLength > 0) {
return a.snapshotItem(0);
}
......@@ -408,12 +410,14 @@
/**
* Retrieves all DOM elements matching a given XPath expression.
*
* @param String expression The XPath expression
* @param String expression The XPath expression
* @param HTMLElement|null scope Element to search child elements within
* @return Array
*/
this.getElementsByXPath = function getElementsByXPath(expression) {
this.getElementsByXPath = function getElementsByXPath(expression, scope) {
scope = scope || this.options.scope;
var nodes = [];
var a = document.evaluate(expression, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
var a = document.evaluate(expression, scope, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
for (var i = 0; i < a.snapshotLength; i++) {
nodes.push(a.snapshotItem(i));
}
......
......@@ -52,6 +52,7 @@ function fakeDocument(html) {
// scoped
var scope = clientutils.findOne('ul');
casper.test.assertType(clientutils.findAll('li', scope), 'nodelist', 'ClientUtils.findAll() can find matching DOM elements within a given scope');
casper.test.assertType(clientutils.findAll(x('//li'), scope), 'array', 'ClientUtils.findAll() can find matching DOM elements using XPath within a given scope');
fakeDocument(null);
})(casper);
......@@ -65,6 +66,7 @@ function fakeDocument(html) {
var scope = clientutils.findOne('ul');
casper.test.assertType(clientutils.findAll('li', scope), 'nodelist', 'ClientUtils.findAll() can find matching DOM elements within a given scope');
casper.test.assertEquals(clientutils.findAll('li', scope).length, 2, 'ClientUtils.findAll() can find matching DOM elements within a given scope');
casper.test.assertType(clientutils.findOne(x('//li'), scope), 'htmllielement', 'ClientUtils.findOne() can find a matching DOM element using XPath within a given scope');
fakeDocument(null);
})(casper);
......@@ -109,5 +111,5 @@ function fakeDocument(html) {
})(casper);
casper.run(function() {
this.test.done(28);
this.test.done(30);
});
......