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 @@ ...@@ -262,7 +262,7 @@
262 try { 262 try {
263 var pSelector = this.processSelector(selector); 263 var pSelector = this.processSelector(selector);
264 if (pSelector.type === 'xpath') { 264 if (pSelector.type === 'xpath') {
265 return this.getElementsByXPath(pSelector.path); 265 return this.getElementsByXPath(pSelector.path, scope);
266 } else { 266 } else {
267 return scope.querySelectorAll(pSelector.path); 267 return scope.querySelectorAll(pSelector.path);
268 } 268 }
...@@ -283,7 +283,7 @@ ...@@ -283,7 +283,7 @@
283 try { 283 try {
284 var pSelector = this.processSelector(selector); 284 var pSelector = this.processSelector(selector);
285 if (pSelector.type === 'xpath') { 285 if (pSelector.type === 'xpath') {
286 return this.getElementByXPath(pSelector.path); 286 return this.getElementByXPath(pSelector.path, scope);
287 } else { 287 } else {
288 return scope.querySelector(pSelector.path); 288 return scope.querySelector(pSelector.path);
289 } 289 }
...@@ -395,11 +395,13 @@ ...@@ -395,11 +395,13 @@
395 /** 395 /**
396 * Retrieves a single DOM element matching a given XPath expression. 396 * Retrieves a single DOM element matching a given XPath expression.
397 * 397 *
398 * @param String expression The XPath expression 398 * @param String expression The XPath expression
399 * @param HTMLElement|null scope Element to search child elements within
399 * @return HTMLElement or null 400 * @return HTMLElement or null
400 */ 401 */
401 this.getElementByXPath = function getElementByXPath(expression) { 402 this.getElementByXPath = function getElementByXPath(expression, scope) {
402 var a = document.evaluate(expression, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); 403 scope = scope || this.options.scope;
404 var a = document.evaluate(expression, scope, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
403 if (a.snapshotLength > 0) { 405 if (a.snapshotLength > 0) {
404 return a.snapshotItem(0); 406 return a.snapshotItem(0);
405 } 407 }
...@@ -408,12 +410,14 @@ ...@@ -408,12 +410,14 @@
408 /** 410 /**
409 * Retrieves all DOM elements matching a given XPath expression. 411 * Retrieves all DOM elements matching a given XPath expression.
410 * 412 *
411 * @param String expression The XPath expression 413 * @param String expression The XPath expression
414 * @param HTMLElement|null scope Element to search child elements within
412 * @return Array 415 * @return Array
413 */ 416 */
414 this.getElementsByXPath = function getElementsByXPath(expression) { 417 this.getElementsByXPath = function getElementsByXPath(expression, scope) {
418 scope = scope || this.options.scope;
415 var nodes = []; 419 var nodes = [];
416 var a = document.evaluate(expression, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); 420 var a = document.evaluate(expression, scope, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
417 for (var i = 0; i < a.snapshotLength; i++) { 421 for (var i = 0; i < a.snapshotLength; i++) {
418 nodes.push(a.snapshotItem(i)); 422 nodes.push(a.snapshotItem(i));
419 } 423 }
......
...@@ -52,6 +52,7 @@ function fakeDocument(html) { ...@@ -52,6 +52,7 @@ function fakeDocument(html) {
52 // scoped 52 // scoped
53 var scope = clientutils.findOne('ul'); 53 var scope = clientutils.findOne('ul');
54 casper.test.assertType(clientutils.findAll('li', scope), 'nodelist', 'ClientUtils.findAll() can find matching DOM elements within a given scope'); 54 casper.test.assertType(clientutils.findAll('li', scope), 'nodelist', 'ClientUtils.findAll() can find matching DOM elements within a given scope');
55 casper.test.assertType(clientutils.findAll(x('//li'), scope), 'array', 'ClientUtils.findAll() can find matching DOM elements using XPath within a given scope');
55 fakeDocument(null); 56 fakeDocument(null);
56 })(casper); 57 })(casper);
57 58
...@@ -65,6 +66,7 @@ function fakeDocument(html) { ...@@ -65,6 +66,7 @@ function fakeDocument(html) {
65 var scope = clientutils.findOne('ul'); 66 var scope = clientutils.findOne('ul');
66 casper.test.assertType(clientutils.findAll('li', scope), 'nodelist', 'ClientUtils.findAll() can find matching DOM elements within a given scope'); 67 casper.test.assertType(clientutils.findAll('li', scope), 'nodelist', 'ClientUtils.findAll() can find matching DOM elements within a given scope');
67 casper.test.assertEquals(clientutils.findAll('li', scope).length, 2, 'ClientUtils.findAll() can find matching DOM elements within a given scope'); 68 casper.test.assertEquals(clientutils.findAll('li', scope).length, 2, 'ClientUtils.findAll() can find matching DOM elements within a given scope');
69 casper.test.assertType(clientutils.findOne(x('//li'), scope), 'htmllielement', 'ClientUtils.findOne() can find a matching DOM element using XPath within a given scope');
68 fakeDocument(null); 70 fakeDocument(null);
69 })(casper); 71 })(casper);
70 72
...@@ -109,5 +111,5 @@ function fakeDocument(html) { ...@@ -109,5 +111,5 @@ function fakeDocument(html) {
109 })(casper); 111 })(casper);
110 112
111 casper.run(function() { 113 casper.run(function() {
112 this.test.done(28); 114 this.test.done(30);
113 }); 115 });
......