Commit 715a4cc8 715a4cc809c47a0bfea51866e8e9b1f37042c494 by Nicolas Perriault

Merge pull request #301 from kastiglione/scoped-xpath-finds

refs #300 - Heed scope for find's using XPath expressions
2 parents 676a1143 59ffeb9a
......@@ -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,8 @@ 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.assertEquals(clientutils.findAll('li', scope).length, 2, '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);
......@@ -63,8 +65,8 @@ function fakeDocument(html) {
casper.test.assertNot(clientutils.findOne('ol'), 'ClientUtils.findOne() can find a matching DOM element');
// 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.assertEquals(clientutils.findAll('li', scope).length, 2, 'ClientUtils.findAll() can find matching DOM elements within a given scope');
casper.test.assertType(clientutils.findOne('li', scope), 'htmllielement', 'ClientUtils.findOne() can find a matching DOM element 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);
});
......