Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. function getDomPath(el) {
  2. if (!el) {
  3. return;
  4. }
  5. var stack = [];
  6. var isShadow = false;
  7. while (el.parentNode != null) {
  8. // console.log(el.nodeName);
  9. var sibCount = 0;
  10. var sibIndex = 0;
  11. // get sibling indexes
  12. for ( var i = 0; i < el.parentNode.childNodes.length; i++ ) {
  13. var sib = el.parentNode.childNodes[i];
  14. if ( sib.nodeName == el.nodeName ) {
  15. if ( sib === el ) {
  16. sibIndex = sibCount;
  17. }
  18. sibCount++;
  19. }
  20. }
  21. // if ( el.hasAttribute('id') && el.id != '' ) { no id shortcuts, ids are not unique in shadowDom
  22. // stack.unshift(el.nodeName.toLowerCase() + '#' + el.id);
  23. // } else
  24. var nodeName = el.nodeName.toLowerCase();
  25. if (isShadow) {
  26. nodeName += "::shadow";
  27. isShadow = false;
  28. }
  29. if ( sibCount > 1 ) {
  30. stack.unshift(nodeName + ':nth-of-type(' + (sibIndex + 1) + ')');
  31. } else {
  32. stack.unshift(nodeName);
  33. }
  34. el = el.parentNode;
  35. if (el.nodeType === 11) { // for shadow dom, we
  36. isShadow = true;
  37. el = el.host;
  38. }
  39. }
  40. stack.splice(0,1); // removes the html element
  41. return stack.join(' > ');
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement