Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: better style binding support #1042

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 39 additions & 6 deletions src/renderer/modules/style.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,44 @@
import { NSVElement } from "../../dom";
import { NormalizedStyle } from "@vue/shared";
import {
NormalizedStyle,
parseStringStyle,
isArray,
isString,
isObject,
} from "@vue/shared";

type Style = string | null;
type Style = string | Record<string, string | number> | null;

function normalizeStyle(style: NormalizedStyle | Style): NormalizedStyle {
if (!style) {
return null;
}

if (typeof style === "string" && style?.trim().charAt(0) === "{") {
return JSON.parse(style);
if (isString(style)) {
if (style.trim().charAt(0) === "{") {
return JSON.parse(style);
}

return parseStringStyle(style);
}

if (isArray(style)) {
return style.reduce(
(
normalizedStyle: NormalizedStyle,
currentStyle: NormalizedStyle | Style
) => {
return Object.assign(normalizedStyle, normalizeStyle(currentStyle));
},
{}
);
}

if (isObject(style)) {
return style as NormalizedStyle;
}

return style as NormalizedStyle;
return {};
}

function normalizeProperty(property: string) {
Expand Down Expand Up @@ -51,7 +77,14 @@ function removeStyleProperty(el: NSVElement, property: string) {
// changing the attribute will not update our originalValue, so when removing
// the previous color will be applied. Fixing this would involve listening to
// individual attribute changes, and it's not worth the overhead.
el.style[property] = originalValue;
try {
el.style[property] = originalValue;
} catch (err) {
// hack: if the original value is invalid, we can't set it back to it's original value
// instead we set it to null, which will remove the style, however this may
// still lead to incorrect styling in some cases.
el.style[property] = null;
}
}
}

Expand Down