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

Handle false properly in class:list #3922

Merged
merged 3 commits into from
Jul 14, 2022
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
5 changes: 5 additions & 0 deletions .changeset/dirty-doors-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Properly handle `false` in `class:list`
6 changes: 5 additions & 1 deletion packages/astro/src/runtime/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,11 @@ Make sure to use the static attribute syntax (\`${key}={value}\`) instead of the

// support "class" from an expression passed into an element (#782)
if (key === 'class:list') {
return markHTMLString(` ${key.slice(0, -5)}="${toAttributeString(serializeListValue(value))}"`);
const listValue = toAttributeString(serializeListValue(value));
natemoo-re marked this conversation as resolved.
Show resolved Hide resolved
if (listValue === '') {
return '';
}
return markHTMLString(` ${key.slice(0, -5)}="${listValue}"`);
}

// Boolean values only need the key
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/runtime/server/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function serializeListValue(value: any) {
// otherwise, push any other values as a string
else {
// get the item as a string
item = item == null ? '' : String(item).trim();
item = item === false || item == null ? '' : String(item).trim();

// add the item if it is filled
if (item) {
Expand Down
4 changes: 4 additions & 0 deletions packages/astro/test/astro-class-list.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ describe('Class List', async () => {
expect($('[class="test truthy"]')).to.have.lengthOf(1);
expect($('[class="test set"]')).to.have.lengthOf(1);
expect($('[class="hello goodbye world friend"]')).to.have.lengthOf(1);
expect($('[class="foo baz"]')).to.have.lengthOf(1);
expect($('span:not([class])')).to.have.lengthOf(1);

expect($('.false, .noshow1, .noshow2, .noshow3, .noshow4')).to.have.lengthOf(0);
});
Expand All @@ -34,5 +36,7 @@ describe('Class List', async () => {
expect($('[class="test truthy"]')).to.have.lengthOf(1);
expect($('[class="test set"]')).to.have.lengthOf(1);
expect($('[class="hello goodbye world friend"]')).to.have.lengthOf(1);
expect($('[class="foo baz"]')).to.have.lengthOf(1);
expect($('span:not([class])')).to.have.lengthOf(1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ import Component from '../components/Span.astro'
<Component class:list={new Set(['test', 'set'])} />

<Component class:list={[ 'hello goodbye', { hello: true, world: true }, new Set([ 'hello', 'friend' ]) ]} />

<Component class:list={['foo', false && 'bar', true && 'baz']} />

<Component class:list={[false && 'empty']} />
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@
<span class:list={new Set(['test', 'set'])} />

<span class:list={[ 'hello goodbye', { hello: true, world: true }, new Set([ 'hello', 'friend' ]) ]} />

<span class:list={['foo', false && 'bar', true && 'baz']} />

<span class:list={[false && 'empty']} />