forked from google/santa-tracker-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcss.js
More file actions
146 lines (126 loc) · 4.57 KB
/
Copy pathcss.js
File metadata and controls
146 lines (126 loc) · 4.57 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
/**
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview Polyfills Constructable Style Sheets for our purposes.
*/
let nativeSupport = false;
try {
new CSSStyleSheet();
nativeSupport = 'adoptedStyleSheets' in ShadowRoot.prototype;
} catch (e) {
// ignore
}
if (nativeSupport) {
// FIXME: This pretends to be a "CSSResult" from `lit-element` and provide a "styleSheet" attr:
// https://github.com/Polymer/lit-element/issues/774
Object.defineProperty(CSSStyleSheet.prototype, 'styleSheet', {
get() {
return this;
},
});
} else {
// This polyfill works by creating a completely new class of CSSStyleSheetConstructor, which is
// used by the CSS Module polyfill code in "build/transform-future-modules.js". However, it's
// largely ignored by lit-element (and only used by 'vanilla' Web Components), _except_ that
// lit-element calls `.cssText` to create its own <style> nodes.
window.CSSStyleSheetConstructor = class {
constructor(options) {
if (options) {
throw new DOMException('NotSupportedError');
}
this._cssText = null;
}
get cssText() {
// FIXME: This pretends to be a "CSSResult" from `lit-element`:
// https://github.com/Polymer/lit-element/issues/774
return this._cssText;
}
replaceSync(cssText) {
if (this._cssText !== null) {
throw new DOMException('NotAllowedException', 'polyfill supports single replaceSync()');
}
this._cssText = String(cssText);
}
replace() {
throw new DOMException('NotSupportedError');
}
insertRule() {
throw new DOMException('NotSupportedError');
}
deleteRule() {
throw new DOMException('NotSupportedError');
}
};
const instantiated = Symbol('instantiated'); // actually created <style> nodes
function rectifyAdoptedStyleSheets(holder) {
const expected = /** @type {!Array<HTMLStyleElement>} */ (this[instantiated] || []);
// Check that nodes [0,n] are the expected HTMLStyleElement instances.
// nb. this doesn't guard against users making local changes to textContent
let ok = (holder.childNodes.length >= expected.length);
for (let i = 0; ok && i < expected.length; ++i) {
if (holder.childNodes[i] !== expected[i]) {
ok = false;
}
}
if (ok) {
return;
}
// Nuke all previous instantiated sheets.
expected.forEach((node) => {
node.parentNode && node.parentNode.removeChild(node);
});
// Prepare clones of target CSSStyleSheetConstructor instances.
const targetBefore = holder.firstChild;
this[instantiated].forEach((node) => holder.insertBefore(node, targetBefore));
}
const adopted = Symbol('adopted'); // CSSStyleSheetConstructor instances adopted
const def = {
get() {
return this[adopted] || [];
},
set(v) {
// nb. "adopted in this" is _always_ true where Symbol is polyfilled, so check value
if (this[adopted]) {
throw new DOMException('NotSupportedError', 'can only set adopted once');
}
if (!v || !v.length) {
return; // nothing to do
}
this[adopted] = v;
this[instantiated] = v.map((instance) => {
if (!(instance instanceof window.CSSStyleSheetConstructor)) {
throw new Error(`unsupported adopted type: ${instance}`);
} else if (instance.cssText === null) {
throw new Error(`cannot adopt uninstantiated CSSStyleSheet`);
}
const node = document.createElement('style');
node.textContent = instance.cssText;
return node;
});
const node = (this === document ? document.head : this);
const call = rectifyAdoptedStyleSheets.bind(this, node);
const o = new MutationObserver(call);
try {
o.observe(this, {childList: true});
} catch (e) {
console.warn('failed to observe', this, 'for adoptedStyleSheets', e);
}
call();
}
};
Object.defineProperty(ShadowRoot.prototype, 'adoptedStyleSheets', def);
Object.defineProperty(HTMLDocument.prototype, 'adoptedStyleSheets', def);
}