generated from shgysk8zer0/npm-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcss.js
92 lines (77 loc) · 2.78 KB
/
css.js
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
const stringify = thing => {
switch(typeof thing) {
case 'undefined':
return '';
case 'boolean':
return thing ? 'true' : 'false';
case 'object':
if (thing === null) {
return '';
} else if (thing instanceof CSSStyleSheet) {
return [...thing.cssRules].map(rule => rule.cssText).join('\n\n');
} else if (thing instanceof CSSRule) {
return thing.cssText;
} else if (thing instanceof HTMLLinkElement) {
return stringify(thing.sheet);
} else if (thing instanceof ArrayBuffer && Uint8Array.prototype.toBase64 instanceof Function) {
return new Uint8Array(thing).toBase64();
} else if (ArrayBuffer.isView(thing) && thing.toBase64 instanceof Function) {
return thing.toBase64();
} else if (thing instanceof Blob) {
return URL.createObjectURL(thing);
} else {
return thing.toString();
}
default:
return thing.toString();
}
};
export function createStyleSheet(cssRules, { media, disabled, baseURL } = {}) {
const sheet = new CSSStyleSheet({
media: media instanceof MediaQueryList ? media.media : media,
disabled,
baseURL
});
sheet.replace(cssRules).catch(reportError);
return sheet;
}
export function createStyleSheetSync(cssRules, { media, disabled, baseURL } = {}) {
const sheet = new CSSStyleSheet({
media: media instanceof MediaQueryList ? media.media : media,
disabled,
baseURL
});
sheet.replaceSync(cssRules);
return sheet;
}
export const createCSSParser = ({ media, disabled, baseURL, sync = false } = {}) => sync
? (strings, ...args) => createStyleSheetSync(String.raw(strings, ...args.map(stringify)).trim(), { media, disabled, baseURL })
: (strings, ...args) => createStyleSheet(String.raw(strings, ...args.map(stringify)).trim(), { media, disabled, baseURL });
export const css = createCSSParser();
export function styleSheetToLink(sheet, { crossOrigin = 'anonymous', referrerPolicy = 'no-referrer' } = {}) {
const link = document.createElement('link');
const file = new File(Array.from(sheet.cssRules, rule => rule.cssText), 'sheet.css', { type: sheet.type });
link.rel = 'stylesheet';
link.crossOrigin = crossOrigin;
link.referrerPolicy = referrerPolicy;
link.disabled = sheet.disabled;
if (sheet.media.length !== 0) {
link.media = sheet.media.mediaText;
}
link.href = URL.createObjectURL(file);
return link;
}
export function setStyleSheets(node, ...styles) {
if (node instanceof HTMLDocument || node instanceof ShadowRoot) {
node.adoptedStyleSheets = styles;
} else {
throw new TypeError('Node must be a `HTMLDocument` or `ShadowRoot`.');
}
}
export function addStyleSheets(node, ...styles) {
if (node instanceof Document || node instanceof ShadowRoot) {
node.adoptedStyleSheets = [...node.adoptedStyleSheets, ...styles];
} else {
throw new TypeError('Node must be a `HTMLDocument` or `ShadowRoot`.');
}
}