diff --git a/lib/restricted-input.js b/lib/restricted-input.js index 0ae94e8..6bea59d 100644 --- a/lib/restricted-input.js +++ b/lib/restricted-input.js @@ -7,6 +7,7 @@ var IosStrategy = require('./strategies/ios'); var AndroidChromeStrategy = require('./strategies/android-chrome'); var IE9Strategy = require('./strategies/ie9'); var BaseStrategy = require('./strategies/base'); +var NoopStrategy = require('./strategies/noop'); /** * Instances of this class can be used to modify the formatter for an input @@ -29,12 +30,7 @@ function RestrictedInput(options) { // SamsungBrowser contains 'Chrome' in userAgent, so check it first if (device.isSamsungBrowser()) { // Disable formatting due to various digits being dropped - this.strategy = { - getUnformattedValue: function () { - return options.element.value; - }, - setPattern: function () {} - }; + this.strategy = new NoopStrategy(options); } else if (device.isIos()) { this.strategy = new IosStrategy(options); } else if (device.isAndroidChrome()) { diff --git a/lib/strategies/noop.js b/lib/strategies/noop.js new file mode 100644 index 0000000..e136799 --- /dev/null +++ b/lib/strategies/noop.js @@ -0,0 +1,15 @@ +'use strict'; + +function NoopStrategy(options) { + this.inputElement = options.element; +} + +NoopStrategy.prototype.getUnformattedValue = function () { + return this.inputElement.value; +}; + +NoopStrategy.prototype.setPattern = function () {}; + +NoopStrategy.prototype.pasteEventHandler = function () {}; + +module.exports = NoopStrategy; diff --git a/test/unit/lib/restricted-input.js b/test/unit/lib/restricted-input.js index 1dac336..bfeac09 100644 --- a/test/unit/lib/restricted-input.js +++ b/test/unit/lib/restricted-input.js @@ -5,6 +5,7 @@ var BaseStrategy = require('../../../lib/strategies/base'); var IosStrategy = require('../../../lib/strategies/ios'); var IE9Strategy = require('../../../lib/strategies/ie9'); var AndroidChromeStrategy = require('../../../lib/strategies/android-chrome'); +var NoopStrategy = require('../../../lib/strategies/noop'); var device = require('../../../lib/device'); describe('RestrictedInput', function () { @@ -78,6 +79,19 @@ describe('RestrictedInput', function () { expect(ri.strategy).to.be.an.instanceof(IE9Strategy); }); + + it('uses NoopStrategy for Samsung browser', function () { + var ri; + + global.sandbox.stub(device, 'isSamsungBrowser').returns(true); + + ri = new RestrictedInput({ + element: document.createElement('input'), + pattern: '{{a}}' + }); + + expect(ri.strategy).to.be.an.instanceof(NoopStrategy); + }); }); describe('getUnformattedValue()', function () { diff --git a/test/unit/lib/strategies/noop.js b/test/unit/lib/strategies/noop.js new file mode 100644 index 0000000..0e66bd3 --- /dev/null +++ b/test/unit/lib/strategies/noop.js @@ -0,0 +1,19 @@ +'use strict'; + +var NoopStrategy = require('../../../../lib/strategies/noop'); +var BaseStrategy = require('../../../../lib/strategies/base'); + +describe('Noop Strategy', function () { + it('has the same public methods as Base Strategy', function () { + var noopPublicMethods = Object.getOwnPropertyNames(NoopStrategy.prototype).filter(function (property) { + return typeof NoopStrategy.prototype[property] === 'function' && property[0] !== '_'; + }); + var basePublicMethods = Object.getOwnPropertyNames(BaseStrategy.prototype).filter(function (property) { + return typeof BaseStrategy.prototype[property] === 'function' && property[0] !== '_'; + }); + + basePublicMethods.forEach(function (method) { + expect(noopPublicMethods).to.contain(method); + }); + }); +});