forked from select2/select2
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support the autocomplete attribute being set on the original <select> (…
…select2#5839) * Add test to ensure search autocomplete attribute is set This adds a basic test to make sure that the `autocomplete` attribute on the search box is set based on the value of the `autocomplete` option that is passed in to Select2. This alows people to set the `autocomplete` attribute on their `<select>`, which is valid according to the spec, and have it copy over to the search box of their Select2 instance. * Add tests for options set by HTML atttributes This adds some missing tests for the disabled and multiple attributes and how they can be used to set the corresponding options within Select2. This also adds tests for the new autocomplete option that ensures it is properly synchronized based on the attribute being set. * Add support for the autocomplete attribute This will now copy the `autocomplete` attribute (based on the current state of the `autocomplete` DOM property) from the `<select>` to any search boxes that are used by Select2. This is necessary to both handle cases where people do not want the autocomplete disabled, as well as handling cases where people are looking to set a custom autocomplete which will assist users when searching. This still maintains backwards compatibility with the `autocomplete` option being set to `off` by default. It also allows the `autocomplete` option to be set through the standard Select2 options to override the `autocomplete` attribute if it is present. This does not follow the precedent set by the `disabled` and `multiple` options where the value is synchronized back to the original `<select>` when it is overridden by the options. This is because it would break backwards compatibility because the default state of the `autocomplete` property is a blank string ('') instead of `off` which is what Select2 uses. * Skip autocomplete attr test when not supported Not all browsers, including older version of PhantomJS, support the `autocomplete` attribute when it is set on a `<select>` element.
- Loading branch information
1 parent
f30702d
commit e41cb2e
Showing
10 changed files
with
223 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
module('Dropdown - Search'); | ||
|
||
var Dropdown = require('select2/dropdown'); | ||
var DropdownSearch = Utils.Decorate( | ||
Dropdown, | ||
require('select2/dropdown/search') | ||
); | ||
|
||
var $ = require('jquery'); | ||
var Options = require('select2/options'); | ||
var Utils = require('select2/utils'); | ||
|
||
var options = new Options({}); | ||
|
||
test('search box defaults autocomplete to off', function (assert) { | ||
var $select = $('#qunit-fixture .single'); | ||
|
||
var dropdown = new DropdownSearch($select, options); | ||
var $dropdown = dropdown.render(); | ||
|
||
var container = new MockContainer(); | ||
dropdown.bind(container, $('<span></span>')); | ||
|
||
assert.equal( | ||
$dropdown.find('input').attr('autocomplete'), | ||
'off', | ||
'The search box has autocomplete disabled' | ||
); | ||
}); | ||
|
||
test('search box sets autocomplete from options', function (assert) { | ||
var $select = $('#qunit-fixture .single'); | ||
|
||
var autocompleteOptions = new Options({ | ||
autocomplete: 'country-name' | ||
}); | ||
|
||
var dropdown = new DropdownSearch($select, autocompleteOptions); | ||
var $dropdown = dropdown.render(); | ||
|
||
var container = new MockContainer(); | ||
dropdown.bind(container, $('<span></span>')); | ||
|
||
assert.equal( | ||
$dropdown.find('input').attr('autocomplete'), | ||
'country-name', | ||
'The search box sets the right autocomplete attribute' | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
module('Options - Copying from element'); | ||
|
||
var $ = require('jquery'); | ||
|
||
var Options = require('select2/options'); | ||
|
||
test('copies disabled attribute when set', function (assert) { | ||
var $test = $('<select disabled></select>'); | ||
|
||
var options = new Options({}, $test); | ||
|
||
assert.ok(options.get('disabled')); | ||
}); | ||
|
||
test('does not copy disabled attribute when not set', function (assert) { | ||
var $test = $('<select></select>'); | ||
|
||
var options = new Options({}, $test); | ||
|
||
assert.ok(!options.get('disabled')); | ||
}); | ||
|
||
test('disabled attribute does not override disable option', function (assert) { | ||
var $test = $('<select disabled></select>'); | ||
|
||
var options = new Options({ | ||
disabled: false | ||
}, $test); | ||
|
||
assert.ok(!options.get('disabled')); | ||
}); | ||
|
||
test('disabled option is synchronized back', function (assert) { | ||
var $test = $('<select disabled></select>'); | ||
|
||
assert.ok($test.prop('disabled')); | ||
|
||
var options = new Options({ | ||
disabled: false | ||
}, $test); | ||
|
||
assert.ok(!$test.prop('disabled')); | ||
}); | ||
|
||
test('copies multiple attribute when set', function (assert) { | ||
var $test = $('<select multiple></select>'); | ||
|
||
var options = new Options({}, $test); | ||
|
||
assert.ok(options.get('multiple')); | ||
}); | ||
|
||
test('does not copy multiple attribute when not set', function (assert) { | ||
var $test = $('<select></select>'); | ||
|
||
var options = new Options({}, $test); | ||
|
||
assert.ok(!options.get('multiple')); | ||
}); | ||
|
||
test('multiple attribute does not override multiple option', function (assert) { | ||
var $test = $('<select multiple></select>'); | ||
|
||
var options = new Options({ | ||
multiple: false | ||
}, $test); | ||
|
||
assert.ok(!options.get('multiple')); | ||
}); | ||
|
||
test('multiple option is synchronized back', function (assert) { | ||
var $test = $('<select multiple></select>'); | ||
|
||
assert.ok($test.prop('multiple')); | ||
|
||
var options = new Options({ | ||
multiple: false | ||
}, $test); | ||
|
||
assert.ok(!$test.prop('multiple')); | ||
}); | ||
|
||
test('copies autocomplete attribute when set', function (assert) { | ||
var $test = $('<select autocomplete="country-name"></select>'); | ||
|
||
if ($test.prop('autocomplete') !== 'country-name') { | ||
// Browser does not support the autocomplete attribute on a select | ||
assert.ok(true); | ||
return; | ||
} | ||
|
||
var options = new Options({}, $test); | ||
|
||
assert.equal(options.get('autocomplete'), 'country-name'); | ||
}); | ||
|
||
test('does not copy autocomplete attribute when not set', function (assert) { | ||
var $test = $('<select></select>'); | ||
|
||
var options = new Options({}, $test); | ||
|
||
assert.equal(options.get('autocomplete'), 'off'); | ||
}); | ||
|
||
test('autocomplete attribute does not override option', function (assert) { | ||
var $test = $('<select autocomplete="country-name"></select>'); | ||
|
||
var options = new Options({ | ||
autocomplete: 'organization' | ||
}, $test); | ||
|
||
assert.ok(options.get('autocomplete'), 'organization'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters