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 @@ ...@@ -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,8 @@ function fakeDocument(html) { ...@@ -52,6 +52,8 @@ 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.assertEquals(clientutils.findAll('li', scope).length, 2, 'ClientUtils.findAll() can find matching DOM elements within a given scope');
56 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); 57 fakeDocument(null);
56 })(casper); 58 })(casper);
57 59
...@@ -63,8 +65,8 @@ function fakeDocument(html) { ...@@ -63,8 +65,8 @@ function fakeDocument(html) {
63 casper.test.assertNot(clientutils.findOne('ol'), 'ClientUtils.findOne() can find a matching DOM element'); 65 casper.test.assertNot(clientutils.findOne('ol'), 'ClientUtils.findOne() can find a matching DOM element');
64 // scoped 66 // scoped
65 var scope = clientutils.findOne('ul'); 67 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'); 68 casper.test.assertType(clientutils.findOne('li', scope), 'htmllielement', 'ClientUtils.findOne() can find a matching DOM element 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'); 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 });
......