Skip to content

Commit

Permalink
Make sure event.composedPath is consistent
Browse files Browse the repository at this point in the history
Fixes #29
  • Loading branch information
dfreedm committed Aug 11, 2016
1 parent 426944c commit 372b667
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 86 deletions.
35 changes: 12 additions & 23 deletions src/compat/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,18 @@
}

initialize() {
var self = this;
Polymer.AsyncRender.afterRender(function() {
self.initializeSelf();
});
if (this.__meta.listeners) {
var l$ = this.__meta.listeners;
for (var i=0, l; (i<l$.length) && (l=l$[i]); i++) {
events.addMethodListener(this, l.event, l.method);
}
}
if (this.__meta.hostAttributes) {
var a$ = this.__meta.hostAttributes;
for (var a in a$) {
attributes.ensureAttribute(this, a, a$[a]);
}
}
if (!this.root) {
if (this._template) {
// BREAKME(sorvell): remove v0 support when we can...
Expand All @@ -201,25 +209,6 @@
behaviors.callMethod(this, 'ready');
}

initializeSelf() {
if (this.__initializedSelf) {
return;
}
this.__initializedSelf = true;
if (this.__meta.listeners) {
var l$ = this.__meta.listeners;
for (var i=0, l; (i<l$.length) && (l=l$[i]); i++) {
events.addMethodListener(this, l.event, l.method);
}
}
if (this.__meta.hostAttributes) {
var a$ = this.__meta.hostAttributes;
for (var a in a$) {
attributes.ensureAttribute(this, a, a$[a]);
}
}
}

// reserved for canonical behavior
disconnectedCallback() {
this.detachedCallback();
Expand Down
92 changes: 45 additions & 47 deletions src/shady/element-mixin.html
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@
// and forces distribution.
var insertionPointTag = ownerRoot && ownerRoot.getInsertionPointTag() || '';
var fragContent = (node.nodeType === Node.DOCUMENT_FRAGMENT_NODE) &&
!node.__noContent &&
!node.__noContent &&
insertionPointTag && node.querySelector(insertionPointTag);
var wrappedContent = fragContent &&
(Tree.Logical.getParentNode(fragContent).nodeType !==
Expand Down Expand Up @@ -278,7 +278,7 @@
firstComposedNode: function(insertionPoint) {
var n$ = insertionPoint.getDistributedNodes();
var root = this.ownerRootForNode(insertionPoint);
for (var i=0, l=n$.length, n, p$; (i<l) && (n=n$[i]); i++) {
for (var i=0, l=n$.length, n; (i<l) && (n=n$[i]); i++) {
// means that we're composed to this spot.
if (root.isFinalDestination(insertionPoint, n)) {
return n;
Expand Down Expand Up @@ -317,17 +317,17 @@

// NOTE: `query` is used primarily for ShadyDOM's querySelector impl,
// but it's also generally useful to recurse through the element tree
// and is used by Polymer's styling system.
// and is used by Polymer's styling system.
query: function(node, matcher, halter) {
var list = [];
this._queryElements(Tree.Logical.getChildNodes(node), matcher,
this._queryElements(Tree.Logical.getChildNodes(node), matcher,
halter, list);
return list;
},

_queryElements: function(elements, matcher, halter, list) {
for (var i=0, l=elements.length, c; (i<l) && (c=elements[i]); i++) {
if (c.nodeType === Node.ELEMENT_NODE &&
if (c.nodeType === Node.ELEMENT_NODE &&
this._queryElement(c, matcher, halter, list)) {
return true;
}
Expand All @@ -342,7 +342,7 @@
if (halter && halter(result)) {
return result;
}
this._queryElements(Tree.Logical.getChildNodes(node), matcher,
this._queryElements(Tree.Logical.getChildNodes(node), matcher,
halter, list);
},

Expand Down Expand Up @@ -394,7 +394,7 @@
};

Object.defineProperties(NodeMixin, {

isConnected: {
get() {
return document.contains(this);
Expand Down Expand Up @@ -485,7 +485,7 @@
}
}
// if adding to a shadyRoot, add to host instead
var container = ShadyDom.isShadyRoot(this) ?
var container = ShadyDom.isShadyRoot(this) ?
this.host : this;
if (ref_node) {
Tree.Composed.insertBefore(container, node, ref_node);
Expand All @@ -510,7 +510,7 @@
if (!mixinImpl.removeNode(node)) {
// if removing from a shadyRoot, remove form host instead
var container = ShadyDom.isShadyRoot(this) ?
this.host :
this.host :
this;
// not guaranteed to physically be in container; e.g.
// undistributed nodes.
Expand Down Expand Up @@ -573,37 +573,7 @@
}
}
return n;
},

// TODO(sorvell): removeEventListener + weakmap...
addEventListener: function(type, fn, capture) {
if (!this.__eventListenerCount) {
this.__eventListenerCount = 0;
}
this.__eventListenerCount++;
var wrappedFn = function(e) {
if (!e.__target) {
e.__target = e.target;
var proto = ShadyDom.patchImpl.prototypeForObject(e);
if (proto) {
e.__proto__ = proto;
}
}
return fn(e);
}
fn.__eventWrapper = wrappedFn;
return origAddEventListener.call(this, type, wrappedFn, capture);
},

removeEventListener: function(type, fn, capture) {
var wrapper = fn.__eventWrapper;
origRemoveEventListener.call(this, type, wrapper || fn, capture);
if (wrapper) {
fn.__eventWrapper = null;
this.__eventListenerCount--;
}
}

};

Object.defineProperties(FragmentMixin, {
Expand Down Expand Up @@ -731,15 +701,15 @@
nativeRemoveAttribute.call(this, name);
if (!mixinImpl.maybeDistributeParent(this)) {
mixinImpl.maybeDistributeAttributeChange(this, name);
};
}
}

};

Object.defineProperties(ElementMixin, {

shadowRoot: {
get() {
get() {
return this.shadyRoot;
}
},
Expand Down Expand Up @@ -772,7 +742,7 @@
});

var EventMixin = {

__patched: 'Event',

composedPath() {
Expand Down Expand Up @@ -802,7 +772,7 @@
},

get target() {
// If ANCESTOR's root is not a shadow root or ANCESTOR's root is BASE's
// If ANCESTOR's root is not a shadow root or ANCESTOR's root is BASE's
// shadow-including inclusive ancestor, return ANCESTOR.
var base = this.currentTarget;
var baseRoot = base && ShadyDom.ownerRootForNode(base);
Expand All @@ -818,18 +788,46 @@

};

ShadyDom.addEventListener = function(type, fn, capture) {
if (!this.__eventListenerCount) {
this.__eventListenerCount = 0;
}
this.__eventListenerCount++;
var wrappedFn = function(e) {
if (!e.__target) {
e.__target = e.target;
var proto = ShadyDom.patchImpl.prototypeForObject(e);
if (proto) {
e.__proto__ = proto;
}
}
return fn(e);
}
fn.__eventWrapper = wrappedFn;
return origAddEventListener.call(this, type, wrappedFn, capture);
};

ShadyDom.removeEventListener = function(type, fn, capture) {
var wrapper = fn.__eventWrapper;
origRemoveEventListener.call(this, type, wrapper || fn, capture);
if (wrapper) {
fn.__eventWrapper = null;
this.__eventListenerCount--;
}
};

ShadyDom.Mixins = {

Node: ShadyDom.extendAll({__patched: 'Node'}, NodeMixin),

Fragment: ShadyDom.extendAll({__patched: 'Fragment'},
Fragment: ShadyDom.extendAll({__patched: 'Fragment'},
NodeMixin, FragmentMixin, ActiveElementMixin),

Element: ShadyDom.extendAll({__patched: 'Element'},
Element: ShadyDom.extendAll({__patched: 'Element'},
NodeMixin, FragmentMixin, ElementMixin, ActiveElementMixin),

// Note: activeElement cannot be patched on document!
Document: ShadyDom.extendAll({__patched: 'Document'},
Document: ShadyDom.extendAll({__patched: 'Document'},
NodeMixin, FragmentMixin, ElementMixin, UnderActiveElementMixin),

Event: EventMixin
Expand Down
34 changes: 18 additions & 16 deletions src/shady/patch.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
<script>

/**
* Patches elements that interacts with ShadyDOM
* Patches elements that interacts with ShadyDOM
* such that tree traversal and mutation apis act like they would under
* ShadowDOM.
*
* This import enables seemless interaction with ShadyDOM powered
* This import enables seemless interaction with ShadyDOM powered
* custom elements, enabling better interoperation with 3rd party code,
* libraries, and frameworks that use DOM tree manipulation apis.
*/
Expand All @@ -28,12 +28,8 @@
var Tree = ShadyDom.Tree;
var Mixins = ShadyDom.Mixins;

var nativeInsertBefore = Element.prototype.insertBefore;
var nativeAppendChild = Element.prototype.appendChild;
var nativeRemoveChild = Element.prototype.removeChild;

ShadyDom.patchedCount = 0;

ShadyDom.patch = function(node) {
if (!ShadyDom.inUse) {
return;
Expand Down Expand Up @@ -61,17 +57,16 @@
case document.head:
case document.documentElement:
return false;
default:
default:
return true;
}
return true;
},

hasPrototypeDescriptors: Boolean(Object.getOwnPropertyDescriptor(
Node.prototype, 'textContent')),

protoCache: {},

patch: function(node) {
ShadyDom.patchedCount++;
log && console.warn('patch node', node);
Expand Down Expand Up @@ -129,7 +124,7 @@
obj.__proto__ = obj.__sourceProto;
}
}

};

ShadyDom.patchImpl = patchImpl;
Expand All @@ -141,21 +136,28 @@
return root;
}

function patchEvents() {
Node.prototype.addEventListener = ShadyDom.addEventListener;
Node.prototype.removeEventListener = ShadyDom.removeEventListener;
}

if (ShadyDom.force || !Element.prototype.createShadowRoot) {

// NOTE: transparent at the cost of intrusiveness.
Element.prototype.createShadowRoot = function() {
return createRootAndEnsurePatched(this, true);
}


patchEvents();
}

if (ShadyDom.force || !Element.prototype.attachShadow) {

Element.prototype.attachShadow = function(dictionary) {
Element.prototype.attachShadow = function() {
return createRootAndEnsurePatched(this);
}


patchEvents();
}

// TODO(sorvell): super experimental auto patching of document fragment
Expand Down

0 comments on commit 372b667

Please sign in to comment.