chore: simplify attributes

pull/13337/head
Rich Harris 2 years ago
parent c636fc67bc
commit a1494de90d

@ -197,11 +197,12 @@ function build_dynamic_element_attributes(attributes, context, element_id) {
'=',
b.id(id),
b.call(
'$.set_dynamic_element_attributes',
'$.set_attributes',
element_id,
b.id(id),
b.object(values),
context.state.analysis.css.hash !== '' && b.literal(context.state.analysis.css.hash)
context.state.analysis.css.hash !== '' && b.literal(context.state.analysis.css.hash),
b.binary('!==', b.member(element_id, 'namespaceURI'), b.id('$.NAMESPACE_SVG'))
)
)
);
@ -218,11 +219,12 @@ function build_dynamic_element_attributes(attributes, context, element_id) {
context.state.init.push(
b.stmt(
b.call(
'$.set_dynamic_element_attributes',
'$.set_attributes',
element_id,
b.literal(null),
b.object(values),
context.state.analysis.css.hash !== '' && b.literal(context.state.analysis.css.hash)
context.state.analysis.css.hash !== '' && b.literal(context.state.analysis.css.hash),
b.binary('!==', b.member(element_id, 'namespaceURI'), b.id('$.NAMESPACE_SVG'))
)
)
);

@ -135,18 +135,8 @@ export function set_xlink_attribute(dom, attribute, value) {
* @param {any} value
*/
export function set_custom_element_data(node, prop, value) {
if (prop in node) {
// Reading the prop could cause an error, we don't want this to fail everything else
try {
var curr_val = node[prop];
} catch {
set_attribute(node, prop, value);
return;
}
var next_val = typeof curr_val === 'boolean' && value === '' ? true : value;
if (typeof curr_val !== 'object' || curr_val !== next_val) {
node[prop] = next_val;
}
if (get_setters(node).includes(prop)) {
node[prop] = value;
} else {
set_attribute(node, prop, value);
}
@ -316,40 +306,6 @@ export function set_attributes(
return current;
}
/**
* @param {Element} node
* @param {Record<string, any> | undefined} prev
* @param {Record<string, any>} next The new attributes - this function mutates this object
* @param {string} [css_hash]
*/
export function set_dynamic_element_attributes(node, prev, next, css_hash) {
if (node.tagName.includes('-')) {
for (var key in prev) {
if (!(key in next)) {
next[key] = null;
}
}
if (css_hash !== undefined) {
next.class = next.class ? next.class + ' ' + css_hash : css_hash;
}
for (key in next) {
set_custom_element_data(node, key, next[key]);
}
return next;
}
return set_attributes(
/** @type {Element & ElementCSSInlineStyle} */ (node),
prev,
next,
css_hash,
node.namespaceURI !== NAMESPACE_SVG
);
}
/**
* List of attributes that should always be set through the attr method,
* because updating them through the property setter doesn't work reliably.

@ -1,4 +1,4 @@
export { FILENAME, HMR } from '../../constants.js';
export { FILENAME, HMR, NAMESPACE_SVG } from '../../constants.js';
export { cleanup_styles } from './dev/css.js';
export { add_locations } from './dev/elements.js';
export { hmr } from './dev/hmr.js';
@ -30,7 +30,6 @@ export {
set_attribute,
set_attributes,
set_custom_element_data,
set_dynamic_element_attributes,
set_xlink_attribute,
handle_lazy_img,
set_value,

Loading…
Cancel
Save