diff --git a/compiler/ast/HtmlAttribute.js b/compiler/ast/HtmlAttribute.js index 67a8593f3..881e59fbe 100644 --- a/compiler/ast/HtmlAttribute.js +++ b/compiler/ast/HtmlAttribute.js @@ -3,6 +3,7 @@ var Node = require('./Node'); var Literal = require('./Literal'); var ok = require('assert').ok; var escapeXmlAttr = require('raptor-util/escapeXml').attr; +var attr = require('raptor-util/attr'); var compiler = require('../'); function isStringLiteral(node) { @@ -158,15 +159,7 @@ class HtmlAttribute extends Node { } if (this.isLiteralValue()) { - var literalValue = value.value; - if (typeof literalValue === 'boolean' || literalValue === '') { - if (literalValue === true || literalValue === '') { - codegen.addWriteLiteral(' ' + name); - } - } else if (literalValue != null) { - codegen.addWriteLiteral(' ' + name + '="' + escapeXmlAttr(literalValue) + '"'); - } - + codegen.addWriteLiteral(attr(name, value.value)); } else if (value != null) { codegen.isInAttribute = true; generateCodeForExpressionAttr(name, value, escape, codegen); diff --git a/docs/language-guide.md b/docs/language-guide.md index 74520f15c..652352514 100644 --- a/docs/language-guide.md +++ b/docs/language-guide.md @@ -365,7 +365,7 @@ With a value of `false` for `active`, the output would be the following:
Hello
``` -_NOTE: If an attribute value expression evaluates to `null`, `false` or an empty string then the attribute is not included in the output._ +_NOTE: If an attribute value expression evaluates to `null` or `false` then the attribute is not included in the output._ A ternary condition can also be used: @@ -381,13 +381,12 @@ With a value of `false` for `active`, the output would be the following: ## Conditional Attributes -Marko supports conditional attributes when the value of an attribute is an expression. Marko also supports [HTML `boolean` attributes](https://html.spec.whatwg.org/#boolean-attributes) (e.g., ``) If an attribute value resolves to `null`, `undefined`, `false` or an empty string then the attribute will not be rendered. If an attribute value resolves to `true` then only the attribute name will rendered. +Marko supports conditional attributes when the value of an attribute is an expression. Marko also supports [HTML `boolean` attributes](https://html.spec.whatwg.org/#boolean-attributes) (e.g., ``) If an attribute value resolves to `null`, `undefined`, or `false` then the attribute will not be rendered. If an attribute value resolves to `true` then only the attribute name will rendered. For example, given the following data: ```javascript { - title: '', active: true, checked: false, disabled: true @@ -397,8 +396,6 @@ For example, given the following data: And the following template: ```xml -data.title/ -
@@ -407,8 +404,6 @@ And the following template: The output HTML will be the following: ```html - -
diff --git a/test/autotests/render/attr-empty/expected.html b/test/autotests/render/attr-empty/expected.html index b9b0e7fd6..1542379c2 100644 --- a/test/autotests/render/attr-empty/expected.html +++ b/test/autotests/render/attr-empty/expected.html @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/test/autotests/render/attr-empty/template.marko b/test/autotests/render/attr-empty/template.marko index df188f83c..be2117767 100644 --- a/test/autotests/render/attr-empty/template.marko +++ b/test/autotests/render/attr-empty/template.marko @@ -1 +1,3 @@ - \ No newline at end of file + + +empty \ No newline at end of file