Skip to content

Commit

Permalink
Merge pull request #925 from sveltejs/gh-915
Browse files Browse the repository at this point in the history
don't use innerHTML for options inside optgroups
  • Loading branch information
Rich-Harris authored Nov 18, 2017
2 parents 0774284 + d28942d commit 3ff208f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/generators/dom/preprocess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ const preprocessors = {
stripWhitespace: boolean,
nextSibling: Node
) => {
if (node.name === 'slot') {
if (node.name === 'slot' || node.name === 'option') {
cannotUseInnerHTML(node);
}

Expand Down Expand Up @@ -369,8 +369,6 @@ const preprocessors = {
// so that if `foo.qux` changes, we know that we need to
// mark `bar` and `baz` as dirty too
if (node.name === 'select') {
cannotUseInnerHTML(node);

const value = node.attributes.find(
(attribute: Node) => attribute.name === 'value'
);
Expand Down
39 changes: 39 additions & 0 deletions test/runtime/samples/binding-select-optgroup/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
export default {
skip: true, // JSDOM

html: `
<h1>Hello Harry!</h1>
<select>
<option value="Harry">Harry</option>
<optgroup label="Group">
<option value="World">World</option>
</optgroup>
</select>
`,

test(assert, component, target, window) {
const select = target.querySelector('select');
const options = [...target.querySelectorAll('option')];

assert.deepEqual(options, select.options);
assert.equal(component.get('name'), 'Harry');

const change = new window.Event('change');

options[1].selected = true;
select.dispatchEvent(change);

assert.equal(component.get('name'), 'World');
assert.htmlEqual(target.innerHTML, `
<h1>Hello World!</h1>
<select>
<option value="Harry">Harry</option>
<optgroup label="Group">
<option value="World">World</option>
</optgroup>
</select>
`);
},
};
8 changes: 8 additions & 0 deletions test/runtime/samples/binding-select-optgroup/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<h1>Hello {{name}}!</h1>

<select bind:value="name">
<option value="Harry">Harry</option>
<optgroup label="Group">
<option value="World">World</option>
</optgroup>
</select>

0 comments on commit 3ff208f

Please sign in to comment.