Skip to content

Commit

Permalink
fix select element can't removeChild optgroup's option
Browse files Browse the repository at this point in the history
  • Loading branch information
evantre committed Sep 11, 2015
1 parent fe9a3db commit 1774e99
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/directives/model/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,12 @@ function initOptions (expression) {
while (i--) {
var option = el.options[i]
if (option !== defaultOption) {
el.removeChild(option)
var parentNode = option.parentNode
if (parentNode === el) {
parentNode.removeChild(option)
} else {
el.removeChild(parentNode)
}
}
}
buildOptions(el, value)
Expand Down
24 changes: 22 additions & 2 deletions test/unit/specs/directives/model_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,8 @@ if (_.inBrowser) {
expect(opts[2].selected).toBe(true)
})

it('select + options + optgroup', function () {
new Vue({
it('select + options + optgroup', function (done) {
var vm = new Vue({
el: el,
data: {
test: 'b',
Expand All @@ -365,6 +365,26 @@ if (_.inBrowser) {
expect(opts[0].selected).toBe(false)
expect(opts[1].selected).toBe(true)
expect(opts[2].selected).toBe(false)
vm.opts = [
{ label: 'X', options: ['x', 'y'] },
{ label: 'Y', options: ['z'] }
]
vm.test = 'y'
_.nextTick(function () {
expect(el.firstChild.innerHTML).toBe(
'<optgroup label="X">' +
'<option value="x">x</option><option value="y">y</option>' +
'</optgroup>' +
'<optgroup label="Y">' +
'<option value="z">z</option>' +
'</optgroup>'
)
var opts = el.firstChild.options
expect(opts[0].selected).toBe(false)
expect(opts[1].selected).toBe(true)
expect(opts[2].selected).toBe(false)
done()
})
})

it('select + options with Object value', function (done) {
Expand Down

0 comments on commit 1774e99

Please sign in to comment.