Skip to content

Commit 943309f

Browse files
committed
Fix normalize.js
1 parent d399c7a commit 943309f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+1784
-812
lines changed

lib/CSSStyleDeclaration.js

Lines changed: 74 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -193,22 +193,10 @@ class CSSStyleDeclaration {
193193
property,
194194
value: { value }
195195
} = item;
196-
const priority = important ? "important" : "";
197-
const isCustomProperty = property.startsWith("--");
198-
if (isCustomProperty || hasVarFunc(value)) {
199-
if (properties.has(property)) {
200-
const { priority: itemPriority } = properties.get(property);
201-
if (!itemPriority) {
202-
properties.set(property, { property, value, priority });
203-
}
204-
} else {
205-
properties.set(property, { property, value, priority });
206-
}
207-
} else {
208-
const parsedValue = parsePropertyValue(property, value, {
209-
globalObject: this._global
210-
});
211-
if (parsedValue) {
196+
if (typeof property === "string" && typeof value === "string") {
197+
const priority = important ? "important" : "";
198+
const isCustomProperty = property.startsWith("--");
199+
if (isCustomProperty || hasVarFunc(value)) {
212200
if (properties.has(property)) {
213201
const { priority: itemPriority } = properties.get(property);
214202
if (!itemPriority) {
@@ -218,7 +206,21 @@ class CSSStyleDeclaration {
218206
properties.set(property, { property, value, priority });
219207
}
220208
} else {
221-
this.removeProperty(property);
209+
const parsedValue = parsePropertyValue(property, value, {
210+
globalObject: this._global
211+
});
212+
if (parsedValue) {
213+
if (properties.has(property)) {
214+
const { priority: itemPriority } = properties.get(property);
215+
if (!itemPriority) {
216+
properties.set(property, { property, value, priority });
217+
}
218+
} else {
219+
properties.set(property, { property, value, priority });
220+
}
221+
} else {
222+
this.removeProperty(property);
223+
}
222224
}
223225
}
224226
}
@@ -227,6 +229,7 @@ class CSSStyleDeclaration {
227229
});
228230
for (const [property, item] of parsedProperties) {
229231
const { priority, value } = item;
232+
this._priorities.set(property, priority);
230233
this.setProperty(property, value, priority);
231234
}
232235
}
@@ -453,19 +456,27 @@ Object.defineProperties(CSSStyleDeclaration.prototype, {
453456
enumerable: false
454457
},
455458

459+
// TODO: Working, but check later if this is really the right way to do.
456460
_shorthandSetter: {
457461
/**
458462
* @param {string} property
459463
* @param {string} val
464+
* @param {string} prior
460465
* @param {object} shorthandFor
461466
*/
462-
value(property, val, shorthandFor) {
467+
value(property, val, prior, shorthandFor) {
463468
const obj = parseShorthand(val, shorthandFor, {
464469
globalObject: this._global
465470
});
466471
if (!obj) {
467472
return;
468473
}
474+
let priority = "";
475+
if (typeof prior === "string") {
476+
priority = prior;
477+
} else {
478+
priority = this._priorities.get(property) ?? "";
479+
}
469480
for (const subprop of Object.keys(obj)) {
470481
// In case subprop is an implicit property, this will clear *its*
471482
// subpropertiesX.
@@ -476,7 +487,7 @@ Object.defineProperties(CSSStyleDeclaration.prototype, {
476487
this.removeProperty(subprop);
477488
// Don't add in empty properties.
478489
if (obj[subprop] !== "") {
479-
this._values.set(subprop, obj[subprop]);
490+
this._values.set(subprop, obj[subprop], priority);
480491
}
481492
}
482493
for (const [subprop] of shorthandFor) {
@@ -492,9 +503,8 @@ Object.defineProperties(CSSStyleDeclaration.prototype, {
492503
this.removeProperty(property);
493504
const calculated = this._shorthandGetter(property, shorthandFor);
494505
if (calculated !== "") {
495-
this._setProperty(property, calculated);
506+
this._setProperty(property, calculated, priority);
496507
}
497-
return obj;
498508
},
499509
enumerable: false
500510
},
@@ -503,8 +513,9 @@ Object.defineProperties(CSSStyleDeclaration.prototype, {
503513
/**
504514
* @param {string} prop
505515
* @param {Array|string} val
516+
* @param {string} prior
506517
*/
507-
value(prop, val) {
518+
value(prop, val, prior) {
508519
if (!shorthandProperties.has(prop)) {
509520
return;
510521
}
@@ -516,24 +527,33 @@ Object.defineProperties(CSSStyleDeclaration.prototype, {
516527
} else {
517528
return;
518529
}
519-
const priority = this._priorities.get(prop) ?? "";
520-
const { shorthandFor } = shorthandProperties.get(prop);
530+
let priority = "";
531+
if (typeof prior === "string") {
532+
priority = prior;
533+
} else {
534+
priority = this._priorities.get(prop) ?? "";
535+
}
536+
const { position, shorthandFor } = shorthandProperties.get(prop);
521537
let hasPriority = false;
522-
for (const [longhandProperty, { position }] of shorthandFor) {
523-
const longhandValue = getPositionValue(shorthandValues, position);
524-
const longhandPriority = this._priorities.get(longhandProperty) ?? "";
538+
for (const [longhandProperty, longhandItem] of shorthandFor) {
539+
const { position: longhandPosition } = longhandItem;
540+
const longhandValue = getPositionValue(shorthandValues, longhandPosition);
525541
if (priority) {
526-
this._setProperty(longhandProperty, longhandValue, longhandPriority);
527-
} else if (longhandPriority) {
528-
hasPriority = true;
542+
this._setProperty(longhandProperty, longhandValue, priority);
529543
} else {
530-
this._setProperty(longhandProperty, longhandValue, longhandPriority);
544+
const longhandPriority = this._priorities.get(longhandProperty) ?? "";
545+
if (longhandPriority) {
546+
hasPriority = true;
547+
} else {
548+
this._setProperty(longhandProperty, longhandValue, priority);
549+
}
531550
}
532551
}
533552
if (hasPriority) {
534553
this.removeProperty(prop);
535554
} else {
536-
this._setProperty(prop, shorthandValues.join(" "), priority);
555+
const shorthandValue = getPositionValue(shorthandValues, position);
556+
this._setProperty(prop, shorthandValue, priority);
537557
}
538558
}
539559
},
@@ -542,15 +562,21 @@ Object.defineProperties(CSSStyleDeclaration.prototype, {
542562
/**
543563
* @param {string} prop
544564
* @param {string} val
565+
* @param {string} prior
545566
*/
546-
value(prop, val) {
567+
value(prop, val, prior) {
547568
const { logicalPropertyGroup: shorthandProperty } = implementedProperties.get(prop) ?? {};
548569
if (!shorthandProperty || !shorthandProperties.has(shorthandProperty)) {
549570
return;
550571
}
551572
const shorthandPriority = this._priorities.get(shorthandProperty);
552-
const priority = this._priorities.get(prop) ?? "";
553573
this._setProperty(shorthandProperty, "");
574+
let priority = "";
575+
if (typeof prior === "string") {
576+
priority = prior;
577+
} else {
578+
priority = this._priorities.get(prop) ?? "";
579+
}
554580
if (shorthandPriority && priority) {
555581
this._setProperty(prop, val);
556582
} else {
@@ -580,18 +606,28 @@ Object.defineProperties(CSSStyleDeclaration.prototype, {
580606
/**
581607
* @param {string} prop
582608
* @param {object|Array|string} val
609+
* @param {string} prior
583610
*/
584-
value(prop, val) {
611+
value(prop, val, prior) {
585612
const properties = new Map();
586613
if (prop === "border") {
587-
const priority = this._priorities.get(prop) ?? "";
614+
let priority = "";
615+
if (typeof prior === "string") {
616+
priority = prior;
617+
} else {
618+
priority = this._priorities.get(prop) ?? "";
619+
}
588620
properties.set(prop, { propery: prop, value: val, priority });
589621
} else {
590622
for (let i = 0; i < this._length; i++) {
591623
const property = this[i];
592624
if (borderProperties.has(property)) {
593625
const value = this.getPropertyValue(property);
594-
const priority = this._priorities.get(property) ?? "";
626+
const longhandPriority = this._priorities.get(property) ?? "";
627+
let priority = longhandPriority;
628+
if (prop === property && typeof prior === "string") {
629+
priority = prior;
630+
}
595631
properties.set(property, { property, value, priority });
596632
}
597633
}
@@ -612,7 +648,7 @@ Object.defineProperties(CSSStyleDeclaration.prototype, {
612648
Object.defineProperties(CSSStyleDeclaration.prototype, generatedProperties);
613649

614650
// Additional properties
615-
[...allProperties, ...allExtraProperties].forEach(function (property) {
651+
[...allProperties, ...allExtraProperties].forEach((property) => {
616652
if (!implementedProperties.has(property)) {
617653
const declaration = getPropertyDescriptor(property);
618654
Object.defineProperty(CSSStyleDeclaration.prototype, property, declaration);

0 commit comments

Comments
 (0)