UNPKG

2 kBJavaScriptView Raw
1import { Trace } from '../../trace';
2import { CoreTypes } from '../../core-types';
3import { Length } from './style-properties';
4export function cleanupImportantFlags(value, propertyName) {
5 if (typeof value !== 'string') {
6 return '' + value;
7 }
8 const index = value?.indexOf('!important');
9 if (index >= 0) {
10 if (Trace.isEnabled()) {
11 Trace.write(`The !important css rule is currently not supported. Property: ${propertyName}`, Trace.categories.Style, Trace.messageType.warn);
12 }
13 return value.substring(0, index).trim();
14 }
15 return value;
16}
17/**
18 * Matches whitespace except if the whitespace is contained in parenthesis - ex. rgb(a), hsl color.
19 */
20const PARTS_RE = /\s(?![^(]*\))/;
21/**
22 * Matches a Length value with or without a unit
23 */
24const LENGTH_RE = /^-?[0-9]+[a-zA-Z%]*?$/;
25/**
26 * Checks if the value is a Length or 0
27 */
28const isLength = (v) => v === '0' || LENGTH_RE.test(v);
29export function parseCSSShorthand(value) {
30 const parts = value.trim().split(PARTS_RE);
31 const first = parts[0];
32 if (['', 'none', 'unset'].includes(first)) {
33 return null;
34 }
35 else {
36 const invalidColors = ['inset', 'unset'];
37 const inset = parts.includes('inset');
38 const last = parts[parts.length - 1];
39 let color = 'black';
40 if (first && !isLength(first) && !invalidColors.includes(first)) {
41 color = first;
42 }
43 else if (last && !isLength(last) && !invalidColors.includes(last)) {
44 color = last;
45 }
46 const values = parts
47 .filter((n) => !invalidColors.includes(n))
48 .filter((n) => n !== color)
49 .map((val) => {
50 try {
51 return Length.parse(val);
52 }
53 catch (err) {
54 return CoreTypes.zeroLength;
55 }
56 });
57 return {
58 inset,
59 color,
60 values,
61 };
62 }
63}
64//# sourceMappingURL=css-utils.js.map
\No newline at end of file