From 9a71368f43d690405d3c60682c7dfee83fd87a4e Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Sat, 10 Mar 2018 14:55:54 +0100 Subject: [PATCH] Manage correctly boolean attributes in HTML output. Fixes #943 --- src/dom_components/model/Component.js | 7 ++++++- test/specs/dom_components/model/Component.js | 13 +++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/dom_components/model/Component.js b/src/dom_components/model/Component.js index 4fe13108d..3c6fc6837 100644 --- a/src/dom_components/model/Component.js +++ b/src/dom_components/model/Component.js @@ -2,6 +2,7 @@ import { isUndefined, isArray, isEmpty, + isBoolean, has, clone, isString, @@ -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}"`); + } } } diff --git a/test/specs/dom_components/model/Component.js b/test/specs/dom_components/model/Component.js index b04f2eede..ae5da0f8e 100644 --- a/test/specs/dom_components/model/Component.js +++ b/test/specs/dom_components/model/Component.js @@ -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( + '
' + ); + }); + it('Component parse empty div', () => { var el = document.createElement('div'); obj = Component.isComponent(el);