Skip to content

Commit 4d9266c

Browse files
authored
Replace more constants with property names
This is a follow-up to e337088.
1 parent 35dbfe7 commit 4d9266c

1 file changed

Lines changed: 60 additions & 67 deletions

File tree

lib/normalize.js

Lines changed: 60 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
const propertyDefinitions = require("./generated/propertyDefinitions");
44
const { hasVarFunc, isGlobalKeyword, isValidPropertyValue, splitValue } = require("./parsers");
55
const background = require("./properties/background");
6+
const backgroundColor = require("./properties/backgroundColor");
7+
const backgroundSize = require("./properties/backgroundSize");
68
const border = require("./properties/border");
79
const borderWidth = require("./properties/borderWidth");
810
const borderStyle = require("./properties/borderStyle");
@@ -17,18 +19,7 @@ const margin = require("./properties/margin");
1719
const padding = require("./properties/padding");
1820

1921
/* constants */
20-
const BACKGROUND = "background";
21-
const BACKGROUND_COLOR = "background-color";
22-
const BACKGROUND_SIZE = "background-size";
23-
const BORDER = "border";
24-
const BORDER_BOTTOM = "border-bottom";
25-
const BORDER_COLOR = "border-color";
2622
const BORDER_IMAGE = "border-image";
27-
const BORDER_LEFT = "border-left";
28-
const BORDER_RIGHT = "border-right";
29-
const BORDER_STYLE = "border-style";
30-
const BORDER_TOP = "border-top";
31-
const BORDER_WIDTH = "border-width";
3223
const TOP = "top";
3324
const RIGHT = "right";
3425
const BOTTOM = "bottom";
@@ -46,9 +37,9 @@ const TRBL_INDICES = {
4637

4738
/* shorthands */
4839
const shorthandProperties = new Map([
49-
[BACKGROUND, background],
40+
[background.property, background],
5041
[
51-
BORDER,
42+
border.property,
5243
{
5344
definition: border.definition,
5445
parse: border.parse,
@@ -59,13 +50,13 @@ const shorthandProperties = new Map([
5950
])
6051
}
6152
],
62-
[BORDER_WIDTH, borderWidth],
63-
[BORDER_STYLE, borderStyle],
64-
[BORDER_COLOR, borderColor],
65-
[BORDER_TOP, borderTop],
66-
[BORDER_RIGHT, borderRight],
67-
[BORDER_BOTTOM, borderBottom],
68-
[BORDER_LEFT, borderLeft],
53+
[borderWidth.property, borderWidth],
54+
[borderStyle.property, borderStyle],
55+
[borderColor.property, borderColor],
56+
[borderTop.property, borderTop],
57+
[borderRight.property, borderRight],
58+
[borderBottom.property, borderBottom],
59+
[borderLeft.property, borderLeft],
6960
["flex", flex],
7061
["font", font],
7162
["margin", margin],
@@ -74,7 +65,7 @@ const shorthandProperties = new Map([
7465

7566
/* borders */
7667
const borderProperties = new Set([
77-
BORDER,
68+
border.property,
7869
BORDER_IMAGE,
7970
...border.shorthandFor.keys(),
8071
...border.positionShorthandFor.keys(),
@@ -199,10 +190,10 @@ const replaceBackgroundShorthand = (property, properties, opt) => {
199190
const values = splitValue(parsedValue, {
200191
delimiter: ","
201192
});
202-
const { value: shorthandValue } = properties.get(BACKGROUND);
193+
const { value: shorthandValue } = properties.get(background.property);
203194
const bgValues = background.parse(shorthandValue, opt);
204195
const bgLength = bgValues.length;
205-
if (property === BACKGROUND_COLOR) {
196+
if (property === backgroundColor.property) {
206197
bgValues[bgLength - 1][property] = parsedValue[0];
207198
} else {
208199
for (let i = 0; i < bgLength; i++) {
@@ -216,7 +207,7 @@ const replaceBackgroundShorthand = (property, properties, opt) => {
216207
if (!value || value === background.initialValues.get(longhand)) {
217208
continue;
218209
}
219-
if (longhand === BACKGROUND_SIZE) {
210+
if (longhand === backgroundSize.property) {
220211
bg.push(`/ ${value}`);
221212
} else {
222213
bg.push(value);
@@ -526,7 +517,7 @@ const prepareBorderStringValue = ({
526517
globalObject,
527518
options
528519
};
529-
const shorthandItem = getPropertyItem(BORDER, properties);
520+
const shorthandItem = getPropertyItem(border.property, properties);
530521
const imageItem = getPropertyItem(BORDER_IMAGE, properties);
531522
// Handle longhand properties.
532523
if (prop3) {
@@ -570,7 +561,7 @@ const prepareBorderStringValue = ({
570561
positionItem.value = "";
571562
}
572563
}
573-
borderItems.set(BORDER, shorthandItem);
564+
borderItems.set(border.property, shorthandItem);
574565
borderItems.set(BORDER_IMAGE, imageItem);
575566
borderItems.set(lineProperty, lineItem);
576567
borderItems.set(positionProperty, positionItem);
@@ -639,7 +630,7 @@ const prepareBorderStringValue = ({
639630
longhandItem.priority = priority;
640631
borderItems.set(longhandProperty, longhandItem);
641632
}
642-
borderItems.set(BORDER, shorthandItem);
633+
borderItems.set(border.property, shorthandItem);
643634
borderItems.set(BORDER_IMAGE, imageItem);
644635
borderItems.set(lineWidthProperty, lineWidthItem);
645636
borderItems.set(lineStyleProperty, lineStyleItem);
@@ -678,7 +669,7 @@ const prepareBorderStringValue = ({
678669
borderItems.set(positionProperty, positionItem);
679670
borderItems.set(longhandProperty, longhandItem);
680671
}
681-
borderItems.set(BORDER, shorthandItem);
672+
borderItems.set(border.property, shorthandItem);
682673
borderItems.set(BORDER_IMAGE, imageItem);
683674
borderItems.set(lineProperty, lineItem);
684675
// Handle border shorthand.
@@ -732,7 +723,7 @@ const prepareBorderArrayValue = ({ value, priority, properties, parts, opt, bord
732723
if (!value.length || !borderLines.has(prop2)) {
733724
return;
734725
}
735-
const shorthandItem = getPropertyItem(BORDER, properties);
726+
const shorthandItem = getPropertyItem(border.property, properties);
736727
const imageItem = getPropertyItem(BORDER_IMAGE, properties);
737728
const lineProperty = `${prop1}-${prop2}`;
738729
const lineItem = getPropertyItem(lineProperty, properties);
@@ -803,7 +794,7 @@ const prepareBorderArrayValue = ({ value, priority, properties, parts, opt, bord
803794
borderItems.set(positionProperty, positionItem);
804795
borderItems.set(longhandProperty, longhandItem);
805796
}
806-
borderItems.set(BORDER, shorthandItem);
797+
borderItems.set(border.property, shorthandItem);
807798
borderItems.set(BORDER_IMAGE, imageItem);
808799
borderItems.set(lineProperty, lineItem);
809800
};
@@ -841,7 +832,7 @@ const prepareBorderObjectValue = ({
841832
if (!borderPositions.has(prop2)) {
842833
return;
843834
}
844-
const shorthandItem = getPropertyItem(BORDER, properties);
835+
const shorthandItem = getPropertyItem(border.property, properties);
845836
const lineWidthProperty = `${prop1}-width`;
846837
const lineWidthItem = getPropertyItem(lineWidthProperty, properties);
847838
const lineStyleProperty = `${prop1}-style`;
@@ -889,7 +880,7 @@ const prepareBorderObjectValue = ({
889880
longhandItem.priority = priority;
890881
borderItems.set(longhandProperty, longhandItem);
891882
}
892-
borderItems.set(BORDER, shorthandItem);
883+
borderItems.set(border.property, shorthandItem);
893884
borderItems.set(BORDER_IMAGE, imageItem);
894885
borderItems.set(lineWidthProperty, lineWidthItem);
895886
borderItems.set(lineStyleProperty, lineStyleItem);
@@ -967,17 +958,17 @@ const prepareBorderProperties = (property, value, priority, properties, opt = {}
967958
if (typeof property !== "string" || value === null) {
968959
return;
969960
}
970-
if (!property.startsWith(BORDER)) {
961+
if (!property.startsWith(border.property)) {
971962
return;
972963
}
973964
let prop2, prop3;
974-
if (property.length > BORDER.length) {
965+
if (property.length > border.property.length) {
975966
// Check if next char is '-'
976-
if (property.charCodeAt(BORDER.length) !== 45) {
967+
if (property.charCodeAt(border.property.length) !== 45) {
977968
return;
978969
}
979970
// property is like "border-..."
980-
const remainder = property.substring(BORDER.length + 1);
971+
const remainder = property.substring(border.property.length + 1);
981972
const hyphenIndex = remainder.indexOf("-");
982973
if (hyphenIndex !== -1) {
983974
prop2 = remainder.substring(0, hyphenIndex);
@@ -993,7 +984,7 @@ const prepareBorderProperties = (property, value, priority, properties, opt = {}
993984
return;
994985
}
995986
const parts = {
996-
prop1: BORDER,
987+
prop1: border.property,
997988
prop2,
998989
prop3
999990
};
@@ -1028,22 +1019,22 @@ const prepareBorderProperties = (property, value, priority, properties, opt = {}
10281019
borderItems
10291020
});
10301021
}
1031-
if (!borderItems.has(BORDER)) {
1022+
if (!borderItems.has(border.property)) {
10321023
return;
10331024
}
1034-
const borderProps = new Map([[BORDER, borderItems.get(BORDER)]]);
1025+
const borderProps = new Map([[border.property, borderItems.get(border.property)]]);
10351026
for (const line of borderLines) {
1036-
const lineProperty = `${BORDER}-${line}`;
1027+
const lineProperty = `${border.property}-${line}`;
10371028
const lineItem = borderItems.get(lineProperty) ?? getPropertyItem(lineProperty, properties);
10381029
borderProps.set(lineProperty, lineItem);
10391030
}
10401031
for (const position of borderPositions) {
1041-
const positionProperty = `${BORDER}-${position}`;
1032+
const positionProperty = `${border.property}-${position}`;
10421033
const positionItem =
10431034
borderItems.get(positionProperty) ?? getPropertyItem(positionProperty, properties);
10441035
borderProps.set(positionProperty, positionItem);
10451036
for (const line of borderLines) {
1046-
const longhandProperty = `${BORDER}-${position}-${line}`;
1037+
const longhandProperty = `${border.property}-${position}-${line}`;
10471038
const longhandItem =
10481039
borderItems.get(longhandProperty) ?? getPropertyItem(longhandProperty, properties);
10491040
borderProps.set(longhandProperty, longhandItem);
@@ -1108,31 +1099,31 @@ const generateBorderShorthand = (items, property, priority = "") => {
11081099

11091100
const borderCollectionConfig = {
11101101
[WIDTH]: {
1111-
shorthand: BORDER_WIDTH,
1102+
shorthand: borderWidth.property,
11121103
generator: generateBorderLineShorthand
11131104
},
11141105
[STYLE]: {
1115-
shorthand: BORDER_STYLE,
1106+
shorthand: borderStyle.property,
11161107
generator: generateBorderLineShorthand
11171108
},
11181109
[COLOR]: {
1119-
shorthand: BORDER_COLOR,
1110+
shorthand: borderColor.property,
11201111
generator: generateBorderLineShorthand
11211112
},
11221113
[TOP]: {
1123-
shorthand: BORDER_TOP,
1114+
shorthand: borderTop.property,
11241115
generator: generateBorderPositionShorthand
11251116
},
11261117
[RIGHT]: {
1127-
shorthand: BORDER_RIGHT,
1118+
shorthand: borderRight.property,
11281119
generator: generateBorderPositionShorthand
11291120
},
11301121
[BOTTOM]: {
1131-
shorthand: BORDER_BOTTOM,
1122+
shorthand: borderBottom.property,
11321123
generator: generateBorderPositionShorthand
11331124
},
11341125
[LEFT]: {
1135-
shorthand: BORDER_LEFT,
1126+
shorthand: borderLeft.property,
11361127
generator: generateBorderPositionShorthand
11371128
}
11381129
};
@@ -1217,15 +1208,15 @@ const prepareBorderShorthands = (properties) => {
12171208
const mixedPriorities = shorthandItems.length && shorthandPriorityItems.length;
12181209
const imageItem = createPropertyItem(BORDER_IMAGE, NONE);
12191210
if (shorthandItems.length === 4) {
1220-
const [property, item] = generateBorderShorthand(shorthandItems, BORDER) ?? [];
1211+
const [property, item] = generateBorderShorthand(shorthandItems, border.property) ?? [];
12211212
if (property && item) {
12221213
properties.set(property, item);
12231214
properties.delete(BORDER_IMAGE);
12241215
properties.set(BORDER_IMAGE, imageItem);
12251216
}
12261217
} else if (shorthandPriorityItems.length === 4) {
12271218
const [property, item] =
1228-
generateBorderShorthand(shorthandPriorityItems, BORDER, "important") ?? [];
1219+
generateBorderShorthand(shorthandPriorityItems, border.property, "important") ?? [];
12291220
if (property && item) {
12301221
properties.set(property, item);
12311222
properties.delete(BORDER_IMAGE);
@@ -1326,7 +1317,7 @@ const processBorderProperties = (borders, parseOpt) => {
13261317
for (const [property, item] of borders) {
13271318
if (shorthandProperties.has(property)) {
13281319
const { value, priority } = item;
1329-
if (property === BORDER) {
1320+
if (property === border.property) {
13301321
const lineItems = border.parse(value, parseOpt);
13311322
for (const [key, initialValue] of border.initialValues) {
13321323
if (!Object.hasOwn(lineItems, key)) {
@@ -1482,25 +1473,27 @@ const prepareProperties = (properties, opt = {}) => {
14821473
}
14831474
}
14841475
if (!omitShorthandProperty) {
1485-
if (property === BACKGROUND) {
1476+
if (property === background.property) {
14861477
hasPrecedingBackground = true;
14871478
}
14881479
parsedProperties.set(property, createPropertyItem(property, value, priority));
14891480
}
14901481
} else {
14911482
parsedProperties.set(property, createPropertyItem(property, value, priority));
14921483
if (hasPrecedingBackground) {
1493-
const { value: shorthandValue, priority: shorthandPriority } = properties.get(BACKGROUND);
1484+
const { value: shorthandValue, priority: shorthandPriority } = properties.get(
1485+
background.property
1486+
);
14941487
if ((!shorthandPriority || priority) && !hasVarFunc(shorthandValue)) {
14951488
const replacedShorthandValue = replaceBackgroundShorthand(
14961489
property,
14971490
parsedProperties,
14981491
parseOpt
14991492
);
1500-
properties.delete(BACKGROUND);
1493+
properties.delete(background.property);
15011494
properties.set(
1502-
BACKGROUND,
1503-
createPropertyItem(BACKGROUND, replacedShorthandValue, shorthandPriority)
1495+
background.property,
1496+
createPropertyItem(background.property, replacedShorthandValue, shorthandPriority)
15041497
);
15051498
}
15061499
}
@@ -1528,35 +1521,35 @@ const prepareProperties = (properties, opt = {}) => {
15281521
* @returns {Map} The normalized properties map.
15291522
*/
15301523
const normalizeProperties = (properties) => {
1531-
if (properties.has(BORDER)) {
1524+
if (properties.has(border.property)) {
15321525
for (const line of borderLines) {
1533-
properties.delete(`${BORDER}-${line}`);
1526+
properties.delete(`${border.property}-${line}`);
15341527
}
15351528
for (const position of borderPositions) {
1536-
properties.delete(`${BORDER}-${position}`);
1529+
properties.delete(`${border.property}-${position}`);
15371530
for (const line of borderLines) {
1538-
properties.delete(`${BORDER}-${position}-${line}`);
1531+
properties.delete(`${border.property}-${position}-${line}`);
15391532
}
15401533
}
1541-
properties.delete(`${BORDER}-image`);
1534+
properties.delete(`${border.property}-image`);
15421535
}
15431536
for (const line of borderLines) {
1544-
const lineProperty = `${BORDER}-${line}`;
1537+
const lineProperty = `${border.property}-${line}`;
15451538
if (properties.has(lineProperty)) {
15461539
for (const position of borderPositions) {
1547-
const positionProperty = `${BORDER}-${position}`;
1548-
const longhandProperty = `${BORDER}-${position}-${line}`;
1540+
const positionProperty = `${border.property}-${position}`;
1541+
const longhandProperty = `${border.property}-${position}-${line}`;
15491542
properties.delete(positionProperty);
15501543
properties.delete(longhandProperty);
15511544
}
15521545
}
15531546
}
15541547
for (const position of borderPositions) {
1555-
const positionProperty = `${BORDER}-${position}`;
1548+
const positionProperty = `${border.property}-${position}`;
15561549
if (properties.has(positionProperty)) {
15571550
const longhandProperties = [];
15581551
for (const line of borderLines) {
1559-
const longhandProperty = `${BORDER}-${position}-${line}`;
1552+
const longhandProperty = `${border.property}-${position}-${line}`;
15601553
longhandProperties.push(longhandProperty);
15611554
}
15621555
if (longhandProperties.length === 3) {

0 commit comments

Comments
 (0)