[go: nahoru, domu]

Skip to content

Commit

Permalink
Move browser checks to browser.js
Browse files Browse the repository at this point in the history
  • Loading branch information
juanjoDiaz committed Jan 30, 2018
1 parent 609a3fa commit 59ef291
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 65 deletions.
2 changes: 1 addition & 1 deletion app/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import * as Log from '../core/util/logging.js';
import _, { l10n } from './localization.js';
import { isTouchDevice } from '../core/util/browsers.js';
import { isTouchDevice } from '../core/util/browser.js';
import { setCapture, getPointerEvent } from '../core/util/events.js';
import KeyTable from "../core/input/keysym.js";
import keysyms from "../core/input/keysymdef.js";
Expand Down
32 changes: 7 additions & 25 deletions core/input/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import * as Log from '../util/logging.js';
import { stopEvent } from '../util/events.js';
import * as KeyboardUtil from "./util.js";
import KeyTable from "./keysym.js";
import * as browser from "../util/browser.js";

//
// Keyboard event handler
Expand All @@ -33,25 +34,6 @@ export default function Keyboard(target) {
};
};

function isMac() {
return navigator && !!(/mac/i).exec(navigator.platform);
}
function isWindows() {
return navigator && !!(/win/i).exec(navigator.platform);
}
function isIOS() {
return navigator &&
(!!(/ipad/i).exec(navigator.platform) ||
!!(/iphone/i).exec(navigator.platform) ||
!!(/ipod/i).exec(navigator.platform));
}
function isIE() {
return navigator && !!(/trident/i).exec(navigator.userAgent);
}
function isEdge() {
return navigator && !!(/edge/i).exec(navigator.userAgent);
}

