2 * jQuery plugin to update the tooltip to show the correct access key
5 // Whether to use 'test-' instead of correct prefix (for unit tests)
11 * Find the modifier keys that need to be pressed together with the accesskey to trigger the input.
13 * The result is dependent on the ua paramater or the current platform.
14 * For browsers that support accessKeyLabel, #getAccessKeyLabel never calls here.
15 * Valid key values that are returned can be: ctrl, alt, option, shift, esc
18 * @param {Object|undefined} [nav] A Navigator object with `userAgent` and `platform` properties.
19 * @return {string} Label with dash-separated segments in this order: ctrl, option, alt, shift, esc
21 function getAccessKeyModifiers( nav ) {
22 if ( !nav && cachedModifiers ) {
23 return cachedModifiers;
26 const profile = $.client.profile( nav );
27 let accessKeyModifiers;
29 switch ( profile.name ) {
30 // Historical: Opera 8-13 used shift-esc- (Presto engine, no longer supported).
31 // Opera 15+ (Blink engine) matches Chromium.
32 // Historical: Konqueror 3-4 (WebKit) behaved the same as Safari (no longer supported).
33 // Konqueror 18+ (QtWebEngine/Chromium engine) is profiled as 'chrome',
34 // and matches Chromium behaviour.
37 if ( profile.platform === 'mac' ) {
39 accessKeyModifiers = 'ctrl-option';
41 // Chromium on Windows or Linux
42 // (both alt- and alt-shift work, but alt with E, D, F etc does not
43 // work since they are native browser shortcuts as well, so advertise
44 // alt-shift- instead)
45 accessKeyModifiers = 'alt-shift';
48 // Historical: Firefox 1.x used alt- (no longer supported).
51 if ( profile.platform === 'mac' ) {
52 if ( profile.versionNumber < 14 ) {
53 accessKeyModifiers = 'ctrl';
55 // Firefox 14+ on macOS
56 accessKeyModifiers = 'ctrl-option';
59 // Firefox 2+ on Windows or Linux
60 accessKeyModifiers = 'alt-shift';
63 // Historical: Safari <= 3 on Windows used alt- (no longer supported).
64 // Historical: Safari <= 3 on macOS used ctrl- (no longer supported).
66 // Safari 4+ (WebKit 526+) on macOS
67 accessKeyModifiers = 'ctrl-option';
71 accessKeyModifiers = 'alt';
74 accessKeyModifiers = profile.platform === 'mac' ? 'ctrl' : 'alt';
79 // If not for a custom UA string, cache and re-use
80 cachedModifiers = accessKeyModifiers;
82 return accessKeyModifiers;
86 * Get the access key label for an element.
88 * Will use native accessKeyLabel if available (currently only in Firefox 8+),
89 * falls back to #getAccessKeyModifiers.
92 * @param {HTMLElement} element Element to get the label for
93 * @return {string} Access key label
95 function getAccessKeyLabel( element ) {
96 // abort early if no access key
97 if ( !element.accessKey ) {
100 // use accessKeyLabel if possible
101 // https://html.spec.whatwg.org/multipage/interaction.html#dom-accesskeylabel
102 if ( !testMode && element.accessKeyLabel ) {
103 return element.accessKeyLabel;
105 return ( testMode ? 'test' : getAccessKeyModifiers() ) + '-' + element.accessKey;
109 * Update the title for an element (on the element with the access key or it's label) to show
110 * the correct access key label.
113 * @param {HTMLElement} element Element with the accesskey
114 * @param {HTMLElement} titleElement Element with the title to update (may be the same as `element`)
116 function updateTooltipOnElement( element, titleElement ) {
117 const oldTitle = titleElement.title;
119 // don't add a title if the element didn't have one before
123 const separatorMsg = mw.message( 'word-separator' ).plain();
124 const parts = ( separatorMsg + mw.message( 'brackets' ).plain() ).split( '$1' );
126 const regexp = new RegExp( parts.map( mw.util.escapeRegExp ).join( '.*?' ) + '$' );
127 let newTitle = oldTitle.replace( regexp, '' );
128 const accessKeyLabel = getAccessKeyLabel( element );
130 if ( accessKeyLabel ) {
131 // Should be build the same as in Linker::titleAttrib
132 newTitle += separatorMsg + mw.message( 'brackets', accessKeyLabel ).plain();
134 if ( oldTitle !== newTitle ) {
135 titleElement.title = newTitle;
139 // HTML elements that can have an associated label
140 // https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Content_categories#Form-associated_content
141 const labelable = 'button, input, textarea, keygen, meter, output, progress, select';
144 * Update the title for an element to show the correct access key label.
147 * @param {HTMLElement} element Element with the accesskey
149 function updateTooltip( element ) {
150 updateTooltipOnElement( element, element );
152 // update associated label if there is one
153 const $element = $( element );
154 if ( $element.is( labelable ) ) {
155 // Search it using 'for' attribute
156 const id = element.id.replace( /"/g, '\\"' );
158 const $label = $( 'label[for="' + id + '"]' );
159 if ( $label.length === 1 ) {
160 updateTooltipOnElement( element, $label[ 0 ] );
164 // Search it as parent, because the form control can also be inside the label element itself
165 const $labelParent = $element.parents( 'label' );
166 if ( $labelParent.length === 1 ) {
167 updateTooltipOnElement( element, $labelParent[ 0 ] );
173 * Update the titles for all elements in a jQuery selection.
175 * To use this {@link jQuery} plugin, load the `mediawiki.util` module using {@link mw.loader}.
177 * @memberof module:mediawiki.util
181 * // Converts tooltip "[z]" to associated browser shortcut key e.g. "[ctrl-option-z]"
182 * mw.loader.using( 'mediawiki.util' ).then( () => {
183 * var $a = $('<a href="/wiki/Main_Page" title="Visit the main page [z]" accesskey="z"><span>Main page</span></a>');
184 * $a.updateTooltipAccessKeys();
188 $.fn.updateTooltipAccessKeys = function () {
189 return this.each( function () {
190 updateTooltip( this );
194 $.fn.updateTooltipAccessKeys.getAccessKeyLabel = getAccessKeyLabel;
199 * @method updateTooltipAccessKeys_getAccessKeyPrefix
200 * @param {Object} [nav] An object with a 'userAgent' and 'platform' property.
204 $.fn.updateTooltipAccessKeys.getAccessKeyPrefix = function ( nav ) {
205 return getAccessKeyModifiers( nav ) + '-';
209 * Switch test mode on and off.
211 * @method updateTooltipAccessKeys_setTestMode
212 * @param {boolean} mode New mode
215 $.fn.updateTooltipAccessKeys.setTestMode = function ( mode ) {