Skip to content
This repository has been archived by the owner on Jul 6, 2020. It is now read-only.

Commit

Permalink
Adds boolean attribute support.
Browse files Browse the repository at this point in the history
Also includes the following major change:
	- Stops unconditionally creating a `className` attribute on DOM nodes.
  • Loading branch information
chrisdotcode committed Mar 4, 2016
1 parent caeafb7 commit 5fb84fa
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 29 deletions.
39 changes: 19 additions & 20 deletions dist/lmth.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 33 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,22 @@ function htmlEscape(content) {
/* Renders a singleton attribute key, or a key-value attribute pair,
* HTML-escaping the value. */
function renderAttribute(key, value) {
if (!value) {
return key;
var isBoolAttr = isBooleanAttribute(key);

if (isBoolAttr && value === true) {
return key + '="' + key + '"';
} else if (isBoolAttr && value === false) {
return '';
} else {
return key + '="' + htmlEscape(value) + '"';
}
}

// https://github.com/kangax/html-minifier/issues/63#issuecomment-37763316
function isBooleanAttribute(attribute) {
return (/^(?: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)$/).test(attribute);
}

function renderStyle(style) {
return Object.keys(style).reduce(function styler(styleString, property) {
return styleString + property + ':' + style[property] + ';';
Expand Down Expand Up @@ -295,9 +304,10 @@ Node.prototype.render = function render() {
// It is still possible for a void node to be given children:
// in such a case, their children are iterated upon anyway.
// Browsers allow this behavior, so it is allowed here.
(!node.isVoid || !(node.children.length === 0) ? childContents +
'</' + node.name + '>' :
'');
(!node.isVoid || !(node.children.length === 0) ?
childContents +
'</' + node.name + '>' :
'');
};

/* Renders a list of nodes into an HTML string. */
Expand All @@ -320,14 +330,29 @@ Node.prototype.toDOM = function toDOM(document_) {
var root = document_.createElement(node.name);

Object.keys(node.attributes).forEach(function setAttr(key) {
root.setAttribute(key, node.attributes[key]);
var value = node.attributes[key];

var isBoolAttr = isBooleanAttribute(key);

// If a known-boolean attribute from the HTML5 spec is
// encountered, it is displayed or not displayed based on
// whether it is `true` or `false`. All other values (including
// other falsy ones) are passed-through (and converted into
// strings):
if (isBoolAttr && value === true) {
root.setAttribute(key, key);
} else if (isBoolAttr && value === false) {
// NOP
} else {
root.setAttribute(key, value);
}
});

if (node.id) {
root.id = node.id;
}

if (Array.isArray(node.class)) {
if (node.class.length > 0) {
root.className = node.class.join(' ');
}

Expand Down Expand Up @@ -368,7 +393,7 @@ Node.appendListToDOM = function appendListToDOM(parent, nodeList, document_) {

return Node;

}));
// And without faith it is impossible to please God, because anyone who comes
// to Him must believe that He exists and that He rewards those who earnestly
// seek Him. - Hebrews 11:6
}));
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lmth",
"version": "3.0.0",
"version": "4.0.0",
"description": "A \"type-safe\" HTML DSL for JavaScript environments.",
"main": "lib/index.js",
"scripts": {
Expand Down

0 comments on commit 5fb84fa

Please sign in to comment.