Skip to content

Commit 0b414e1

Browse files
ca-dJakobVogelsang
andauthored
fix(Editing): set false attribute values on update (openscd#899)
* fix(editing): set false attribute values on update Since we switched from Replace to Update, we now no longer set falsey attribute values (like the empty string `''` or `0`). This fixes that issue. * test(Editing): add regression test Co-authored-by: Jakob Vogelsang <[email protected]>
1 parent f6e96b5 commit 0b414e1

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/Editing.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,8 @@ export function Editing<TBase extends LitElementConstructor>(Base: TBase) {
382382
);
383383

384384
Object.entries(action.newAttributes).forEach(([key, value]) => {
385-
if (value) action.element.setAttribute(key, value);
385+
if (value !== null && value !== undefined)
386+
action.element.setAttribute(key, value);
386387
});
387388

388389
return true;

test/unit/Editing.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,19 @@ describe('EditingElement', () => {
341341
expect(element).to.not.have.attribute('desc');
342342
});
343343

344+
it('allows empty string as attribute value', () => {
345+
const newAttributes: Record<string, string | null> = {};
346+
newAttributes['name'] = '';
347+
348+
elm.dispatchEvent(
349+
newActionEvent(createUpdateAction(element, newAttributes))
350+
);
351+
352+
expect(element.parentElement).to.equal(parent);
353+
expect(element).to.have.attribute('name', '');
354+
expect(element).to.not.have.attribute('desc');
355+
});
356+
344357
it('does not update an element in case of name conflict', () => {
345358
const newAttributes: Record<string, string | null> = {};
346359
newAttributes['name'] = 'Q02';

0 commit comments

Comments
 (0)