From e21da8da60c29aad167d07d8015e1b9e00b71627 Mon Sep 17 00:00:00 2001 From: Keanu Lee Date: Thu, 22 Mar 2018 12:02:29 -0700 Subject: [PATCH 1/2] Get physical index by key in Polymer 1.x --- iron-list.html | 50 +++++++++++++++++++++++++++++-------------------- test/focus.html | 19 +++++++++++++++++++ 2 files changed, 49 insertions(+), 20 deletions(-) diff --git a/iron-list.html b/iron-list.html index 3f0a505f..b6f48dcf 100644 --- a/iron-list.html +++ b/iron-list.html @@ -1109,32 +1109,42 @@ _forwardItemPath: function(path, value) { path = path.slice(6); // 'items.'.length == 6 - var dot = path.indexOf('.') + 1; - if (dot === 0) { + var dot = path.indexOf('.'); + if (dot === -1) { dot = path.length; } - var vidx = IS_V2 - ? parseInt(path.substring(0, dot), 10) - // Extract `#` from `path`. - : parseInt(path.substring(1, dot), 10); - var offscreenItem = this._offscreenFocusedItem; - var isIndexRendered = this._isIndexRendered(vidx); - var inst; + var isIndexRendered; var pidx; + var inst; + var offscreenInstance = this.modelForElement(this._offscreenFocusedItem); + if (IS_V2) { + var vidx = parseInt(path.substring(0, dot), 10); + isIndexRendered = this._isIndexRendered(vidx); + if (isIndexRendered) { + pidx = this._getPhysicalIndex(vidx); + inst = this.modelForElement(this._physicalItems[pidx]); + } else if (offscreenInstance) { + inst = offscreenInstance; + } - if (isIndexRendered) { - pidx = this._getPhysicalIndex(vidx); - inst = this.modelForElement(this._physicalItems[pidx]); - } else if (offscreenItem) { - inst = this.modelForElement(offscreenItem); - } - if (!inst || inst[this.indexAs] !== vidx) { - // NOTE: consider valid instance if it has correct __key__ (Polymer 1.x). - if (!inst || inst.__key__ !== '#' + vidx) { + if (!inst || inst[this.indexAs] !== vidx) { return; } + } else { + // Polymer 1.x - get physical instance by key (`#1`), not index. + var key = path.substring(0, dot); + if (offscreenInstance && offscreenInstance.__key__ === key) { + inst = offscreenInstance; + } else { + pidx = this._physicalIndexForKey[key]; + inst = this.modelForElement(this._physicalItems[pidx]); + + if (!inst || inst.__key__ !== key) { + return; + } + } } - path = path.substring(dot); + path = path.substring(dot + 1); path = this.as + (path ? '.' + path : ''); IS_V2 ? inst._setPendingPropertyOrPath(path, value, false, true) @@ -1709,7 +1719,7 @@ _getPhysicalIndex: function(vidx) { return IS_V2 ? (this._physicalStart + (vidx - this._virtualStart)) % this._physicalCount - : this._physicalIndexForKey['#' + vidx]; + : this._physicalIndexForKey[this._collection.getKey(this.items[vidx])]; }, focusItem: function(idx) { diff --git a/test/focus.html b/test/focus.html index 0da78be1..99c90ba7 100644 --- a/test/focus.html +++ b/test/focus.html @@ -194,6 +194,25 @@ }); }); + test('list focus change with down arrow after removing item (#512)', function(done) { + container.useTabIndex = true; + list.items = buildDataSet(100); + list.splice('items', 0, 1); + + flush(function() { + var initialItem = getFirstItemFromList(list); + var itemToFocus = getNthItemFromList(list, 1); + initialItem.focus(); + flush(function() { + MockInteractions.pressAndReleaseKeyOn(list, 40); // down + flush(function() { + assert.notEqual(itemToFocus.tabIndex, -1); + done(); + }); + }); + }); + }); + test('list focus change with up arrow', function(done) { list.items = buildDataSet(100); From 8e779d56caf71596c3a42983f87e2680c753120a Mon Sep 17 00:00:00 2001 From: Keanu Lee Date: Fri, 23 Mar 2018 12:15:10 -0700 Subject: [PATCH 2/2] Fix test helper on Safari --- test/fixtures/helpers.html | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/fixtures/helpers.html b/test/fixtures/helpers.html index 8724e967..55920690 100644 --- a/test/fixtures/helpers.html +++ b/test/fixtures/helpers.html @@ -74,6 +74,9 @@ var root = document; while (root && root.elementFromPoint) { el = root.elementFromPoint(x, y); + // In Safari, root.elementFromPoint() will return the host if nothing matches + // in the shadow root (https://bugs.webkit.org/show_bug.cgi?id=170743). + if (el === r) break; r = el || r; root = el ? el.shadowRoot : null; }