dynamicCSS.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import canUseDom from '../../_util/canUseDom';
  2. var MARK_KEY = "vc-util-key";
  3. function getMark() {
  4. var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
  5. mark = _ref.mark;
  6. if (mark) {
  7. return mark.startsWith('data-') ? mark : "data-".concat(mark);
  8. }
  9. return MARK_KEY;
  10. }
  11. function getContainer(option) {
  12. if (option.attachTo) {
  13. return option.attachTo;
  14. }
  15. var head = document.querySelector('head');
  16. return head || document.body;
  17. }
  18. export function injectCSS(css) {
  19. var _option$csp;
  20. var option = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  21. if (!canUseDom()) {
  22. return null;
  23. }
  24. var styleNode = document.createElement('style');
  25. if ((_option$csp = option.csp) !== null && _option$csp !== void 0 && _option$csp.nonce) {
  26. var _option$csp2;
  27. styleNode.nonce = (_option$csp2 = option.csp) === null || _option$csp2 === void 0 ? void 0 : _option$csp2.nonce;
  28. }
  29. styleNode.innerHTML = css;
  30. var container = getContainer(option);
  31. var firstChild = container.firstChild;
  32. if (option.prepend && container.prepend) {
  33. // Use `prepend` first
  34. container.prepend(styleNode);
  35. } else if (option.prepend && firstChild) {
  36. // Fallback to `insertBefore` like IE not support `prepend`
  37. container.insertBefore(styleNode, firstChild);
  38. } else {
  39. container.appendChild(styleNode);
  40. }
  41. return styleNode;
  42. }
  43. var containerCache = new Map();
  44. function findExistNode(key) {
  45. var option = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  46. var container = getContainer(option);
  47. return Array.from(containerCache.get(container).children).find(function (node) {
  48. return node.tagName === 'STYLE' && node.getAttribute(getMark(option)) === key;
  49. });
  50. }
  51. export function removeCSS(key) {
  52. var _existNode$parentNode;
  53. var option = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  54. var existNode = findExistNode(key, option);
  55. existNode === null || existNode === void 0 ? void 0 : (_existNode$parentNode = existNode.parentNode) === null || _existNode$parentNode === void 0 ? void 0 : _existNode$parentNode.removeChild(existNode);
  56. }
  57. export function updateCSS(css, key) {
  58. var option = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  59. var container = getContainer(option);
  60. // Get real parent
  61. if (!containerCache.has(container)) {
  62. var placeholderStyle = injectCSS('', option);
  63. var parentNode = placeholderStyle.parentNode;
  64. containerCache.set(container, parentNode);
  65. parentNode.removeChild(placeholderStyle);
  66. }
  67. var existNode = findExistNode(key, option);
  68. if (existNode) {
  69. var _option$csp3, _option$csp4;
  70. if ((_option$csp3 = option.csp) !== null && _option$csp3 !== void 0 && _option$csp3.nonce && existNode.nonce !== ((_option$csp4 = option.csp) === null || _option$csp4 === void 0 ? void 0 : _option$csp4.nonce)) {
  71. var _option$csp5;
  72. existNode.nonce = (_option$csp5 = option.csp) === null || _option$csp5 === void 0 ? void 0 : _option$csp5.nonce;
  73. }
  74. if (existNode.innerHTML !== css) {
  75. existNode.innerHTML = css;
  76. }
  77. return existNode;
  78. }
  79. var newNode = injectCSS(css, option);
  80. newNode.setAttribute(getMark(option), key);
  81. return newNode;
  82. }