Skip to content

Commit

Permalink
in SSR, adjust spread with boolean attributes (sveltejs#2916)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conduitry committed Oct 26, 2019
1 parent 3f6b743 commit 63ebcc8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 48 deletions.
42 changes: 1 addition & 41 deletions src/compiler/compile/render_ssr/handlers/Element.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,12 @@
import { is_void } from '../../../utils/names';
import { get_attribute_value, get_class_attribute_value } from './shared/get_attribute_value';
import { get_slot_scope } from './shared/get_slot_scope';
import { boolean_attributes } from './shared/boolean_attributes';
import Renderer, { RenderOptions } from '../Renderer';
import Element from '../../nodes/Element';
import { x } from 'code-red';
import Expression from '../../nodes/shared/Expression';

// source: https://gist.github.com/ArjanSchouten/0b8574a6ad7f5065a5e7
const boolean_attributes = 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'
]);

export default function(node: Element, renderer: Renderer, options: RenderOptions & {
slot_scopes: Map<any, any>;
}) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// source: https://html.spec.whatwg.org/multipage/indices.html
export const boolean_attributes = new Set([
'allowfullscreen',
'allowpaymentrequest',
'async',
'autofocus',
'autoplay',
'checked',
'controls',
'default',
'defer',
'disabled',
'formnovalidate',
'hidden',
'ismap',
'loop',
'multiple',
'muted',
'nomodule',
'novalidate',
'open',
'playsinline',
'readonly',
'required',
'reversed',
'selected'
]);
15 changes: 8 additions & 7 deletions src/runtime/internal/ssr.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { set_current_component, current_component } from './lifecycle';
import { run_all, blank_object } from './utils';
import { boolean_attributes } from '../../compiler/compile/render_ssr/handlers/shared/boolean_attributes';

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;
// https://html.spec.whatwg.org/multipage/syntax.html#attributes-2
Expand All @@ -20,14 +21,14 @@ export function spread(args, classes_to_add) {
if (invalid_attribute_name_character.test(name)) return;

const value = attributes[name];
if (value == null) return;
if (value === true) str += " " + name;

const escaped = String(value)
.replace(/"/g, '&#34;')
.replace(/'/g, '&#39;');

str += " " + name + "=" + JSON.stringify(escaped);
else if (boolean_attributes.has(name.toLowerCase())) {
if (value) str += " " + name;
} else if (value != null) {
str += " " + name + "=" + JSON.stringify(String(value)
.replace(/"/g, '&#34;')
.replace(/'/g, '&#39;'));
}
});

return str;
Expand Down

0 comments on commit 63ebcc8

Please sign in to comment.