Skip to content

Commit fc7f5aa

Browse files
committed
Update CSSStyleDeclaration.js
1 parent 60c6d16 commit fc7f5aa

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

lib/CSSStyleDeclaration.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,11 @@ const {
1414
} = require("./handlers");
1515
const { normalizeProperties, prepareProperties, shorthandProperties } = require("./normalize");
1616
const { hasVarFunc, parseCSS, parsePropertyValue, prepareValue } = require("./parsers");
17+
const { getCache, setCache } = require("./utils/cache");
1718
const { ELEMENT_NODE, NO_MODIFICATION_ALLOWED_ERR } = require("./utils/constants");
1819
const { getPropertyDescriptor } = require("./utils/propertyDescriptors");
1920
const { asciiLowercase } = require("./utils/strings");
2021

21-
// Internal Setters (Dynamic Assignment)
22-
// These handlers are mapped to internal method names expected by generated properties.
23-
const internalSetters = {
24-
_borderSetter: handleBorder,
25-
_flexBoxSetter: handleFlex,
26-
_positionShorthandSetter: handlePositionShorthand,
27-
_positionLonghandSetter: handlePositionLonghand
28-
};
29-
3022
/**
3123
* @see https://drafts.csswg.org/cssom/#the-cssstyledeclaration-interface
3224
*/
@@ -389,8 +381,12 @@ class CSSStyleDeclaration {
389381
* @returns {object} The parsed CSS object.
390382
*/
391383
_parseCSSText(val) {
392-
// TBD: use cache?
393-
return parseCSS(
384+
const cacheKey = `parseCSSText_${val}`;
385+
const cachedValue = getCache(cacheKey);
386+
if (cachedValue && Object.hasOwn(cachedValue, "children")) {
387+
return cachedValue;
388+
}
389+
const parsedValue = parseCSS(
394390
val,
395391
{
396392
globalObject: this._global,
@@ -401,6 +397,8 @@ class CSSStyleDeclaration {
401397
},
402398
true
403399
);
400+
setCache(cacheKey, parsedValue);
401+
return parsedValue;
404402
}
405403

406404
/**
@@ -451,13 +449,12 @@ class CSSStyleDeclaration {
451449
if (isCustomProperty || hasVarFunc(value)) {
452450
this._addPropertyToMap(properties, property, value, priority);
453451
} else {
454-
// TBD: use cache?
455452
const parsedValue = parsePropertyValue(property, value, {
456453
globalObject: this._global,
457454
options: this._options
458455
});
459456
if (parsedValue) {
460-
this._addPropertyToMap(properties, property, value, priority);
457+
this._addPropertyToMap(properties, property, parsedValue, priority);
461458
} else {
462459
this.removeProperty(property);
463460
}
@@ -518,8 +515,15 @@ for (const definition of propertyDefinitions.values()) {
518515
}
519516
}
520517

518+
// These handlers are mapped to internal method names expected by generated properties.
519+
const internalSetters = {
520+
_borderSetter: handleBorder,
521+
_flexBoxSetter: handleFlex,
522+
_positionShorthandSetter: handlePositionShorthand,
523+
_positionLonghandSetter: handlePositionLonghand
524+
};
525+
521526
// Assign the internal setters to the prototype.
522-
// These methods are used by the properties generated in generated/properties.js.
523527
for (const [methodName, handler] of Object.entries(internalSetters)) {
524528
// NOTE: 'this' must be bound to the CSSStyleDeclaration instance.
525529
CSSStyleDeclaration.prototype[methodName] = function (...args) {

0 commit comments

Comments
 (0)