-
Notifications
You must be signed in to change notification settings - Fork 791
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(options): add ancestry CSS selector to nodes (#2389)
* feat(options): add ancestry CSS selector to nodes * chore: separate PR * chore: code cleanup * chore: fix broken tests * chore: fix more tests fixed * Update process-aggregate.js
- Loading branch information
1 parent
9f2d8db
commit f2cccf5
Showing
14 changed files
with
593 additions
and
243 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import getShadowSelector from './get-shadow-selector'; | ||
|
||
function generateAncestry(node) { | ||
const nodeName = node.nodeName.toLowerCase(); | ||
const parent = node.parentElement; | ||
if (!parent) { | ||
return nodeName; | ||
} | ||
|
||
let nthChild = ''; | ||
if ( | ||
nodeName !== 'head' && | ||
nodeName !== 'body' && | ||
parent.children.length > 1 | ||
) { | ||
const index = Array.prototype.indexOf.call(parent.children, node) + 1; | ||
nthChild = `:nth-child(${index})`; | ||
} | ||
|
||
return generateAncestry(parent) + ' > ' + nodeName + nthChild; | ||
} | ||
|
||
/** | ||
* Gets a unique CSS selector | ||
* @param {HTMLElement} node The element to get the selector for | ||
* @param {Object} optional options | ||
* @returns {String|Array<String>} Unique CSS selector for the node | ||
*/ | ||
export default function getAncestry(elm, options) { | ||
return getShadowSelector(generateAncestry, elm, options); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Gets a unique CSS selector | ||
* @param {HTMLElement} node The element to get the selector for | ||
* @param {Object} optional options | ||
* @returns {String|Array<String>} Unique CSS selector for the node | ||
*/ | ||
function getShadowSelector(generateSelector, elm, options = {}) { | ||
if (!elm) { | ||
return ''; | ||
} | ||
let doc = (elm.getRootNode && elm.getRootNode()) || document; | ||
// Not a DOCUMENT_FRAGMENT - shadow DOM | ||
if (doc.nodeType !== 11) { | ||
return generateSelector(elm, options, doc); | ||
} | ||
|
||
let stack = []; | ||
while (doc.nodeType === 11) { | ||
if (!doc.host) { | ||
return ''; | ||
} | ||
stack.unshift({ elm, doc }); | ||
elm = doc.host; | ||
doc = elm.getRootNode(); | ||
} | ||
|
||
stack.unshift({ elm, doc }); | ||
return stack.map(({ elm, doc }) => generateSelector(elm, options, doc)); | ||
} | ||
|
||
export default getShadowSelector; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.