Skip to content

Commit

Permalink
More shady compatible wrapping
Browse files Browse the repository at this point in the history
Sync'd with Polymer 1 wrap locations
  • Loading branch information
Steven Orvell committed Dec 8, 2018
1 parent acbe649 commit b8f3b79
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 36 deletions.
3 changes: 2 additions & 1 deletion lib/elements/dom-bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { PropertyEffects } from '../mixins/property-effects.js';
import { OptionalMutableData } from '../mixins/mutable-data.js';
import { GestureEventListeners } from '../mixins/gesture-event-listeners.js';
import { strictTemplatePolicy } from '../utils/settings.js';
import { wrap } from '../utils/wrap.js';

/**
* @constructor
Expand Down Expand Up @@ -87,7 +88,7 @@ export class DomBind extends domBindBase {
}

__insertChildren() {
this.parentNode.insertBefore(this.root, this);
wrap(wrap(this).parentNode).insertBefore(this.root, this);
}

__removeChildren() {
Expand Down
12 changes: 9 additions & 3 deletions lib/legacy/legacy-element-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,9 @@ export const LegacyElementMixin = dedupingMixin((base) => {
* @return {Element} Element found by the selector, or null if not found.
*/
$$(slctr) {
return this.root.querySelector(slctr);
const root = /** @type {Element} */ (this.root);
const domApi = /** @type {DomApi} */(dom(root));
return domApi.querySelector(slctr);
}

/**
Expand All @@ -503,7 +505,9 @@ export const LegacyElementMixin = dedupingMixin((base) => {
* @return {void}
*/
distributeContent() {
if (window.ShadyDOM && this.shadowRoot) {
const thisEl = /** @type {Element} */ (this);
const domApi = /** @type {DomApi} */(dom(thisEl));
if (window.ShadyDOM && domApi.shadowRoot) {
ShadyDOM.flush();
}
}
Expand Down Expand Up @@ -603,7 +607,9 @@ export const LegacyElementMixin = dedupingMixin((base) => {
* @return {!Array<!Node>} List of distributed nodes for the `<slot>`.
*/
getContentChildNodes(slctr) {
let content = this.root.querySelector(slctr || 'slot');
const root = /** @type {Element} */ (this.root);
const domApi = /** @type {DomApi} */(dom(root));
let content = domApi.querySelector(slctr || 'slot');
return content ? /** @type {DomApi} */(dom(content)).getDistributedNodes() : [];
}

Expand Down
64 changes: 32 additions & 32 deletions lib/utils/gestures.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,23 +173,21 @@ let mouseCanceller = function(mouseEvent) {
// disable "ghost clicks"
if (mouseEvent.type === 'click') {
let clickFromLabel = false;
let path = mouseEvent.composedPath && mouseEvent.composedPath();
if (path) {
for (let i = 0; i < path.length; i++) {
if (path[i].nodeType === Node.ELEMENT_NODE) {
if (path[i].localName === 'label') {
clickedLabels.push(path[i]);
} else if (canBeLabelled(path[i])) {
let ownerLabels = matchingLabels(path[i]);
// check if one of the clicked labels is labelling this element
for (let j = 0; j < ownerLabels.length; j++) {
clickFromLabel = clickFromLabel || clickedLabels.indexOf(ownerLabels[j]) > -1;
}
let path = getComposedPath(mouseEvent);
for (let i = 0; i < path.length; i++) {
if (path[i].nodeType === Node.ELEMENT_NODE) {
if (path[i].localName === 'label') {
clickedLabels.push(path[i]);
} else if (canBeLabelled(path[i])) {
let ownerLabels = matchingLabels(path[i]);
// check if one of the clicked labels is labelling this element
for (let j = 0; j < ownerLabels.length; j++) {
clickFromLabel = clickFromLabel || clickedLabels.indexOf(ownerLabels[j]) > -1;
}
}
if (path[i] === POINTERSTATE.mouse.target) {
return;
}
}
if (path[i] === POINTERSTATE.mouse.target) {
return;
}
}
// if one of the clicked labels was labelling the target element,
Expand Down Expand Up @@ -229,7 +227,7 @@ function ignoreMouse(e) {
POINTERSTATE.mouse.target = null;
POINTERSTATE.mouse.mouseIgnoreJob = null;
};
POINTERSTATE.mouse.target = e.composedPath()[0];
POINTERSTATE.mouse.target = getComposedPath(e)[0];
POINTERSTATE.mouse.mouseIgnoreJob = Debouncer.debounce(
POINTERSTATE.mouse.mouseIgnoreJob
, timeOut.after(MOUSE_TIMEOUT)
Expand Down Expand Up @@ -303,14 +301,12 @@ let POINTERSTATE = {

function firstTouchAction(ev) {
let ta = 'auto';
let path = ev.composedPath && ev.composedPath();
if (path) {
for (let i = 0, n; i < path.length; i++) {
n = path[i];
if (n[TOUCH_ACTION]) {
ta = n[TOUCH_ACTION];
break;
}
let path = getComposedPath(ev);
for (let i = 0, n; i < path.length; i++) {
n = path[i];
if (n[TOUCH_ACTION]) {
ta = n[TOUCH_ACTION];
break;
}
}
return ta;
Expand All @@ -334,6 +330,15 @@ function untrackDocument(stateObj) {
// Use passive event listeners, if supported, to not affect scrolling performance
document.addEventListener('touchend', ignoreMouse, SUPPORTS_PASSIVE ? {passive: true} : false);

/**
* Returns the composedPath for the given event.
* @param {Event} event to process
* @return {!Array<!EventTarget>} Path of the event
*/
const getComposedPath = window.ShadyDOM && window.ShadyDOM.noPatch ?
window.ShadyDOM.composedPath :
(event) => event.composedPath && event.composedPath() || [];

/** @type {!Object<string, !GestureRecognizer>} */
export const gestures = {};

Expand Down Expand Up @@ -380,14 +385,9 @@ export function deepTargetFind(x, y) {
* @return {EventTarget} Returns the event target.
*/
function _findOriginalTarget(ev) {
// shadowdom
if (ev.composedPath) {
const targets = /** @type {!Array<!EventTarget>} */(ev.composedPath());
// It shouldn't be, but sometimes targets is empty (window on Safari).
return targets.length > 0 ? targets[0] : ev.target;
}
// shadydom
return ev.target;
const path = getComposedPath(ev);
// It shouldn't be, but sometimes path is empty (window on Safari).
return path.length > 0 ? path[0] : ev.target;
}

/**
Expand Down

0 comments on commit b8f3b79

Please sign in to comment.