Skip to content

Commit

Permalink
Core: Throw error on non-supported input types. Fixes #754
Browse files Browse the repository at this point in the history
This is a breaking change since before the library would
convert number & email types to a text type input
  • Loading branch information
Mottie committed Aug 7, 2019
1 parent 1ca038e commit cf31565
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
12 changes: 12 additions & 0 deletions js/jquery.keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;

Expand Down
30 changes: 27 additions & 3 deletions testing/testing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<input type="number"><input type="CoLoR">');
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
************************************************/
Expand Down Expand Up @@ -100,10 +125,10 @@ jQuery(function($){
************************************************/
QUnit.test( 'make preview', function( assert ) {
var done = assert.async();
assert.expect(3);
assert.expect(2);

$('#test')
.html('<input id="keyboard_test" type="number" data-test="zzz" data-this-is-a-fake="attr" aria-haspopup="true">')
.html('<input id="keyboard_test" type="text" data-test="zzz" data-this-is-a-fake="attr" aria-haspopup="true">')
.find('input')
.keyboard({
layout : 'qwerty',
Expand All @@ -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();
Expand Down

0 comments on commit cf31565

Please sign in to comment.