Skip to content

Commit 5132990

Browse files
authored
Merge pull request #510 from PolymerElements/dont-blur
Only blur activeElement if it is a descendant of the list (#505, #507)
2 parents db5879b + 87629db commit 5132990

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

iron-list.html

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,9 @@
10911091
} else if (change.path === 'items.splices') {
10921092
this._adjustVirtualIndex(change.value.indexSplices);
10931093
this._virtualCount = this.items ? this.items.length : 0;
1094+
// Only blur activeElement if it is a descendant of the list (#505, #507).
1095+
var activeElement = this._getActiveElement();
1096+
if (this.contains(activeElement)) activeElement.blur();
10941097
// Render only if the affected index is rendered.
10951098
var affectedIndexRendered = change.value.indexSplices.some(function(splice) {
10961099
return splice.index + splice.addedCount >= this._virtualStart &&
@@ -1170,7 +1173,6 @@
11701173
// remove the current focused item
11711174
if (this._focusedItem && this.modelForElement(this._focusedItem)[this.as] === item) {
11721175
this._removeFocusedItem();
1173-
document.activeElement && document.activeElement.blur && document.activeElement.blur();
11741176
}
11751177
},
11761178

@@ -1608,8 +1610,7 @@
16081610
}
16091611
var modelTabIndex, activeElTabIndex;
16101612
var target = Polymer.dom(e).path[0];
1611-
var itemsHost = this._itemsParent.node.domHost;
1612-
var activeEl = Polymer.dom(itemsHost ? itemsHost.root : document).activeElement;
1613+
var activeEl = this._getActiveElement();
16131614
var physicalItem = this._physicalItems[this._getPhysicalIndex(model[this.indexAs])];
16141615
// Safari does not focus certain form controls via mouse
16151616
// https://bugs.webkit.org/show_bug.cgi?id=118043
@@ -1938,6 +1939,12 @@
19381939
this.modelForElement(item)[prop] = value;
19391940
}
19401941
}, this);
1942+
},
1943+
1944+
/* Gets the activeElement of the shadow root/host that contains the list. */
1945+
_getActiveElement: function() {
1946+
var itemsHost = this._itemsParent.node.domHost;
1947+
return Polymer.dom(itemsHost ? itemsHost.root : document).activeElement;
19411948
}
19421949

19431950
});

test/focus.html

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@
116116
var button = document.createElement('button');
117117
list.parentNode.appendChild(button);
118118
button.focus();
119-
// getFirstItemFromList(list).focus();
120119

121120
assert.notEqual(document.activeElement, document.body);
122121

@@ -127,6 +126,23 @@
127126
});
128127
});
129128

129+
test('don\'t blur when activeElement is not a descendant of the list (#505, #507)', function(done) {
130+
list.items = buildDataSet(100);
131+
132+
flush(function() {
133+
var button = document.createElement('button');
134+
list.parentNode.appendChild(button);
135+
button.focus();
136+
137+
assert.notEqual(document.activeElement, document.body);
138+
139+
list.splice('items', 0, 1);
140+
141+
assert.notEqual(document.activeElement, document.body);
142+
done();
143+
});
144+
});
145+
130146
test('focusItem()', function(done) {
131147
list.items = buildDataSet(100);
132148

test/mutations.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,11 @@
179179
list.scrollToIndex(45);
180180
PolymerFlush();
181181
assert.equal(getFirstItemFromList(list).textContent, '45');
182-
var spliceItems = new Array(50).fill(buildItem(phrase));
182+
var spliceItems = new Array(50);
183+
var item = buildItem(phrase);
184+
for (var i = 0; i < 50; ++i) {
185+
spliceItems[i] = item;
186+
}
183187
list.splice.apply(list, ['items', 0, 50].concat(spliceItems));
184188
PolymerFlush();
185189
assert.equal(getFirstItemFromList(list).textContent, phrase);

0 commit comments

Comments
 (0)