Skip to content

Commit

Permalink
fix(Editing): set false attribute values on update (#899)
Browse files Browse the repository at this point in the history
* 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 <jakob-vogelsang@posteo.de>
  • Loading branch information
ca-d and JakobVogelsang authored Oct 26, 2022
1 parent f6e96b5 commit 0b414e1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Editing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,8 @@ export function Editing<TBase extends LitElementConstructor>(Base: TBase) {
);

Object.entries(action.newAttributes).forEach(([key, value]) => {
if (value) action.element.setAttribute(key, value);
if (value !== null && value !== undefined)
action.element.setAttribute(key, value);
});

return true;
Expand Down
13 changes: 13 additions & 0 deletions test/unit/Editing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,19 @@ describe('EditingElement', () => {
expect(element).to.not.have.attribute('desc');
});

it('allows empty string as attribute value', () => {
const newAttributes: Record<string, string | null> = {};
newAttributes['name'] = '';

elm.dispatchEvent(
newActionEvent(createUpdateAction(element, newAttributes))
);

expect(element.parentElement).to.equal(parent);
expect(element).to.have.attribute('name', '');
expect(element).to.not.have.attribute('desc');
});

it('does not update an element in case of name conflict', () => {
const newAttributes: Record<string, string | null> = {};
newAttributes['name'] = 'Q02';
Expand Down

0 comments on commit 0b414e1

Please sign in to comment.