From de97c456662fb80dd76998f88415304f7cebaa3c Mon Sep 17 00:00:00 2001 From: Krinkle Date: Wed, 27 Oct 2010 23:19:26 +0000 Subject: [PATCH] porting is_opera, is_safari_win etc. to mw.util as isBrowser('..'), isPlatform('...') etc. --- resources/mediawiki.util/mediawiki.util.js | 101 ++++++++++++++---- .../mediawiki.util/mediawiki.util.test.js | 25 +++-- 2 files changed, 99 insertions(+), 27 deletions(-) diff --git a/resources/mediawiki.util/mediawiki.util.js b/resources/mediawiki.util/mediawiki.util.js index 93f7320b7a..57248e55bc 100644 --- a/resources/mediawiki.util/mediawiki.util.js +++ b/resources/mediawiki.util/mediawiki.util.js @@ -12,35 +12,50 @@ if ( this.initialised === false ) { this.initialised = true; - // Set tooltipAccessKeyPrefix - if ( is_opera ) { - this.tooltipAccessKeyPrefix = 'shift-esc-'; - } else if ( is_chrome ) { - this.tooltipAccessKeyPrefix = is_chrome_mac ? 'ctrl-option-' : 'alt-'; - } else if ( !is_safari_win && is_safari && webkit_version > 526 ) { - this.tooltipAccessKeyPrefix = 'ctrl-alt-'; - } else if ( !is_safari_win && ( is_safari - || clientPC.indexOf('mac') !== -1 - || clientPC.indexOf('konqueror') !== -1 ) ) { - this.tooltipAccessKeyPrefix = 'ctrl-'; - } else if ( is_ff2 ) { - this.tooltipAccessKeyPrefix = 'alt-shift-'; - } - // Any initialisation after the DOM is ready $(function () { + + // Populate clientProfile var + mw.util.clientProfile = $.client.profile(); + + // Set tooltipAccessKeyPrefix + + // Opera on any platform + if ( mw.util.isBrowser('opera') ) { + this.tooltipAccessKeyPrefix = 'shift-esc-'; + + // Chrome on any platform + } else if ( mw.util.isBrowser('chrome') ) { + // Chrome on Mac or Chrome on other platform ? + this.tooltipAccessKeyPrefix = mw.util.isPlatform('mac') ? 'ctrl-option-' : 'alt-'; + + // Non-Windows Safari with webkit_version > 526 + } else if ( !mw.util.isPlatform('win') && mw.util.isBrowser('safari') && webkit_version > 526 ) { + this.tooltipAccessKeyPrefix = 'ctrl-alt-'; + + // Safari/Konqueror on any platform, or any browser on Mac (but not Safari on Windows) + } else if ( !( mw.util.isPlatform('win') && mw.util.isBrowser('safari') ) + && ( mw.util.isBrowser('safari') + || mw.util.isPlatform('mac') + || mw.util.isBrowser('konqueror') ) ) { + this.tooltipAccessKeyPrefix = 'ctrl-'; + + // Firefox 2.x + } else if ( mw.util.isBrowser('firefox') && mw.util.isBrowserVersion('2') ) { + this.tooltipAccessKeyPrefix = 'alt-shift-'; + } // Enable CheckboxShiftClick $('input[type=checkbox]:not(.noshiftselect)').checkboxShiftClick(); - // Fill bodyContant var + // Fill $content var if ( $('#bodyContent').length ) { mw.util.$content = $('#bodyContent'); } else if ( $('#article').length ) { mw.util.$content = $('#article'); } else { mw.util.$content = $('#content'); - } + } }); @@ -51,6 +66,57 @@ /* Main body */ + // Holds result of $.client.profile() + // Populated by init() + 'clientProfile' : {}, + + /** + * Checks if the current browser matches + * + * @example mw.util.isBrowser( 'safari' ); + * @param String str name of a browser (case insensitive). Check jquery.client.js for possible values + * @return Boolean true of the browsername matches the clients browser + */ + 'isBrowser' : function( str ) { + str = (str + '').toLowerCase(); + return this.clientProfile.name == str; + }, + + /** + * Checks if the current layout matches + * + * @example mw.util.isLayout( 'webkit' ); + * @param String str name of a layout engine (case insensitive). Check jquery.client.js for possible values + * @return Boolean true of the layout engine matches the clients browser + */ + 'isLayout' : function( str ) { + str = (str + '').toLowerCase(); + return this.clientProfile.layout == str; + }, + + /** + * Checks if the current layout matches + * + * @example mw.util.isPlatform( 'mac' ); + * @param String str name of a platform (case insensitive). Check jquery.client.js for possible values + * @return Boolean true of the platform matches the clients platform + */ + 'isPlatform' : function( str ) { + str = (str + '').toLowerCase(); + return this.clientProfile.platform == str; + }, + + /** + * Checks if the current browser version matches + * + * @example mw.util.isBrowserVersion( '5' ); + * @param String str version number without decimals + * @return Boolean true of the version number matches the clients browser + */ + 'isBrowserVersion' : function( str ) { + return this.clientProfile.versionBase === str; + }, + /** * Encodes the string like PHP's rawurlencode * @@ -193,7 +259,6 @@ // Populated by init() '$content' : null, - /** * Add a link to a portlet menu on the page, such as: * diff --git a/resources/mediawiki.util/mediawiki.util.test.js b/resources/mediawiki.util/mediawiki.util.test.js index 11a214d3e8..8852ae36af 100644 --- a/resources/mediawiki.util/mediawiki.util.test.js +++ b/resources/mediawiki.util/mediawiki.util.test.js @@ -78,35 +78,42 @@ '\\.st\\{e\\}\\$st (string)'); mw.test.addTest('typeof $.fn.checkboxShiftClick', 'function (string)'); + mw.test.addTest('typeof mw.util.isBrowser( \'safari\' )', + 'boolean (string)'); + mw.test.addTest('typeof mw.util.isLayout( \'webKit\' )', + 'boolean (string)'); + mw.test.addTest('typeof mw.util.isPlatform( \'MAC\' )', + 'boolean (string)'); + mw.test.addTest('typeof mw.util.isBrowserVersion( \'5\' )', + 'boolean (string)'); mw.test.addTest('typeof mw.util.rawurlencode', 'function (string)'); - mw.test.addTest('mw.util.rawurlencode(\'Test: A&B/Here\')', + mw.test.addTest('mw.util.rawurlencode( \'Test: A&B/Here\' )', 'Test%3A%20A%26B%2FHere (string)'); mw.test.addTest('typeof mw.util.wfGetlink', 'function (string)'); mw.test.addTest('typeof mw.util.getParamValue', 'function (string)'); - mw.test.addTest('mw.util.getParamValue(\'action\')', + mw.test.addTest('mw.util.getParamValue( \'action\' )', 'mwutiltest (string)'); mw.test.addTest('typeof mw.util.htmlEscape', 'function (string)'); - mw.test.addTest('mw.util.htmlEscape(\'link\')', + mw.test.addTest('mw.util.htmlEscape( \'link\' )', '<a href="http://mw.org/?a=b&c=d">link</a> (string)'); mw.test.addTest('typeof mw.util.htmlUnescape', 'function (string)'); - mw.test.addTest('mw.util.htmlUnescape(\'<a href="http://mw.org/?a=b&c=d">link</a>\')', + mw.test.addTest('mw.util.htmlUnescape( \'<a href="http://mw.org/?a=b&c=d">link</a>\' )', 'link (string)'); - mw.test.addTest('typeof mw.util.tooltipAccessKeyRegexp', - 'function (string)'); + mw.test.addTest('mw.util.tooltipAccessKeyRegexp.constructor.name', + 'RegExp (string)'); mw.test.addTest('typeof mw.util.updateTooltipAccessKeys', 'function (string)'); mw.test.addTest('typeof mw.util.addPortletLink', 'function (string)'); mw.test.addTest('typeof mw.util.addPortletLink("p-tb", "http://mediawiki.org/", "MediaWiki.org", "t-mworg", "Go to MediaWiki.org ", "m", "#t-print")', 'object (string)'); - mw.test.addTest('a = mw.util.addPortletLink("p-tb", "http://mediawiki.org/", "MediaWiki.org", "t-mworg", "Go to MediaWiki.org ", "m", "#t-print"); if(a){ a.outerHTML; }', - '
  • MediaWiki.org
  • (string)', - 'href="http://mediawiki.org/"'); + mw.test.addTest('a = mw.util.addPortletLink("p-tb", "http://mediawiki.org/", "MediaWiki.org", "t-mworg", "Go to MediaWiki.org ", "m", "#t-print"); $(a).text();', + 'MediaWiki.org (string)'); // Run tests and compare results var exec, -- 2.20.1