Bug 1164195 - Add a resultType param to $x console helper. r=nchevobbe.

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/D23307

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Dhruvi Butti 2019-03-13 13:16:15 +00:00
Родитель 8f0afd4311
Коммит 2c074bae46
2 изменённых файлов: 81 добавлений и 2 удалений

Просмотреть файл

@ -37,4 +37,55 @@ async function performTests() {
jsterm.execute("$x('.//li', document.body)[0]"); jsterm.execute("$x('.//li', document.body)[0]");
message = await onMessage; message = await onMessage;
ok(message, "`$x()` result can be used right away"); 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");
} }

Просмотреть файл

@ -362,7 +362,11 @@ WebConsoleCommands._registerOriginal("$_", {
* Context to run the xPath query on. Uses window.document if not set. * Context to run the xPath query on. Uses window.document if not set.
* @return array of Node * @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(); const nodes = new owner.window.Array();
// Not waiving Xrays, since we want the original Document.evaluate function, // Not waiving Xrays, since we want the original Document.evaluate function,
@ -371,7 +375,31 @@ WebConsoleCommands._registerOriginal("$x", function(owner, xPath, context) {
context = context || doc; context = context || doc;
const results = doc.evaluate(xPath, context, null, 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; let node;
while ((node = results.iterateNext())) { while ((node = results.iterateNext())) {
nodes.push(node); nodes.push(node);