UNPKG

5.53 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4Object.defineProperty(exports, "__esModule", {
5 value: true
6});
7exports.clearContainerCache = clearContainerCache;
8exports.injectCSS = injectCSS;
9exports.removeCSS = removeCSS;
10exports.updateCSS = updateCSS;
11var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
12var _canUseDom = _interopRequireDefault(require("./canUseDom"));
13var _contains = _interopRequireDefault(require("./contains"));
14var APPEND_ORDER = 'data-rc-order';
15var APPEND_PRIORITY = 'data-rc-priority';
16var MARK_KEY = "rc-util-key";
17var containerCache = new Map();
18function getMark() {
19 var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
20 mark = _ref.mark;
21 if (mark) {
22 return mark.startsWith('data-') ? mark : "data-".concat(mark);
23 }
24 return MARK_KEY;
25}
26function getContainer(option) {
27 if (option.attachTo) {
28 return option.attachTo;
29 }
30 var head = document.querySelector('head');
31 return head || document.body;
32}
33function getOrder(prepend) {
34 if (prepend === 'queue') {
35 return 'prependQueue';
36 }
37 return prepend ? 'prepend' : 'append';
38}
39
40/**
41 * Find style which inject by rc-util
42 */
43function findStyles(container) {
44 return Array.from((containerCache.get(container) || container).children).filter(function (node) {
45 return node.tagName === 'STYLE';
46 });
47}
48function injectCSS(css) {
49 var option = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
50 if (!(0, _canUseDom.default)()) {
51 return null;
52 }
53 var csp = option.csp,
54 prepend = option.prepend,
55 _option$priority = option.priority,
56 priority = _option$priority === void 0 ? 0 : _option$priority;
57 var mergedOrder = getOrder(prepend);
58 var isPrependQueue = mergedOrder === 'prependQueue';
59 var styleNode = document.createElement('style');
60 styleNode.setAttribute(APPEND_ORDER, mergedOrder);
61 if (isPrependQueue && priority) {
62 styleNode.setAttribute(APPEND_PRIORITY, "".concat(priority));
63 }
64 if (csp !== null && csp !== void 0 && csp.nonce) {
65 styleNode.nonce = csp === null || csp === void 0 ? void 0 : csp.nonce;
66 }
67 styleNode.innerHTML = css;
68 var container = getContainer(option);
69 var firstChild = container.firstChild;
70 if (prepend) {
71 // If is queue `prepend`, it will prepend first style and then append rest style
72 if (isPrependQueue) {
73 var existStyle = (option.styles || findStyles(container)).filter(function (node) {
74 // Ignore style which not injected by rc-util with prepend
75 if (!['prepend', 'prependQueue'].includes(node.getAttribute(APPEND_ORDER))) {
76 return false;
77 }
78
79 // Ignore style which priority less then new style
80 var nodePriority = Number(node.getAttribute(APPEND_PRIORITY) || 0);
81 return priority >= nodePriority;
82 });
83 if (existStyle.length) {
84 container.insertBefore(styleNode, existStyle[existStyle.length - 1].nextSibling);
85 return styleNode;
86 }
87 }
88
89 // Use `insertBefore` as `prepend`
90 container.insertBefore(styleNode, firstChild);
91 } else {
92 container.appendChild(styleNode);
93 }
94 return styleNode;
95}
96function findExistNode(key) {
97 var option = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
98 var container = getContainer(option);
99 return (option.styles || findStyles(container)).find(function (node) {
100 return node.getAttribute(getMark(option)) === key;
101 });
102}
103function removeCSS(key) {
104 var option = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
105 var existNode = findExistNode(key, option);
106 if (existNode) {
107 var container = getContainer(option);
108 container.removeChild(existNode);
109 }
110}
111
112/**
113 * qiankun will inject `appendChild` to insert into other
114 */
115function syncRealContainer(container, option) {
116 var cachedRealContainer = containerCache.get(container);
117
118 // Find real container when not cached or cached container removed
119 if (!cachedRealContainer || !(0, _contains.default)(document, cachedRealContainer)) {
120 var placeholderStyle = injectCSS('', option);
121 var parentNode = placeholderStyle.parentNode;
122 containerCache.set(container, parentNode);
123 container.removeChild(placeholderStyle);
124 }
125}
126
127/**
128 * manually clear container cache to avoid global cache in unit testes
129 */
130function clearContainerCache() {
131 containerCache.clear();
132}
133function updateCSS(css, key) {
134 var originOption = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
135 var container = getContainer(originOption);
136 var styles = findStyles(container);
137 var option = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, originOption), {}, {
138 styles: styles
139 });
140
141 // Sync real parent
142 syncRealContainer(container, option);
143 var existNode = findExistNode(key, option);
144 if (existNode) {
145 var _option$csp, _option$csp2;
146 if ((_option$csp = option.csp) !== null && _option$csp !== void 0 && _option$csp.nonce && existNode.nonce !== ((_option$csp2 = option.csp) === null || _option$csp2 === void 0 ? void 0 : _option$csp2.nonce)) {
147 var _option$csp3;
148 existNode.nonce = (_option$csp3 = option.csp) === null || _option$csp3 === void 0 ? void 0 : _option$csp3.nonce;
149 }
150 if (existNode.innerHTML !== css) {
151 existNode.innerHTML = css;
152 }
153 return existNode;
154 }
155 var newNode = injectCSS(css, option);
156 newNode.setAttribute(getMark(option), key);
157 return newNode;
158}
\No newline at end of file