Skip to content

Commit e2d23cc

Browse files
committed
Small bug fix in overwrite f
1 parent 36c883b commit e2d23cc

File tree

6 files changed

+25
-13
lines changed

6 files changed

+25
-13
lines changed

Diff for: dist/index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
* @param element
44
* @param propertyName Representing the CSS property name (hyphen case) to be modified.
55
* @param newValue The new property value. If not specified, treated as the empty string.
6-
* @param overwrite
6+
* @param overwrite Set to false when you want to have multiple values for one property, used for defining fallbacks.
77
*/
88
export declare function setStyle(element: Element, propertyName: string, newValue: string | null, overwrite?: boolean): void;

Diff for: dist/index.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: dist/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "style-element-instance",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "set inline styles for any elment instance",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

Diff for: src/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
* @param element
44
* @param propertyName Representing the CSS property name (hyphen case) to be modified.
55
* @param newValue The new property value. If not specified, treated as the empty string.
6-
* @param overwrite
6+
* @param overwrite Set to false when you want to have multiple values for one property, used for defining fallbacks.
77
*/
88
export function setStyle(
99
element: Element,
1010
propertyName: string,
1111
newValue: string | null,
1212
overwrite = true
1313
) {
14-
if ((element as HTMLElement).style && !overwrite)
14+
if ((element as HTMLElement).style && overwrite && !newValue)
1515
return (element as HTMLElement).style.setProperty(propertyName, newValue);
1616

1717
let inlineCssText = element.getAttribute("style") || "";
@@ -21,7 +21,7 @@ export function setStyle(
2121
if (cssText) {
2222
let [property, value] = cssText.split(":");
2323
if (property) property = property.trim();
24-
if (property !== propertyName) result += `; ${propertyName}:${value}`;
24+
if (property !== propertyName) result += `; ${property}:${value}`;
2525
}
2626
return result;
2727
}, "");

Diff for: test/index.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,35 @@ for (const { title, namespace, tagName } of testElements) {
6464
setStyle(
6565
testElement,
6666
"background-image",
67-
"-webkit-linear-gradient(45deg, red 0%, blue 100%);"
67+
"-webkit-linear-gradient(45deg, red 0%, blue 100%);",
68+
false
6869
);
6970
setStyle(
7071
testElement,
7172
"background-image",
72-
"-moz-linear-gradient(45deg, red 0%, blue 100%);"
73+
"-moz-linear-gradient(45deg, red 0%, blue 100%);",
74+
false
7375
);
7476
setStyle(
7577
testElement,
7678
"background-image",
77-
"-ms-linear-gradient(45deg, red 0%, blue 100%);"
79+
"-ms-linear-gradient(45deg, red 0%, blue 100%);",
80+
false
7881
);
79-
setStyle(testElement, "background-image", "foo");
82+
setStyle(testElement, "background-image", "foo", false);
8083
expect(getComputedStyle(testElement).backgroundImage).to.have.string(
8184
"linear-gradient"
8285
);
8386
});
87+
88+
it("should keep other inline styles when one is removed", () => {
89+
testElement.setAttribute(
90+
"style",
91+
"background-color: rgb(0, 255, 0); color: rgb(255, 0, 0)"
92+
);
93+
setStyle(testElement, "background-color", null);
94+
expect(getComputedStyle(testElement).color).to.equal("rgb(255, 0, 0)");
95+
});
8496
});
8597
});
8698
}

0 commit comments

Comments
 (0)