omit falsy boolean attributes

pull/3330/head
Richard Harris 6 years ago
parent c53563b207
commit c82a9ea7f2

@ -140,7 +140,7 @@ export default function(node: Element, renderer: Renderer, options: RenderOption
} else if (attribute.chunks.length === 1 && attribute.chunks[0].type !== 'Text') { } else if (attribute.chunks.length === 1 && attribute.chunks[0].type !== 'Text') {
const { name } = attribute; const { name } = attribute;
const snippet = snip(attribute.chunks[0]); const snippet = snip(attribute.chunks[0]);
opening_tag += '${@add_attribute("' + name + '", ' + snippet + ')}'; opening_tag += '${@add_attribute("' + name + '", ' + snippet + ', ' + (boolean_attributes.has(name) ? 1 : 0) + ')}';
} else { } else {
opening_tag += ` ${attribute.name}="${stringify_attribute(attribute, true)}"`; opening_tag += ` ${attribute.name}="${stringify_attribute(attribute, true)}"`;
} }
@ -164,7 +164,7 @@ export default function(node: Element, renderer: Renderer, options: RenderOption
node_contents = '${(' + snippet + ') || ""}'; node_contents = '${(' + snippet + ') || ""}';
} else { } else {
const snippet = snip(expression); const snippet = snip(expression);
opening_tag += '${@add_attribute("' + name + '", ' + snippet + ')}'; opening_tag += '${@add_attribute("' + name + '", ' + snippet + ', 1)}';
} }
}); });

@ -133,9 +133,9 @@ export function get_store_value<T>(store: Readable<T>): T | undefined {
return value; return value;
} }
export function add_attribute(name, value) { export function add_attribute(name, value, boolean) {
if (value == null) return ''; if (value == null || (boolean && !value)) return '';
return ` ${name}${value === true ? '' : `=${typeof value === 'string' ? JSON.stringify(value) : `"${value}"`}`}`; return ` ${name}${value === true ? '' : `=${typeof value === 'string' ? JSON.stringify(escape(value)) : `"${value}"`}`}`;
} }
export function add_classes(classes) { export function add_classes(classes) {

Loading…
Cancel
Save