Skip to content

Commit 79c77a0

Browse files
committed
Don't change selection state when mutating select elements
1 parent 8dc7b0c commit 79c77a0

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ module.exports.update = function (fromNode, toNode, opts) {
2727
}
2828
}
2929
// copy values for form elements
30-
if ((f.nodeName === 'INPUT' && f.type !== 'file') || f.nodeName === 'SELECT') {
30+
if (f.nodeName === 'INPUT' && f.type !== 'file') {
31+
// Keep value if not set on mutating element
3132
if (t.getAttribute('value') === null) t.value = f.value
3233
} else if (f.nodeName === 'TEXTAREA') {
3334
if (t.getAttribute('value') === null) f.value = t.value

test.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,29 @@ test('custom event listeners and properties are ignored', function (t) {
3737
el.click()
3838
})
3939

40-
test('input values get copied', function (t) {
40+
test('input value gets copied from mutating element', function (t) {
4141
t.plan(1)
4242
var el = yo`<input type="text" />`
43-
el.value = 'hi'
43+
var newEl = yo`<input type="text" />`
44+
newEl.setAttribute('value', 'hi')
45+
yo.update(el, newEl)
46+
t.equal(el.value, 'hi')
47+
})
48+
49+
test('input value can be cleared from mutating element', function (t) {
50+
t.plan(1)
51+
var el = yo`<input type="text" />`
52+
el.setAttribute('value', 'hi')
53+
var newEl = yo`<input type="text" />`
54+
newEl.setAttribute('value', '')
55+
yo.update(el, newEl)
56+
t.equal(el.value, '')
57+
})
58+
59+
test('input value is kept if mutating element doesn\'t have one', function (t) {
60+
t.plan(1)
61+
var el = yo`<input type="text" />`
62+
el.setAttribute('value', 'hi')
4463
var newEl = yo`<input type="text" />`
4564
yo.update(el, newEl)
4665
t.equal(el.value, 'hi')
@@ -55,3 +74,12 @@ test('textarea values get copied', function (t) {
5574
yo.update(el, textarea('bar'))
5675
t.equal(el.value, 'bar')
5776
})
77+
78+
test('select element selection state gets copied', function (t) {
79+
t.plan(1)
80+
var el = yo`<select><option>0</option><option>1</option></select>`
81+
var newEl = yo`<select><option>0</option><option selected>1</option></select>`
82+
83+
yo.update(el, newEl)
84+
t.equal(el.selectedIndex, 1)
85+
})

0 commit comments

Comments
 (0)