From a2e597c2eaa10257bec8447620481a17367e98c0 Mon Sep 17 00:00:00 2001 From: Steven Orvell Date: Mon, 10 Dec 2018 18:52:22 -0800 Subject: [PATCH] Make `noPatch` safe with older versions of ShadyDOM Everything should work even if a version of ShadyDOM that does not support `noPatch` is loaded. Also, re-enable type generation and privatize problematic type. --- lib/legacy/polymer.dom.js | 9 +++++---- lib/utils/wrap.js | 4 ++-- package.json | 3 ++- test/unit/polymer-dom-nopatch.html | 14 +++++++++++--- 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/lib/legacy/polymer.dom.js b/lib/legacy/polymer.dom.js index 6739ee621e..cb6fc45732 100644 --- a/lib/legacy/polymer.dom.js +++ b/lib/legacy/polymer.dom.js @@ -378,9 +378,10 @@ DomApi.prototype.textContent; DomApi.prototype.innerHTML; -if (window.ShadyDOM && window.ShadyDOM.inUse && window.ShadyDOM.noPatch) { +if (window['ShadyDOM'] && window['ShadyDOM']['inUse'] && window['ShadyDOM']['noPatch'] && window['ShadyDOM']['Wrapper']) { - class Wrapper extends ShadyDOM.Wrapper {} + /** @private */ + class Wrapper extends window['ShadyDOM']['Wrapper'] {} // copy bespoke API onto wrapper Object.getOwnPropertyNames(DomApi.prototype).forEach((prop) => { @@ -395,14 +396,14 @@ if (window.ShadyDOM && window.ShadyDOM.inUse && window.ShadyDOM.noPatch) { localTarget: { get() { - return window.ShadyDOM && window.ShadyDOM.noPatch ? this.event.currentTarget : this.event.target; + return this.event.currentTarget; }, configurable: true }, path: { get() { - return ShadyDOM.composedPath(this.event); + return window['ShadyDOM']['composedPath'](this.event); }, configurable: true } diff --git a/lib/utils/wrap.js b/lib/utils/wrap.js index dd5890ee98..0fb006c63d 100644 --- a/lib/utils/wrap.js +++ b/lib/utils/wrap.js @@ -14,6 +14,6 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN // This is similar to using `Polymer.dom` but relies exclusively // on the presence of the ShadyDOM polyfill rather than requiring the loading // of legacy (Polymer.dom) API. -export const wrap = (window.ShadyDOM && window.ShadyDOM.noPatch) ? - window.ShadyDOM.wrap : (n) => n; +export const wrap = (window['ShadyDOM'] && window['ShadyDOM']['noPatch'] && window['ShadyDOM']['wrap']) ? + window['ShadyDOM']['wrap'] : (n) => n; diff --git a/package.json b/package.json index 4f9f414508..edf1a593a7 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,8 @@ "serve": "polymer serve --npm --module-resolution=node", "lint": "gulp lint", "generate-types": "gen-typescript-declarations --outDir . --deleteExisting --verify && gulp generate-externs", - "regen-package-lock": "rm -rf node_modules package-lock.json; npm install" + "regen-package-lock": "rm -rf node_modules package-lock.json; npm install", + "prepare": "npm run generate-types" }, "repository": { "type": "git", diff --git a/test/unit/polymer-dom-nopatch.html b/test/unit/polymer-dom-nopatch.html index d5c87afb6b..54017de804 100644 --- a/test/unit/polymer-dom-nopatch.html +++ b/test/unit/polymer-dom-nopatch.html @@ -159,6 +159,10 @@ suite('events', function() { test('localTarget, rootTarget, path', function(done) { + // skip if noPatch is not available + if (!window.ShadyDOM.wrap) { + this.skip(); + } var el = fixture('scoped'); el.addEventListener('composed', function(e) { assert.equal(dom(e).rootTarget, el.$.scoped); @@ -167,7 +171,7 @@ let p = el.$.scoped; while (p) { nodes.push(p); - p = dom(p).parentNode || dom(p).host; + p = dom(p).parentNode || ShadyDOM.wrap(p).host; } nodes.push(window); const path = dom(e).path; @@ -182,9 +186,13 @@ suite('activeElement getter', function() { test('Retrieves `activeElement`', function() { + // skip if noPatch is not available + if (!window.ShadyDOM.wrap) { + this.skip(); + } var focusableInShadow = fixture('focusableInShadow'); - focusableInShadow.$.focusable.focus(); - var rootNode = dom(focusableInShadow).getRootNode(); + ShadyDOM.wrap(focusableInShadow.$.focusable).focus(); + var rootNode = ShadyDOM.wrap(focusableInShadow).getRootNode(); assert.equal(dom(rootNode).activeElement, focusableInShadow); assert.equal(dom(dom(focusableInShadow).shadowRoot).activeElement, focusableInShadow.$.focusable); });