1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
4 | Object.defineProperty(exports, "__esModule", {
|
5 | value: true
|
6 | });
|
7 | exports.clearContainerCache = clearContainerCache;
|
8 | exports.injectCSS = injectCSS;
|
9 | exports.removeCSS = removeCSS;
|
10 | exports.updateCSS = updateCSS;
|
11 | var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
12 | var _canUseDom = _interopRequireDefault(require("./canUseDom"));
|
13 | var _contains = _interopRequireDefault(require("./contains"));
|
14 | var APPEND_ORDER = 'data-rc-order';
|
15 | var APPEND_PRIORITY = 'data-rc-priority';
|
16 | var MARK_KEY = "rc-util-key";
|
17 | var containerCache = new Map();
|
18 | function 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 | }
|
26 | function getContainer(option) {
|
27 | if (option.attachTo) {
|
28 | return option.attachTo;
|
29 | }
|
30 | var head = document.querySelector('head');
|
31 | return head || document.body;
|
32 | }
|
33 | function getOrder(prepend) {
|
34 | if (prepend === 'queue') {
|
35 | return 'prependQueue';
|
36 | }
|
37 | return prepend ? 'prepend' : 'append';
|
38 | }
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | function findStyles(container) {
|
44 | return Array.from((containerCache.get(container) || container).children).filter(function (node) {
|
45 | return node.tagName === 'STYLE';
|
46 | });
|
47 | }
|
48 | function 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 |
|
72 | if (isPrependQueue) {
|
73 | var existStyle = (option.styles || findStyles(container)).filter(function (node) {
|
74 |
|
75 | if (!['prepend', 'prependQueue'].includes(node.getAttribute(APPEND_ORDER))) {
|
76 | return false;
|
77 | }
|
78 |
|
79 |
|
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 |
|
90 | container.insertBefore(styleNode, firstChild);
|
91 | } else {
|
92 | container.appendChild(styleNode);
|
93 | }
|
94 | return styleNode;
|
95 | }
|
96 | function 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 | }
|
103 | function 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 |
|
114 |
|
115 | function syncRealContainer(container, option) {
|
116 | var cachedRealContainer = containerCache.get(container);
|
117 |
|
118 |
|
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 |
|
129 |
|
130 | function clearContainerCache() {
|
131 | containerCache.clear();
|
132 | }
|
133 | function 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 |
|
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 |