Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set strings as attributes, non-strings as properties if property exists #13327

Merged
merged 7 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/perfect-ants-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: set strings as attributes, non-strings as properties if property exists
41 changes: 16 additions & 25 deletions packages/svelte/src/internal/client/dom/elements/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ export function set_checked(element, checked) {
* @param {boolean} [skip_warning]
*/
export function set_attribute(element, attribute, value, skip_warning) {
value = value == null ? null : value + '';

// @ts-expect-error
var attributes = (element.__attributes ??= {});

Expand All @@ -95,7 +93,7 @@ export function set_attribute(element, attribute, value, skip_warning) {
(attribute === 'href' && element.nodeName === 'LINK')
) {
if (!skip_warning) {
check_src_in_dev_hydration(element, attribute, value);
check_src_in_dev_hydration(element, attribute, value ?? '');
}

// If we reset these attributes, they would result in another network request, which we want to avoid.
Expand All @@ -113,8 +111,11 @@ export function set_attribute(element, attribute, value, skip_warning) {
element[LOADING_ATTR_SYMBOL] = value;
}

if (value === null) {
if (value == null) {
element.removeAttribute(attribute);
} else if (attribute in element && typeof value !== 'string') {
// @ts-ignore
element[attribute] = value;
} else {
element.setAttribute(attribute, value);
}
Expand Down Expand Up @@ -287,15 +288,15 @@ export function set_attributes(
name = normalize_attribute(name);
}

if (setters.includes(name)) {
if (setters.includes(name) && typeof value !== 'string') {
// @ts-ignore
element[name] = value;
} else if (typeof value !== 'function') {
if (hydrating && (name === 'src' || name === 'href' || name === 'srcset')) {
if (!skip_warning) check_src_in_dev_hydration(element, name, value);
if (!skip_warning) check_src_in_dev_hydration(element, name, value ?? '');
} else {
// @ts-ignore
element[name] = value;
set_attribute(element, name, value);
}
} else if (typeof value !== 'function') {
set_attribute(element, name, value);
}
}
}
Expand Down Expand Up @@ -350,16 +351,6 @@ export function set_dynamic_element_attributes(node, prev, next, css_hash) {
);
}

/**
* List of attributes that should always be set through the attr method,
* because updating them through the property setter doesn't work reliably.
* In the example of `width`/`height`, the problem is that the setter only
* accepts numeric values, but the attribute can also be set to a string like `50%`.
* In case of draggable trying to set `element.draggable='false'` will actually set
* draggable to `true`. If this list becomes too big, rethink this approach.
*/
var always_set_through_set_attribute = ['width', 'height', 'draggable'];

/** @type {Map<string, string[]>} */
var setters_cache = new Map();

Expand All @@ -375,7 +366,7 @@ function get_setters(element) {
descriptors = get_descriptors(proto);

for (var key in descriptors) {
if (descriptors[key].set && !always_set_through_set_attribute.includes(key)) {
if (descriptors[key].set) {
setters.push(key);
}
}
Expand All @@ -389,12 +380,12 @@ function get_setters(element) {
/**
* @param {any} element
* @param {string} attribute
* @param {string | null} value
* @param {string} value
*/
function check_src_in_dev_hydration(element, attribute, value) {
if (!DEV) return;
if (attribute === 'srcset' && srcset_url_equal(element, value)) return;
if (src_url_equal(element.getAttribute(attribute) ?? '', value ?? '')) return;
if (src_url_equal(element.getAttribute(attribute) ?? '', value)) return;

w.hydration_attribute_changed(
attribute,
Expand All @@ -420,12 +411,12 @@ function split_srcset(srcset) {

/**
* @param {HTMLSourceElement | HTMLImageElement} element
* @param {string | undefined | null} srcset
* @param {string} srcset
* @returns {boolean}
*/
function srcset_url_equal(element, srcset) {
var element_urls = split_srcset(element.srcset);
var urls = split_srcset(srcset ?? '');
var urls = split_srcset(srcset);

return (
urls.length === element_urls.length &&
Expand Down
16 changes: 15 additions & 1 deletion packages/svelte/src/internal/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,19 @@ export function head(payload, fn) {
head_payload.out += BLOCK_CLOSE;
}

/**
* `<div translate={false}>` should be rendered as `<div translate="no">` and _not_

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how on gods green earth did we get here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uSe THe PlaTfOrM

* `<div translate="false">`, which is equivalent to `<div translate="yes">`. There
* may be other odd cases that need to be added to this list in future
* @type {Record<string, Map<any, string>>}
*/
const replacements = {
translate: new Map([
[true, 'yes'],
[false, 'no']
])
};

/**
* @template V
* @param {string} name
Expand All @@ -156,7 +169,8 @@ export function head(payload, fn) {
*/
export function attr(name, value, is_boolean = false) {
if (value == null || (!value && is_boolean) || (value === '' && name === 'class')) return '';
const assignment = is_boolean ? '' : `="${escape_html(value, true)}"`;
const normalized = (name in replacements && replacements[name].get(value)) || value;
const assignment = is_boolean ? '' : `="${escape_html(normalized, true)}"`;
return ` ${name}${assignment}`;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { test } from '../../test';

export default test({
test({ assert, target, variant, hydrate }) {
function check(/** @type {boolean} */ condition) {
const divs = /** @type {NodeListOf<HTMLDivElement>} */ (
target.querySelectorAll(`.translate-${condition} div`)
);

divs.forEach((div, i) => {
assert.equal(div.translate, condition, `${i + 1} of ${divs.length}: ${div.outerHTML}`);
});
}

check(false);
check(true);

if (variant === 'hydrate') {
hydrate();
check(false);
check(true);
}
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<div class="translate-false">
<div translate={false}></div>
<div translate="no"></div>
<div {...{ translate: false }}></div>
<div {...{ translate: 'no' }}></div>
</div>

<div class="translate-true">
<div></div>
<div translate={true}></div>
<div translate="yes"></div>
<div {...{ translate: true }}></div>
<div {...{ translate: 'yes' }}></div>

<div translate="false"></div>
<div translate="banana"></div>
<div {...{ translate: 'false' }}></div>
<div {...{ translate: 'banana' }}></div>
</div>