From bd7ac9df03b008f03bb15c66fcab4682589bfc2a Mon Sep 17 00:00:00 2001 From: Kevin Brown Date: Tue, 9 Jul 2019 20:58:13 -0400 Subject: [PATCH] Results respect disabled state of `` is disabled, the `` elements held within it do not have their `disabled` property set to `true`. As a result, we needed to use the `matches` method to check if the `:disabled` state is present for the element. The `matches` method is part of the official standard, but it was not implemented under that name for a while and as a result Internet Explorer only supports it under the prefixed `msMatchesSelector` method and older versions of Webkit have it implemented as `webkitMatchesSelector`. But once we use this method, it appears to consistently return the expected results. This `matches` method and prefixed predecessors are not supported in IE 8, but they are supported in IE 9 and any browsers newer than that. Instead of buulding a very hacky solution using `querySelectorAll` that was brittle, I have chosen to act like everyone else and pretend IE 8 no longer exists. Fixes #3347 Closes #4818 --- src/js/select2/results.js | 7 +++- tests/results/option-tests.js | 63 +++++++++++++++++++++++++++++++++++ tests/unit-jq1.html | 1 + tests/unit-jq2.html | 1 + tests/unit-jq3.html | 1 + 5 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 tests/results/option-tests.js diff --git a/src/js/select2/results.js b/src/js/select2/results.js index b56f8d3b..e76d106b 100644 --- a/src/js/select2/results.js +++ b/src/js/select2/results.js @@ -175,7 +175,12 @@ define([ 'aria-selected': 'false' }; - if (data.disabled) { + var matches = window.Element.prototype.matches || + window.Element.prototype.msMatchesSelector || + window.Element.prototype.webkitMatchesSelector; + + if ((data.element != null && matches.call(data.element, ':disabled')) || + (data.element == null && data.disabled)) { delete attrs['aria-selected']; attrs['aria-disabled'] = 'true'; } diff --git a/tests/results/option-tests.js b/tests/results/option-tests.js new file mode 100644 index 00000000..6ffbb373 --- /dev/null +++ b/tests/results/option-tests.js @@ -0,0 +1,63 @@ +module('Results - option'); + +var $ = require('jquery'); + +var Options = require('select2/options'); + +var Results = require('select2/results'); + +test('disabled property on option is respected - enabled', function (assert) { + var results = new Results($(''), new Options({})); + + var $option = $(''); + var option = results.option({ + element: $option[0] + }); + + assert.notEqual(option.getAttribute('aria-disabled'), 'true'); +}); + +test('disabled property on option is respected - disabled', function (assert) { + var results = new Results($(''), new Options({})); + + var $option = $(''); + var option = results.option({ + element: $option[0] + }); + + assert.equal(option.getAttribute('aria-disabled'), 'true'); +}); + +test('disabled property on enabled optgroup is respected', function (assert) { + var results = new Results($(''), new Options({})); + + var $option = $(''); + var option = results.option({ + element: $option[0] + }); + + assert.notEqual(option.getAttribute('aria-disabled'), 'true'); +}); + +test('disabled property on disabled optgroup is respected', function (assert) { + var results = new Results($(''), new Options({})); + + var $option = $(''); + var option = results.option({ + element: $option[0] + }); + + assert.equal(option.getAttribute('aria-disabled'), 'true'); +}); + +test('option in disabled optgroup is disabled', function (assert) { + var results = new Results($(''), new Options({})); + + var $option = $('') + .find('option'); + var option = results.option({ + element: $option[0] + }); + + assert.equal(option.getAttribute('aria-disabled'), 'true'); +}); \ No newline at end of file diff --git a/tests/unit-jq1.html b/tests/unit-jq1.html index 7de1bfa2..ce66eee6 100644 --- a/tests/unit-jq1.html +++ b/tests/unit-jq1.html @@ -82,6 +82,7 @@ + diff --git a/tests/unit-jq2.html b/tests/unit-jq2.html index eb7eb12d..b1c2cf33 100644 --- a/tests/unit-jq2.html +++ b/tests/unit-jq2.html @@ -82,6 +82,7 @@ + diff --git a/tests/unit-jq3.html b/tests/unit-jq3.html index a14fa9e6..687eafc2 100644 --- a/tests/unit-jq3.html +++ b/tests/unit-jq3.html @@ -82,6 +82,7 @@ +