Skip to content
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/tricky-planets-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: recognize option value on spread attribute
32 changes: 30 additions & 2 deletions packages/svelte/src/compiler/compile/nodes/Element.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { is_html, is_svg, is_void } from '../../../shared/utils/names.js';
import Node from './shared/Node.js';
import { walk } from 'estree-walker';
import Attribute from './Attribute.js';
import Binding from './Binding.js';
import EventHandler from './EventHandler.js';
Expand Down Expand Up @@ -430,7 +431,7 @@ export default class Element extends Node {
}
if (this.name === 'textarea') {
if (info.children.length > 0) {
const value_attribute = info.attributes.find((node) => node.name === 'value');
const value_attribute = get_value_attribute(info.attributes);
if (value_attribute) {
component.error(value_attribute, compiler_errors.textarea_duplicate_value);
return;
Expand All @@ -449,7 +450,7 @@ export default class Element extends Node {
// Special case — treat these the same way:
// <option>{foo}</option>
// <option value={foo}>{foo}</option>
const value_attribute = info.attributes.find((attribute) => attribute.name === 'value');
const value_attribute = get_value_attribute(info.attributes);
if (!value_attribute) {
info.attributes.push({
type: 'Attribute',
Expand Down Expand Up @@ -1420,3 +1421,30 @@ function within_custom_element(parent) {
}
return false;
}

/**
* @param {any[]} attributes
*/
function get_value_attribute(attributes) {
let node_value;
attributes.forEach((node) => {
if (node.type !== 'Spread' && node.name.toLowerCase() === 'value') {
node_value = node;
}
if (node.type === 'Spread') {
walk(/** @type {any} */ (node.expression), {
enter(/** @type {import('estree').Node} */ node) {
if (node_value) {
this.skip();
}
if (node.type === 'Identifier') {
if (/** @type {import('estree').Identifier} */ (node).name.toLowerCase() === 'value') {
node_value = node;
}
}
}
});
}
});
return node_value;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
html: `
<select>
<option value="value" class="option">Label</option>
</select>
`
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<select>
<option {...{ value: 'value', class: 'option' }}>Label</option>
</select>