Skip to content

Commit dad72d8

Browse files
committed
Export as single object and fix nits
1 parent 1015f63 commit dad72d8

File tree

8 files changed

+130
-88
lines changed

8 files changed

+130
-88
lines changed

lib/CSSStyleDeclaration.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
/**
2+
* This is a fork from the CSS Style Declaration part of
3+
* https://github.com/NV/CSSOM
4+
*/
15
"use strict";
26

37
const allProperties = require("./generated/allProperties");
48
const {
59
borderProperties,
610
getPositionValue,
7-
normalizeBorderProperties,
11+
normalizeProperties,
812
prepareBorderProperties,
913
prepareProperties,
1014
shorthandProperties
@@ -24,9 +28,13 @@ const { asciiLowercase } = require("./utils/strings");
2428
*/
2529
class CSSStyleDeclaration {
2630
/**
27-
* @param {object} context - Window, Element or CSSRule
28-
* @param {object} opt - options
29-
* @param {object} [opt.onChange] - on change callback
31+
* @param {object} context - Window, Element or CSSStyleRule
32+
* @param {object} opt - Options
33+
* @param {object} opt.element - Required when the context is a Window
34+
* @param {string} opt.pseudoElement - Applies when the context is a Window
35+
* @param {Function} opt.onChange
36+
* - Executed when cssText changes or property is removed
37+
* - Applies when the context is an Element
3038
*/
3139
constructor(context = globalThis, opt = {}) {
3240
// Make constructor and internals non-enumerable.
@@ -111,6 +119,9 @@ class CSSStyleDeclaration {
111119
this._global = context;
112120
this._computed = true;
113121
this._readonly = true;
122+
if (opt.element && opt.element.nodeType === 1) {
123+
this._ownerNode = opt.element;
124+
}
114125
} else if (context.nodeType === 1 && Object.hasOwn(context, "style")) {
115126
this._global = context.ownerDocument.defaultView;
116127
this._ownerNode = context;
@@ -151,7 +162,7 @@ class CSSStyleDeclaration {
151162
}
152163
properties.set(property, { property, value, priority });
153164
}
154-
const normalizedProperties = normalizeBorderProperties(properties);
165+
const normalizedProperties = normalizeProperties(properties);
155166
const parts = [];
156167
for (const { property, value, priority } of normalizedProperties.values()) {
157168
if (priority) {

lib/CSSStyleProperties.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,29 @@ class CSSStyleProperties extends CSSStyleDeclaration {
1717
}
1818

1919
set cssFloat(value) {
20-
this._setProperty("float", value);
20+
this.setProperty("float", value);
2121
}
2222
}
2323

2424
// Properties
2525
Object.defineProperties(CSSStyleProperties.prototype, generatedProperties);
2626

2727
// Additional properties
28-
[...allProperties, ...allExtraProperties].forEach((property) => {
28+
const propertiesUnion = allProperties.union(allExtraProperties);
29+
// typeof allProperties.union === "function"
30+
// ? allProperties.union(allExtraProperties)
31+
// : new Set([...allProperties, ...allExtraProperties]);
32+
propertiesUnion.forEach((property) => {
2933
if (!implementedProperties.has(property)) {
3034
const declaration = getPropertyDescriptor(property);
3135
Object.defineProperty(CSSStyleProperties.prototype, property, declaration);
3236
const camel = dashedToCamelCase(property);
33-
Object.defineProperty(CSSStyleProperties.prototype, camel, declaration);
34-
if (/^webkit[A-Z]/.test(camel)) {
35-
const pascal = camel.replace(/^webkit/, "Webkit");
36-
Object.defineProperty(CSSStyleProperties.prototype, pascal, declaration);
37+
if (camel !== property) {
38+
Object.defineProperty(CSSStyleProperties.prototype, camel, declaration);
39+
if (/^webkit[A-Z]/.test(camel)) {
40+
const pascal = camel.replace(/^webkit/, "Webkit");
41+
Object.defineProperty(CSSStyleProperties.prototype, pascal, declaration);
42+
}
3743
}
3844
}
3945
});

lib/index.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
/**
2-
* This is a fork from the CSS Style Declaration part of
3-
* https://github.com/NV/CSSOM
4-
*/
51
"use strict";
62

73
const { CSSStyleDeclaration } = require("./CSSStyleDeclaration");

lib/normalize.js

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const padding = require("./properties/padding");
1818

1919
const borderImageProperty = "border-image";
2020

21-
exports.shorthandProperties = new Map([
21+
const shorthandProperties = new Map([
2222
["background", background],
2323
[
2424
"border",
@@ -45,7 +45,7 @@ exports.shorthandProperties = new Map([
4545
["padding", padding]
4646
]);
4747

48-
exports.borderProperties = new Set([
48+
const borderProperties = new Set([
4949
"border",
5050
borderImageProperty,
5151
...border.shorthandFor.keys(),
@@ -56,7 +56,7 @@ exports.borderProperties = new Set([
5656
...borderLeft.shorthandFor.keys()
5757
]);
5858

59-
exports.getPositionValue = (positionValues, position) => {
59+
const getPositionValue = (positionValues, position) => {
6060
switch (positionValues.length) {
6161
case 1: {
6262
const [val1] = positionValues;
@@ -351,7 +351,7 @@ const replacePositionValue = (value, positionValues, position) => {
351351
}
352352
};
353353

354-
exports.prepareBorderProperties = (property, value, priority, properties, opt = {}) => {
354+
const prepareBorderProperties = (property, value, priority, properties, opt = {}) => {
355355
if (typeof property !== "string" || value === null) {
356356
return;
357357
}
@@ -796,7 +796,7 @@ exports.prepareBorderProperties = (property, value, priority, properties, opt =
796796
if (!borderItems.has(name)) {
797797
return;
798798
}
799-
const borderProperties = new Map([[name, borderItems.get(name)]]);
799+
const borderProps = new Map([[name, borderItems.get(name)]]);
800800
for (const line of lines) {
801801
const lineProperty = `${name}-${line}`;
802802
const lineItem = borderItems.get(lineProperty) ??
@@ -805,7 +805,7 @@ exports.prepareBorderProperties = (property, value, priority, properties, opt =
805805
value: "",
806806
priority: ""
807807
};
808-
borderProperties.set(lineProperty, lineItem);
808+
borderProps.set(lineProperty, lineItem);
809809
}
810810
for (const position of positions) {
811811
const positionProperty = `${name}-${position}`;
@@ -815,7 +815,7 @@ exports.prepareBorderProperties = (property, value, priority, properties, opt =
815815
value: "",
816816
priority: ""
817817
};
818-
borderProperties.set(positionProperty, positionItem);
818+
borderProps.set(positionProperty, positionItem);
819819
for (const line of lines) {
820820
const longhandProperty = `${name}-${position}-${line}`;
821821
const longhandItem = borderItems.get(longhandProperty) ??
@@ -824,16 +824,16 @@ exports.prepareBorderProperties = (property, value, priority, properties, opt =
824824
value: "",
825825
priority: ""
826826
};
827-
borderProperties.set(longhandProperty, longhandItem);
827+
borderProps.set(longhandProperty, longhandItem);
828828
}
829829
}
830830
const borderImageItem = borderItems.get(borderImageProperty) ?? {
831831
property: borderImageProperty,
832832
value: "",
833833
priority: ""
834834
};
835-
borderProperties.set(borderImageProperty, borderImageItem);
836-
return borderProperties;
835+
borderProps.set(borderImageProperty, borderImageItem);
836+
return borderProps;
837837
};
838838

839839
const generateBorderLineShorthand = (items, property, prior) => {
@@ -842,7 +842,7 @@ const generateBorderLineShorthand = (items, property, prior) => {
842842
const { value: itemValue } = item;
843843
values.push(itemValue);
844844
}
845-
const value = exports.getPositionValue(values);
845+
const value = getPositionValue(values);
846846
const priority = prior ? prior : "";
847847
return [property, { property, value, priority }];
848848
};
@@ -1149,7 +1149,7 @@ const prepareBorderShorthands = (properties) => {
11491149
return properties;
11501150
};
11511151

1152-
exports.prepareProperties = (properties, opt = {}) => {
1152+
const prepareProperties = (properties, opt = {}) => {
11531153
const { globalObject, options } = opt;
11541154
const parseOpt = {
11551155
globalObject,
@@ -1158,13 +1158,13 @@ exports.prepareProperties = (properties, opt = {}) => {
11581158
const { positions } = borderElements;
11591159
const parsedProperties = new Map();
11601160
const prepareShorthands = new Map();
1161-
const borderProperties = new Map();
1161+
const borderProps = new Map();
11621162
for (const [property, item] of properties) {
11631163
const { value, priority } = item;
11641164
const { logicalPropertyGroup: shorthandProperty } = implementedProperties.get(property) ?? {};
1165-
if (exports.borderProperties.has(property)) {
1166-
borderProperties.set(property, { property, value, priority });
1167-
} else if (exports.shorthandProperties.has(shorthandProperty)) {
1165+
if (borderProperties.has(property)) {
1166+
borderProps.set(property, { property, value, priority });
1167+
} else if (shorthandProperties.has(shorthandProperty)) {
11681168
if (!prepareShorthands.has(shorthandProperty)) {
11691169
prepareShorthands.set(shorthandProperty, new Map());
11701170
}
@@ -1183,8 +1183,8 @@ exports.prepareProperties = (properties, opt = {}) => {
11831183
prepareShorthands.set(shorthandProperty, longhandItems);
11841184
}
11851185
parsedProperties.set(property, item);
1186-
} else if (exports.shorthandProperties.has(property)) {
1187-
const shorthandItem = exports.shorthandProperties.get(property);
1186+
} else if (shorthandProperties.has(property)) {
1187+
const shorthandItem = shorthandProperties.get(property);
11881188
const parsedValues = shorthandItem.parse(value, parseOpt);
11891189
let omitShorthandProperty = false;
11901190
if (Array.isArray(parsedValues)) {
@@ -1199,7 +1199,7 @@ exports.prepareProperties = (properties, opt = {}) => {
11991199
}
12001200
}
12011201
const { position } = longhandItem;
1202-
const longhandValue = exports.getPositionValue([parsedValue], position);
1202+
const longhandValue = getPositionValue([parsedValue], position);
12031203
parsedProperties.set(longhandProperty, {
12041204
property: longhandProperty,
12051205
value: longhandValue,
@@ -1235,7 +1235,7 @@ exports.prepareProperties = (properties, opt = {}) => {
12351235
}
12361236
if (prepareShorthands.size) {
12371237
for (const [property, item] of prepareShorthands) {
1238-
const shorthandItem = exports.shorthandProperties.get(property);
1238+
const shorthandItem = shorthandProperties.get(property);
12391239
if (item.size === shorthandItem.shorthandFor.size) {
12401240
if (shorthandItem.position) {
12411241
const positionValues = [];
@@ -1246,7 +1246,7 @@ exports.prepareProperties = (properties, opt = {}) => {
12461246
priority = longhandPriority;
12471247
}
12481248
}
1249-
const value = exports.getPositionValue(positionValues, shorthandItem.position);
1249+
const value = getPositionValue(positionValues, shorthandItem.position);
12501250
parsedProperties.set(property, {
12511251
property,
12521252
value,
@@ -1256,10 +1256,10 @@ exports.prepareProperties = (properties, opt = {}) => {
12561256
}
12571257
}
12581258
}
1259-
if (borderProperties.size) {
1259+
if (borderProps.size) {
12601260
const longhandProperties = new Map();
1261-
for (const [property, item] of borderProperties) {
1262-
if (exports.shorthandProperties.has(property)) {
1261+
for (const [property, item] of borderProps) {
1262+
if (shorthandProperties.has(property)) {
12631263
const { value, priority } = item;
12641264
if (property === "border") {
12651265
const lineItems = border.parse(value, parseOpt);
@@ -1297,13 +1297,13 @@ exports.prepareProperties = (properties, opt = {}) => {
12971297
});
12981298
}
12991299
} else {
1300-
const shorthandItem = exports.shorthandProperties.get(property);
1300+
const shorthandItem = shorthandProperties.get(property);
13011301
const parsedItem = shorthandItem.parse(value, parseOpt);
13021302
if (Array.isArray(parsedItem)) {
13031303
const [namePart, linePart] = property.split("-");
13041304
for (const position of positions) {
13051305
const longhandProperty = `${namePart}-${position}-${linePart}`;
1306-
const longhandValue = exports.getPositionValue(parsedItem, position);
1306+
const longhandValue = getPositionValue(parsedItem, position);
13071307
const longhandItem = {
13081308
property: longhandProperty,
13091309
value: longhandValue,
@@ -1362,7 +1362,7 @@ exports.prepareProperties = (properties, opt = {}) => {
13621362
return parsedProperties;
13631363
};
13641364

1365-
exports.normalizeBorderProperties = (properties) => {
1365+
const normalizeProperties = (properties) => {
13661366
const { lines, name, positions } = borderElements;
13671367
if (properties.has(name)) {
13681368
for (const line of lines) {
@@ -1406,3 +1406,12 @@ exports.normalizeBorderProperties = (properties) => {
14061406
}
14071407
return properties;
14081408
};
1409+
1410+
module.exports = {
1411+
borderProperties,
1412+
getPositionValue,
1413+
normalizeProperties,
1414+
prepareBorderProperties,
1415+
prepareProperties,
1416+
shorthandProperties
1417+
};

0 commit comments

Comments
 (0)