From 05133fa5bcb2827261d37265d3ba265cdb0a422b Mon Sep 17 00:00:00 2001 From: Solinx Date: Fri, 31 Jul 2015 00:05:31 +0200 Subject: [PATCH 1/2] Handle Boolean attributes Currently when trying to bind the 'checked' state of a checkbox, Platesjs will always check the checkbox, regardless of the 'checked' value being provided. This is because Platesjs requires all bound elements to be part of the template and the html spec specifies that Boolean attributes such as 'checked' merely have to be present on the element to perform their function. This PR fixes the issue by effectively removing Boolean attributes from the element in question if the mapped value evaluates to false. For details: http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.3.4.2 https://github.com/kangax/html-minifier/issues/63#issuecomment-37763316 --- lib/plates.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/plates.js b/lib/plates.js index 26cb322..d8255eb 100644 --- a/lib/plates.js +++ b/lib/plates.js @@ -147,7 +147,17 @@ function matchClosing(input, tagname, html) { // separators. So we need to detect these elements based on the tag name. // selfClosing: /^(area|base|br|col|command|embed|hr|img|input|keygen|link|meta|param|source|track|wbr)$/, - + + // + // Boolean Attributes - attributes that require no value + // + booleanAttr: ['allowfullscreen','async','autofocus','autoplay','checked','compact','controls', + 'declare','default','defaultchecked','defaultmuted','defaultselected','defer','disabled', + 'draggable','enabled','formnovalidate','hidden','indeterminate','inert','ismap','itemscope', + 'loop','multiple','muted','nohref','noresize','noshade','novalidate','nowrap','open', + 'pauseonexit','readonly','required','reversed','scoped','seamless','selected','sortable', + 'spellcheck','translate','truespeed','typemustmatch','visible'], + // // ### function hasClass(str, className) // #### @str {String} the class attribute @@ -327,6 +337,13 @@ function matchClosing(input, tagname, html) { } else { newdata = fetch(data, mapping, value, tagbody, key); } + + // + // remove a Boolean attribute if the value evaluates to false + // + if (Merge.prototype.booleanAttr.indexOf(key.toLowerCase()) != -1) { + return !newdata ? '' : key + '="' + key + '"'; + } return key + '="' + (newdata || '') + '"'; } else if (!mapping.replace && mapping.attribute === key) { From a8bc70237ec8f76f70b779ba711ecb772cbf361b Mon Sep 17 00:00:00 2001 From: Solinx Date: Fri, 31 Jul 2015 00:16:34 +0200 Subject: [PATCH 2/2] Minor efficiency improvement to boolean attr check --- lib/plates.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/plates.js b/lib/plates.js index d8255eb..85b8c92 100644 --- a/lib/plates.js +++ b/lib/plates.js @@ -341,8 +341,8 @@ function matchClosing(input, tagname, html) { // // remove a Boolean attribute if the value evaluates to false // - if (Merge.prototype.booleanAttr.indexOf(key.toLowerCase()) != -1) { - return !newdata ? '' : key + '="' + key + '"'; + if (!newdata && Merge.prototype.booleanAttr.indexOf(key.toLowerCase()) != -1) { + return ''; } return key + '="' + (newdata || '') + '"';