UNPKG

8.33 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports["default"] = void 0;
5
6function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
7
8function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
9
10/*
11Based on Glamor's sheet
12https://github.com/threepointone/glamor/blob/667b480d31b3721a905021b26e1290ce92ca2879/src/sheet.js
13*/
14var isProd = typeof process !== 'undefined' && process.env && process.env.NODE_ENV === 'production';
15
16var isString = function isString(o) {
17 return Object.prototype.toString.call(o) === '[object String]';
18};
19
20var StyleSheet =
21/*#__PURE__*/
22function () {
23 function StyleSheet(_temp) {
24 var _ref = _temp === void 0 ? {} : _temp,
25 _ref$name = _ref.name,
26 name = _ref$name === void 0 ? 'stylesheet' : _ref$name,
27 _ref$optimizeForSpeed = _ref.optimizeForSpeed,
28 optimizeForSpeed = _ref$optimizeForSpeed === void 0 ? isProd : _ref$optimizeForSpeed,
29 _ref$isBrowser = _ref.isBrowser,
30 isBrowser = _ref$isBrowser === void 0 ? typeof window !== 'undefined' : _ref$isBrowser;
31
32 invariant(isString(name), '`name` must be a string');
33 this._name = name;
34 this._deletedRulePlaceholder = "#" + name + "-deleted-rule____{}";
35 invariant(typeof optimizeForSpeed === 'boolean', '`optimizeForSpeed` must be a boolean');
36 this._optimizeForSpeed = optimizeForSpeed;
37 this._isBrowser = isBrowser;
38 this._serverSheet = undefined;
39 this._tags = [];
40 this._injected = false;
41 this._rulesCount = 0;
42 var node = this._isBrowser && document.querySelector('meta[property="csp-nonce"]');
43 this._nonce = node ? node.getAttribute('content') : null;
44 }
45
46 var _proto = StyleSheet.prototype;
47
48 _proto.setOptimizeForSpeed = function setOptimizeForSpeed(bool) {
49 invariant(typeof bool === 'boolean', '`setOptimizeForSpeed` accepts a boolean');
50 invariant(this._rulesCount === 0, 'optimizeForSpeed cannot be when rules have already been inserted');
51 this.flush();
52 this._optimizeForSpeed = bool;
53 this.inject();
54 };
55
56 _proto.isOptimizeForSpeed = function isOptimizeForSpeed() {
57 return this._optimizeForSpeed;
58 };
59
60 _proto.inject = function inject() {
61 var _this = this;
62
63 invariant(!this._injected, 'sheet already injected');
64 this._injected = true;
65
66 if (this._isBrowser && this._optimizeForSpeed) {
67 this._tags[0] = this.makeStyleTag(this._name);
68 this._optimizeForSpeed = 'insertRule' in this.getSheet();
69
70 if (!this._optimizeForSpeed) {
71 if (!isProd) {
72 console.warn('StyleSheet: optimizeForSpeed mode not supported falling back to standard mode.');
73 }
74
75 this.flush();
76 this._injected = true;
77 }
78
79 return;
80 }
81
82 this._serverSheet = {
83 cssRules: [],
84 insertRule: function insertRule(rule, index) {
85 if (typeof index === 'number') {
86 _this._serverSheet.cssRules[index] = {
87 cssText: rule
88 };
89 } else {
90 _this._serverSheet.cssRules.push({
91 cssText: rule
92 });
93 }
94
95 return index;
96 },
97 deleteRule: function deleteRule(index) {
98 _this._serverSheet.cssRules[index] = null;
99 }
100 };
101 };
102
103 _proto.getSheetForTag = function getSheetForTag(tag) {
104 if (tag.sheet) {
105 return tag.sheet;
106 } // this weirdness brought to you by firefox
107
108
109 for (var i = 0; i < document.styleSheets.length; i++) {
110 if (document.styleSheets[i].ownerNode === tag) {
111 return document.styleSheets[i];
112 }
113 }
114 };
115
116 _proto.getSheet = function getSheet() {
117 return this.getSheetForTag(this._tags[this._tags.length - 1]);
118 };
119
120 _proto.insertRule = function insertRule(rule, index) {
121 invariant(isString(rule), '`insertRule` accepts only strings');
122
123 if (!this._isBrowser) {
124 if (typeof index !== 'number') {
125 index = this._serverSheet.cssRules.length;
126 }
127
128 this._serverSheet.insertRule(rule, index);
129
130 return this._rulesCount++;
131 }
132
133 if (this._optimizeForSpeed) {
134 var sheet = this.getSheet();
135
136 if (typeof index !== 'number') {
137 index = sheet.cssRules.length;
138 } // this weirdness for perf, and chrome's weird bug
139 // https://stackoverflow.com/questions/20007992/chrome-suddenly-stopped-accepting-insertrule
140
141
142 try {
143 sheet.insertRule(rule, index);
144 } catch (error) {
145 if (!isProd) {
146 console.warn("StyleSheet: illegal rule: \n\n" + rule + "\n\nSee https://stackoverflow.com/q/20007992 for more info");
147 }
148
149 return -1;
150 }
151 } else {
152 var insertionPoint = this._tags[index];
153
154 this._tags.push(this.makeStyleTag(this._name, rule, insertionPoint));
155 }
156
157 return this._rulesCount++;
158 };
159
160 _proto.replaceRule = function replaceRule(index, rule) {
161 if (this._optimizeForSpeed || !this._isBrowser) {
162 var sheet = this._isBrowser ? this.getSheet() : this._serverSheet;
163
164 if (!rule.trim()) {
165 rule = this._deletedRulePlaceholder;
166 }
167
168 if (!sheet.cssRules[index]) {
169 // @TBD Should we throw an error?
170 return index;
171 }
172
173 sheet.deleteRule(index);
174
175 try {
176 sheet.insertRule(rule, index);
177 } catch (error) {
178 if (!isProd) {
179 console.warn("StyleSheet: illegal rule: \n\n" + rule + "\n\nSee https://stackoverflow.com/q/20007992 for more info");
180 } // In order to preserve the indices we insert a deleteRulePlaceholder
181
182
183 sheet.insertRule(this._deletedRulePlaceholder, index);
184 }
185 } else {
186 var tag = this._tags[index];
187 invariant(tag, "old rule at index `" + index + "` not found");
188 tag.textContent = rule;
189 }
190
191 return index;
192 };
193
194 _proto.deleteRule = function deleteRule(index) {
195 if (!this._isBrowser) {
196 this._serverSheet.deleteRule(index);
197
198 return;
199 }
200
201 if (this._optimizeForSpeed) {
202 this.replaceRule(index, '');
203 } else {
204 var tag = this._tags[index];
205 invariant(tag, "rule at index `" + index + "` not found");
206 tag.parentNode.removeChild(tag);
207 this._tags[index] = null;
208 }
209 };
210
211 _proto.flush = function flush() {
212 this._injected = false;
213 this._rulesCount = 0;
214
215 if (this._isBrowser) {
216 this._tags.forEach(function (tag) {
217 return tag && tag.parentNode.removeChild(tag);
218 });
219
220 this._tags = [];
221 } else {
222 // simpler on server
223 this._serverSheet.cssRules = [];
224 }
225 };
226
227 _proto.cssRules = function cssRules() {
228 var _this2 = this;
229
230 if (!this._isBrowser) {
231 return this._serverSheet.cssRules;
232 }
233
234 return this._tags.reduce(function (rules, tag) {
235 if (tag) {
236 rules = rules.concat(Array.prototype.map.call(_this2.getSheetForTag(tag).cssRules, function (rule) {
237 return rule.cssText === _this2._deletedRulePlaceholder ? null : rule;
238 }));
239 } else {
240 rules.push(null);
241 }
242
243 return rules;
244 }, []);
245 };
246
247 _proto.makeStyleTag = function makeStyleTag(name, cssString, relativeToTag) {
248 if (cssString) {
249 invariant(isString(cssString), 'makeStyleTag acceps only strings as second parameter');
250 }
251
252 var tag = document.createElement('style');
253 if (this._nonce) tag.setAttribute('nonce', this._nonce);
254 tag.type = 'text/css';
255 tag.setAttribute("data-" + name, '');
256
257 if (cssString) {
258 tag.appendChild(document.createTextNode(cssString));
259 }
260
261 var head = document.head || document.getElementsByTagName('head')[0];
262
263 if (relativeToTag) {
264 head.insertBefore(tag, relativeToTag);
265 } else {
266 head.appendChild(tag);
267 }
268
269 return tag;
270 };
271
272 _createClass(StyleSheet, [{
273 key: "length",
274 get: function get() {
275 return this._rulesCount;
276 }
277 }]);
278
279 return StyleSheet;
280}();
281
282exports["default"] = StyleSheet;
283
284function invariant(condition, message) {
285 if (!condition) {
286 throw new Error("StyleSheet: " + message + ".");
287 }
288}
\No newline at end of file