Add a "copy full CSS path" option to the inspector's menu

Issue #3
This commit is contained in:
janekptacijarabaci 2018-02-02 20:42:28 +01:00 committed by Roy Tam
commit c3a7fd56e7
11 changed files with 241 additions and 3 deletions

View file

@ -169,6 +169,10 @@ Inspector.prototype = {
return this._target.client.traits.getUniqueSelector;
},
get canGetCssPath() {
return this._target.client.traits.getCssPath;
},
get canGetUsedFontFaces() {
return this._target.client.traits.getUsedFontFaces;
},
@ -1073,6 +1077,15 @@ Inspector.prototype = {
hidden: !this.canGetUniqueSelector,
click: () => this.copyUniqueSelector(),
}));
copySubmenu.append(new MenuItem({
id: "node-menu-copycsspath",
label: INSPECTOR_L10N.getStr("inspectorCopyCSSPath.label"),
accesskey:
INSPECTOR_L10N.getStr("inspectorCopyCSSPath.accesskey"),
disabled: !isSelectionElement,
hidden: !this.canGetCssPath,
click: () => this.copyCssPath(),
}));
copySubmenu.append(new MenuItem({
id: "node-menu-copyimagedatauri",
label: INSPECTOR_L10N.getStr("inspectorImageDataUri.label"),
@ -1677,9 +1690,24 @@ Inspector.prototype = {
return;
}
this.selection.nodeFront.getUniqueSelector().then((selector) => {
this.telemetry.toolOpened("copyuniquecssselector");
this.selection.nodeFront.getUniqueSelector().then(selector => {
clipboardHelper.copyString(selector);
}).then(null, console.error);
}).catch(e => console.error);
},
/**
* Copy the full CSS Path of the selected Node to the clipboard.
*/
copyCssPath: function () {
if (!this.selection.isNode()) {
return;
}
this.telemetry.toolOpened("copyfullcssselector");
this.selection.nodeFront.getCssPath().then(path => {
clipboardHelper.copyString(path);
}).catch(e => console.error);
},
/**

View file

@ -26,6 +26,7 @@ const ALL_MENU_ITEMS = [
"node-menu-copyinner",
"node-menu-copyouter",
"node-menu-copyuniqueselector",
"node-menu-copycsspath",
"node-menu-copyimagedatauri",
"node-menu-delete",
"node-menu-pseudo-hover",

View file

@ -25,6 +25,12 @@ const COPY_ITEMS_TEST_DATA = [
selector: "[data-id=\"copy\"]",
text: "body > div:nth-child(1) > p:nth-child(2)",
},
{
desc: "copy css path",
id: "node-menu-copycsspath",
selector: "[data-id=\"copy\"]",
text: "html body div p",
},
{
desc: "copy image data uri",
id: "node-menu-copyimagedatauri",

View file

@ -154,6 +154,12 @@ inspectorCopyOuterHTML.accesskey=O
inspectorCopyCSSSelector.label=CSS Selector
inspectorCopyCSSSelector.accesskey=S
# LOCALIZATION NOTE (inspectorCopyCSSPath.label): This is the label
# shown in the inspector contextual-menu for the item that lets users copy
# the full CSS path of the current node
inspectorCopyCSSPath.label=CSS Path
inspectorCopyCSSPath.accesskey=P
# LOCALIZATION NOTE (inspectorPasteOuterHTML.label): This is the label shown
# in the inspector contextual-menu for the item that lets users paste outer
# HTML in the current node

View file

@ -163,6 +163,12 @@ Telemetry.prototype = {
toolbareyedropper: {
histogram: "DEVTOOLS_TOOLBAR_EYEDROPPER_OPENED_COUNT",
},
copyuniquecssselector: {
histogram: "DEVTOOLS_COPY_UNIQUE_CSS_SELECTOR_OPENED_COUNT",
},
copyfullcssselector: {
histogram: "DEVTOOLS_COPY_FULL_CSS_SELECTOR_OPENED_COUNT",
},
developertoolbar: {
histogram: "DEVTOOLS_DEVELOPERTOOLBAR_OPENED_COUNT",
timerHistogram: "DEVTOOLS_DEVELOPERTOOLBAR_TIME_ACTIVE_SECONDS"

View file

@ -625,6 +625,18 @@ var NodeActor = exports.NodeActor = protocol.ActorClassWithSpec(nodeSpec, {
return CssLogic.findCssSelector(this.rawNode);
},
/**
* Get the full CSS path for this node.
*
* @return {String} A CSS selector with a part for the node and each of its ancestors.
*/
getCssPath: function () {
if (Cu.isDeadWrapper(this.rawNode)) {
return "";
}
return CssLogic.getCssPath(this.rawNode);
},
/**
* Scroll the selected node into view.
*/

View file

@ -145,6 +145,8 @@ RootActor.prototype = {
addNewRule: true,
// Whether the dom node actor implements the getUniqueSelector method
getUniqueSelector: true,
// Whether the dom node actor implements the getCssPath method
getCssPath: true,
// Whether the director scripts are supported
directorScripts: true,
// Whether the debugger server supports

View file

@ -792,6 +792,55 @@ CssLogic.findCssSelector = function (ele) {
return selector;
};
/**
* Get the full CSS path for a given element.
* @returns a string that can be used as a CSS selector for the element. It might not
* match the element uniquely. It does however, represent the full path from the root
* node to the element.
*/
CssLogic.getCssPath = function (ele) {
ele = getRootBindingParent(ele);
const document = ele.ownerDocument;
if (!document || !document.contains(ele)) {
throw new Error("getCssPath received element not inside document");
}
const getElementSelector = element => {
if (!element.localName) {
return "";
}
let label = element.nodeName == element.nodeName.toUpperCase()
? element.localName.toLowerCase()
: element.localName;
if (element.id) {
label += "#" + element.id;
}
if (element.classList) {
for (let cl of element.classList) {
label += "." + cl;
}
}
return label;
};
let paths = [];
while (ele) {
if (!ele || ele.nodeType !== Node.ELEMENT_NODE) {
break;
}
paths.splice(0, 0, getElementSelector(ele));
ele = ele.parentNode;
}
return paths.length ? paths.join(" ") : "";
}
/**
* A safe way to access cached bits of information about a stylesheet.
*

View file

@ -37,6 +37,12 @@ const nodeSpec = generateActorSpec({
value: RetVal("string")
}
},
getCssPath: {
request: {},
response: {
value: RetVal("string")
}
},
scrollIntoView: {
request: {},
response: {}

View file

@ -2,6 +2,7 @@
tags = devtools
skip-if = os == 'android'
[test_eventemitter_basic.html]
[test_css-logic-getCssPath.html]
[test_devtools_extensions.html]
[test_eventemitter_basic.html]
skip-if = os == 'linux' && debug # Bug 1205739

View file

@ -0,0 +1,121 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1323700
-->
<head>
<meta charset="utf-8">
<title>Test for Bug 1323700</title>
<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
<script type="application/javascript;version=1.8">
const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
let { require } = Cu.import("resource://devtools/shared/Loader.jsm", {});
const CssLogic = require("devtools/shared/inspector/css-logic");
var _tests = [];
function addTest(test) {
_tests.push(test);
}
function runNextTest() {
if (_tests.length == 0) {
SimpleTest.finish()
return;
}
_tests.shift()();
}
window.onload = function() {
SimpleTest.waitForExplicitFinish();
runNextTest();
}
addTest(function getCssPathForUnattachedElement() {
var unattached = document.createElement("div");
unattached.id = "unattached";
try {
CssLogic.getCssPath(unattached);
ok(false, "Unattached node did not throw")
} catch(e) {
ok(e, "Unattached node throws an exception");
}
var unattachedChild = document.createElement("div");
unattached.appendChild(unattachedChild);
try {
CssLogic.getCssPath(unattachedChild);
ok(false, "Unattached child node did not throw")
} catch(e) {
ok(e, "Unattached child node throws an exception");
}
var unattachedBody = document.createElement("body");
try {
CssLogic.getCssPath(unattachedBody);
ok(false, "Unattached body node did not throw")
} catch(e) {
ok(e, "Unattached body node throws an exception");
}
runNextTest();
});
addTest(function cssPathHasOneStepForEachAncestor() {
for (let el of [...document.querySelectorAll('*')]) {
let splitPath = CssLogic.getCssPath(el).split(" ");
let expectedNbOfParts = 0;
var parent = el.parentNode;
while (parent) {
expectedNbOfParts ++;
parent = parent.parentNode;
}
is(splitPath.length, expectedNbOfParts, "There are enough parts in the full path");
}
runNextTest();
});
addTest(function getCssPath() {
let data = [{
selector: "#id",
path: "html body div div div.class div#id"
}, {
selector: "html",
path: "html"
}, {
selector: "body",
path: "html body"
}, {
selector: ".c1.c2.c3",
path: "html body span.c1.c2.c3"
}, {
selector: "#i",
path: "html body span#i.c1.c2"
}];
for (let {selector, path} of data) {
let node = document.querySelector(selector);
is (CssLogic.getCssPath(node), path, `Full css path is correct for ${selector}`);
}
runNextTest();
});
</script>
</head>
<body>
<div>
<div>
<div class="class">
<div id="id"></div>
</div>
</div>
</div>
<span class="c1 c2 c3"></span>
<span id="i" class="c1 c2"></span>
</body>
</html>