Skip to content

Commit bb569b7

Browse files
committed
fix svg hmi prop parsing
1 parent b7c49a4 commit bb569b7

1 file changed

Lines changed: 25 additions & 19 deletions

File tree

src/SvgHmi.ts

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ export class SvgHmi extends BaseCustomWebComponentConstructorAppend {
5555

5656
if (attributeName == 'src')
5757
this.src = this.getAttribute('src');
58-
else {
59-
const prp = this._svgHmiProperties.get(attributeName);
60-
if (prp != null) {
61-
this['__' + prp.name] = SvgHmi.parseValue(this.getAttribute(attributeName), prp.type);
62-
}
63-
}
64-
}
58+
else {
59+
const prp = this._svgHmiProperties.get(attributeName);
60+
if (prp != null) {
61+
this['__' + prp.name] = this._readPropertyAttribute(attributeName, prp.type, prp.default);
62+
}
63+
}
64+
}
6565
}
6666
this._evaluateSvgBindings();
6767
});
@@ -108,13 +108,12 @@ export class SvgHmi extends BaseCustomWebComponentConstructorAppend {
108108
let d = SvgHmi.parseValue(n.getAttribute('default') ?? "", tp);
109109
this._svgHmiProperties.set(nm, { name: name, type: tp, default: d });
110110

111-
let val = this.getAttribute(nm);
112-
this['__' + name] = val == null ? d : SvgHmi.parseValue(val, tp);
113-
114-
if (this[name]) {
115-
this['__' + name] = SvgHmi.parseValue(this[name], tp);
116-
delete this[name];
117-
}
111+
this['__' + name] = this._readPropertyAttribute(nm, tp, d);
112+
113+
if (Object.prototype.hasOwnProperty.call(this, name)) {
114+
this['__' + name] = SvgHmi.parseValue(this[name], tp);
115+
delete this[name];
116+
}
118117

119118
Object.defineProperty(this, name, {
120119
get() {
@@ -162,9 +161,16 @@ export class SvgHmi extends BaseCustomWebComponentConstructorAppend {
162161

163162

164163
this._evaluateSvgBindings();
165-
this.shadowRoot!.appendChild(svgNode);
166-
}
167-
}
164+
this.shadowRoot!.appendChild(svgNode);
165+
}
166+
}
167+
168+
private _readPropertyAttribute(attributeName: string, type: string, defaultValue: unknown) {
169+
if (this.hasAttribute(attributeName))
170+
return SvgHmi.parseValue(this.getAttribute(attributeName) ?? "", type);
171+
172+
return defaultValue;
173+
}
168174

169175
public static camelToDashCase(text: string) {
170176
return text[0].toLowerCase() + text.substring(1).replace(/([A-Z])/g, (g) => `-${g[0].toLowerCase()}`);
@@ -181,8 +187,8 @@ export class SvgHmi extends BaseCustomWebComponentConstructorAppend {
181187
switch (type) {
182188
case "number":
183189
return typeof value == "number" ? value : Number(value);
184-
case "boolean":
185-
return typeof value == "boolean" ? value : String(value).toLowerCase() == "true";
190+
case "boolean":
191+
return typeof value == "boolean" ? value : value === "" || String(value).toLowerCase() == "true";
186192
case "string":
187193
return String(value);
188194
case "HmiColor":

0 commit comments

Comments
 (0)