Skip to content

Commit 3ff208f

Browse files
authored
Merge pull request #925 from sveltejs/gh-915
don't use innerHTML for options inside optgroups
2 parents 0774284 + d28942d commit 3ff208f

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

src/generators/dom/preprocess.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ const preprocessors = {
309309
stripWhitespace: boolean,
310310
nextSibling: Node
311311
) => {
312-
if (node.name === 'slot') {
312+
if (node.name === 'slot' || node.name === 'option') {
313313
cannotUseInnerHTML(node);
314314
}
315315

@@ -369,8 +369,6 @@ const preprocessors = {
369369
// so that if `foo.qux` changes, we know that we need to
370370
// mark `bar` and `baz` as dirty too
371371
if (node.name === 'select') {
372-
cannotUseInnerHTML(node);
373-
374372
const value = node.attributes.find(
375373
(attribute: Node) => attribute.name === 'value'
376374
);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
export default {
2+
skip: true, // JSDOM
3+
4+
html: `
5+
<h1>Hello Harry!</h1>
6+
7+
<select>
8+
<option value="Harry">Harry</option>
9+
<optgroup label="Group">
10+
<option value="World">World</option>
11+
</optgroup>
12+
</select>
13+
`,
14+
15+
test(assert, component, target, window) {
16+
const select = target.querySelector('select');
17+
const options = [...target.querySelectorAll('option')];
18+
19+
assert.deepEqual(options, select.options);
20+
assert.equal(component.get('name'), 'Harry');
21+
22+
const change = new window.Event('change');
23+
24+
options[1].selected = true;
25+
select.dispatchEvent(change);
26+
27+
assert.equal(component.get('name'), 'World');
28+
assert.htmlEqual(target.innerHTML, `
29+
<h1>Hello World!</h1>
30+
31+
<select>
32+
<option value="Harry">Harry</option>
33+
<optgroup label="Group">
34+
<option value="World">World</option>
35+
</optgroup>
36+
</select>
37+
`);
38+
},
39+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<h1>Hello {{name}}!</h1>
2+
3+
<select bind:value="name">
4+
<option value="Harry">Harry</option>
5+
<optgroup label="Group">
6+
<option value="World">World</option>
7+
</optgroup>
8+
</select>

0 commit comments

Comments
 (0)