From 6e16619a4a0c87676d156c552608f48bdcf12eb4 Mon Sep 17 00:00:00 2001 From: Steven Orvell Date: Thu, 14 Jan 2016 15:12:34 -0800 Subject: [PATCH] Fixes #3295. Only cache a false-y result for an element's owner shady root iff the element is currently in the document. --- src/lib/dom-api-shady.html | 15 +++++++--- test/smoke/owner-root.html | 59 ++++++++++++++++++++++++++++++++++++++ test/unit/polymer-dom.js | 18 ++++++++++++ 3 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 test/smoke/owner-root.html diff --git a/src/lib/dom-api-shady.html b/src/lib/dom-api-shady.html index e1f62ffe41..3e4a216e62 100644 --- a/src/lib/dom-api-shady.html +++ b/src/lib/dom-api-shady.html @@ -170,8 +170,8 @@ if (!node) { return; } - if (node._ownerShadyRoot === undefined) { - var root; + var root = node._ownerShadyRoot; + if (root === undefined) { if (node._isShadyRoot) { root = node; } else { @@ -183,9 +183,16 @@ root = null; } } - node._ownerShadyRoot = root; + // memo-ize result for performance but only memo-ize a false-y + // result if node is in the document. This avoids a problem where a root + // can be cached while an element is inside a fragment. + // If this happens and we cache the result, the value can become stale + // because for perf we avoid processing the subtree of added fragments. + if (root || document.contains(node)) { + node._ownerShadyRoot = root; + } } - return node._ownerShadyRoot; + return root; }, _maybeDistribute: function(node) { diff --git a/test/smoke/owner-root.html b/test/smoke/owner-root.html new file mode 100644 index 0000000000..f4ef1120fa --- /dev/null +++ b/test/smoke/owner-root.html @@ -0,0 +1,59 @@ + + + + + early owner-root + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/unit/polymer-dom.js b/test/unit/polymer-dom.js index ab9f735d7f..80a330775e 100644 --- a/test/unit/polymer-dom.js +++ b/test/unit/polymer-dom.js @@ -1207,6 +1207,24 @@ suite('Polymer.dom non-distributed elements', function() { assert.equal(Polymer.dom(test).getOwnerRoot(), c1.root, 'getOwnerRoot incorrect for child added to element in root'); }); + test('getOwnerRoot when out of tree and adding subtree', function() { + var container = document.createDocumentFragment(); + var test = document.createElement('div'); + container.appendChild(test); + assert.notOk(Polymer.dom(test).getOwnerRoot(), 'getOwnerRoot incorrect when not in root'); + var c1 = document.createElement('x-compose'); + var project = c1.$.project; + Polymer.dom(project).appendChild(container); + Polymer.dom.flush(); + assert.equal(Polymer.dom(test).getOwnerRoot(), c1.root, 'getOwnerRoot incorrect for child added to element in root'); + Polymer.dom(project).removeChild(test); + Polymer.dom.flush(); + assert.notOk(Polymer.dom(test).getOwnerRoot(), 'getOwnerRoot incorrect for child moved from a root to no root'); + Polymer.dom(project).appendChild(test); + Polymer.dom.flush(); + assert.equal(Polymer.dom(test).getOwnerRoot(), c1.root, 'getOwnerRoot incorrect for child added to element in root'); + }); + test('getOwnerRoot, subtree', function() { var test = document.createElement('div'); var testChild = document.createElement('div');