Skip to content

Commit abd09d1

Browse files
committed
Fix #14 - Explicitly find the selected index
1 parent fb07c76 commit abd09d1

File tree

5 files changed

+57
-15
lines changed

5 files changed

+57
-15
lines changed

cjs/utils.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
'use strict';
22
const Map = (require('@ungap/essential-map'));
33

4+
const {indexOf: iOF} = [];
45
const append = (get, parent, children, start, end, before) => {
56
const isSelect = 'selectedIndex' in parent;
6-
let selectedIndex = -1;
7+
let noSelection = isSelect;
78
while (start < end) {
89
const child = get(children[start], 1);
9-
if (isSelect && selectedIndex < 0 && child.selected)
10-
selectedIndex = start;
1110
parent.insertBefore(child, before);
11+
if (isSelect && noSelection && child.selected) {
12+
noSelection = !noSelection;
13+
let {selectedIndex} = parent;
14+
parent.selectedIndex = selectedIndex < 0 ?
15+
start :
16+
iOF.call(parent.querySelectorAll('option'), child);
17+
}
1218
start++;
1319
}
14-
if (isSelect && -1 < selectedIndex)
15-
parent.selectedIndex = selectedIndex;
1620
};
1721
exports.append = append;
1822

esm/utils.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
import Map from '@ungap/essential-map';
22

3+
const {indexOf: iOF} = [];
34
export const append = (get, parent, children, start, end, before) => {
45
const isSelect = 'selectedIndex' in parent;
5-
let selectedIndex = -1;
6+
let noSelection = isSelect;
67
while (start < end) {
78
const child = get(children[start], 1);
8-
if (isSelect && selectedIndex < 0 && child.selected)
9-
selectedIndex = start;
109
parent.insertBefore(child, before);
10+
if (isSelect && noSelection && child.selected) {
11+
noSelection = !noSelection;
12+
let {selectedIndex} = parent;
13+
parent.selectedIndex = selectedIndex < 0 ?
14+
start :
15+
iOF.call(parent.querySelectorAll('option'), child);
16+
}
1117
start++;
1218
}
13-
if (isSelect && -1 < selectedIndex)
14-
parent.selectedIndex = selectedIndex;
1519
};
1620

1721
export const eqeq = (a, b) => a == b;

index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,22 @@ try {
4343
}
4444
var Map$1 = self.Map;
4545

46+
var iOF = [].indexOf;
47+
4648
var append = function append(get, parent, children, start, end, before) {
4749
var isSelect = 'selectedIndex' in parent;
48-
var selectedIndex = -1;
50+
var noSelection = isSelect;
4951
while (start < end) {
5052
var child = get(children[start], 1);
51-
if (isSelect && selectedIndex < 0 && child.selected) selectedIndex = start;
5253
parent.insertBefore(child, before);
54+
if (isSelect && noSelection && child.selected) {
55+
noSelection = !noSelection;
56+
var selectedIndex = parent.selectedIndex;
57+
58+
parent.selectedIndex = selectedIndex < 0 ? start : iOF.call(parent.querySelectorAll('option'), child);
59+
}
5360
start++;
5461
}
55-
if (isSelect && -1 < selectedIndex) parent.selectedIndex = selectedIndex;
5662
};
5763

5864
var eqeq = function eqeq(a, b) {

min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/test.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1236,8 +1236,36 @@ newState = domdiff(
12361236
document.createTextNode('c')
12371237
]
12381238
);
1239-
12401239
console.assert(document.body.selectedIndex === 1, 'selectedIndex is OK');
1240+
1241+
newState = domdiff(
1242+
document.body,
1243+
newState,
1244+
[]
1245+
);
1246+
document.body.selectedIndex = 0;
1247+
document.body.querySelectorAll = _ => newState.concat(newOptions);
1248+
option = document.createTextNode('option');
1249+
option.selected = true;
1250+
newState = domdiff(
1251+
document.body,
1252+
newState,
1253+
[
1254+
document.createTextNode('option')
1255+
]
1256+
);
1257+
var newOptions = [
1258+
document.createTextNode('option'),
1259+
option,
1260+
document.createTextNode('option')
1261+
];
1262+
newState = domdiff(
1263+
document.body,
1264+
newState,
1265+
newOptions
1266+
);
1267+
console.assert(document.body.selectedIndex === 2, 'partial selectedIndex is OK');
1268+
12411269
// */
12421270

12431271
tressa.end();

0 commit comments

Comments
 (0)