Kaynağa Gözat

Implemented basic env utility to detect Macintosh environment.

Aleksander Nowodzinski 9 yıl önce
ebeveyn
işleme
d0d6bb6f0e

+ 33 - 0
packages/ckeditor5-utils/src/env.js

@@ -0,0 +1,33 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* globals navigator:false */
+
+const userAgent = navigator.userAgent.toLowerCase();
+
+/**
+ * A namespace containing environment and browser information.
+ *
+ * @namespace utils.env
+ */
+export default {
+	/**
+	 * Indicates that application is running on Macintosh.
+	 *
+	 * @member {Boolean} utils.env.mac
+	 */
+	mac: isMac( userAgent )
+};
+
+/**
+ * Checks if User Agent represented by the string is running on Macintosh.
+ *
+ * @function utils.env.isMac
+ * @param {String} userAgent **Lowercase** `navigator.userAgent` string.
+ * @returns {Boolean} Whether User Agent is running on Macintosh or not.
+ */
+export function isMac( userAgent ) {
+	return userAgent.indexOf( 'macintosh' ) > -1;
+}

+ 34 - 0
packages/ckeditor5-utils/tests/env.js

@@ -0,0 +1,34 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+import env, { isMac } from '/ckeditor5/utils/env.js';
+
+describe( 'Env', () => {
+	beforeEach( () => {
+	} );
+
+	it( 'is an object', () => {
+		expect( env ).to.be.an( 'object' );
+	} );
+
+	describe( 'mac', () => {
+		it( 'is a boolean', () => {
+			expect( env.mac ).to.be.a( 'boolean' );
+		} );
+	} );
+
+	describe( 'isMac', () => {
+		it( 'returns true for macintosh UA strings', () => {
+			expect( isMac( 'macintosh' ) ).to.be.true;
+			expect( isMac( 'foo macintosh bar' ) ).to.be.true;
+		} );
+
+		it( 'returns false for non–macintosh UA strings', () => {
+			expect( isMac( '' ) ).to.be.false;
+			expect( isMac( 'mac' ) ).to.be.false;
+			expect( isMac( 'foo' ) ).to.be.false;
+		} );
+	} );
+} );