Move browser checks to browser.js

This commit is contained in:
Juanjo Diaz 2018-01-30 08:35:47 -08:00
parent 609a3fac74
commit 59ef29163e
8 changed files with 50 additions and 65 deletions

View File

@ -13,7 +13,7 @@
import * as Log from '../core/util/logging.js'; import * as Log from '../core/util/logging.js';
import _, { l10n } from './localization.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 { setCapture, getPointerEvent } from '../core/util/events.js';
import KeyTable from "../core/input/keysym.js"; import KeyTable from "../core/input/keysym.js";
import keysyms from "../core/input/keysymdef.js"; import keysyms from "../core/input/keysymdef.js";

View File

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

View File

@ -9,7 +9,7 @@
/*global window, Util */ /*global window, Util */
import * as Log from '../util/logging.js'; 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'; import { setCapture, stopEvent, getPointerEvent } from '../util/events.js';
var WHEEL_STEP = 10; // Delta threshold for a mouse wheel step var WHEEL_STEP = 10; // Delta threshold for a mouse wheel step

View File

@ -3,16 +3,7 @@ import keysyms from "./keysymdef.js";
import vkeys from "./vkeys.js"; import vkeys from "./vkeys.js";
import fixedkeys from "./fixedkeys.js"; import fixedkeys from "./fixedkeys.js";
import DOMKeyTable from "./domkeytable.js"; import DOMKeyTable from "./domkeytable.js";
import * as browser from "../util/browser.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);
}
// Get 'KeyboardEvent.code', handling legacy browsers // Get 'KeyboardEvent.code', handling legacy browsers
export function getKeycode(evt){ export function getKeycode(evt){
@ -37,7 +28,7 @@ export function getKeycode(evt){
var code = vkeys[evt.keyCode]; var code = vkeys[evt.keyCode];
// macOS has messed up this code for some reason // macOS has messed up this code for some reason
if (isMac() && (code === 'ContextMenu')) { if (browser.isMac() && (code === 'ContextMenu')) {
code = 'MetaRight'; code = 'MetaRight';
} }
@ -114,7 +105,7 @@ export function getKey(evt) {
// IE and Edge have broken handling of AltGraph so we cannot // IE and Edge have broken handling of AltGraph so we cannot
// trust them for printable characters // trust them for printable characters
if ((evt.key.length !== 1) || (!isIE() && !isEdge())) { if ((evt.key.length !== 1) || (!browser.isIE() && !browser.isEdge())) {
return evt.key; return evt.key;
} }
} }

View File

@ -12,7 +12,7 @@
import * as Log from './util/logging.js'; import * as Log from './util/logging.js';
import { decodeUTF8 } from './util/strings.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 EventTargetMixin from './util/eventtarget.js';
import Display from "./display.js"; import Display from "./display.js";
import Keyboard from "./input/keyboard.js"; import Keyboard from "./input/keyboard.js";
@ -1275,7 +1275,7 @@ RFB.prototype = {
encs.push(encodings.pseudoEncodingFence); encs.push(encodings.pseudoEncodingFence);
encs.push(encodings.pseudoEncodingContinuousUpdates); encs.push(encodings.pseudoEncodingContinuousUpdates);
if (browserSupportsCursorURIs() && if (supportsCursorURIs() &&
!isTouchDevice && this._fb_depth == 24) { !isTouchDevice && this._fb_depth == 24) {
encs.push(encodings.pseudoEncodingCursor); encs.push(encodings.pseudoEncodingCursor);
} }

View File

@ -22,7 +22,7 @@ window.addEventListener('touchstart', function onFirstTouch() {
var _cursor_uris_supported = null; var _cursor_uris_supported = null;
export function browserSupportsCursorURIs () { export function supportsCursorURIs () {
if (_cursor_uris_supported === null) { if (_cursor_uris_supported === null) {
try { try {
var target = document.createElement('canvas'); var target = document.createElement('canvas');
@ -43,3 +43,27 @@ export function browserSupportsCursorURIs () {
return _cursor_uris_supported; 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));
}

View File

@ -3,13 +3,7 @@ var expect = chai.expect;
import keysyms from '../core/input/keysymdef.js'; import keysyms from '../core/input/keysymdef.js';
import * as KeyboardUtil from "../core/input/util.js"; import * as KeyboardUtil from "../core/input/util.js";
import * as browser from '../core/util/browser.js';
function isIE() {
return navigator && !!(/trident/i).exec(navigator.userAgent);
}
function isEdge() {
return navigator && !!(/edge/i).exec(navigator.userAgent);
}
describe('Helpers', function() { describe('Helpers', function() {
"use strict"; "use strict";
@ -107,7 +101,7 @@ describe('Helpers', function() {
describe('getKey', function() { describe('getKey', function() {
it('should prefer key', 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'); expect(KeyboardUtil.getKey({key: 'a', charCode: 'Š'.charCodeAt(), keyCode: 0x42, which: 0x43})).to.be.equal('a');
}); });
it('should map legacy values', function() { it('should map legacy values', function() {
@ -210,7 +204,7 @@ describe('Helpers', function() {
describe('Numpad', function() { describe('Numpad', function() {
it('should handle Numpad numbers', 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: 'Digit5', key: '5', location: 0})).to.be.equal(0x0035);
expect(KeyboardUtil.getKeysym({code: 'Numpad5', key: '5', location: 3})).to.be.equal(0xFFB5); expect(KeyboardUtil.getKeysym({code: 'Numpad5', key: '5', location: 3})).to.be.equal(0xFFB5);
}); });
@ -221,7 +215,7 @@ describe('Helpers', function() {
expect(KeyboardUtil.getKeysym({code: 'NumpadDecimal', key: 'Delete', location: 3})).to.be.equal(0xFF9F); expect(KeyboardUtil.getKeysym({code: 'NumpadDecimal', key: 'Delete', location: 3})).to.be.equal(0xFF9F);
}); });
it('should handle Numpad Decimal key', function() { 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(0xFFAE);
expect(KeyboardUtil.getKeysym({code: 'NumpadDecimal', key: ',', location: 3})).to.be.equal(0xFFAC); expect(KeyboardUtil.getKeysym({code: 'NumpadDecimal', key: ',', location: 3})).to.be.equal(0xFFAC);
}); });

View File

@ -4,13 +4,7 @@ var expect = chai.expect;
import sinon from '../vendor/sinon.js'; import sinon from '../vendor/sinon.js';
import Keyboard from '../core/input/keyboard.js'; import Keyboard from '../core/input/keyboard.js';
import * as browser from '../core/util/browser.js';
function isIE() {
return navigator && !!(/trident/i).exec(navigator.userAgent);
}
function isEdge() {
return navigator && !!(/edge/i).exec(navigator.userAgent);
}
/* jshint newcap: false, expr: true */ /* jshint newcap: false, expr: true */
describe('Key Event Handling', function() { describe('Key Event Handling', function() {
@ -30,7 +24,7 @@ describe('Key Event Handling', function() {
describe('Decode Keyboard Events', function() { describe('Decode Keyboard Events', function() {
it('should decode keydown events', function(done) { it('should decode keydown events', function(done) {
if (isIE() || isEdge()) this.skip(); if (browser.isIE() || browser.isEdge()) this.skip();
var kbd = new Keyboard(document); var kbd = new Keyboard(document);
kbd.onkeyevent = function(keysym, code, down) { kbd.onkeyevent = function(keysym, code, down) {
expect(keysym).to.be.equal(0x61); expect(keysym).to.be.equal(0x61);
@ -41,7 +35,7 @@ describe('Key Event Handling', function() {
kbd._handleKeyDown(keyevent('keydown', {code: 'KeyA', key: 'a'})); kbd._handleKeyDown(keyevent('keydown', {code: 'KeyA', key: 'a'}));
}); });
it('should decode keyup events', function(done) { it('should decode keyup events', function(done) {
if (isIE() || isEdge()) this.skip(); if (browser.isIE() || browser.isEdge()) this.skip();
var calls = 0; var calls = 0;
var kbd = new Keyboard(document); var kbd = new Keyboard(document);
kbd.onkeyevent = function(keysym, code, down) { kbd.onkeyevent = function(keysym, code, down) {
@ -136,7 +130,7 @@ describe('Key Event Handling', function() {
describe('suppress the right events at the right time', function() { describe('suppress the right events at the right time', function() {
beforeEach(function () { beforeEach(function () {
if (isIE() || isEdge()) this.skip(); if (browser.isIE() || browser.isEdge()) this.skip();
}); });
it('should suppress anything with a valid key', function() { it('should suppress anything with a valid key', function() {
var kbd = new Keyboard(document, {}); var kbd = new Keyboard(document, {});
@ -166,7 +160,7 @@ describe('Key Event Handling', function() {
describe('Fake keyup', function() { describe('Fake keyup', function() {
it('should fake keyup events for virtual keyboards', function(done) { 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 count = 0;
var kbd = new Keyboard(document); var kbd = new Keyboard(document);
kbd.onkeyevent = function(keysym, code, down) { kbd.onkeyevent = function(keysym, code, down) {
@ -213,7 +207,7 @@ describe('Key Event Handling', function() {
}); });
it('should fake keyup events on iOS', function(done) { 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 count = 0;
var kbd = new Keyboard(document); var kbd = new Keyboard(document);
kbd.onkeyevent = function(keysym, code, down) { kbd.onkeyevent = function(keysym, code, down) {
@ -237,7 +231,7 @@ describe('Key Event Handling', function() {
describe('Track Key State', function() { describe('Track Key State', function() {
beforeEach(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) { it('should send release using the same keysym as the press', function(done) {
var kbd = new Keyboard(document); var kbd = new Keyboard(document);