Skip to content

Commit

Permalink
Manage correctly boolean attributes in HTML output. Fixes GrapesJS#943
Browse files Browse the repository at this point in the history
  • Loading branch information
artf committed Mar 10, 2018
1 parent b20dd6f commit 9a71368
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/dom_components/model/Component.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
isUndefined,
isArray,
isEmpty,
isBoolean,
has,
clone,
isString,
Expand Down Expand Up @@ -674,7 +675,11 @@ const Component = Backbone.Model.extend(Styleable).extend(
const value = isString(val) ? val.replace(/"/g, '"') : val;

if (!isUndefined(value)) {
attrs.push(`${attr}="${value}"`);
if (isBoolean(value)) {
value && attrs.push(attr);
} else {
attrs.push(`${attr}="${value}"`);
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions test/specs/dom_components/model/Component.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,19 @@ module.exports = {
);
});

it('Manage correctly boolean attributes', () => {
obj = new Component();
obj.set('attributes', {
'data-test': 'value',
checked: false,
required: true,
avoid: true
});
expect(obj.toHTML()).toEqual(
'<div data-test="value" required avoid></div>'
);
});

it('Component parse empty div', () => {
var el = document.createElement('div');
obj = Component.isComponent(el);
Expand Down

0 comments on commit 9a71368

Please sign in to comment.