Bug 1164195 - Add a resultType param to $x console helper. .
☠☠ backed out by c4a4baf50679 ☠ ☠
authorDhruvi Butti <dhruvibutti9477@gmail.com>
Wed, 13 Mar 2019 06:39:50 +0000
changeset 521663 52e8296f4d36
parent 521662 303a1f4c20e8
child 521664 c4a4baf50679
push id10867
push userdvarga@mozilla.com
push dateThu, 14 Mar 2019 15:20:45 +0000
treeherdermozilla-beta@abad13547875 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
bugs1164195
milestone67.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1164195 - Add a resultType param to $x console helper. . This makes it possible to pass a third parameter which is a XPathResult constant. Test cases are added to ensure this works as expected. Differential Revision: https://phabricator.services.mozilla.com/D21476
devtools/client/webconsole/test/mochitest/browser_jsterm_helper_dollar_x.js
devtools/server/actors/webconsole/utils.js
--- a/devtools/client/webconsole/test/mochitest/browser_jsterm_helper_dollar_x.js
+++ b/devtools/client/webconsole/test/mochitest/browser_jsterm_helper_dollar_x.js
@@ -32,9 +32,60 @@ async function performTests() {
   jsterm.execute("$x('.//li')");
   let message = await onMessage;
   ok(message, "`$x` worked");
 
   onMessage = waitForMessage(hud, "<li>");
   jsterm.execute("$x('.//li', document.body)[0]");
   message = await onMessage;
   ok(message, "`$x()` result can be used right away");
+
+  onMessage = waitForMessage(hud, "2");
+  jsterm.execute("$x('count(.//li)', document.body, XPathResult.NUMBER_TYPE)");
+  message = await onMessage;
+  ok(message, "$x works as expected with XPathResult.NUMBER_TYPE");
+
+  onMessage = waitForMessage(hud, "First");
+  jsterm.execute("$x('.//li', document.body, XPathResult.STRING_TYPE)");
+  message = await onMessage;
+  ok(message, "$x works as expected with XPathResult.STRING_TYPE");
+
+  onMessage = waitForMessage(hud, "true");
+  jsterm.execute("$x('//li[not(@foo)]', document.body, XPathResult.BOOLEAN_TYPE)");
+  message = await onMessage;
+  ok(message, "$x works as expected with XPathResult.BOOLEAN_TYPE");
+
+  onMessage = waitForMessage(hud, "Array [ li, li ]");
+  jsterm
+  .execute("$x('.//li', document.body, XPathResult.UNORDERED_NODE_ITERATOR_TYPE)");
+  message = await onMessage;
+  ok(message, "$x works as expected with XPathResult.UNORDERED_NODE_ITERATOR_TYPE");
+
+  onMessage = waitForMessage(hud, "Array [ li, li ]");
+  jsterm
+  .execute("$x('.//li', document.body, XPathResult.ORDERED_NODE_ITERATOR_TYPE)");
+  message = await onMessage;
+  ok(message, "$x works as expected with XPathResult.ORDERED_NODE_ITERATOR_TYPE");
+
+  onMessage = waitForMessage(hud, "<li>");
+  jsterm
+  .execute("$x('.//li', document.body, XPathResult.ANY_UNORDERED_NODE_TYPE)");
+  message = await onMessage;
+  ok(message, "$x works as expected with XPathResult.ANY_UNORDERED_NODE_TYPE");
+
+  onMessage = waitForMessage(hud, "<li>");
+  jsterm
+  .execute("$x('.//li', document.body, XPathResult.FIRST_ORDERED_NODE_TYPE)");
+  message = await onMessage;
+  ok(message, "$x works as expected with XPathResult.FIRST_ORDERED_NODE_TYPE");
+
+  onMessage = waitForMessage(hud, "Array [ li, li ]");
+  jsterm
+  .execute("$x('.//li', document.body, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE)");
+  message = await onMessage;
+  ok(message, "$x works as expected with XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE");
+
+  onMessage = waitForMessage(hud, "Array [ li, li ]");
+  jsterm
+  .execute("$x('.//li', document.body, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE)");
+  message = await onMessage;
+  ok(message, "$x works as expected with XPathResult.ORDERED_NODE_SNAPSHOT_TYPE");
 }
--- a/devtools/server/actors/webconsole/utils.js
+++ b/devtools/server/actors/webconsole/utils.js
@@ -357,26 +357,54 @@ WebConsoleCommands._registerOriginal("$_
  * Runs an xPath query and returns all matched nodes.
  *
  * @param string xPath
  *        xPath search query to execute.
  * @param [optional] Node context
  *        Context to run the xPath query on. Uses window.document if not set.
  * @return array of Node
  */
-WebConsoleCommands._registerOriginal("$x", function(owner, xPath, context) {
+WebConsoleCommands._registerOriginal("$x", function(
+  owner,
+  xPath,
+  context,
+  resultType = owner.window.XPathResult.ANY_TYPE) {
   const nodes = new owner.window.Array();
 
   // Not waiving Xrays, since we want the original Document.evaluate function,
   // instead of anything that's been redefined.
   const doc = owner.window.document;
   context = context || doc;
 
   const results = doc.evaluate(xPath, context, null,
-                             owner.window.XPathResult.ANY_TYPE, null);
+                             resultType, null);
+  if (results.resultType === owner.window.XPathResult.NUMBER_TYPE) {
+    return results.numberValue;
+  }
+  if (results.resultType === owner.window.XPathResult.STRING_TYPE) {
+    return results.stringValue;
+  }
+  if (results.resultType === owner.window.XPathResult.BOOLEAN_TYPE) {
+    return results.booleanValue;
+  }
+  if
+  (results.resultType === owner.window.XPathResult.ANY_UNORDERED_NODE_TYPE ||
+    results.resultType === owner.window.XPathResult.FIRST_ORDERED_NODE_TYPE) {
+    return results.singleNodeValue;
+  }
+  if
+  (results.resultType === owner.window.XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE ||
+    results.resultType === owner.window.XPathResult.ORDERED_NODE_SNAPSHOT_TYPE
+) {
+    for (let i = 0; i < results.snapshotLength; i++) {
+      nodes.push(results.snapshotItem(i));
+    }
+    return nodes;
+  }
+
   let node;
   while ((node = results.iterateNext())) {
     nodes.push(node);
   }
 
   return nodes;
 });