UNPKG

marko

Version:

UI Components + streaming, async, high performance, HTML templating for Node.js and the browser.

79 lines (59 loc) 1.73 kB
"use strict"; var escapeQuoteHelpers = require("./escape-quotes"); var escapeDoubleQuotes = escapeQuoteHelpers.n_; var escapeSingleQuotes = escapeQuoteHelpers.bv_; module.exports = maybeEmptyAttr; maybeEmptyAttr.bt_ = notEmptyAttr; maybeEmptyAttr.bu_ = isEmpty; function maybeEmptyAttr(name, value) { if (isEmpty(value)) { return ""; } return notEmptyAttr(name, value); } function notEmptyAttr(name, value) { switch (typeof value) { case "string": return " " + name + guessQuotes(value); case "boolean": return " " + name; case "number": return " " + name + "=" + value; case "object": switch (value.toString) { case Object.prototype.toString: case Array.prototype.toString: // eslint-disable-next-line no-constant-condition return " " + name + singleQuote(JSON.stringify(value), 2); case RegExp.prototype.toString: return " " + name + guessQuotes(value.source); } } return " " + name + guessQuotes(value + ""); } function isEmpty(value) { return value == null || value === false; } function doubleQuote(value, startPos) { return '="' + escapeDoubleQuotes(value, startPos) + '"'; } function singleQuote(value, startPos) { return "='" + escapeSingleQuotes(value, startPos) + "'"; } function guessQuotes(value) { for (var i = 0, len = value.length; i < len; i++) { switch (value[i]) { case '"': return singleQuote(value, i + 1); case "'": case ">": case " ": case "\t": case "\n": case "\r": case "\f": return doubleQuote(value, i + 1); } } return value && "=" + (value[len - 1] === "/" ? value + " " : value); }