normalize style value if its end with semi-colon

pull/7510/head
baseballyama 4 years ago
parent 880168a5e3
commit 7036bc7db5

@ -1,4 +1,5 @@
import { has_prop } from './utils'; import { has_prop } from './utils';
import { normalize_style_value } from './style_manager';
// Track which nodes are claimed during hydration. Unclaimed nodes can then be removed from the DOM // Track which nodes are claimed during hydration. Unclaimed nodes can then be removed from the DOM
// at the end of hydration without touching the remaining nodes. // at the end of hydration without touching the remaining nodes.
@ -530,11 +531,11 @@ export function set_input_type(input, type) {
} }
} }
export function set_style(node, key, value, important) { export function set_style(node, key: string, value: unknown, important: boolean) {
if (value === null) { if (value === null) {
node.style.removeProperty(key); node.style.removeProperty(key);
} else { } else {
node.style.setProperty(key, value, important ? 'important' : ''); node.style.setProperty(key, normalize_style_value(value), important ? 'important' : '');
} }
} }

@ -2,6 +2,7 @@ import { set_current_component, current_component } from './lifecycle';
import { run_all, blank_object } from './utils'; import { run_all, blank_object } from './utils';
import { boolean_attributes } from '../../shared/boolean_attributes'; import { boolean_attributes } from '../../shared/boolean_attributes';
export { is_void } from '../../shared/utils/names'; export { is_void } from '../../shared/utils/names';
import { normalize_style_value } from './style_manager';
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; 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 // https://html.spec.whatwg.org/multipage/syntax.html#attributes-2
@ -186,14 +187,14 @@ export function add_classes(classes) {
return classes ? ` class="${classes}"` : ''; return classes ? ` class="${classes}"` : '';
} }
function style_object_to_string(style_object) { function style_object_to_string(style_object: { [key: string]: unknown }): string {
return Object.keys(style_object) return Object.keys(style_object)
.filter(key => style_object[key]) .filter(key => style_object[key])
.map(key => `${key}: ${style_object[key]};`) .map(key => `${key}: ${normalize_style_value(style_object[key])};`)
.join(' '); .join(' ');
} }
export function add_styles(style_object) { export function add_styles(style_object: { [key: string]: unknown }): string {
const styles = style_object_to_string(style_object); const styles = style_object_to_string(style_object);
return styles ? ` style="${styles}"` : ''; return styles ? ` style="${styles}"` : '';

@ -79,3 +79,7 @@ export function clear_rules() {
managed_styles.clear(); managed_styles.clear();
}); });
} }
export function normalize_style_value(value: unknown): unknown {
return (typeof value === 'string' && value.slice(-1) === ';') ? value.slice(0, -1) : value;
}

Loading…
Cancel
Save