diff options
author | Solly Ross <directxman12+github@gmail.com> | 2018-02-01 10:33:28 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-01 10:33:28 -0500 |
commit | a4d51bd22048df40963c999903e5156f343df293 (patch) | |
tree | 70f98cfd5ba34b9ecc9568fe2a836c79b4e3dff8 | |
parent | 690d07baba325ce7e362181d0d544d146938963a (diff) | |
parent | 59ef29163e007cd9d7416616b50a489359a17826 (diff) | |
download | novnc-a4d51bd22048df40963c999903e5156f343df293.tar.gz |
Merge pull request #1009 from juanjoDiaz/move_browser_checks
Move browser checks to browser.js
-rw-r--r-- | app/ui.js | 2 | ||||
-rw-r--r-- | core/input/keyboard.js | 32 | ||||
-rw-r--r-- | core/input/mouse.js | 2 | ||||
-rw-r--r-- | core/input/util.js | 15 | ||||
-rw-r--r-- | core/rfb.js | 4 | ||||
-rw-r--r-- | core/util/browser.js (renamed from core/util/browsers.js) | 26 | ||||
-rw-r--r-- | tests/test.helper.js | 14 | ||||
-rw-r--r-- | tests/test.keyboard.js | 20 |
8 files changed, 50 insertions, 65 deletions
@@ -10,7 +10,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"; diff --git a/core/input/keyboard.js b/core/input/keyboard.js index 05fbbfb..4e8dc0d 100644 --- a/core/input/keyboard.js +++ b/core/input/keyboard.js @@ -9,6 +9,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 @@ -30,25 +31,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 ===== @@ -65,7 +47,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) && @@ -131,7 +113,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 @@ -148,7 +130,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; @@ -175,7 +157,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); @@ -186,7 +168,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 @@ -274,7 +256,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; diff --git a/core/input/mouse.js b/core/input/mouse.js index 8f181fc..524b065 100644 --- a/core/input/mouse.js +++ b/core/input/mouse.js @@ -6,7 +6,7 @@ */ 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 diff --git a/core/input/util.js b/core/input/util.js index 78a829c..96a5a23 100644 --- a/core/input/util.js +++ b/core/input/util.js @@ -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){ @@ -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'; } @@ -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; } } diff --git a/core/rfb.js b/core/rfb.js index d1921a1..7c4e0c9 100644 --- a/core/rfb.js +++ b/core/rfb.js @@ -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"; @@ -1274,7 +1274,7 @@ RFB.prototype = { encs.push(encodings.pseudoEncodingFence); encs.push(encodings.pseudoEncodingContinuousUpdates); - if (browserSupportsCursorURIs() && + if (supportsCursorURIs() && !isTouchDevice && this._fb_depth == 24) { encs.push(encodings.pseudoEncodingCursor); } diff --git a/core/util/browsers.js b/core/util/browser.js index 50f5986..ab0e7ee 100644 --- a/core/util/browsers.js +++ b/core/util/browser.js @@ -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'); @@ -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)); +} + diff --git a/tests/test.helper.js b/tests/test.helper.js index aa64e95..48e1def 100644 --- a/tests/test.helper.js +++ b/tests/test.helper.js @@ -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"; @@ -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() { @@ -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); }); @@ -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); }); diff --git a/tests/test.keyboard.js b/tests/test.keyboard.js index cb273c3..1c78fd6 100644 --- a/tests/test.keyboard.js +++ b/tests/test.keyboard.js @@ -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'; describe('Key Event Handling', function() { "use strict"; @@ -29,7 +23,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); @@ -40,7 +34,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) { @@ -135,7 +129,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, {}); @@ -165,7 +159,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) { @@ -212,7 +206,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) { @@ -236,7 +230,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); |