From cf315658db22cc9842572496e300fa0bc576f285 Mon Sep 17 00:00:00 2001 From: Rob Garrison Date: Wed, 7 Aug 2019 05:26:22 -0500 Subject: [PATCH] Core: Throw error on non-supported input types. Fixes #754 This is a breaking change since before the library would convert number & email types to a text type input --- js/jquery.keyboard.js | 12 ++++++++++++ testing/testing.js | 30 +++++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/js/jquery.keyboard.js b/js/jquery.keyboard.js index dcfdc070..b19e38ae 100644 --- a/js/jquery.keyboard.js +++ b/js/jquery.keyboard.js @@ -56,6 +56,10 @@ http://www.opensource.org/licenses/mit-license.php var k, position, tmp, kbcss = $keyboard.css, kbevents = $keyboard.events; + if ($.inArray((base.el.type || '').toLowerCase(), $keyboard.supportedInputTypes) < 0) { + throw new TypeError('Input of type "' + base.el.type + '" is not supported; use type text, search, URL, tel or password'); + } + base.settings = options || {}; // shallow copy position to prevent performance issues; see #357 if (options && options.position) { @@ -3134,6 +3138,14 @@ http://www.opensource.org/licenses/mit-license.php }; + $keyboard.supportedInputTypes = [ + 'text', + 'search', + 'url', + 'tel', + 'password' + ]; + // for checking combos $keyboard.comboRegex = /([`\'~\^\"ao])([a-z])/mig; diff --git a/testing/testing.js b/testing/testing.js index 00d58afc..92ef5774 100644 --- a/testing/testing.js +++ b/testing/testing.js @@ -33,6 +33,31 @@ jQuery(function($){ runTests = function( kb ) { QUnit.module('core'); + /************************************************ + Throws on unsupported input types + ************************************************/ + QUnit.test('throws on unsupported input types', function(assert) { + $('#test').html(''); + assert.throws( + function() { + $('#test').find('input[type=number]').keyboard(); + }, + function(err) { + return err.message === 'Input of type "number" is not supported; use type text, search, URL, tel or password'; + }, + 'Throw on number type input' + ); + assert.throws( + function() { + $('#test').find('input[type=color]').keyboard(); + }, + function(err) { + return err.message === 'Input of type "color" is not supported; use type text, search, URL, tel or password'; + }, + 'Throw on color type input' + ); + }); + /************************************************ processName ************************************************/ @@ -100,10 +125,10 @@ jQuery(function($){ ************************************************/ QUnit.test( 'make preview', function( assert ) { var done = assert.async(); - assert.expect(3); + assert.expect(2); $('#test') - .html('') + .html('') .find('input') .keyboard({ layout : 'qwerty', @@ -113,7 +138,6 @@ jQuery(function($){ setTimeout(function(){ var el = keyboard.preview, dataRemoved = typeof el['data-test'] === 'undefined' && typeof el['data-this-is-a-fake'] === 'undefined'; - assert.equal( el.type, 'text', 'Preview type changed from number to text' ); assert.equal( typeof el['aria-haspopup'], 'undefined', 'Preview aria-haspopup removed' ); assert.equal( dataRemoved, true, 'Preview data-attributes removed' ); keyboard.destroy();