Skip to content

Commit c35eb67

Browse files
authored
Merge pull request #1123 from sveltejs/gh-1109
fix handling of boolean attributes in SSR (#1109)
2 parents a4f6fed + 57b737b commit c35eb67

File tree

5 files changed

+30
-7
lines changed

5 files changed

+30
-7
lines changed

src/generators/server-side-rendering/visitors/Element.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import { Node } from '../../../interfaces';
99
import stringifyAttributeValue from './shared/stringifyAttributeValue';
1010
import { escape } from '../../../utils/stringify';
1111

12+
// source: https://gist.github.com/ArjanSchouten/0b8574a6ad7f5065a5e7
13+
const booleanAttributes = new Set('async autocomplete autofocus autoplay border challenge checked compact contenteditable controls default defer disabled formnovalidate frameborder hidden indeterminate ismap loop multiple muted nohref noresize noshade novalidate nowrap open readonly required reversed scoped scrolling seamless selected sortable spellcheck translate'.split(' '));
14+
1215
export default function visitElement(
1316
generator: SsrGenerator,
1417
block: Block,
@@ -36,14 +39,18 @@ export default function visitElement(
3639

3740
if (attribute.name === 'value' && node.name === 'textarea') {
3841
textareaContents = stringifyAttributeValue(block, attribute.value);
42+
} else if (attribute.value === true) {
43+
openingTag += ` ${attribute.name}`;
44+
} else if (
45+
booleanAttributes.has(attribute.name) &&
46+
attribute.value.length === 1 &&
47+
attribute.value[0].type !== 'Text'
48+
) {
49+
// a boolean attribute with one non-Text chunk
50+
block.contextualise(attribute.value[0].expression);
51+
openingTag += '${' + attribute.value[0].metadata.snippet + ' ? " ' + attribute.name + '" : "" }';
3952
} else {
40-
let str = ` ${attribute.name}`;
41-
42-
if (attribute.value !== true) {
43-
str += `="${stringifyAttributeValue(block, attribute.value)}"`;
44-
}
45-
46-
openingTag += str;
53+
openingTag += ` ${attribute.name}="${stringifyAttributeValue(block, attribute.value)}"`;
4754
}
4855
});
4956

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default {
2+
html: `<textarea></textarea>`,
3+
test (assert, component, target) {
4+
const textarea = target.querySelector('textarea');
5+
assert.ok(textarea.readOnly === false);
6+
},
7+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<textarea readonly="{{false}}"></textarea>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default {
2+
html: `<textarea readonly></textarea>`,
3+
test (assert, component, target) {
4+
const textarea = target.querySelector('textarea');
5+
assert.ok(textarea.readOnly);
6+
},
7+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<textarea readonly="{{true}}"></textarea>

0 commit comments

Comments
 (0)