Skip to content

fix: only escape attribute values for elements, not components #9456

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

Merged
merged 2 commits into from
Nov 15, 2023
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/sour-rules-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: only escape attribute values for elements, not components
Original file line number Diff line number Diff line change
Expand Up @@ -611,9 +611,15 @@ const javascript_visitors_runes = {
* @param {true | Array<import('#compiler').Text | import('#compiler').ExpressionTag>} attribute_value
* @param {import('./types').ComponentContext} context
* @param {boolean} trim_whitespace
* @param {boolean} is_component
* @returns {import('estree').Expression}
*/
function serialize_attribute_value(attribute_value, context, trim_whitespace = false) {
function serialize_attribute_value(
attribute_value,
context,
trim_whitespace = false,
is_component = false
) {
if (attribute_value === true) {
return b.true;
}
Expand All @@ -629,7 +635,8 @@ function serialize_attribute_value(attribute_value, context, trim_whitespace = f
if (trim_whitespace) {
data = data.replace(regex_whitespaces_strict, ' ').trim();
}
return b.literal(escape_html(data, true));

return b.literal(is_component ? data : escape_html(data, true));
} else {
return /** @type {import('estree').Expression} */ (context.visit(value.expression));
}
Expand Down Expand Up @@ -777,12 +784,12 @@ function serialize_inline_component(node, component_name, context) {
} else if (attribute.type === 'Attribute') {
if (attribute.name === 'slot') continue;
if (attribute.name.startsWith('--')) {
const value = serialize_attribute_value(attribute.value, context);
const value = serialize_attribute_value(attribute.value, context, false, true);
custom_css_props.push(b.init(attribute.name, value));
continue;
}

const value = serialize_attribute_value(attribute.value, context);
const value = serialize_attribute_value(attribute.value, context, false, true);
push_prop(b.prop('init', b.key(attribute.name), value));
} else if (attribute.type === 'BindDirective') {
// TODO this needs to turn the whole thing into a while loop because the binding could be mutated eagerly in the child
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
const { prop } = $props();
</script>

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

export default test({
html: `&quot;`
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
import Child from './Child.svelte';
</script>

<Child prop='"'/>