Skip to content

Commit 1ded2e7

Browse files
committed
Implements StylePropertyMap append
1 parent 408ebae commit 1ded2e7

File tree

2 files changed

+66
-3
lines changed

2 files changed

+66
-3
lines changed

lib/StylePropertyMap.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,37 @@ export default class StylePropertyMap extends StylePropertyMapReadOnly {
1919
* @param {string} value
2020
* @returns {undefined}
2121
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/StylePropertyMap/append}
22-
* @todo Implement
22+
* @see {@link https://drafts.css-houdini.org/css-typed-om/#dom-stylepropertymap-append}
23+
* @todo implement
2324
*/
24-
append() {
25+
append(propertyName = null, propertyValue = null) {
26+
if (!propertyName || !propertyValue) {
27+
throw new TypeError(
28+
`Failed to execute 'set' on 'StylePropertyMap': propertyName and propertyValue arguments are required.`,
29+
);
30+
}
31+
32+
if (Array.isArray(propertyValue)) {
33+
if (propertyValue.length === 0) {
34+
throw new TypeError(
35+
`Failed to execute 'append' on 'StylePropertyMap': Invalid type for property`,
36+
);
37+
}
38+
}
39+
40+
// 1. If property is not a custom property name string, set property to property ASCII lowercased.
41+
const isCustomProp = propertyName.startsWith("--");
42+
// TODO: Handle custom property scenario
43+
const prop = !isCustomProp ? propertyName.toLowerCase() : propertyName;
44+
console.log(prop);
45+
46+
// TODO: 2. If property is not a valid CSS property, throw a TypeError with:
47+
// "Failed to execute 'append' on 'StylePropertyMap': Invalid propertyName: <propertyName>"
48+
// TODO: 3. If property is not a list-valued property, throw a TypeError with:
49+
// "Failed to execute 'append' on 'StylePropertyMap': Property does not support multiple values"
50+
// TODO: 4. If any of the items in values have a non-null [[associatedProperty]] internal slot, and that slot’s value is anything other than property, throw a TypeError.
51+
// TODO: 5. If any of the items in values are a CSSUnparsedValue or CSSVariableReferenceValue object, throw a TypeError.
52+
2553
console.warn("StylePropertyMap.append not implemented yet.");
2654
return undefined;
2755
}

lib/StylePropertyMap.test.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import StylePropertyMap, {
1212
describe("class StylePropertyMap", () => {
1313
const mockStyles = {
1414
background: "red",
15+
"background-image": "url(/path/to/url.jpg)",
1516
color: "#1f1f1f",
1617
width: "500px",
1718
};
@@ -31,7 +32,7 @@ describe("class StylePropertyMap", () => {
3132
});
3233

3334
it("the size property returns as expected", () => {
34-
assert.equal(styleMapInstance.size, 3);
35+
assert.equal(styleMapInstance.size, 4);
3536
});
3637

3738
it("#has returns true when a property exists", () => {
@@ -110,4 +111,38 @@ describe("class StylePropertyMap", () => {
110111
assert.equal(expected.includes(val.toString()), true);
111112
}
112113
});
114+
115+
it("#append throws without required arguments", () => {
116+
assert.throws(() => {
117+
styleMapInstance.append();
118+
}, TypeError);
119+
120+
assert.throws(() => {
121+
styleMapInstance.append("background-image");
122+
}, TypeError);
123+
124+
assert.throws(() => {
125+
styleMapInstance.append("background-image", []);
126+
}, TypeError);
127+
});
128+
129+
it("#append adds the expected property and value", () => {
130+
// Check the initial value that should have a single background image.
131+
assert.equal(
132+
styleMapInstance.get("background-image"),
133+
"url(/path/to/url.jpg)",
134+
);
135+
136+
// Append the new background image
137+
styleMapInstance.append(
138+
"background-image",
139+
"linear-gradient(90deg, red, blue)",
140+
);
141+
142+
// Check that the second background image has been appended
143+
// assert.equal(
144+
// styleMapInstance.get("background-image"),
145+
// "url(/path/to/url.jpg) linear-gradient(90deg, red, blue)",
146+
// );
147+
});
113148
});

0 commit comments

Comments
 (0)