From ae8833f8a3d6c664b8b2183bf426032ac35ea490 Mon Sep 17 00:00:00 2001 From: Daniel Freedman Date: Tue, 8 Oct 2013 18:01:32 -0700 Subject: [PATCH 1/4] Avoid for-in loops with explicity copied keys --- src/PointerEvent.js | 62 ++++++++++++++++++++++------------- src/dispatcher.js | 80 +++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 114 insertions(+), 28 deletions(-) diff --git a/src/PointerEvent.js b/src/PointerEvent.js index f1896c99..79d9eeb8 100644 --- a/src/PointerEvent.js +++ b/src/PointerEvent.js @@ -29,6 +29,40 @@ } catch(e) { } + var MOUSE_PROPS = [ + 'bubbles', + 'cancelable', + 'view', + 'detail', + 'screenX', + 'screenY', + 'clientX', + 'clientY', + 'ctrlKey', + 'altKey', + 'shiftKey', + 'metaKey', + 'button', + 'relatedTarget', + ]; + + var MOUSE_DEFAULTS = [ + false, + false, + null, + null, + 0, + 0, + 0, + 0, + false, + false, + false, + false, + 0, + null + ]; + function PointerEvent(inType, inDict) { inDict = inDict || {}; // According to the w3c spec, @@ -67,29 +101,13 @@ e = new MouseEvent(inType, inDict); } else { e = document.createEvent('MouseEvent'); - // import values from the given dictionary - var props = { - bubbles: false, - cancelable: false, - view: null, - detail: null, - screenX: 0, - screenY: 0, - clientX: 0, - clientY: 0, - ctrlKey: false, - altKey: false, - shiftKey: false, - metaKey: false, - button: 0, - relatedTarget: null - }; - Object.keys(props).forEach(function(k) { - if (k in inDict) { - props[k] = inDict[k]; - } - }); + // import values from the given dictionary + var props = {}, p; + for(var i = 0; i < MOUSE_PROPS.length; i++) { + p = MOUSE_PROPS[i]; + props[p] = inDict[p] || MOUSE_DEFAULTS[i]; + } // define the properties inherited from MouseEvent e.initMouseEvent( diff --git a/src/dispatcher.js b/src/dispatcher.js index d80608a5..442f51a3 100644 --- a/src/dispatcher.js +++ b/src/dispatcher.js @@ -5,6 +5,73 @@ */ (function(scope) { + var CLONE_PROPS = [ + // MouseEvent + 'bubbles', + 'cancelable', + 'view', + 'detail', + 'screenX', + 'screenY', + 'clientX', + 'clientY', + 'ctrlKey', + 'altKey', + 'shiftKey', + 'metaKey', + 'button', + 'relatedTarget', + // DOM Level 3 + 'buttons', + // PointerEvent + 'pointerId', + 'width', + 'height', + 'pressure', + 'tiltX', + 'tiltY', + 'pointerType', + 'hwTimestamp', + 'isPrimary', + // event instance + 'type', + 'target', + 'currentTarget' + ]; + + var CLONE_DEFAULTS = [ + // MouseEvent + false, + false, + null, + null, + 0, + 0, + 0, + 0, + false, + false, + false, + false, + 0, + null, + // DOM Level 3 + 0, + // PointerEvent + 0, + 0, + 0, + 0, + 0, + 0, + '', + 0, + false, + // event instance + '', + null, + null + ]; /** * This module is for normalizing events. Mouse and Touch events will be @@ -19,9 +86,9 @@ * - pointercancel: a pointer will no longer generate events */ var dispatcher = { - targets: new SideTable, - handledEvents: new SideTable, - pointermap: new scope.PointerMap, + targets: new SideTable(), + handledEvents: new SideTable(), + pointermap: new scope.PointerMap(), eventMap: {}, // Scope objects for native events. // This exists for ease of testing. @@ -163,9 +230,10 @@ * properties. */ cloneEvent: function(inEvent) { - var eventCopy = {}; - for (var n in inEvent) { - eventCopy[n] = inEvent[n]; + var eventCopy = {}, p; + for (var i = 0; i < CLONE_PROPS.length; i++) { + p = CLONE_PROPS[i]; + eventCopy[p] = inEvent[p] || CLONE_DEFAULTS[i]; } return eventCopy; }, From 2b9bb40eb4537c4c8cecdc1d3355ae2945cb1083 Mon Sep 17 00:00:00 2001 From: Daniel Freedman Date: Wed, 9 Oct 2013 16:24:50 -0700 Subject: [PATCH 2/4] use shared WeakMap repo --- build.json | 2 +- pointerevents.js | 2 +- src/dispatcher.js | 4 ++-- src/sidetable.js | 40 ---------------------------------------- src/touch.js | 2 +- tests/loader.js | 4 ++-- 6 files changed, 7 insertions(+), 47 deletions(-) delete mode 100644 src/sidetable.js diff --git a/build.json b/build.json index 6c9fa332..c9378877 100644 --- a/build.json +++ b/build.json @@ -1,9 +1,9 @@ [ + "../WeakMap/weakmap.js", "src/boot.js", "src/touch-action.js", "src/PointerEvent.js", "src/pointermap.js", - "src/sidetable.js", "src/dispatcher.js", "src/installer.js", "src/mouse.js", diff --git a/pointerevents.js b/pointerevents.js index 9ca6e2f3..8489ab4a 100644 --- a/pointerevents.js +++ b/pointerevents.js @@ -7,10 +7,10 @@ var thisFile = 'pointerevents.js'; var scopeName = 'PointerEventsPolyfill'; var modules = [ + '../WeakMap/weakmap.js', 'src/boot.js', 'src/touch-action.js', 'src/PointerEvent.js', - 'src/pointermap.js', 'src/sidetable.js', 'src/dispatcher.js', 'src/installer.js', diff --git a/src/dispatcher.js b/src/dispatcher.js index 442f51a3..816a0bcb 100644 --- a/src/dispatcher.js +++ b/src/dispatcher.js @@ -86,8 +86,8 @@ * - pointercancel: a pointer will no longer generate events */ var dispatcher = { - targets: new SideTable(), - handledEvents: new SideTable(), + targets: new WeakMap(), + handledEvents: new WeakMap(), pointermap: new scope.PointerMap(), eventMap: {}, // Scope objects for native events. diff --git a/src/sidetable.js b/src/sidetable.js deleted file mode 100644 index 4d79d455..00000000 --- a/src/sidetable.js +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 2012 The Polymer Authors. All rights reserved. - * Use of this source code is governed by a BSD-style - * license that can be found in the LICENSE file. - */ - -// SideTable is a weak map where possible. If WeakMap is not available the -// association is stored as an expando property. -var SideTable; -// TODO(arv): WeakMap does not allow for Node etc to be keys in Firefox -if (typeof WeakMap !== 'undefined' && navigator.userAgent.indexOf('Firefox/') < 0) { - SideTable = WeakMap; -} else { - (function() { - var defineProperty = Object.defineProperty; - var counter = Date.now() % 1e9; - - SideTable = function() { - this.name = '__st' + (Math.random() * 1e9 >>> 0) + (counter++ + '__'); - }; - - SideTable.prototype = { - set: function(key, value) { - var entry = key[this.name]; - if (entry && entry[0] === key) - entry[1] = value; - else - defineProperty(key, this.name, {value: [key, value], writable: true}); - }, - get: function(key) { - var entry; - return (entry = key[this.name]) && entry[0] === key ? - entry[1] : undefined; - }, - delete: function(key) { - this.set(key, undefined); - } - } - })(); -} diff --git a/src/touch.js b/src/touch.js index 1092c4c0..012edbbd 100644 --- a/src/touch.js +++ b/src/touch.js @@ -19,7 +19,7 @@ // handler block for native touch events var touchEvents = { - scrollType: new SideTable, + scrollType: new WeakMap(), events: [ 'touchstart', 'touchmove', diff --git a/tests/loader.js b/tests/loader.js index c6b5bd5e..6dd11594 100644 --- a/tests/loader.js +++ b/tests/loader.js @@ -16,8 +16,8 @@ suite('Loader', function() { test('PointerMap', function() { expect(pep.PointerMap).to.be.ok; }); - test('SideTable', function() { - expect(SideTable).to.be.ok; + test('WeakMap', function() { + expect(WeakMap).to.be.ok; }); test('Dispatcher', function() { expect(pep.dispatcher).to.be.ok; From b094f645c39896467466932206d87ddaf5fccd6f Mon Sep 17 00:00:00 2001 From: Daniel Freedman Date: Wed, 9 Oct 2013 16:31:54 -0700 Subject: [PATCH 3/4] fix karma paths for WeakMap --- karma.conf.js | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/karma.conf.js b/karma.conf.js index 01778400..7787a37b 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -2,26 +2,26 @@ module.exports = function(karma) { var common = require('../tools/test/karma-common.conf.js'); karma.set(common.mixin_common_opts(karma, { // base path, that will be used to resolve files and exclude - basePath: '', + basePath: '../', // list of files / patterns to load in the browser files: [ - 'node_modules/chai/chai.js', - 'node_modules/chai-spies/chai-spies.js', - 'src/boot.js', - 'src/PointerEvent.js', - 'src/pointermap.js', - 'src/sidetable.js', - 'src/dispatcher.js', - 'src/installer.js', - 'src/mouse.js', - 'src/touch.js', - 'src/ms.js', - 'src/platform-events.js', - 'src/capture.js', - 'tests/setup.js', - 'tests/karma-setup.js', - 'tests/!(setup|karma-setup).js' + 'PointerEvents/node_modules/chai/chai.js', + 'PointerEvents/node_modules/chai-spies/chai-spies.js', + 'WeakMap/weakmap.js', + 'PointerEvents/src/boot.js', + 'PointerEvents/src/PointerEvent.js', + 'PointerEvents/src/pointermap.js', + 'PointerEvents/src/dispatcher.js', + 'PointerEvents/src/installer.js', + 'PointerEvents/src/mouse.js', + 'PointerEvents/src/touch.js', + 'PointerEvents/src/ms.js', + 'PointerEvents/src/platform-events.js', + 'PointerEvents/src/capture.js', + 'PointerEvents/tests/setup.js', + 'PointerEvents/tests/karma-setup.js', + 'PointerEvents/tests/!(setup|karma-setup).js' ], })); }; From 46ea56932f0b7b4b2752f489be71e4061e191454 Mon Sep 17 00:00:00 2001 From: Daniel Freedman Date: Wed, 9 Oct 2013 16:48:16 -0700 Subject: [PATCH 4/4] Put pointermap back, remove sidetable reference --- pointerevents.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pointerevents.js b/pointerevents.js index 8489ab4a..e5995cca 100644 --- a/pointerevents.js +++ b/pointerevents.js @@ -11,7 +11,7 @@ 'src/boot.js', 'src/touch-action.js', 'src/PointerEvent.js', - 'src/sidetable.js', + 'src/pointermap.js', 'src/dispatcher.js', 'src/installer.js', 'src/mouse.js',