index.mjs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. function cssHasPseudo(document) {
  2. const observedItems = []; // document.createAttribute() doesn't support `:` in the name. innerHTML does
  3. const attributeElement = document.createElement('x'); // walk all stylesheets to collect observed css rules
  4. [].forEach.call(document.styleSheets, walkStyleSheet);
  5. transformObservedItems(); // observe DOM modifications that affect selectors
  6. const mutationObserver = new MutationObserver(mutationsList => {
  7. mutationsList.forEach(mutation => {
  8. [].forEach.call(mutation.addedNodes || [], node => {
  9. // walk stylesheets to collect observed css rules
  10. if (node.nodeType === 1 && node.sheet) {
  11. walkStyleSheet(node.sheet);
  12. }
  13. }); // transform observed css rules
  14. cleanupObservedCssRules();
  15. transformObservedItems();
  16. });
  17. });
  18. mutationObserver.observe(document, {
  19. childList: true,
  20. subtree: true
  21. }); // observe DOM events that affect pseudo-selectors
  22. document.addEventListener('focus', transformObservedItems, true);
  23. document.addEventListener('blur', transformObservedItems, true);
  24. document.addEventListener('input', transformObservedItems); // transform observed css rules
  25. function transformObservedItems() {
  26. requestAnimationFrame(() => {
  27. observedItems.forEach(item => {
  28. const nodes = [];
  29. [].forEach.call(document.querySelectorAll(item.scopeSelector), element => {
  30. const nthChild = [].indexOf.call(element.parentNode.children, element) + 1;
  31. const relativeSelectors = item.relativeSelectors.map(relativeSelector => item.scopeSelector + ':nth-child(' + nthChild + ') ' + relativeSelector).join(); // find any relative :has element from the :scope element
  32. const relativeElement = element.parentNode.querySelector(relativeSelectors);
  33. const shouldElementMatch = item.isNot ? !relativeElement : relativeElement;
  34. if (shouldElementMatch) {
  35. // memorize the node
  36. nodes.push(element); // set an attribute with an irregular attribute name
  37. // document.createAttribute() doesn't support special characters
  38. attributeElement.innerHTML = '<x ' + item.attributeName + '>';
  39. element.setAttributeNode(attributeElement.children[0].attributes[0].cloneNode()); // trigger a style refresh in IE and Edge
  40. document.documentElement.style.zoom = 1;
  41. document.documentElement.style.zoom = null;
  42. }
  43. }); // remove the encoded attribute from all nodes that no longer match them
  44. item.nodes.forEach(node => {
  45. if (nodes.indexOf(node) === -1) {
  46. node.removeAttribute(item.attributeName); // trigger a style refresh in IE and Edge
  47. document.documentElement.style.zoom = 1;
  48. document.documentElement.style.zoom = null;
  49. }
  50. }); // update the
  51. item.nodes = nodes;
  52. });
  53. });
  54. } // remove any observed cssrules that no longer apply
  55. function cleanupObservedCssRules() {
  56. [].push.apply(observedItems, observedItems.splice(0).filter(item => item.rule.parentStyleSheet && item.rule.parentStyleSheet.ownerNode && document.documentElement.contains(item.rule.parentStyleSheet.ownerNode)));
  57. } // walk a stylesheet to collect observed css rules
  58. function walkStyleSheet(styleSheet) {
  59. try {
  60. // walk a css rule to collect observed css rules
  61. [].forEach.call(styleSheet.cssRules || [], rule => {
  62. if (rule.selectorText) {
  63. // decode the selector text in all browsers to:
  64. // [1] = :scope, [2] = :not(:has), [3] = :has relative, [4] = :scope relative
  65. const selectors = decodeURIComponent(rule.selectorText.replace(/\\(.)/g, '$1')).match(/^(.*?)\[:(not-)?has\((.+?)\)\](.*?)$/);
  66. if (selectors) {
  67. const attributeName = ':' + (selectors[2] ? 'not-' : '') + 'has(' + // encode a :has() pseudo selector as an attribute name
  68. encodeURIComponent(selectors[3]).replace(/%3A/g, ':').replace(/%5B/g, '[').replace(/%5D/g, ']').replace(/%2C/g, ',') + ')';
  69. observedItems.push({
  70. rule,
  71. scopeSelector: selectors[1],
  72. isNot: selectors[2],
  73. relativeSelectors: selectors[3].split(/\s*,\s*/),
  74. attributeName,
  75. nodes: []
  76. });
  77. }
  78. } else {
  79. walkStyleSheet(rule);
  80. }
  81. });
  82. } catch (error) {
  83. /* do nothing and continue */
  84. }
  85. }
  86. }
  87. export default cssHasPseudo;
  88. //# sourceMappingURL=index.mjs.map