Skip to content

Commit

Permalink
fix: only escape attribute values for elements, not components (#9456)
Browse files Browse the repository at this point in the history
* only escape attribute values for elements, not components - closes #9454

* changeset

---------

Co-authored-by: Rich Harris <rich.harris@vercel.com>
  • Loading branch information
Rich-Harris and Rich-Harris committed Nov 15, 2023
1 parent 9abfb52 commit 6f6c3a0
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 4 deletions.
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='"'/>

1 comment on commit 6f6c3a0

@vercel
Copy link

@vercel vercel bot commented on 6f6c3a0 Nov 15, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

svelte-5-preview – ./sites/svelte-5-preview

svelte-octane.vercel.app
svelte-5-preview.vercel.app
svelte-5-preview-svelte.vercel.app
svelte-5-preview-git-main-svelte.vercel.app

Please sign in to comment.