Skip to content

Commit

Permalink
Make noPatch safe with older versions of ShadyDOM
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Steven Orvell committed Dec 11, 2018
1 parent bade986 commit a2e597c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
9 changes: 5 additions & 4 deletions lib/legacy/polymer.dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
14 changes: 11 additions & 3 deletions test/unit/polymer-dom-nopatch.html
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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);
});
Expand Down

0 comments on commit a2e597c

Please sign in to comment.