single-file-core 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,343 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Author: Gildas Lormeau
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ // derived from https://github.com/jedmao/parse-css-font/
26
+
27
+ /*
28
+ * The MIT License (MIT)
29
+ *
30
+ * Copyright (c) 2015 Jed Mao
31
+ *
32
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
33
+ * of this software and associated documentation files (the "Software"), to deal
34
+ * in the Software without restriction, including without limitation the rights
35
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
36
+ * copies of the Software, and to permit persons to whom the Software is
37
+ * furnished to do so, subject to the following conditions:
38
+
39
+ * The above copyright notice and this permission notice shall be included in all
40
+ * copies or substantial portions of the Software.
41
+
42
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
43
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
44
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
45
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
46
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
47
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
48
+ * SOFTWARE.
49
+ */
50
+
51
+ const REGEXP_SIMPLE_QUOTES_STRING = /^'(.*?)'$/;
52
+ const REGEXP_DOUBLE_QUOTES_STRING = /^"(.*?)"$/;
53
+
54
+ const globalKeywords = [
55
+ "inherit",
56
+ "initial",
57
+ "unset"
58
+ ];
59
+
60
+ const systemFontKeywords = [
61
+ "caption",
62
+ "icon",
63
+ "menu",
64
+ "message-box",
65
+ "small-caption",
66
+ "status-bar"
67
+ ];
68
+
69
+ const fontWeightKeywords = [
70
+ "normal",
71
+ "bold",
72
+ "bolder",
73
+ "lighter",
74
+ "100",
75
+ "200",
76
+ "300",
77
+ "400",
78
+ "500",
79
+ "600",
80
+ "700",
81
+ "800",
82
+ "900"
83
+ ];
84
+
85
+ const fontStyleKeywords = [
86
+ "normal",
87
+ "italic",
88
+ "oblique"
89
+ ];
90
+
91
+ const fontStretchKeywords = [
92
+ "normal",
93
+ "condensed",
94
+ "semi-condensed",
95
+ "extra-condensed",
96
+ "ultra-condensed",
97
+ "expanded",
98
+ "semi-expanded",
99
+ "extra-expanded",
100
+ "ultra-expanded"
101
+ ];
102
+
103
+ const cssFontSizeKeywords = [
104
+ "xx-small",
105
+ "x-small",
106
+ "small",
107
+ "medium",
108
+ "large",
109
+ "x-large",
110
+ "xx-large",
111
+ "larger",
112
+ "smaller"
113
+ ];
114
+
115
+ const cssListHelpers = {
116
+ splitBySpaces,
117
+ split,
118
+ splitByCommas
119
+ };
120
+
121
+ const helpers = {
122
+ isSize
123
+ };
124
+
125
+ const errorPrefix = "[parse-css-font] ";
126
+
127
+ export {
128
+ parse
129
+ };
130
+
131
+ function parse(value) {
132
+ if (typeof value !== "string") {
133
+ throw new TypeError(errorPrefix + "Expected a string.");
134
+ }
135
+ if (value === "") {
136
+ throw error("Cannot parse an empty string.");
137
+ }
138
+ if (systemFontKeywords.indexOf(value) !== -1) {
139
+ return { system: value };
140
+ }
141
+
142
+ const font = {
143
+ lineHeight: "normal",
144
+ stretch: "normal",
145
+ style: "normal",
146
+ variant: "normal",
147
+ weight: "normal",
148
+ };
149
+
150
+ let isLocked = false;
151
+ const tokens = cssListHelpers.splitBySpaces(value);
152
+ let token = tokens.shift();
153
+ for (; token; token = tokens.shift()) {
154
+
155
+ if (token === "normal" || globalKeywords.indexOf(token) !== -1) {
156
+ ["style", "variant", "weight", "stretch"].forEach((prop) => {
157
+ font[prop] = token;
158
+ });
159
+ isLocked = true;
160
+ continue;
161
+ }
162
+
163
+ if (fontWeightKeywords.indexOf(token) !== -1) {
164
+ if (isLocked) {
165
+ continue;
166
+ }
167
+ font.weight = token;
168
+ continue;
169
+ }
170
+
171
+ if (fontStyleKeywords.indexOf(token) !== -1) {
172
+ if (isLocked) {
173
+ continue;
174
+ }
175
+ font.style = token;
176
+ continue;
177
+ }
178
+
179
+ if (fontStretchKeywords.indexOf(token) !== -1) {
180
+ if (isLocked) {
181
+ continue;
182
+ }
183
+ font.stretch = token;
184
+ continue;
185
+ }
186
+
187
+ if (helpers.isSize(token)) {
188
+ const parts = cssListHelpers.split(token, ["/"]);
189
+ font.size = parts[0];
190
+ if (parts[1]) {
191
+ font.lineHeight = parseLineHeight(parts[1]);
192
+ } else if (tokens[0] === "/") {
193
+ tokens.shift();
194
+ font.lineHeight = parseLineHeight(tokens.shift());
195
+ }
196
+ if (!tokens.length) {
197
+ throw error("Missing required font-family.");
198
+ }
199
+ font.family = cssListHelpers.splitByCommas(tokens.join(" ")).map(removeQuotes);
200
+ return font;
201
+ }
202
+
203
+ if (font.variant !== "normal") {
204
+ throw error("Unknown or unsupported font token: " + font.variant);
205
+ }
206
+
207
+ if (isLocked) {
208
+ continue;
209
+ }
210
+ font.variant = token;
211
+ }
212
+
213
+ throw error("Missing required font-size.");
214
+ }
215
+
216
+ function error(message) {
217
+ return new Error(errorPrefix + message);
218
+ }
219
+
220
+ function parseLineHeight(value) {
221
+ const parsed = parseFloat(value);
222
+ if (parsed.toString() === value) {
223
+ return parsed;
224
+ }
225
+ return value;
226
+ }
227
+
228
+ /**
229
+ * Splits a CSS declaration value (shorthand) using provided separators
230
+ * as the delimiters.
231
+ */
232
+ function split(
233
+ /**
234
+ * A CSS declaration value (shorthand).
235
+ */
236
+ value,
237
+ /**
238
+ * Any number of separator characters used for splitting.
239
+ */
240
+ separators,
241
+ {
242
+ last = false,
243
+ } = {},
244
+ ) {
245
+ if (typeof value !== "string") {
246
+ throw new TypeError("expected a string");
247
+ }
248
+ if (!Array.isArray(separators)) {
249
+ throw new TypeError("expected a string array of separators");
250
+ }
251
+ if (typeof last !== "boolean") {
252
+ throw new TypeError("expected a Boolean value for options.last");
253
+ }
254
+ const array = [];
255
+ let current = "";
256
+ let splitMe = false;
257
+
258
+ let func = 0;
259
+ let quote = false;
260
+ let escape = false;
261
+
262
+ for (const char of value) {
263
+
264
+ if (quote) {
265
+ if (escape) {
266
+ escape = false;
267
+ } else if (char === "\\") {
268
+ escape = true;
269
+ } else if (char === quote) {
270
+ quote = false;
271
+ }
272
+ } else if (char === "\"" || char === "'") {
273
+ quote = char;
274
+ } else if (char === "(") {
275
+ func += 1;
276
+ } else if (char === ")") {
277
+ if (func > 0) {
278
+ func -= 1;
279
+ }
280
+ } else if (func === 0) {
281
+ if (separators.indexOf(char) !== -1) {
282
+ splitMe = true;
283
+ }
284
+ }
285
+
286
+ if (splitMe) {
287
+ if (current !== "") {
288
+ array.push(current.trim());
289
+ }
290
+ current = "";
291
+ splitMe = false;
292
+ } else {
293
+ current += char;
294
+ }
295
+ }
296
+
297
+ if (last || current !== "") {
298
+ array.push(current.trim());
299
+ }
300
+ return array;
301
+ }
302
+
303
+ /**
304
+ * Splits a CSS declaration value (shorthand) using whitespace characters
305
+ * as the delimiters.
306
+ */
307
+ function splitBySpaces(
308
+ /**
309
+ * A CSS declaration value (shorthand).
310
+ */
311
+ value,
312
+ ) {
313
+ const spaces = [" ", "\n", "\t"];
314
+ return split(value, spaces);
315
+ }
316
+
317
+ /**
318
+ * Splits a CSS declaration value (shorthand) using commas as the delimiters.
319
+ */
320
+ function splitByCommas(
321
+ /**
322
+ * A CSS declaration value (shorthand).
323
+ */
324
+ value,
325
+ ) {
326
+ const comma = ",";
327
+ return split(value, [comma], { last: true });
328
+ }
329
+
330
+ function isSize(value) {
331
+ return !isNaN(parseFloat(value))
332
+ || value.indexOf("/") !== -1
333
+ || cssFontSizeKeywords.indexOf(value) !== -1;
334
+ }
335
+
336
+ function removeQuotes(string) {
337
+ if (string.match(REGEXP_SIMPLE_QUOTES_STRING)) {
338
+ string = string.replace(REGEXP_SIMPLE_QUOTES_STRING, "$1");
339
+ } else {
340
+ string = string.replace(REGEXP_DOUBLE_QUOTES_STRING, "$1");
341
+ }
342
+ return string.trim();
343
+ }