From 3bb421904a616f51918295e18f3e3e73fda986f8 Mon Sep 17 00:00:00 2001 From: Trevor Parscal Date: Wed, 15 Sep 2010 00:58:59 +0000 Subject: [PATCH] Moved client library to a jQuery extension - which makes it more useful to non-mediawiki people. --- resources/Resources.php | 4 +- resources/jquery/jquery.client.js | 183 +++++++++++++++++ .../mediawiki.util/mediawiki.util.client.js | 185 ------------------ 3 files changed, 185 insertions(+), 187 deletions(-) create mode 100644 resources/jquery/jquery.client.js delete mode 100644 resources/mediawiki.util/mediawiki.util.client.js diff --git a/resources/Resources.php b/resources/Resources.php index 13433e97fd..5d378355ab 100644 --- a/resources/Resources.php +++ b/resources/Resources.php @@ -1,6 +1,6 @@ new ResourceLoaderFileModule( array( 'styles' => array( 'skins/common/wikiprintable.css' => array( 'media' => 'print' ) ), ) ), -) ); +) ); \ No newline at end of file diff --git a/resources/jquery/jquery.client.js b/resources/jquery/jquery.client.js new file mode 100644 index 0000000000..479bcff0bd --- /dev/null +++ b/resources/jquery/jquery.client.js @@ -0,0 +1,183 @@ +/* + * User-agent detection + */ + +( function( $, mw ) { + +/* Extension */ + +$.client = { + /** + * Returns an object containing information about the browser + * + * The resulting client object will be in the following format: + * { + * 'name': 'firefox', + * 'layout': 'gecko', + * 'os': 'linux' + * 'version': '3.5.1', + * 'versionBase': '3', + * 'versionNumber': 3.5, + * } + */ + 'profile': function() { + // Use the cached version if possible + if ( typeof this.profile === 'undefined' ) { + + /* Configuration */ + + // Name of browsers or layout engines we don't recognize + var uk = 'unknown'; + // Generic version digit + var x = 'x'; + // Strings found in user agent strings that need to be conformed + var wildUserAgents = [ 'Opera', 'Navigator', 'Minefield', 'KHTML', 'Chrome', 'PLAYSTATION 3']; + // Translations for conforming user agent strings + var userAgentTranslations = [ + // Tons of browsers lie about being something they are not + [/(Firefox|MSIE|KHTML,\slike\sGecko|Konqueror)/, ''], + // Chrome lives in the shadow of Safari still + ['Chrome Safari', 'Chrome'], + // KHTML is the layout engine not the browser - LIES! + ['KHTML', 'Konqueror'], + // Firefox nightly builds + ['Minefield', 'Firefox'], + // This helps keep differnt versions consistent + ['Navigator', 'Netscape'], + // This prevents version extraction issues, otherwise translation would happen later + ['PLAYSTATION 3', 'PS3'], + ]; + // Strings which precede a version number in a user agent string - combined and used as match 1 in + // version detectection + var versionPrefixes = [ + 'camino', 'chrome', 'firefox', 'netscape', 'netscape6', 'opera', 'version', 'konqueror', 'lynx', + 'msie', 'safari', 'ps3' + ]; + // Used as matches 2, 3 and 4 in version extraction - 3 is used as actual version number + var versionSuffix = '(\/|\;?\s|)([a-z0-9\.\+]*?)(\;|dev|rel|\\)|\s|$)'; + // Names of known browsers + var browserNames = [ + 'camino', 'chrome', 'firefox', 'netscape', 'konqueror', 'lynx', 'msie', 'opera', 'safari', 'ipod', + 'iphone', 'blackberry', 'ps3' + ]; + // Tanslations for conforming browser names + var browserTranslations = []; + // Names of known layout engines + var layoutNames = ['gecko', 'konqueror', 'msie', 'opera', 'webkit']; + // Translations for conforming layout names + var layoutTranslations = [['konqueror', 'khtml'], ['msie', 'trident'], ['opera', 'presto']]; + // Names of known operating systems + var osNames = ['win', 'mac', 'linux', 'sunos', 'solaris', 'iphone']; + // Translations for conforming operating system names + var osTranslations = [['sunos', 'solaris']]; + + /* Methods */ + + // Performs multiple replacements on a string + function translate( source, translations ) { + for ( var i = 0; i < translations.length; i++ ) { + source = source.replace( translations[i][0], translations[i][1] ); + } + return source; + }; + + /* Pre-processing */ + + var userAgent = navigator.userAgent, match, browser = uk, layout = uk, os = uk, version = x; + if ( match = new RegExp( '(' + wildUserAgents.join( '|' ) + ')' ).exec( userAgent ) ) { + // Takes a userAgent string and translates given text into something we can more easily work with + userAgent = translate( userAgent, userAgentTranslations ); + } + // Everything will be in lowercase from now on + userAgent = userAgent.toLowerCase(); + + /* Extraction */ + + if ( match = new RegExp( '(' + browserNames.join( '|' ) + ')' ).exec( userAgent ) ) { + browser = translate( match[1], browserTranslations ); + } + if ( match = new RegExp( '(' + layoutNames.join( '|' ) + ')' ).exec( userAgent ) ) { + layout = translate( match[1], layoutTranslations ); + } + if ( match = new RegExp( '(' + osNames.join( '|' ) + ')' ).exec( navigator.platform.toLowerCase() ) ) { + var os = translate( match[1], osTranslations ); + } + if ( match = new RegExp( '(' + versionPrefixes.join( '|' ) + ')' + versionSuffix ).exec( userAgent ) ) { + version = match[3]; + } + + /* Edge Cases -- did I mention about how user agent string lie? */ + + // Decode Safari's crazy 400+ version numbers + if ( name.match( /safari/ ) && version > 400 ) { + version = '2.0'; + } + // Expose Opera 10's lies about being Opera 9.8 + if ( name === 'opera' && version >= 9.8) { + version = userAgent.match( /version\/([0-9\.]*)/i )[1] || 10; + } + + /* Caching */ + + this.profile = { + 'browser': browser, + 'layout': layout, + 'os': os, + 'version': version, + 'versionBase': ( version !== x ? new String( version ).substr( 0, 1 ) : x ), + 'versionNumber': ( parseFloat( version, 10 ) || 0.0 ) + }; + } + return this.profile; + }, + /** + * Checks the current browser against a support map object to determine if the browser has been black-listed or + * not. If the browser was not configured specifically it is assumed to work. It is assumed that the body + * element is classified as either "ltr" or "rtl". If neither is set, "ltr" is assumed. + * + * A browser map is in the following format: + * { + * 'ltr': { + * // Multiple rules with configurable operators + * 'msie': [['>=', 7], ['!=', 9]], + * // Blocked entirely + * 'iphone': false + * }, + * 'rtl': { + * // Test against a string + * 'msie': [['!==', '8.1.2.3']], + * // RTL rules do not fall through to LTR rules, you must explicity set each of them + * 'iphone': false + * } + * } + * + * @param map Object of browser support map + * + * @return Boolean true if browser known or assumed to be supported, false if blacklisted + */ + 'test': function( map ) { + var client = this.client(); + // Check over each browser condition to determine if we are running in a compatible client + var browser = map[$( 'body' ).is( '.rtl' ) ? 'rtl' : 'ltr'][client.browser]; + if ( typeof browser !== 'object' ) { + // Unknown, so we assume it's working + return true; + } + for ( var condition in browser ) { + var op = browser[condition][0]; + var val = browser[condition][1]; + if ( val === false ) { + return false; + } else if ( typeof val == 'string' ) { + if ( !( eval( 'client.version' + op + '"' + val + '"' ) ) ) { + return false; + } + } else if ( typeof val == 'number' ) { + if ( !( eval( 'client.versionNumber' + op + val ) ) ) { + return false; + } + } + } + return true; + } +}; \ No newline at end of file diff --git a/resources/mediawiki.util/mediawiki.util.client.js b/resources/mediawiki.util/mediawiki.util.client.js deleted file mode 100644 index 44f39e9f03..0000000000 --- a/resources/mediawiki.util/mediawiki.util.client.js +++ /dev/null @@ -1,185 +0,0 @@ -/* - * User-agent detection - */ - -( function( $, mw ) { - -/* Extension */ - -$.extend( mw.util, { - 'client': { - /** - * Returns an object containing information about the browser - * - * The resulting client object will be in the following format: - * { - * 'name': 'firefox', - * 'layout': 'gecko', - * 'os': 'linux' - * 'version': '3.5.1', - * 'versionBase': '3', - * 'versionNumber': 3.5, - * } - */ - this.profile = function() { - // Use the cached version if possible - if ( typeof this.profile === 'undefined' ) { - - /* Configuration */ - - // Name of browsers or layout engines we don't recognize - var uk = 'unknown'; - // Generic version digit - var x = 'x'; - // Strings found in user agent strings that need to be conformed - var wildUserAgents = [ 'Opera', 'Navigator', 'Minefield', 'KHTML', 'Chrome', 'PLAYSTATION 3']; - // Translations for conforming user agent strings - var userAgentTranslations = [ - // Tons of browsers lie about being something they are not - [/(Firefox|MSIE|KHTML,\slike\sGecko|Konqueror)/, ''], - // Chrome lives in the shadow of Safari still - ['Chrome Safari', 'Chrome'], - // KHTML is the layout engine not the browser - LIES! - ['KHTML', 'Konqueror'], - // Firefox nightly builds - ['Minefield', 'Firefox'], - // This helps keep differnt versions consistent - ['Navigator', 'Netscape'], - // This prevents version extraction issues, otherwise translation would happen later - ['PLAYSTATION 3', 'PS3'], - ]; - // Strings which precede a version number in a user agent string - combined and used as match 1 in - // version detectection - var versionPrefixes = [ - 'camino', 'chrome', 'firefox', 'netscape', 'netscape6', 'opera', 'version', 'konqueror', 'lynx', - 'msie', 'safari', 'ps3' - ]; - // Used as matches 2, 3 and 4 in version extraction - 3 is used as actual version number - var versionSuffix = '(\/|\;?\s|)([a-z0-9\.\+]*?)(\;|dev|rel|\\)|\s|$)'; - // Names of known browsers - var browserNames = [ - 'camino', 'chrome', 'firefox', 'netscape', 'konqueror', 'lynx', 'msie', 'opera', 'safari', 'ipod', - 'iphone', 'blackberry', 'ps3' - ]; - // Tanslations for conforming browser names - var browserTranslations = []; - // Names of known layout engines - var layoutNames = ['gecko', 'konqueror', 'msie', 'opera', 'webkit']; - // Translations for conforming layout names - var layoutTranslations = [['konqueror', 'khtml'], ['msie', 'trident'], ['opera', 'presto']]; - // Names of known operating systems - var osNames = ['win', 'mac', 'linux', 'sunos', 'solaris', 'iphone']; - // Translations for conforming operating system names - var osTranslations = [['sunos', 'solaris']]; - - /* Methods */ - - // Performs multiple replacements on a string - function translate( source, translations ) { - for ( var i = 0; i < translations.length; i++ ) { - source = source.replace( translations[i][0], translations[i][1] ); - } - return source; - }; - - /* Pre-processing */ - - var userAgent = navigator.userAgent, match, browser = uk, layout = uk, os = uk, version = x; - if ( match = new RegExp( '(' + wildUserAgents.join( '|' ) + ')' ).exec( userAgent ) ) { - // Takes a userAgent string and translates given text into something we can more easily work with - userAgent = translate( userAgent, userAgentTranslations ); - } - // Everything will be in lowercase from now on - userAgent = userAgent.toLowerCase(); - - /* Extraction */ - - if ( match = new RegExp( '(' + browserNames.join( '|' ) + ')' ).exec( userAgent ) ) { - browser = translate( match[1], browserTranslations ); - } - if ( match = new RegExp( '(' + layoutNames.join( '|' ) + ')' ).exec( userAgent ) ) { - layout = translate( match[1], layoutTranslations ); - } - if ( match = new RegExp( '(' + osNames.join( '|' ) + ')' ).exec( navigator.platform.toLowerCase() ) ) { - var os = translate( match[1], osTranslations ); - } - if ( match = new RegExp( '(' + versionPrefixes.join( '|' ) + ')' + versionSuffix ).exec( userAgent ) ) { - version = match[3]; - } - - /* Edge Cases -- did I mention about how user agent string lie? */ - - // Decode Safari's crazy 400+ version numbers - if ( name.match( /safari/ ) && version > 400 ) { - version = '2.0'; - } - // Expose Opera 10's lies about being Opera 9.8 - if ( name === 'opera' && version >= 9.8) { - version = userAgent.match( /version\/([0-9\.]*)/i )[1] || 10; - } - - /* Caching */ - - this.profile = { - 'browser': browser, - 'layout': layout, - 'os': os, - 'version': version, - 'versionBase': ( version !== x ? new String( version ).substr( 0, 1 ) : x ), - 'versionNumber': ( parseFloat( version, 10 ) || 0.0 ) - }; - } - return this.profile; - }; - /** - * Checks the current browser against a support map object to determine if the browser has been black-listed or - * not. If the browser was not configured specifically it is assumed to work. It is assumed that the body - * element is classified as either "ltr" or "rtl". If neither is set, "ltr" is assumed. - * - * A browser map is in the following format: - * { - * 'ltr': { - * // Multiple rules with configurable operators - * 'msie': [['>=', 7], ['!=', 9]], - * // Blocked entirely - * 'iphone': false - * }, - * 'rtl': { - * // Test against a string - * 'msie': [['!==', '8.1.2.3']], - * // RTL rules do not fall through to LTR rules, you must explicity set each of them - * 'iphone': false - * } - * } - * - * @param map Object of browser support map - * - * @return Boolean true if browser known or assumed to be supported, false if blacklisted - */ - this.test = function( map ) { - var client = this.client(); - // Check over each browser condition to determine if we are running in a compatible client - var browser = map[$( 'body' ).is( '.rtl' ) ? 'rtl' : 'ltr'][client.browser]; - if ( typeof browser !== 'object' ) { - // Unknown, so we assume it's working - return true; - } - for ( var condition in browser ) { - var op = browser[condition][0]; - var val = browser[condition][1]; - if ( val === false ) { - return false; - } else if ( typeof val == 'string' ) { - if ( !( eval( 'client.version' + op + '"' + val + '"' ) ) ) { - return false; - } - } else if ( typeof val == 'number' ) { - if ( !( eval( 'client.versionNumber' + op + val ) ) ) { - return false; - } - } - } - return true; - }; - } -} ); \ No newline at end of file -- 2.20.1