-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathdynamicCSS.ts
More file actions
159 lines (127 loc) · 3.92 KB
/
dynamicCSS.ts
File metadata and controls
159 lines (127 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import canUseDom from './canUseDom';
import contains from './contains';
const APPEND_ORDER = 'data-rc-order';
const MARK_KEY = `rc-util-key`;
const containerCache = new Map<ContainerType, Node & ParentNode>();
export type ContainerType = Element | ShadowRoot;
export type Prepend = boolean | 'queue';
export type AppendType = 'prependQueue' | 'append' | 'prepend';
interface Options {
attachTo?: ContainerType;
csp?: { nonce?: string };
prepend?: Prepend;
mark?: string;
}
function getMark({ mark }: Options = {}) {
if (mark) {
return mark.startsWith('data-') ? mark : `data-${mark}`;
}
return MARK_KEY;
}
function getContainer(option: Options) {
if (option.attachTo) {
return option.attachTo;
}
const head = document.querySelector('head');
return head || document.body;
}
function getOrder(prepend?: Prepend): AppendType {
if (prepend === 'queue') {
return 'prependQueue';
}
return prepend ? 'prepend' : 'append';
}
/**
* Find style which inject by rc-util
*/
function findStyles(container: ContainerType) {
return Array.from(
(containerCache.get(container) || container)?.children ?? [],
).filter(node => node.tagName === 'STYLE') as HTMLStyleElement[];
}
export function injectCSS(css: string, option: Options = {}) {
if (!canUseDom()) {
return null;
}
const { csp, prepend } = option;
const styleNode = document.createElement('style');
styleNode.setAttribute(APPEND_ORDER, getOrder(prepend));
if (csp?.nonce) {
styleNode.nonce = csp?.nonce;
}
styleNode.innerHTML = css;
const container = getContainer(option);
const firstChild = container?.firstChild;
if (prepend) {
// If is queue `prepend`, it will prepend first style and then append rest style
if (prepend === 'queue') {
const existStyle = findStyles(container).filter(node =>
['prepend', 'prependQueue'].includes(node.getAttribute(APPEND_ORDER)),
);
if (existStyle.length) {
container.insertBefore(
styleNode,
existStyle[existStyle.length - 1].nextSibling,
);
return styleNode;
}
}
// Use `insertBefore` as `prepend`
if (firstChild) {
container.insertBefore(styleNode, firstChild);
}
} else {
container.appendChild(styleNode);
}
return styleNode;
}
function findExistNode(key: string, option: Options = {}) {
const container = getContainer(option);
return findStyles(container).find(
node => node.getAttribute(getMark(option)) === key,
);
}
export function removeCSS(key: string, option: Options = {}) {
const existNode = findExistNode(key, option);
if (existNode) {
const container = getContainer(option);
container.removeChild(existNode);
}
}
/**
* qiankun will inject `appendChild` to insert into other
*/
function syncRealContainer(container: ContainerType, option: Options) {
const cachedRealContainer = containerCache.get(container);
// Find real container when not cached or cached container removed
if (!cachedRealContainer || !contains(document, cachedRealContainer)) {
const placeholderStyle = injectCSS('', option);
const { parentNode } = placeholderStyle;
containerCache.set(container, parentNode);
container.removeChild(placeholderStyle);
}
}
/**
* manually clear container cache to avoid global cache in unit testes
*/
export function clearContainerCache() {
containerCache.clear();
}
export function updateCSS(css: string, key: string, option: Options = {}) {
const container = getContainer(option);
// Sync real parent
syncRealContainer(container, option);
const existNode = findExistNode(key, option);
if (existNode) {
if (option.csp?.nonce && existNode.nonce !== option.csp?.nonce) {
existNode.nonce = option.csp?.nonce;
}
if (existNode.innerHTML !== css) {
existNode.innerHTML = css;
}
return existNode;
}
const newNode = injectCSS(css, option);
newNode.setAttribute(getMark(option), key);
return newNode;
}