From 4a2f593dba852047ee65ffdc06ec2e5ce1d8beea Mon Sep 17 00:00:00 2001 From: Erik Arvidsson Date: Tue, 29 Oct 2013 17:06:15 -0400 Subject: [PATCH] Add the host property to the ShadowRoot interface https://dvcs.w3.org/hg/webcomponents/raw-file/tip/spec/shadow/index.html#api-shadow-root-host --- src/ShadowRenderer.js | 3 +-- src/wrappers/ShadowRoot.js | 7 ++++--- src/wrappers/events.js | 12 +++++------- test/js/ShadowRoot.js | 9 +++++++++ 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/ShadowRenderer.js b/src/ShadowRenderer.js index 575949a..736d9e0 100644 --- a/src/ShadowRenderer.js +++ b/src/ShadowRenderer.js @@ -11,7 +11,6 @@ var Node = scope.wrappers.Node; var ShadowRoot = scope.wrappers.ShadowRoot; var assert = scope.assert; - var getHostForShadowRoot = scope.getHostForShadowRoot; var mixin = scope.mixin; var muteMutationEvents = scope.muteMutationEvents; var oneOf = scope.oneOf; @@ -253,7 +252,7 @@ } function getRendererForShadowRoot(shadowRoot) { - return getRendererForHost(getHostForShadowRoot(shadowRoot)); + return getRendererForHost(shadowRoot.host); } var spliceDiff = new ArraySplice(); diff --git a/src/wrappers/ShadowRoot.js b/src/wrappers/ShadowRoot.js index 75b7c78..899ef98 100644 --- a/src/wrappers/ShadowRoot.js +++ b/src/wrappers/ShadowRoot.js @@ -43,6 +43,10 @@ return nextOlderShadowTreeTable.get(this) || null; }, + get host() { + return shadowHostTable.get(this) || null; + }, + invalidateShadowRenderer: function() { return shadowHostTable.get(this).invalidateShadowRenderer(); }, @@ -57,7 +61,4 @@ }); scope.wrappers.ShadowRoot = ShadowRoot; - scope.getHostForShadowRoot = function(node) { - return shadowHostTable.get(node); - }; })(this.ShadowDOMPolyfill); \ No newline at end of file diff --git a/src/wrappers/events.js b/src/wrappers/events.js index 7e64bb9..b6669cf 100644 --- a/src/wrappers/events.js +++ b/src/wrappers/events.js @@ -49,7 +49,7 @@ // 1. if (isShadowRoot(node)) - return getInsertionParent(node) || scope.getHostForShadowRoot(node); + return getInsertionParent(node) || node.host; // 2. var eventParents = scope.eventParentsTable.get(node); @@ -145,7 +145,7 @@ ancestor = calculateParents(ancestor, context, ancestors); // 3.4.7. } if (isShadowRoot(target)) // 3.5. - target = scope.getHostForShadowRoot(target); + target = target.host; else target = target.parentNode; // 3.6. } @@ -174,10 +174,8 @@ function enclosedBy(a, b) { if (a === b) return true; - if (a instanceof wrappers.ShadowRoot) { - var host = scope.getHostForShadowRoot(a); - return enclosedBy(rootOfNode(host), b); - } + if (a instanceof wrappers.ShadowRoot) + return enclosedBy(rootOfNode(a.host), b); return false; } @@ -599,7 +597,7 @@ function getTargetToListenAt(wrapper) { if (wrapper instanceof wrappers.ShadowRoot) - wrapper = scope.getHostForShadowRoot(wrapper); + wrapper = wrapper.host; return unwrap(wrapper); } diff --git a/test/js/ShadowRoot.js b/test/js/ShadowRoot.js index 04b8823..acb2537 100644 --- a/test/js/ShadowRoot.js +++ b/test/js/ShadowRoot.js @@ -56,4 +56,13 @@ suite('ShadowRoot', function() { assert.equal(sr2.olderShadowRoot, sr); }); + test('host', function() { + var host = document.createElement('div'); + var sr = host.createShadowRoot(); + assert.equal(host, sr.host); + + var sr2 = host.createShadowRoot(); + assert.equal(host, sr2.host); + }); + });