Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Make sure pressure is correctly inferred from MouseEvent button[s] state
Browse files Browse the repository at this point in the history
Add a test as well
Fixes #120
  • Loading branch information
dfreedm committed Jan 24, 2014
1 parent 8045762 commit f7dd105
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/PointerEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@
// is to call initMouseEvent with a buttonArg value of -1.
//
// This is fixed with DOM Level 4's use of buttons
var buttons = inDict.buttons;
if (buttons === undefined) {
var buttons;
if (inDict.buttons || HAS_BUTTONS) {
buttons = inDict.buttons;
} else {
switch (inDict.which) {
case 1: buttons = 1; break;
case 2: buttons = 4; break;
Expand Down
2 changes: 1 addition & 1 deletion src/dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
0,
null,
// DOM Level 3
undefined,
0,
// PointerEvent
0,
0,
Expand Down
19 changes: 17 additions & 2 deletions tests/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

suite('Constructor', function() {
test('PointerEvent extends MouseEvent', function() {
var p = new PointerEvent;
var p = new PointerEvent();
expect(p).to.be.an.instanceof(MouseEvent);
});

Expand Down Expand Up @@ -63,12 +63,27 @@ suite('Constructor', function() {
'hwTimestamp',
'isPrimary'
];
var p = new PointerEvent;
var p = new PointerEvent();
var v;
props.forEach(function(k) {
v = p[k];
p[k] = NaN;
expect(p[k]).to.equal(v);
});
});

test('Button properties are used for pressure', function() {
var p = new PointerEvent('foo', {buttons: 1});
expect(p.pressure).to.equal(0.5);
// test for buttons property
var m = document.createEvent('MouseEvent');
m.initEvent('test', false, false, null, null, 0, 0, 0, 0, false, false, false, false, 0, null);
// only run this test if <MouseEvent>.buttons is not supported
if (m.buttons === undefined) {
p = new PointerEvent('baz', {button: 0, which: 1});
expect(p.pressure).to.equal(0.5);
}
p = new PointerEvent('bar');
expect(p.pressure).to.equal(0);
});
});

0 comments on commit f7dd105

Please sign in to comment.