Skip to content

Commit b3ebfef

Browse files
committed
also bind static special attrs as props (fix #4530)
1 parent 71cc0a5 commit b3ebfef

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

src/compiler/parser/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,15 @@ function processAttrs (el) {
459459
}
460460
}
461461
addAttr(el, name, JSON.stringify(value))
462+
// #4530 also bind special attributes as props even if they are static
463+
// so that patches between dynamic/static are consistent
464+
if (platformMustUseProp(el.tag, name)) {
465+
if (name === 'value') {
466+
addProp(el, name, JSON.stringify(value))
467+
} else {
468+
addProp(el, name, 'true')
469+
}
470+
}
462471
}
463472
}
464473
}

test/unit/modules/compiler/parser.spec.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ describe('parser', () => {
391391

392392
it('literal attribute', () => {
393393
// basic
394-
const ast1 = parse('<input type="text" name="field1" value="hello world">', baseOptions)
394+
const ast1 = parse('<input type="text" name="field1" value="hello world" checked>', baseOptions)
395395
expect(ast1.attrsList[0].name).toBe('type')
396396
expect(ast1.attrsList[0].value).toBe('text')
397397
expect(ast1.attrsList[1].name).toBe('name')
@@ -407,6 +407,13 @@ describe('parser', () => {
407407
expect(ast1.attrs[1].value).toBe('"field1"')
408408
expect(ast1.attrs[2].name).toBe('value')
409409
expect(ast1.attrs[2].value).toBe('"hello world"')
410+
expect(ast1.attrs[3].name).toBe('checked')
411+
expect(ast1.attrs[3].value).toBe('""')
412+
// also bind speicals as props
413+
expect(ast1.props[0].name).toBe('value')
414+
expect(ast1.props[0].value).toBe('"hello world"')
415+
expect(ast1.props[1].name).toBe('checked')
416+
expect(ast1.props[1].value).toBe('true')
410417
// interpolation warning
411418
parse('<input type="text" name="field1" value="{{msg}}">', baseOptions)
412419
expect('Interpolation inside attributes has been removed').toHaveBeenWarned()

test/unit/modules/vdom/patch/edge-cases.spec.js

+26
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,30 @@ describe('vdom patch: edge cases', () => {
114114
})
115115
.then(done)
116116
})
117+
118+
// #4530
119+
it('should not reset value when patching bewteen dyanmic/static bindings', done => {
120+
const vm = new Vue({
121+
data: { ok: true },
122+
template: `
123+
<div>
124+
<input v-if="ok" value="a">
125+
<input v-else :value="'b'">
126+
<input v-if="ok" type="checkbox" checked>
127+
<input v-else type="checkbox" :checked="false">
128+
</div>
129+
`
130+
}).$mount()
131+
expect(vm.$el.children[0].value).toBe('a')
132+
expect(vm.$el.children[1].checked).toBe(true)
133+
vm.ok = false
134+
waitForUpdate(() => {
135+
expect(vm.$el.children[0].value).toBe('b')
136+
expect(vm.$el.children[1].checked).toBe(false)
137+
vm.ok = true
138+
}).then(() => {
139+
expect(vm.$el.children[0].value).toBe('a')
140+
expect(vm.$el.children[1].checked).toBe(true)
141+
}).then(done)
142+
})
117143
})

0 commit comments

Comments
 (0)