Skip to content

Commit

Permalink
Merge pull request #542 from Zirro/queryselector-checked
Browse files Browse the repository at this point in the history
Use .querySelector(':checked') instead of .selectedOptions, enable several <select> tests
  • Loading branch information
Rich-Harris authored May 1, 2017
2 parents a2ce4a4 + 552c62a commit bd82455
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/generators/dom/visitors/Element/Binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default function visitBinding ( generator, block, state, node, attribute
// <select> special case
if ( node.name === 'select' ) {
if ( !isMultipleSelect ) {
setter = `var selectedOption = ${state.parentNode}.selectedOptions[0] || ${state.parentNode}.options[0];\n${setter}`;
setter = `var selectedOption = ${state.parentNode}.querySelector(':checked') || ${state.parentNode}.options[0];\n${setter}`;
}

const value = block.getUniqueName( 'value' );
Expand Down Expand Up @@ -160,7 +160,7 @@ function getBindingEventName ( node, attribute ) {
function getBindingValue ( generator, block, state, node, attribute, isMultipleSelect, bindingGroup, type ) {
// <select multiple bind:value='selected>
if ( isMultipleSelect ) {
return `[].map.call( ${state.parentNode}.selectedOptions, function ( option ) { return option.__value; })`;
return `[].map.call( ${state.parentNode}.querySelectorAll(':checked'), function ( option ) { return option.__value; })`;
}

// <select bind:value='selected>
Expand Down
2 changes: 0 additions & 2 deletions test/runtime/samples/binding-select-initial-value/_config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
export default {
skip: true, // selectedOptions doesn't work in JSDOM???

html: `
<p>selected: b</p>
Expand Down
6 changes: 3 additions & 3 deletions test/runtime/samples/binding-select-initial-value/main.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<p>selected: {{selected}}</p>

<select bind:value='selected'>
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
<option>a</option>
<option>b</option>
<option>c</option>
</select>

<p>selected: {{selected}}</p>
10 changes: 5 additions & 5 deletions test/runtime/samples/binding-select-multiple/_config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
export default {
skip: true, // selectedOptions doesn't work in JSDOM???
skip: true, // JSDOM

data: {
selected: [ 'two', 'three' ]
},

html: `
<select>
<select multiple>
<option>one</option>
<option>two</option>
<option>three</option>
Expand All @@ -26,7 +26,7 @@ export default {

assert.deepEqual( component.get( 'selected' ), [ 'three' ] );
assert.htmlEqual( target.innerHTML, `
<select>
<select multiple>
<option>one</option>
<option>two</option>
<option>three</option>
Expand All @@ -40,7 +40,7 @@ export default {

assert.deepEqual( component.get( 'selected' ), [ 'one', 'three' ] );
assert.htmlEqual( target.innerHTML, `
<select>
<select multiple>
<option>one</option>
<option>two</option>
<option>three</option>
Expand All @@ -56,7 +56,7 @@ export default {
assert.ok( !options[2].selected );

assert.htmlEqual( target.innerHTML, `
<select>
<select multiple>
<option>one</option>
<option>two</option>
<option>three</option>
Expand Down
6 changes: 4 additions & 2 deletions test/runtime/samples/binding-select/_config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
export default {
skip: true, // selectedOptions doesn't work in JSDOM???

html: `
<p>selected: one</p>
Expand All @@ -13,6 +11,10 @@ export default {
<p>selected: one</p>
`,

data: {
selected: 'one'
},

test ( assert, component, target, window ) {
const select = target.querySelector( 'select' );
const options = [ ...target.querySelectorAll( 'option' ) ];
Expand Down
2 changes: 0 additions & 2 deletions test/runtime/samples/select-change-handler/_config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
export default {
skip: true, // JSDOM

data: {
options: [ { id: 'a' }, { id: 'b' }, { id: 'c' } ],
selected: 'b'
Expand Down
2 changes: 0 additions & 2 deletions test/runtime/samples/select-one-way-bind-object/_config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const items = [ {}, {} ];

export default {
skip: true, // JSDOM quirks

'skip-ssr': true,

data: {
Expand Down

0 comments on commit bd82455

Please sign in to comment.