Skip to content

Commit a35b915

Browse files
committed
Refactor longhand property updates into helper function
Introduces the updateLonghandProperties helper to consolidate logic for updating longhand properties in multiple places. This reduces code duplication and improves maintainability in normalize.js.
1 parent 4ba7f29 commit a35b915

File tree

1 file changed

+27
-28
lines changed

1 file changed

+27
-28
lines changed

lib/normalize.js

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,7 +1303,6 @@ const updatePositionLonghands = (properties, namePart, linePart, value, priority
13031303
const longhandProperty = `${namePart}-${position}-${linePart}`;
13041304
const longhandValue = Array.isArray(value) ? getPositionValue(value, position) : value;
13051305
const longhandItem = createPropertyItem(longhandProperty, longhandValue, priority);
1306-
13071306
if (properties.has(longhandProperty)) {
13081307
const { priority: existingPriority } = properties.get(longhandProperty);
13091308
if (!existingPriority) {
@@ -1316,6 +1315,30 @@ const updatePositionLonghands = (properties, namePart, linePart, value, priority
13161315
}
13171316
};
13181317

1318+
/**
1319+
* Updates longhand properties based on a values object.
1320+
*
1321+
* @param {Map} properties - The map of properties to update.
1322+
* @param {Object} values - The object containing property keys and values.
1323+
* @param {string} priority - The priority of the properties.
1324+
* @param {boolean} [safe=false] - Whether to preserve existing priority.
1325+
*/
1326+
const updateLonghandProperties = (properties, values, priority, safe = false) => {
1327+
for (const property of Object.keys(values)) {
1328+
const value = values[property];
1329+
const item = createPropertyItem(property, value, priority);
1330+
if (safe && properties.has(property)) {
1331+
const { priority: existingPriority } = properties.get(property);
1332+
if (!existingPriority) {
1333+
properties.delete(property);
1334+
properties.set(property, item);
1335+
}
1336+
} else {
1337+
properties.set(property, item);
1338+
}
1339+
}
1340+
};
1341+
13191342
/**
13201343
* Processes border properties from the borders map, expanding and normalizing them.
13211344
*
@@ -1372,19 +1395,7 @@ const processBorderProperties = (borders, parseOpt) => {
13721395
parsedItem[key] = initialValue;
13731396
}
13741397
}
1375-
for (const longhandProperty of Object.keys(parsedItem)) {
1376-
const longhandValue = parsedItem[longhandProperty];
1377-
const longhandItem = createPropertyItem(longhandProperty, longhandValue, priority);
1378-
if (longhandProperties.has(longhandProperty)) {
1379-
const { priority: longhandPriority } = longhandProperties.get(longhandProperty);
1380-
if (!longhandPriority) {
1381-
longhandProperties.delete(longhandProperty);
1382-
longhandProperties.set(longhandProperty, longhandItem);
1383-
}
1384-
} else {
1385-
longhandProperties.set(longhandProperty, longhandItem);
1386-
}
1387-
}
1398+
updateLonghandProperties(longhandProperties, parsedItem, priority, true);
13881399
}
13891400
}
13901401
} else if (longhandProperties.has(property)) {
@@ -1465,22 +1476,10 @@ const prepareProperties = (properties, opt = {}) => {
14651476
);
14661477
}
14671478
} else if (parsedValue) {
1468-
for (const longhandProperty of Object.keys(parsedValue)) {
1469-
const longhandValue = parsedValue[longhandProperty];
1470-
parsedProperties.set(
1471-
longhandProperty,
1472-
createPropertyItem(longhandProperty, longhandValue, priority)
1473-
);
1474-
}
1479+
updateLonghandProperties(parsedProperties, parsedValue, priority);
14751480
}
14761481
} else if (parsedValues && typeof parsedValues !== "string") {
1477-
for (const longhandProperty of Object.keys(parsedValues)) {
1478-
const longhandValue = parsedValues[longhandProperty];
1479-
parsedProperties.set(
1480-
longhandProperty,
1481-
createPropertyItem(longhandProperty, longhandValue, priority)
1482-
);
1483-
}
1482+
updateLonghandProperties(parsedProperties, parsedValues, priority);
14841483
}
14851484
if (!omitShorthandProperty) {
14861485
if (property === BACKGROUND) {

0 commit comments

Comments
 (0)