Skip to content

Commit

Permalink
Merge pull request sveltejs#71 from sveltejs/sveltejsgh-63
Browse files Browse the repository at this point in the history
handle empty attributes in elements and components
  • Loading branch information
Rich-Harris authored Nov 30, 2016
2 parents 14e4684 + 9ba18c6 commit 1de8794
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down
12 changes: 12 additions & 0 deletions compiler/generate/visitors/attributes/addElementAttributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down
7 changes: 7 additions & 0 deletions test/compiler/attribute-empty-svg/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
html: `
<svg>
<g class=''></g>
</svg>
`
};
3 changes: 3 additions & 0 deletions test/compiler/attribute-empty-svg/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<svg>
<g class=''></g>
</svg>
3 changes: 3 additions & 0 deletions test/compiler/attribute-empty/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
html: `<div class=""></div>`
};
1 change: 1 addition & 0 deletions test/compiler/attribute-empty/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class=''></div>
1 change: 1 addition & 0 deletions test/compiler/component-data-empty/Widget.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>foo: '{{foo}}'</p>
3 changes: 3 additions & 0 deletions test/compiler/component-data-empty/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
html: `<div><p>foo: ''</p></div>`
};
11 changes: 11 additions & 0 deletions test/compiler/component-data-empty/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<div>
<Widget foo=''/>
</div>

<script>
import Widget from './Widget.html';

export default {
components: { Widget }
};
</script>

0 comments on commit 1de8794

Please sign in to comment.