From 7b15ff93acfa7b572fc8476791ad0ab86dc060f3 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Wed, 30 Nov 2016 16:38:17 -0500 Subject: [PATCH] handle empty attributes in elements and components. fixes #63 --- .../visitors/attributes/addComponentAttributes.js | 7 +++++++ .../visitors/attributes/addElementAttributes.js | 12 ++++++++++++ test/compiler/attribute-empty/_config.js | 3 +++ test/compiler/attribute-empty/main.html | 1 + test/compiler/component-data-empty/Widget.html | 1 + test/compiler/component-data-empty/_config.js | 3 +++ test/compiler/component-data-empty/main.html | 11 +++++++++++ 7 files changed, 38 insertions(+) create mode 100644 test/compiler/attribute-empty/_config.js create mode 100644 test/compiler/attribute-empty/main.html create mode 100644 test/compiler/component-data-empty/Widget.html create mode 100644 test/compiler/component-data-empty/_config.js create mode 100644 test/compiler/component-data-empty/main.html diff --git a/compiler/generate/visitors/attributes/addComponentAttributes.js b/compiler/generate/visitors/attributes/addComponentAttributes.js index 9f002cd6c4..9995352dc8 100644 --- a/compiler/generate/visitors/attributes/addComponentAttributes.js +++ b/compiler/generate/visitors/attributes/addComponentAttributes.js @@ -16,6 +16,13 @@ export default function addComponentAttributes ( generator, node, local ) { }); } + else if ( attribute.value.length === 0 ) { + local.staticAttributes.push({ + name: attribute.name, + value: `''` + }); + } + else if ( attribute.value.length === 1 ) { const value = attribute.value[0]; diff --git a/compiler/generate/visitors/attributes/addElementAttributes.js b/compiler/generate/visitors/attributes/addElementAttributes.js index b7aa5c4056..cb81a9a1f6 100644 --- a/compiler/generate/visitors/attributes/addElementAttributes.js +++ b/compiler/generate/visitors/attributes/addElementAttributes.js @@ -31,6 +31,18 @@ export default function addElementAttributes ( generator, node, local ) { } } + else if ( attribute.value.length === 0 ) { + if ( propertyName ) { + local.init.push( deindent` + ${local.name}.${propertyName} = ''; + ` ); + } else { + local.init.push( deindent` + ${local.name}.setAttribute( '${attribute.name}', '' ); + ` ); + } + } + else if ( attribute.value.length === 1 ) { const value = attribute.value[0]; diff --git a/test/compiler/attribute-empty/_config.js b/test/compiler/attribute-empty/_config.js new file mode 100644 index 0000000000..b9ce14cce1 --- /dev/null +++ b/test/compiler/attribute-empty/_config.js @@ -0,0 +1,3 @@ +export default { + html: `
` +}; diff --git a/test/compiler/attribute-empty/main.html b/test/compiler/attribute-empty/main.html new file mode 100644 index 0000000000..7c7b3d103a --- /dev/null +++ b/test/compiler/attribute-empty/main.html @@ -0,0 +1 @@ +
diff --git a/test/compiler/component-data-empty/Widget.html b/test/compiler/component-data-empty/Widget.html new file mode 100644 index 0000000000..89d0010a35 --- /dev/null +++ b/test/compiler/component-data-empty/Widget.html @@ -0,0 +1 @@ +

foo: '{{foo}}'

diff --git a/test/compiler/component-data-empty/_config.js b/test/compiler/component-data-empty/_config.js new file mode 100644 index 0000000000..eaf16a72af --- /dev/null +++ b/test/compiler/component-data-empty/_config.js @@ -0,0 +1,3 @@ +export default { + html: `

foo: ''

` +}; diff --git a/test/compiler/component-data-empty/main.html b/test/compiler/component-data-empty/main.html new file mode 100644 index 0000000000..8b797c8a60 --- /dev/null +++ b/test/compiler/component-data-empty/main.html @@ -0,0 +1,11 @@ +
+ +
+ +