Keyboard.prototype = {
// ===== EVENT HANDLERS =====

Expand All @@ -68,7 +50,7 @@ Keyboard.prototype = {
// remote systems. Fake a release of these keys until
// there is a way to detect AltGraph properly.
var fakeAltGraph = false;
if (down && isWindows()) {
if (down && browser.isWindows()) {
if ((code !== 'ControlLeft') &&
(code !== 'AltRight') &&
('ControlLeft' in this._keyDownList) &&
Expand Down Expand Up @@ -134,7 +116,7 @@ Keyboard.prototype = {
// to deal with virtual keyboards which omit key info
// (iOS omits tracking info on keyup events, which forces us to
// special treat that platform here)
if ((code === 'Unidentified') || isIOS()) {
if ((code === 'Unidentified') || browser.isIOS()) {
if (keysym) {
// If it's a virtual keyboard then it should be
// sufficient to just send press and release right
Expand All @@ -151,7 +133,7 @@ Keyboard.prototype = {
// keys around a bit to make things more sane for the remote
// server. This method is used by RealVNC and TigerVNC (and
// possibly others).
if (isMac()) {
if (browser.isMac()) {
switch (keysym) {
case KeyTable.XK_Super_L:
keysym = KeyTable.XK_Alt_L;
Expand All @@ -178,7 +160,7 @@ Keyboard.prototype = {
// state change events. That gets extra confusing for CapsLock
// which toggles on each press, but not on release. So pretend
// it was a quick press and release of the button.
if (isMac() && (code === 'CapsLock')) {
if (browser.isMac() && (code === 'CapsLock')) {
this._sendKeyEvent(KeyTable.XK_Caps_Lock, 'CapsLock', true);
this._sendKeyEvent(KeyTable.XK_Caps_Lock, 'CapsLock', false);
stopEvent(e);
Expand All @@ -189,7 +171,7 @@ Keyboard.prototype = {
// a keypress event as well
// (IE and Edge has a broken KeyboardEvent.key, so we can't
// just check for the presence of that field)
if (!keysym && (!e.key || isIE() || isEdge())) {
if (!keysym && (!e.key || browser.isIE() || browser.isEdge())) {
this._pendingKey = code;
// However we might not get a keypress event if the key
// is non-printable, which needs some special fallback
Expand Down Expand Up @@ -277,7 +259,7 @@ Keyboard.prototype = {
var code = this._getKeyCode(e);

// See comment in _handleKeyDown()
if (isMac() && (code === 'CapsLock')) {
if (browser.isMac() && (code === 'CapsLock')) {
this._sendKeyEvent(KeyTable.XK_Caps_Lock, 'CapsLock', true);
this._sendKeyEvent(KeyTable.XK_Caps_Lock, 'CapsLock', false);
return;
Expand Down
2 changes: 1 addition & 1 deletion core/input/mouse.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/*global window, Util */

import * as Log from '../util/logging.js';
import { isTouchDevice } from '../util/browsers.js';
import { isTouchDevice } from '../util/browser.js';
import { setCapture, stopEvent, getPointerEvent } from '../util/events.js';

var WHEEL_STEP = 10; // Delta threshold for a mouse wheel step
Expand Down
15 changes: 3 additions & 12 deletions core/input/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,7 @@ import keysyms from "./keysymdef.js";
import vkeys from "./vkeys.js";
import fixedkeys from "./fixedkeys.js";
import DOMKeyTable from "./domkeytable.js";

function isMac() {
return navigator && !!(/mac/i).exec(navigator.platform);
}
function isIE() {
return navigator && !!(/trident/i).exec(navigator.userAgent);
}
function isEdge() {
return navigator && !!(/edge/i).exec(navigator.userAgent);
}
import * as browser from "../util/browser.js";

// Get 'KeyboardEvent.code', handling legacy browsers
export function getKeycode(evt){
Expand All @@ -37,7 +28,7 @@ export function getKeycode(evt){
var code = vkeys[evt.keyCode];

// macOS has messed up this code for some reason
if (isMac() && (code === 'ContextMenu')) {
if (browser.isMac() && (code === 'ContextMenu')) {
code = 'MetaRight';
}

Expand Down Expand Up @@ -114,7 +105,7 @@ export function getKey(evt) {

// IE and Edge have broken handling of AltGraph so we cannot
// trust them for printable characters
if ((evt.key.length !== 1) || (!isIE() && !isEdge())) {
if ((evt.key.length !== 1) || (!browser.isIE() && !browser.isEdge())) {
return evt.key;
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/rfb.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import * as Log from './util/logging.js';
import { decodeUTF8 } from './util/strings.js';
import { browserSupportsCursorURIs, isTouchDevice } from './util/browsers.js';
import { supportsCursorURIs, isTouchDevice } from './util/browser.js';
import EventTargetMixin from './util/eventtarget.js';
import Display from "./display.js";
import Keyboard from "./input/keyboard.js";
Expand Down Expand Up @@ -1275,7 +1275,7 @@ RFB.prototype = {
encs.push(encodings.pseudoEncodingFence);
encs.push(encodings.pseudoEncodingContinuousUpdates);

if (browserSupportsCursorURIs() &&
if (supportsCursorURIs() &&
!isTouchDevice && this._fb_depth == 24) {
encs.push(encodings.pseudoEncodingCursor);
}
Expand Down
26 changes: 25 additions & 1 deletion core/util/browsers.js → core/util/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ window.addEventListener('touchstart', function onFirstTouch() {

var _cursor_uris_supported = null;

export function browserSupportsCursorURIs () {
export function supportsCursorURIs () {
if (_cursor_uris_supported === null) {
try {
var target = document.createElement('canvas');
Expand All @@ -43,3 +43,27 @@ export function browserSupportsCursorURIs () {

return _cursor_uris_supported;
};

export function isMac() {
return navigator && !!(/mac/i).exec(navigator.platform);
}

export function isIE() {
return navigator && !!(/trident/i).exec(navigator.userAgent);
}

export function isEdge() {
return navigator && !!(/edge/i).exec(navigator.userAgent);
}

export function isWindows() {
return navigator && !!(/win/i).exec(navigator.platform);
}

export function isIOS() {
return navigator &&
(!!(/ipad/i).exec(navigator.platform) ||
!!(/iphone/i).exec(navigator.platform) ||
!!(/ipod/i).exec(navigator.platform));
}

14 changes: 4 additions & 10 deletions tests/test.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@ var expect = chai.expect;

import keysyms from '../core/input/keysymdef.js';
import * as KeyboardUtil from "../core/input/util.js";

function isIE() {
return navigator && !!(/trident/i).exec(navigator.userAgent);
}
function isEdge() {
return navigator && !!(/edge/i).exec(navigator.userAgent);
}
import * as browser from '../core/util/browser.js';

describe('Helpers', function() {
"use strict";
Expand Down Expand Up @@ -107,7 +101,7 @@ describe('Helpers', function() {

describe('getKey', function() {
it('should prefer key', function() {
if (isIE() || isEdge()) this.skip();
if (browser.isIE() || browser.isEdge()) this.skip();
expect(KeyboardUtil.getKey({key: 'a', charCode: 'Š'.charCodeAt(), keyCode: 0x42, which: 0x43})).to.be.equal('a');
});
it('should map legacy values', function() {
Expand Down Expand Up @@ -210,7 +204,7 @@ describe('Helpers', function() {

describe('Numpad', function() {
it('should handle Numpad numbers', function() {
if (isIE() || isEdge()) this.skip();
if (browser.isIE() || browser.isEdge()) this.skip();
expect(KeyboardUtil.getKeysym({code: 'Digit5', key: '5', location: 0})).to.be.equal(0x0035);
expect(KeyboardUtil.getKeysym({code: 'Numpad5', key: '5', location: 3})).to.be.equal(0xFFB5);
});
Expand All @@ -221,7 +215,7 @@ describe('Helpers', function() {
expect(KeyboardUtil.getKeysym({code: 'NumpadDecimal', key: 'Delete', location: 3})).to.be.equal(0xFF9F);
});
it('should handle Numpad Decimal key', function() {
if (isIE() || isEdge()) this.skip();
if (browser.isIE() || browser.isEdge()) this.skip();
expect(KeyboardUtil.getKeysym({code: 'NumpadDecimal', key: '.', location: 3})).to.be.equal(0xFFAE);
expect(KeyboardUtil.getKeysym({code: 'NumpadDecimal', key: ',', location: 3})).to.be.equal(0xFFAC);
});
Expand Down
20 changes: 7 additions & 13 deletions tests/test.keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@ var expect = chai.expect;
import sinon from '../vendor/sinon.js';

import Keyboard from '../core/input/keyboard.js';

function isIE() {
return navigator && !!(/trident/i).exec(navigator.userAgent);
}
function isEdge() {
return navigator && !!(/edge/i).exec(navigator.userAgent);
}
import * as browser from '../core/util/browser.js';

/* jshint newcap: false, expr: true */
describe('Key Event Handling', function() {
Expand All @@ -30,7 +24,7 @@ describe('Key Event Handling', function() {

describe('Decode Keyboard Events', function() {
it('should decode keydown events', function(done) {
if (isIE() || isEdge()) this.skip();
if (browser.isIE() || browser.isEdge()) this.skip();
var kbd = new Keyboard(document);
kbd.onkeyevent = function(keysym, code, down) {
expect(keysym).to.be.equal(0x61);
Expand All @@ -41,7 +35,7 @@ describe('Key Event Handling', function() {
kbd._handleKeyDown(keyevent('keydown', {code: 'KeyA', key: 'a'}));
});
it('should decode keyup events', function(done) {
if (isIE() || isEdge()) this.skip();
if (browser.isIE() || browser.isEdge()) this.skip();
var calls = 0;
var kbd = new Keyboard(document);
kbd.onkeyevent = function(keysym, code, down) {
Expand Down Expand Up @@ -136,7 +130,7 @@ describe('Key Event Handling', function() {

describe('suppress the right events at the right time', function() {
beforeEach(function () {
if (isIE() || isEdge()) this.skip();
if (browser.isIE() || browser.isEdge()) this.skip();
});
it('should suppress anything with a valid key', function() {
var kbd = new Keyboard(document, {});
Expand Down Expand Up @@ -166,7 +160,7 @@ describe('Key Event Handling', function() {

describe('Fake keyup', function() {
it('should fake keyup events for virtual keyboards', function(done) {
if (isIE() || isEdge()) this.skip();
if (browser.isIE() || browser.isEdge()) this.skip();
var count = 0;
var kbd = new Keyboard(document);
kbd.onkeyevent = function(keysym, code, down) {
Expand Down Expand Up @@ -213,7 +207,7 @@ describe('Key Event Handling', function() {
});

it('should fake keyup events on iOS', function(done) {
if (isIE() || isEdge()) this.skip();
if (browser.isIE() || browser.isEdge()) this.skip();
var count = 0;
var kbd = new Keyboard(document);
kbd.onkeyevent = function(keysym, code, down) {
Expand All @@ -237,7 +231,7 @@ describe('Key Event Handling', function() {

describe('Track Key State', function() {
beforeEach(function () {
if (isIE() || isEdge()) this.skip();
if (browser.isIE() || browser.isEdge()) this.skip();
});
it('should send release using the same keysym as the press', function(done) {
var kbd = new Keyboard(document);
Expand Down

0 comments on commit 59ef291

Please sign in to comment.