diff options
| author | Steven Bassett <[email protected]> | 2014-05-08 20:19:12 -0700 |
|---|---|---|
| committer | Steven Bassett <[email protected]> | 2014-05-16 10:45:38 -0700 |
| commit | a70da16f6fb8c665f22b78b49a1dff998f1da8a7 (patch) | |
| tree | 1d6316e9dc74efc53646533c94e9dfc2d0f8720f /js | |
| parent | a40ff40b8fb852af8034b215e2102ca8ff1618e0 (diff) | |
| download | bootstrap-a70da16f6fb8c665f22b78b49a1dff998f1da8a7.tar.xz bootstrap-a70da16f6fb8c665f22b78b49a1dff998f1da8a7.zip | |
Adds aria described by to tooltip plugin for accessibility
Generates a unique id for tooltip and adds [aria-describedby] to the element
it is called on. Resolves issue #13480
- set up test
- linted the code styles
- passed the tests
- integrated feedback from @cvrebert
Diffstat (limited to 'js')
| -rw-r--r-- | js/tests/unit/tooltip.js | 30 | ||||
| -rw-r--r-- | js/tooltip.js | 12 |
2 files changed, 42 insertions, 0 deletions
diff --git a/js/tests/unit/tooltip.js b/js/tests/unit/tooltip.js index e579a9ef7..e95b91cbb 100644 --- a/js/tests/unit/tooltip.js +++ b/js/tests/unit/tooltip.js @@ -41,6 +41,36 @@ $(function () { equal(tooltip.attr('data-original-title'), 'Another tooltip', 'original title preserved in data attribute') }) + test('should add set set aria describedby to the element called on show', function() { + var tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"></a>').bootstrapTooltip() + .appendTo('#qunit-fixture') + .bootstrapTooltip('show') + ok(tooltip.attr('aria-describedby'), 'has the right attributes') + var id = $('.tooltip').attr('id') + + ok($('#' + id).length == 1, 'has a unique id') + ok($('.tooltip').attr('aria-describedby') === tooltip.attr('id'), 'they match!') + ok(tooltip.attr('aria-describedby') !== undefined, 'has the right attributes') + }) + + test('should remove the aria-describedby attributes on hide', function() { + var tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"></a>').bootstrapTooltip() + .appendTo('#qunit-fixture') + .bootstrapTooltip('show') + ok(tooltip.attr('aria-describedby'), 'has the right attributes') + tooltip.bootstrapTooltip('hide') + ok(!tooltip.attr('aria-describedby'), 'removed the attributes on hide') + }) + + test('should assign a unique id tooltip element', function () { + $('<a href="#" rel="tooltip" title="Another tooltip"></a>') + .appendTo('#qunit-fixture') + .bootstrapTooltip('show'), + id = $('.tooltip').attr('id') + + ok( $('#' + id).length == 1 && id.indexOf('tooltip') === 0, 'generated prefixed and unique tooltip id') + }) + test('should place tooltips relative to placement option', function () { $.support.transition = false var tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"></a>') diff --git a/js/tooltip.js b/js/tooltip.js index d985f96e3..936424830 100644 --- a/js/tooltip.js +++ b/js/tooltip.js @@ -150,7 +150,11 @@ var $tip = this.tip() + var tipId = this.getUID(this.type) + this.setContent() + $tip.attr('id', tipId) + this.$element.attr('aria-describedby', tipId) if (this.options.animation) $tip.addClass('fade') @@ -273,6 +277,8 @@ var $tip = this.tip() var e = $.Event('hide.bs.' + this.type) + this.$element.removeAttr('aria-describedby') + function complete() { if (that.hoverState != 'in') $tip.detach() that.$element.trigger('hidden.bs.' + that.type) @@ -364,6 +370,12 @@ return title } + Tooltip.prototype.getUID = function (prefix) { + do prefix += ~~(Math.random() * 1000000) + while (document.getElementById(prefix)) + return prefix + } + Tooltip.prototype.tip = function () { return this.$tip = this.$tip || $(this.options.template) } |
