Debian Codegraph @

mixmark-io-domino/lib/style_parser.js

raw symbols SCIP
1 "use strict";
2 /**
3 * @license
4 * Copyright Google LLC All Rights Reserved.
5 *
6 * Use of this source code is governed by an MIT-style license that can be
7 * found in the LICENSE file at https://angular.io/license
8 */
9
10 // The below is a compiled copy of https://github.com/angular/angular/blob/92e41e9cb417223d9888a4c23b4c0e73188f87d0/packages/compiler/src/render3/view/style_parser.ts
11
12 Object.defineProperty(exports, "__esModule", { value: true });
13 exports.hyphenate = exports.parse = void 0;
14 /**
15 * Parses string representation of a style and converts it into object literal.
16 *
17 * @param value string representation of style as used in the `style` attribute in HTML.
18 * Example: `color: red; height: auto`.
19 * @returns An array of style property name and value pairs, e.g. `['color', 'red', 'height',
20 * 'auto']`
21 */
22 function parse(value) {
23 // we use a string array here instead of a string map
24 // because a string-map is not guaranteed to retain the
25 // order of the entries whereas a string array can be
26 // constructed in a [key, value, key, value] format.
27 const styles = [];
28 let i = 0;
29 let parenDepth = 0;
30 let quote = 0; /* Char.QuoteNone */
31 let valueStart = 0;
32 let propStart = 0;
33 let currentProp = null;
34 while (i < value.length) {
35 const token = value.charCodeAt(i++);
36 switch (token) {
37 case 40 /* Char.OpenParen */:
38 parenDepth++;
39 break;
40 case 41 /* Char.CloseParen */:
41 parenDepth--;
42 break;
43 case 39 /* Char.QuoteSingle */:
44 // valueStart needs to be there since prop values don't
45 // have quotes in CSS
46 if (quote === 0 /* Char.QuoteNone */) {
47 quote = 39 /* Char.QuoteSingle */;
48 } else if (
49 quote === 39 /* Char.QuoteSingle */ &&
50 value.charCodeAt(i - 1) !== 92 /* Char.BackSlash */
51 ) {
52 quote = 0 /* Char.QuoteNone */;
53 }
54 break;
55 case 34 /* Char.QuoteDouble */:
56 // same logic as above
57 if (quote === 0 /* Char.QuoteNone */) {
58 quote = 34 /* Char.QuoteDouble */;
59 } else if (
60 quote === 34 /* Char.QuoteDouble */ &&
61 value.charCodeAt(i - 1) !== 92 /* Char.BackSlash */
62 ) {
63 quote = 0 /* Char.QuoteNone */;
64 }
65 break;
66 case 58 /* Char.Colon */:
67 if (
68 !currentProp &&
69 parenDepth === 0 &&
70 quote === 0 /* Char.QuoteNone */
71 ) {
72 currentProp = hyphenate(value.substring(propStart, i - 1).trim());
73 valueStart = i;
74 }
75 break;
76 case 59 /* Char.Semicolon */:
77 if (
78 currentProp &&
79 valueStart > 0 &&
80 parenDepth === 0 &&
81 quote === 0 /* Char.QuoteNone */
82 ) {
83 const styleVal = value.substring(valueStart, i - 1).trim();
84 styles.push(currentProp, styleVal);
85 propStart = i;
86 valueStart = 0;
87 currentProp = null;
88 }
89 break;
90 }
91 }
92 if (currentProp && valueStart) {
93 const styleVal = value.slice(valueStart).trim();
94 styles.push(currentProp, styleVal);
95 }
96 return styles;
97 }
98 exports.parse = parse;
99 function hyphenate(value) {
100 return value
101 .replace(/[a-z][A-Z]/g, (v) => {
102 return v.charAt(0) + "-" + v.charAt(1);
103 })
104 .toLowerCase();
105 }
106 exports.hyphenate = hyphenate;