Skip to content

Commit

Permalink
Add getNodeSelector page function
Browse files Browse the repository at this point in the history
  • Loading branch information
mattzeunert committed Dec 2, 2018
1 parent 5e341c0 commit 030fb89
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions lighthouse-core/lib/page-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,38 @@ function getNodePath(node) {
return path.join(',');
}

/**
* @param {Element} node
* @returns {string}
*/
function getNodeSelector(node) {
/**
* @param {Element} node
*/
function getSelectorPart(node) {
let part = node.tagName.toLowerCase();
if (node.id) {
part += '#' + node.id;
} else if (node.classList.length > 0) {
part += '.' + node.classList[0];
}
return part;
}

const parts = [];
while (parts.length < 4) {
parts.unshift(getSelectorPart(node));
if (!node.parentElement) {
break;
}
node = node.parentElement;
if (node.tagName === 'HTML') {
break;
}
}
return parts.join(' > ');
}

module.exports = {
wrapRuntimeEvalErrorInBrowserString: wrapRuntimeEvalErrorInBrowser.toString(),
registerPerformanceObserverInPageString: registerPerformanceObserverInPage.toString(),
Expand All @@ -194,4 +226,5 @@ module.exports = {
ultradumbBenchmark: ultradumbBenchmark,
ultradumbBenchmarkString: ultradumbBenchmark.toString(),
getNodePathString: getNodePath.toString(),
getNodeSelectorString: getNodeSelector.toString(),
};

0 comments on commit 030fb89

Please sign in to comment.