/
css-tag.ts
207 lines (189 loc) · 6.47 KB
/
css-tag.ts
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**
* @license
* Copyright 2019 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/
const NODE_MODE = false;
const global = NODE_MODE ? globalThis : window;
/**
* Whether the current browser supports `adoptedStyleSheets`.
*/
export const supportsAdoptingStyleSheets =
// In Node mode, it doesn't really matter whether we believe adopted style
// sheets are supported or not, because styles are handled separately as part
// of SSR anyway; however if we set this true then we avoid `instanceof
// CSSStyleSheet` checks during initialization, meaning we don't need to shim
// that global.
NODE_MODE ||
(global.ShadowRoot &&
(global.ShadyCSS === undefined || global.ShadyCSS.nativeShadow) &&
'adoptedStyleSheets' in Document.prototype &&
'replace' in CSSStyleSheet.prototype);
/**
* A CSSResult or native CSSStyleSheet.
*
* In browsers that support constructible CSS style sheets, CSSStyleSheet
* object can be used for styling along side CSSResult from the `css`
* template tag.
*/
export type CSSResultOrNative = CSSResult | CSSStyleSheet;
export type CSSResultArray = Array<CSSResultOrNative | CSSResultArray>;
/**
* A single CSSResult, CSSStyleSheet, or an array or nested arrays of those.
*/
export type CSSResultGroup = CSSResultOrNative | CSSResultArray;
const constructionToken = Symbol();
const cssTagCache = new WeakMap<TemplateStringsArray, CSSStyleSheet>();
/**
* A container for a string of CSS text, that may be used to create a CSSStyleSheet.
*
* CSSResult is the return value of `css`-tagged template literals and
* `unsafeCSS()`. In order to ensure that CSSResults are only created via the
* `css` tag and `unsafeCSS()`, CSSResult cannot be constructed directly.
*/
export class CSSResult {
// This property needs to remain unminified.
['_$cssResult$'] = true;
readonly cssText: string;
private _styleSheet?: CSSStyleSheet;
private _strings: TemplateStringsArray | undefined;
private constructor(
cssText: string,
strings: TemplateStringsArray | undefined,
safeToken: symbol
) {
if (safeToken !== constructionToken) {
throw new Error(
'CSSResult is not constructable. Use `unsafeCSS` or `css` instead.'
);
}
this.cssText = cssText;
this._strings = strings;
}
// This is a getter so that it's lazy. In practice, this means stylesheets
// are not created until the first element instance is made.
get styleSheet(): CSSStyleSheet | undefined {
// If `supportsAdoptingStyleSheets` is true then we assume CSSStyleSheet is
// constructable.
let styleSheet = this._styleSheet;
const strings = this._strings;
if (supportsAdoptingStyleSheets && styleSheet === undefined) {
const cacheable = strings !== undefined && strings.length === 1;
if (cacheable) {
styleSheet = cssTagCache.get(strings);
}
if (styleSheet === undefined) {
(this._styleSheet = styleSheet = new CSSStyleSheet()).replaceSync(
this.cssText
);
if (cacheable) {
cssTagCache.set(strings, styleSheet);
}
}
}
return styleSheet;
}
toString(): string {
return this.cssText;
}
}
type ConstructableCSSResult = CSSResult & {
new (
cssText: string,
strings: TemplateStringsArray | undefined,
safeToken: symbol
): CSSResult;
};
const textFromCSSResult = (value: CSSResultGroup | number) => {
// This property needs to remain unminified.
if ((value as CSSResult)['_$cssResult$'] === true) {
return (value as CSSResult).cssText;
} else if (typeof value === 'number') {
return value;
} else {
throw new Error(
`Value passed to 'css' function must be a 'css' function result: ` +
`${value}. Use 'unsafeCSS' to pass non-literal values, but take care ` +
`to ensure page security.`
);
}
};
/**
* Wrap a value for interpolation in a {@linkcode css} tagged template literal.
*
* This is unsafe because untrusted CSS text can be used to phone home
* or exfiltrate data to an attacker controlled site. Take care to only use
* this with trusted input.
*/
export const unsafeCSS = (value: unknown) =>
new (CSSResult as ConstructableCSSResult)(
typeof value === 'string' ? value : String(value),
undefined,
constructionToken
);
/**
* A template literal tag which can be used with LitElement's
* {@linkcode LitElement.styles} property to set element styles.
*
* For security reasons, only literal string values and number may be used in
* embedded expressions. To incorporate non-literal values {@linkcode unsafeCSS}
* may be used inside an expression.
*/
export const css = (
strings: TemplateStringsArray,
...values: (CSSResultGroup | number)[]
): CSSResult => {
const cssText =
strings.length === 1
? strings[0]
: values.reduce(
(acc, v, idx) => acc + textFromCSSResult(v) + strings[idx + 1],
strings[0]
);
return new (CSSResult as ConstructableCSSResult)(
cssText,
strings,
constructionToken
);
};
/**
* Applies the given styles to a `shadowRoot`. When Shadow DOM is
* available but `adoptedStyleSheets` is not, styles are appended to the
* `shadowRoot` to [mimic spec behavior](https://wicg.github.io/construct-stylesheets/#using-constructed-stylesheets).
* Note, when shimming is used, any styles that are subsequently placed into
* the shadowRoot should be placed *before* any shimmed adopted styles. This
* will match spec behavior that gives adopted sheets precedence over styles in
* shadowRoot.
*/
export const adoptStyles = (
renderRoot: ShadowRoot,
styles: Array<CSSResultOrNative>
) => {
if (supportsAdoptingStyleSheets) {
(renderRoot as ShadowRoot).adoptedStyleSheets = styles.map((s) =>
s instanceof CSSStyleSheet ? s : s.styleSheet!
);
} else {
styles.forEach((s) => {
const style = document.createElement('style');
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const nonce = (global as any)['litNonce'];
if (nonce !== undefined) {
style.setAttribute('nonce', nonce);
}
style.textContent = (s as CSSResult).cssText;
renderRoot.appendChild(style);
});
}
};
const cssResultFromStyleSheet = (sheet: CSSStyleSheet) => {
let cssText = '';
for (const rule of sheet.cssRules) {
cssText += rule.cssText;
}
return unsafeCSS(cssText);
};
export const getCompatibleStyle = supportsAdoptingStyleSheets
? (s: CSSResultOrNative) => s
: (s: CSSResultOrNative) =>
s instanceof CSSStyleSheet ? cssResultFromStyleSheet(s) : s;