-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Keep generated event handlers, attach with WeakMap to host element Add tests for events Fixes #1639
- Loading branch information
Showing
7 changed files
with
427 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<!-- | ||
@license | ||
Copyright (c) 2015 The Polymer Project Authors. All rights reserved. | ||
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt | ||
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | ||
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt | ||
Code distributed by Google as part of the polymer project is also | ||
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt | ||
--> | ||
|
||
<link rel="import" href="../../polymer.html"> | ||
|
||
<script> | ||
var EventLoggerImpl = { | ||
created: function() { | ||
this._handled = {}; | ||
this._warned = []; | ||
this._removed = []; | ||
}, | ||
handle: function(e) { | ||
this._handled[e.currentTarget.localName] = e.type; | ||
}, | ||
_warn: function(array) { | ||
var hasColor = array[0].indexOf('%c') === 0; | ||
var funIdx = hasColor ? 3 : 2; | ||
var messageIdx = hasColor ? 4 : 3; | ||
if (array[funIdx] === '_createEventHandler') { | ||
this._warned.push(array[messageIdx]); | ||
} else { | ||
Polymer.Base._warn(array); | ||
} | ||
}, | ||
_unlisten: function(node, eventName, handler) { | ||
this._removed.push({target: node.localName, event: eventName}); | ||
Polymer.Base._unlisten(node, eventName, handler); | ||
} | ||
}; | ||
</script> | ||
|
||
<dom-module id="x-listeners"> | ||
<script> | ||
Polymer({ | ||
is: 'x-listeners', | ||
behaviors: [EventLoggerImpl], | ||
listeners: { | ||
foo: 'handle', | ||
bar: 'missing' | ||
}, | ||
teardown: function() { | ||
this.unlisten(this, 'foo'); | ||
this.unlisten(this, 'bar'); | ||
} | ||
}); | ||
</script> | ||
</dom-module> | ||
|
||
<dom-module id="x-on"> | ||
<template> | ||
<div id="inner" on-foo="handle" on-bar="missing"></div> | ||
</template> | ||
<script> | ||
Polymer({ | ||
is: 'x-on', | ||
behaviors: [EventLoggerImpl], | ||
teardown: function() { | ||
this.unlisten(this.$.inner, 'foo'); | ||
this.unlisten(this.$.inner, 'bar'); | ||
} | ||
}); | ||
</script> | ||
</dom-module> | ||
|
||
<dom-module id="x-dynamic"> | ||
<template> | ||
<div id="inner"></div> | ||
</template> | ||
<script> | ||
Polymer({ | ||
is: 'x-dynamic', | ||
behaviors: [EventLoggerImpl], | ||
setup: function() { | ||
this.listen(this, 'foo', 'handle'); | ||
this.listen(this.$.inner, 'foo', 'handle'); | ||
this.listen(this, 'bar', 'missing'); | ||
this.listen(this.$.inner, 'bar', 'missing'); | ||
}, | ||
teardown: function() { | ||
this.unlisten(this, 'foo'); | ||
this.unlisten(this.$.inner, 'foo'); | ||
this.unlisten(this, 'bar'); | ||
this.unlisten(this.$.inner, 'bar'); | ||
} | ||
}); | ||
</script> | ||
</dom-module> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
<!DOCTYPE html> | ||
<!-- | ||
@license | ||
Copyright (c) 2015 The Polymer Project Authors. All rights reserved. | ||
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt | ||
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | ||
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt | ||
Code distributed by Google as part of the polymer project is also | ||
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt | ||
--> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8"> | ||
|
||
<script src="../../../webcomponentsjs/webcomponents-lite.js"></script> | ||
<script src="../../../web-component-tester/browser.js"></script> | ||
|
||
<link rel="import" href="../../polymer.html"> | ||
<link rel="import" href="events-elements.html"> | ||
</head> | ||
<body> | ||
|
||
<script> | ||
suite('Event Listeners', function() { | ||
var el; | ||
|
||
suite('listeners block', function() { | ||
suiteSetup(function() { | ||
el = document.createElement('x-listeners'); | ||
document.body.appendChild(el); | ||
}); | ||
|
||
suiteTeardown(function() { | ||
document.body.removeChild(el); | ||
}); | ||
|
||
test('setup', function() { | ||
var boundListeners = el.__boundListeners; | ||
assert(boundListeners, 'listeners are bound'); | ||
var map = boundListeners.get(el); | ||
assert(map, 'events have handlers'); | ||
Object.keys(el.listeners).forEach(function(l) { | ||
assert(map[l], 'listener is in event map'); | ||
}); | ||
}); | ||
|
||
test('fire', function() { | ||
el.fire('foo'); | ||
assert.equal(el._handled[el.localName], 'foo'); | ||
el.fire('bar'); | ||
assert.equal(el._warned.length, 1); | ||
assert.equal(el._warned[0], 'listener method `missing` not defined'); | ||
}); | ||
|
||
test('teardown', function() { | ||
el.teardown(); | ||
assert.deepEqual(el._removed[0], {target: el.localName, event: 'foo'}); | ||
assert.deepEqual(el._removed[1], {target: el.localName, event: 'bar'}); | ||
}); | ||
}); | ||
|
||
suite('on-*', function() { | ||
|
||
suiteSetup(function() { | ||
el = document.createElement('x-on'); | ||
document.body.appendChild(el); | ||
}); | ||
|
||
suiteTeardown(function() { | ||
document.body.removeChild(el); | ||
}); | ||
|
||
test('setup', function() { | ||
var boundListeners = el.__boundListeners; | ||
assert(boundListeners, 'listeners are bound'); | ||
var map = boundListeners.get(el.$.inner); | ||
assert(map, 'events have handlers'); | ||
assert(el._notes[0].events.length, 2); | ||
el._notes[0].events.forEach(function(l) { | ||
assert(map[l.name], 'listener is in event map'); | ||
}); | ||
}); | ||
|
||
test('fire', function() { | ||
var options = {node: el.$.inner}; | ||
el.fire('foo', {}, options); | ||
assert.equal(el._handled.div, 'foo'); | ||
assert(!el._handled[el.localName]); | ||
el.fire('bar', {}, options); | ||
assert.equal(el._warned.length, 1); | ||
assert.equal(el._warned[0], 'listener method `missing` not defined'); | ||
}); | ||
|
||
test('teardown', function() { | ||
el.teardown(); | ||
assert.deepEqual(el._removed[0], {target: 'div', event: 'foo'}); | ||
assert.deepEqual(el._removed[1], {target: 'div', event: 'bar'}); | ||
}); | ||
}); | ||
|
||
suite('dynamic', function() { | ||
|
||
suiteSetup(function() { | ||
el = document.createElement('x-dynamic'); | ||
document.body.appendChild(el); | ||
}); | ||
|
||
suiteTeardown(function() { | ||
document.body.removeChild(el); | ||
}); | ||
|
||
test('setup', function() { | ||
assert(!el.__boundListeners, 'listeners are not bound'); | ||
el.setup(); | ||
var boundListeners = el.__boundListeners; | ||
assert(boundListeners, 'listeners are bound'); | ||
var outerMap = boundListeners.get(el); | ||
assert(outerMap, 'element events have handlers'); | ||
var innerMap = boundListeners.get(el.$.inner); | ||
assert(innerMap, 'inner div events have handlers'); | ||
}); | ||
|
||
test('fire', function() { | ||
var options = {node: el.$.inner}; | ||
el.fire('foo', {}, options); | ||
assert.equal(el._handled.div, 'foo', 'inner handler'); | ||
assert.equal(el._handled[el.localName], 'foo', 'outer handler'); | ||
el.fire('bar', {}, options); | ||
assert.equal(el._warned.length, 2); | ||
assert.equal(el._warned[0], 'listener method `missing` not defined'); | ||
assert.equal(el._warned[1], 'listener method `missing` not defined'); | ||
}); | ||
|
||
test('teardown', function() { | ||
el.teardown(); | ||
assert.deepEqual(el._removed[0], {target: el.localName, event: 'foo'}); | ||
assert.deepEqual(el._removed[1], {target: 'div', event: 'foo'}); | ||
assert.deepEqual(el._removed[2], {target: el.localName, event: 'bar'}); | ||
assert.deepEqual(el._removed[3], {target: 'div', event: 'bar'}); | ||
}); | ||
}); | ||
}); | ||
</script> | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.