Skip to content

Commit 63ebcc8

Browse files
committed
in SSR, adjust spread with boolean attributes (sveltejs#2916)
1 parent 3f6b743 commit 63ebcc8

File tree

3 files changed

+36
-48
lines changed

3 files changed

+36
-48
lines changed

src/compiler/compile/render_ssr/handlers/Element.ts

+1-41
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,12 @@
11
import { is_void } from '../../../utils/names';
22
import { get_attribute_value, get_class_attribute_value } from './shared/get_attribute_value';
33
import { get_slot_scope } from './shared/get_slot_scope';
4+
import { boolean_attributes } from './shared/boolean_attributes';
45
import Renderer, { RenderOptions } from '../Renderer';
56
import Element from '../../nodes/Element';
67
import { x } from 'code-red';
78
import Expression from '../../nodes/shared/Expression';
89

9-
// source: https://gist.github.com/ArjanSchouten/0b8574a6ad7f5065a5e7
10-
const boolean_attributes = new Set([
11-
'async',
12-
'autocomplete',
13-
'autofocus',
14-
'autoplay',
15-
'border',
16-
'challenge',
17-
'checked',
18-
'compact',
19-
'contenteditable',
20-
'controls',
21-
'default',
22-
'defer',
23-
'disabled',
24-
'formnovalidate',
25-
'frameborder',
26-
'hidden',
27-
'indeterminate',
28-
'ismap',
29-
'loop',
30-
'multiple',
31-
'muted',
32-
'nohref',
33-
'noresize',
34-
'noshade',
35-
'novalidate',
36-
'nowrap',
37-
'open',
38-
'readonly',
39-
'required',
40-
'reversed',
41-
'scoped',
42-
'scrolling',
43-
'seamless',
44-
'selected',
45-
'sortable',
46-
'spellcheck',
47-
'translate'
48-
]);
49-
5010
export default function(node: Element, renderer: Renderer, options: RenderOptions & {
5111
slot_scopes: Map<any, any>;
5212
}) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// source: https://html.spec.whatwg.org/multipage/indices.html
2+
export const boolean_attributes = new Set([
3+
'allowfullscreen',
4+
'allowpaymentrequest',
5+
'async',
6+
'autofocus',
7+
'autoplay',
8+
'checked',
9+
'controls',
10+
'default',
11+
'defer',
12+
'disabled',
13+
'formnovalidate',
14+
'hidden',
15+
'ismap',
16+
'loop',
17+
'multiple',
18+
'muted',
19+
'nomodule',
20+
'novalidate',
21+
'open',
22+
'playsinline',
23+
'readonly',
24+
'required',
25+
'reversed',
26+
'selected'
27+
]);

src/runtime/internal/ssr.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { set_current_component, current_component } from './lifecycle';
22
import { run_all, blank_object } from './utils';
3+
import { boolean_attributes } from '../../compiler/compile/render_ssr/handlers/shared/boolean_attributes';
34

45
export const invalid_attribute_name_character = /[\s'">/=\u{FDD0}-\u{FDEF}\u{FFFE}\u{FFFF}\u{1FFFE}\u{1FFFF}\u{2FFFE}\u{2FFFF}\u{3FFFE}\u{3FFFF}\u{4FFFE}\u{4FFFF}\u{5FFFE}\u{5FFFF}\u{6FFFE}\u{6FFFF}\u{7FFFE}\u{7FFFF}\u{8FFFE}\u{8FFFF}\u{9FFFE}\u{9FFFF}\u{AFFFE}\u{AFFFF}\u{BFFFE}\u{BFFFF}\u{CFFFE}\u{CFFFF}\u{DFFFE}\u{DFFFF}\u{EFFFE}\u{EFFFF}\u{FFFFE}\u{FFFFF}\u{10FFFE}\u{10FFFF}]/u;
56
// https://html.spec.whatwg.org/multipage/syntax.html#attributes-2
@@ -20,14 +21,14 @@ export function spread(args, classes_to_add) {
2021
if (invalid_attribute_name_character.test(name)) return;
2122

2223
const value = attributes[name];
23-
if (value == null) return;
2424
if (value === true) str += " " + name;
25-
26-
const escaped = String(value)
27-
.replace(/"/g, '&#34;')
28-
.replace(/'/g, '&#39;');
29-
30-
str += " " + name + "=" + JSON.stringify(escaped);
25+
else if (boolean_attributes.has(name.toLowerCase())) {
26+
if (value) str += " " + name;
27+
} else if (value != null) {
28+
str += " " + name + "=" + JSON.stringify(String(value)
29+
.replace(/"/g, '&#34;')
30+
.replace(/'/g, '&#39;'));
31+
}
3132
});
3233

3334
return str;

0 commit comments

Comments
 (0)