Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly set boolean attributes on custom elements #6073

Merged
merged 1 commit into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/runtime/internal/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export function set_svg_attributes(node: Element & ElementCSSInlineStyle, attrib

export function set_custom_element_data(node, prop, value) {
if (prop in node) {
node[prop] = value;
node[prop] = typeof node[prop] === 'boolean' && value === '' ? true : value;
} else {
attr(node, prop, value);
}
Expand Down
3 changes: 2 additions & 1 deletion test/custom-elements/samples/props/main.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import './my-widget.svelte';

export let items = ['a', 'b', 'c'];
export let flagged = false;
</script>

<my-widget class="foo" {items}/>
<my-widget class="foo" {items} flag1={flagged} flag2 />
4 changes: 4 additions & 0 deletions test/custom-elements/samples/props/my-widget.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

<script>
export let items = [];
export let flag1 = false;
export let flag2 = false;
</script>

<p>{items.length} items</p>
<p>{items.join(', ')}</p>
<p>{flag1 ? 'flagged (dynamic attribute)' : 'not flagged'}</p>
<p>{flag2 ? 'flagged (static attribute)' : 'not flagged'}</p>
6 changes: 5 additions & 1 deletion test/custom-elements/samples/props/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ export default function (target) {
const el = target.querySelector('custom-element');
const widget = el.shadowRoot.querySelector('my-widget');

const [p1, p2] = widget.shadowRoot.querySelectorAll('p');
const [p1, p2, p3, p4] = widget.shadowRoot.querySelectorAll('p');

assert.equal(p1.textContent, '3 items');
assert.equal(p2.textContent, 'a, b, c');
assert.equal(p3.textContent, 'not flagged');
assert.equal(p4.textContent, 'flagged (static attribute)');

el.items = ['d', 'e', 'f', 'g', 'h'];
el.flagged = true;

assert.equal(p1.textContent, '5 items');
assert.equal(p2.textContent, 'd, e, f, g, h');
assert.equal(p3.textContent, 'flagged (dynamic attribute)');
}