]> git.proxmox.com Git - mirror_novnc.git/commitdiff
Move browser checks to browser.js
authorJuanjo Diaz <juanjo.diaz@intopalo.com>
Tue, 30 Jan 2018 16:35:47 +0000 (08:35 -0800)
committerJuanjo Diaz <juanjo.diaz@intopalo.com>
Tue, 30 Jan 2018 16:35:47 +0000 (08:35 -0800)
app/ui.js
core/input/keyboard.js
core/input/mouse.js
core/input/util.js
core/rfb.js
core/util/browser.js [new file with mode: 0644]
core/util/browsers.js [deleted file]
tests/test.helper.js
tests/test.keyboard.js

index 5fc1ce459cac4ef7ac7a7e6c9ea1a9982c38dd8d..84cc62c76cc24b3ceee5d907052c634ff95d47f5 100644 (file)
--- a/app/ui.js
+++ b/app/ui.js
@@ -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";
index 464d1ce8c49c441bd81b867950450c8cf64448b0..d610eb239de0ad6fd014252525092a750bede628 100644 (file)
@@ -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
@@ -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 =====
 
@@ -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) &&
@@ -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
@@ -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;
@@ -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);
@@ -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
@@ -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;
index eaf908adcb1f6ac09f11db0f23590cb1c8bdb99b..c59af0051a2d7ae244289ffc3fb52c916e3f6a7d 100644 (file)
@@ -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
index 78a829c8bf4c6a2a43fc75c5da793cf792cb733c..96a5a233a839885c343634f05b6ca0f206735527 100644 (file)
@@ -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;
         }
     }
index 8f9a54690e4a7805b187b70aba410e2c5b073a9f..39a3bce50078b914cd52f027491b3ef5260b7353 100644 (file)
@@ -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";
@@ -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);
         }
diff --git a/core/util/browser.js b/core/util/browser.js
new file mode 100644 (file)
index 0000000..ab0e7ee
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ * noVNC: HTML5 VNC client
+ * Copyright (C) 2012 Joel Martin
+ * Licensed under MPL 2.0 (see LICENSE.txt)
+ *
+ * See README.md for usage and integration instructions.
+ */
+
+import * as Log from './logging.js';
+
+// Touch detection
+export var isTouchDevice = ('ontouchstart' in document.documentElement) ||
+                                 // requried for Chrome debugger
+                                 (document.ontouchstart !== undefined) ||
+                                 // required for MS Surface
+                                 (navigator.maxTouchPoints > 0) ||
+                                 (navigator.msMaxTouchPoints > 0);
+window.addEventListener('touchstart', function onFirstTouch() {
+    isTouchDevice = true;
+    window.removeEventListener('touchstart', onFirstTouch, false);
+}, false);
+
+var _cursor_uris_supported = null;
+
+export function supportsCursorURIs () {
+    if (_cursor_uris_supported === null) {
+        try {
+            var target = document.createElement('canvas');
+            target.style.cursor = 'url("data:image/x-icon;base64,AAACAAEACAgAAAIAAgA4AQAAFgAAACgAAAAIAAAAEAAAAAEAIAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAD/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////AAAAAAAAAAAAAAAAAAAAAA==") 2 2, default';
+
+            if (target.style.cursor) {
+                Log.Info("Data URI scheme cursor supported");
+                _cursor_uris_supported = true;
+            } else {
+                Log.Warn("Data URI scheme cursor not supported");
+                _cursor_uris_supported = false;
+            }
+        } catch (exc) {
+            Log.Error("Data URI scheme cursor test exception: " + exc);
+            _cursor_uris_supported = false;
+        }
+    }
+
+    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/core/util/browsers.js b/core/util/browsers.js
deleted file mode 100644 (file)
index 50f5986..0000000
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- * noVNC: HTML5 VNC client
- * Copyright (C) 2012 Joel Martin
- * Licensed under MPL 2.0 (see LICENSE.txt)
- *
- * See README.md for usage and integration instructions.
- */
-
-import * as Log from './logging.js';
-
-// Touch detection
-export var isTouchDevice = ('ontouchstart' in document.documentElement) ||
-                                 // requried for Chrome debugger
-                                 (document.ontouchstart !== undefined) ||
-                                 // required for MS Surface
-                                 (navigator.maxTouchPoints > 0) ||
-                                 (navigator.msMaxTouchPoints > 0);
-window.addEventListener('touchstart', function onFirstTouch() {
-    isTouchDevice = true;
-    window.removeEventListener('touchstart', onFirstTouch, false);
-}, false);
-
-var _cursor_uris_supported = null;
-
-export function browserSupportsCursorURIs () {
-    if (_cursor_uris_supported === null) {
-        try {
-            var target = document.createElement('canvas');
-            target.style.cursor = 'url("data:image/x-icon;base64,AAACAAEACAgAAAIAAgA4AQAAFgAAACgAAAAIAAAAEAAAAAEAIAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAD/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////AAAAAAAAAAAAAAAAAAAAAA==") 2 2, default';
-
-            if (target.style.cursor) {
-                Log.Info("Data URI scheme cursor supported");
-                _cursor_uris_supported = true;
-            } else {
-                Log.Warn("Data URI scheme cursor not supported");
-                _cursor_uris_supported = false;
-            }
-        } catch (exc) {
-            Log.Error("Data URI scheme cursor test exception: " + exc);
-            _cursor_uris_supported = false;
-        }
-    }
-
-    return _cursor_uris_supported;
-};
index aa64e9550edc3f6c3c8694fa36d5be0ca837c4ae..48e1def8f357b43fbda35aae9c1efe107b4449c9 100644 (file)
@@ -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);
             });
index 513d797f99ab1d2119d2be923b15e81d287bbdc2..54a23e67abe8c45c436cac408bb927cfd4e98223 100644 (file)
@@ -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() {
@@ -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);
@@ -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) {
@@ -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, {});
@@ -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) {
@@ -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) {
@@ -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);