Index: trunk/phase3/resources/mediawiki.util/mediawiki.util.js |
— | — | @@ -0,0 +1,611 @@ |
| 2 | +/** |
| 3 | + * Utilities |
| 4 | + */ |
| 5 | +( function( $, mw ) { |
| 6 | + |
| 7 | + mw.util = { |
| 8 | + |
| 9 | + /* Initialisation */ |
| 10 | + 'initialised' : false, |
| 11 | + 'init' : function() { |
| 12 | + if ( this.initialised === false ) { |
| 13 | + this.initialised = true; |
| 14 | + |
| 15 | + // Any initialisation after the DOM is ready |
| 16 | + $( function() { |
| 17 | + |
| 18 | + /* Set up $.messageBox */ |
| 19 | + $.messageBoxNew( { |
| 20 | + 'id': 'mw-js-message', |
| 21 | + 'parent': '#content' |
| 22 | + } ); |
| 23 | + |
| 24 | + // Shortcut to client profile return |
| 25 | + var profile = $.client.profile(); |
| 26 | + |
| 27 | + /* Set tooltipAccessKeyPrefix */ |
| 28 | + |
| 29 | + // Opera on any platform |
| 30 | + if ( profile.name == 'opera' ) { |
| 31 | + mw.util.tooltipAccessKeyPrefix = 'shift-esc-'; |
| 32 | + |
| 33 | + // Chrome on any platform |
| 34 | + } else if ( profile.name == 'chrome' ) { |
| 35 | + // Chrome on Mac or Chrome on other platform ? |
| 36 | + mw.util.tooltipAccessKeyPrefix = ( profile.platform == 'mac' |
| 37 | + ? 'ctrl-option-' : 'alt-' ); |
| 38 | + |
| 39 | + // Non-Windows Safari with webkit_version > 526 |
| 40 | + } else if ( profile.platform !== 'win' |
| 41 | + && profile.name == 'safari' |
| 42 | + && profile.layoutVersion > 526 ) { |
| 43 | + mw.util.tooltipAccessKeyPrefix = 'ctrl-alt-'; |
| 44 | + |
| 45 | + // Safari/Konqueror on any platform, or any browser on Mac |
| 46 | + // (but not Safari on Windows) |
| 47 | + } else if ( !( profile.platform == 'win' && profile.name == 'safari' ) |
| 48 | + && ( profile.name == 'safari' |
| 49 | + || profile.platform == 'mac' |
| 50 | + || profile.name == 'konqueror' ) ) { |
| 51 | + mw.util.tooltipAccessKeyPrefix = 'ctrl-'; |
| 52 | + |
| 53 | + // Firefox 2.x |
| 54 | + } else if ( profile.name == 'firefox' && profile.versionBase == '2' ) { |
| 55 | + mw.util.tooltipAccessKeyPrefix = 'alt-shift-'; |
| 56 | + } |
| 57 | + |
| 58 | + /* Enable CheckboxShiftClick */ |
| 59 | + $( 'input[type=checkbox]:not(.noshiftselect)' ).checkboxShiftClick(); |
| 60 | + |
| 61 | + /* Emulate placeholder if not supported by browser */ |
| 62 | + if ( !( 'placeholder' in document.createElement( 'input' ) ) ) { |
| 63 | + $( 'input[placeholder]' ).placeholder(); |
| 64 | + } |
| 65 | + |
| 66 | + /* Fill $content var */ |
| 67 | + if ( $( '#bodyContent' ).length ) { |
| 68 | + // Vector, Monobook, Chick etc. |
| 69 | + mw.util.$content = $( '#bodyContent' ); |
| 70 | + |
| 71 | + } else if ( $( '#mw_contentholder' ).length ) { |
| 72 | + // Modern |
| 73 | + mw.util.$content = $( '#mw_contentholder' ); |
| 74 | + |
| 75 | + } else if ( $( '#article' ).length ) { |
| 76 | + // Standard, CologneBlue |
| 77 | + mw.util.$content = $( '#article' ); |
| 78 | + |
| 79 | + } else { |
| 80 | + // #content is present on almost all if not all skins. Most skins (the above cases) |
| 81 | + // have #content too, but as an outer wrapper instead of the article text container. |
| 82 | + // The skins that don't have an outer wrapper do have #content for everything |
| 83 | + // so it's a good fallback |
| 84 | + mw.util.$content = $( '#content' ); |
| 85 | + } |
| 86 | + |
| 87 | + /* Enable makeCollapse */ |
| 88 | + $( '.mw-collapsible' ).makeCollapsible(); |
| 89 | + |
| 90 | + /* Table of Contents toggle */ |
| 91 | + var $tocContainer = $( '#toc' ), |
| 92 | + $tocTitle = $( '#toctitle' ), |
| 93 | + $tocToggleLink = $( '#togglelink' ); |
| 94 | + // Only add it if there is a TOC and there is no toggle added already |
| 95 | + if ( $tocContainer.size() && $tocTitle.size() && !$tocToggleLink.size() ) { |
| 96 | + var hideTocCookie = $.cookie( 'mw_hidetoc' ); |
| 97 | + $tocToggleLink = $( '<a href="#" class="internal" id="togglelink">' ).text( mw.msg( 'hidetoc' ) ).click( function(e){ |
| 98 | + e.preventDefault(); |
| 99 | + mw.util.toggleToc( $(this) ); |
| 100 | + } ); |
| 101 | + $tocTitle.append( $tocToggleLink.wrap( '<span class="toctoggle">' ).parent().prepend( ' [' ).append( '] ' ) ); |
| 102 | + |
| 103 | + if ( hideTocCookie == '1' ) { |
| 104 | + // Cookie says user want toc hidden |
| 105 | + $tocToggleLink.click(); |
| 106 | + } |
| 107 | + } |
| 108 | + } ); |
| 109 | + |
| 110 | + return true; |
| 111 | + } |
| 112 | + return false; |
| 113 | + }, |
| 114 | + |
| 115 | + /* Main body */ |
| 116 | + |
| 117 | + /** |
| 118 | + * Encode the string like PHP's rawurlencode |
| 119 | + * |
| 120 | + * @param str String to be encoded |
| 121 | + */ |
| 122 | + 'rawurlencode' : function( str ) { |
| 123 | + str = ( str + '' ).toString(); |
| 124 | + return encodeURIComponent( str ) |
| 125 | + .replace( /!/g, '%21' ).replace( /'/g, '%27' ).replace( /\(/g, '%28' ) |
| 126 | + .replace( /\)/g, '%29' ).replace( /\*/g, '%2A' ).replace( /~/g, '%7E' ); |
| 127 | + }, |
| 128 | + |
| 129 | + /** |
| 130 | + * Encode page titles for use in a URL |
| 131 | + * We want / and : to be included as literal characters in our title URLs |
| 132 | + * as they otherwise fatally break the title |
| 133 | + * |
| 134 | + * @param str String to be encoded |
| 135 | + */ |
| 136 | + 'wikiUrlencode' : function( str ) { |
| 137 | + return this.rawurlencode( str ) |
| 138 | + .replace( /%20/g, '_' ).replace( /%3A/g, ':' ).replace( /%2F/g, '/' ); |
| 139 | + }, |
| 140 | + |
| 141 | + /** |
| 142 | + * Append a new style block to the head |
| 143 | + * |
| 144 | + * @param text String CSS to be appended |
| 145 | + * @return CSSStyleSheet object |
| 146 | + */ |
| 147 | + 'addCSS' : function( text ) { |
| 148 | + var s = document.createElement( 'style' ); |
| 149 | + s.type = 'text/css'; |
| 150 | + s.rel = 'stylesheet'; |
| 151 | + if ( s.styleSheet ) { |
| 152 | + s.styleSheet.cssText = text; // IE |
| 153 | + } else { |
| 154 | + s.appendChild( document.createTextNode( text + '' ) ); // Safari sometimes borks on null |
| 155 | + } |
| 156 | + document.getElementsByTagName("head")[0].appendChild( s ); |
| 157 | + return s.sheet || s; |
| 158 | + }, |
| 159 | + |
| 160 | + /** |
| 161 | + * Hide/show the table of contents element |
| 162 | + * |
| 163 | + * @param $toggleLink jQuery object of the toggle link |
| 164 | + * @return String boolean visibility of the toc (true means it's visible) |
| 165 | + */ |
| 166 | + 'toggleToc' : function( $toggleLink ) { |
| 167 | + var $tocList = $( '#toc ul:first' ); |
| 168 | + |
| 169 | + // This function shouldn't be called if there's no TOC, |
| 170 | + // but just in case... |
| 171 | + if ( $tocList.size() ) { |
| 172 | + if ( $tocList.is( ':hidden' ) ) { |
| 173 | + $tocList.slideDown( 'fast' ); |
| 174 | + $toggleLink.text( mw.msg( 'hidetoc' ) ); |
| 175 | + $.cookie( 'mw_hidetoc', null, { |
| 176 | + expires: 30, |
| 177 | + path: '/' |
| 178 | + } ); |
| 179 | + return true; |
| 180 | + } else { |
| 181 | + $tocList.slideUp( 'fast' ); |
| 182 | + $toggleLink.text( mw.msg( 'showtoc' ) ); |
| 183 | + $.cookie( 'mw_hidetoc', '1', { |
| 184 | + expires: 30, |
| 185 | + path: '/' |
| 186 | + } ); |
| 187 | + return false; |
| 188 | + } |
| 189 | + } else { |
| 190 | + return false; |
| 191 | + } |
| 192 | + }, |
| 193 | + |
| 194 | + /** |
| 195 | + * Get the full URL to a page name |
| 196 | + * |
| 197 | + * @param str Page name to link to |
| 198 | + * @return Full URL for page with name of 'str' or false on error |
| 199 | + */ |
| 200 | + 'wikiGetlink' : function( str ) { |
| 201 | + |
| 202 | + return mw.config.get( 'wgArticlePath' ).replace( '$1', this.wikiUrlencode( str ) ); |
| 203 | + }, |
| 204 | + |
| 205 | + /** |
| 206 | + * Grab the URL parameter value for the given parameter. |
| 207 | + * Returns null if not found. |
| 208 | + * Beware! When action paths are enabled (wgActionPaths) using this function |
| 209 | + * to retrieve the 'action' or 'title' parameter will probably fail since |
| 210 | + * those parameters are hidden in the path. |
| 211 | + * To safely query for: |
| 212 | + * 'action' use getActionFrom( url ) |
| 213 | + * 'title' use getTitleFrom( url ) |
| 214 | + * |
| 215 | + * @param param The parameter name |
| 216 | + * @param url URL to search through (optional) |
| 217 | + */ |
| 218 | + 'getParamValue' : function( param, url ) { |
| 219 | + url = url ? url : document.location.href; |
| 220 | + // Get last match, stop at hash |
| 221 | + var re = new RegExp( '[^#]*[&?]' + $.escapeRE( param ) + '=([^&#]*)' ); |
| 222 | + var m = re.exec( url ); |
| 223 | + if ( m && m.length > 1 ) { |
| 224 | + return decodeURIComponent( m[1] ); |
| 225 | + } |
| 226 | + return null; |
| 227 | + }, |
| 228 | + |
| 229 | + /** |
| 230 | + * Try to find the wiki action for a given URL with actions paths support |
| 231 | + * |
| 232 | + * @param url URL to search for a wiki action |
| 233 | + */ |
| 234 | + 'getActionFrom' : function( url ) { |
| 235 | + // attempt to get the action from the parameter [&?]action= |
| 236 | + var action = mw.util.getParamValue( 'action', url ); |
| 237 | + if( action !== null ) { |
| 238 | + return action; |
| 239 | + } |
| 240 | + |
| 241 | + // now from the action paths |
| 242 | + var actionPaths = mw.config.get( 'wgActionPaths' ); |
| 243 | + if( actionPaths.length == 0 ) { |
| 244 | + actionPaths['view'] = mw.config.get( 'wgArticlePath' ); |
| 245 | + } |
| 246 | + var action = ''; |
| 247 | + for ( action in actionPaths ) { |
| 248 | + var actionRe = new RegExp( actionPaths[action].replace( '$1', '.*?' ) ); |
| 249 | + if( url.match( actionRe ) ) { |
| 250 | + return action; |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + return null; |
| 255 | + }, |
| 256 | + |
| 257 | + /** |
| 258 | + * Try to find the wiki title for a given URL with actions paths support |
| 259 | + * |
| 260 | + * @param url URL to search for a title |
| 261 | + */ |
| 262 | + 'getTitleFrom' : function( url ) { |
| 263 | + // attempt to get the title from the parameter [&?]title= |
| 264 | + var title = mw.util.getParamValue( 'title', url ); |
| 265 | + if( title !== null ) { |
| 266 | + return title; |
| 267 | + } |
| 268 | + |
| 269 | + // now from the action paths |
| 270 | + var actionPaths = mw.config.get( 'wgActionPaths' ); |
| 271 | + if( actionPaths.length == 0 ) { |
| 272 | + actionPaths['view'] = mw.config.get( 'wgArticlePath' ); |
| 273 | + } |
| 274 | + var action = ''; |
| 275 | + for ( action in actionPaths ) { |
| 276 | + var actionRe = new RegExp( '.*' + actionPaths[action].replace( '$1', '([^&?#]+)' ) ); |
| 277 | + var title = url.match( actionRe ); |
| 278 | + if( title !== null ) { |
| 279 | + return title[1]; |
| 280 | + } |
| 281 | + } |
| 282 | + |
| 283 | + return null; |
| 284 | + }, |
| 285 | + |
| 286 | + // Access key prefix. |
| 287 | + // Will be re-defined based on browser/operating system detection in |
| 288 | + // mw.util.init(). |
| 289 | + 'tooltipAccessKeyPrefix' : 'alt-', |
| 290 | + |
| 291 | + // Regex to match accesskey tooltips |
| 292 | + 'tooltipAccessKeyRegexp': /\[(ctrl-)?(alt-)?(shift-)?(esc-)?(.)\]$/, |
| 293 | + |
| 294 | + /** |
| 295 | + * Add the appropriate prefix to the accesskey shown in the tooltip. |
| 296 | + * If the nodeList parameter is given, only those nodes are updated; |
| 297 | + * otherwise, all the nodes that will probably have accesskeys by |
| 298 | + * default are updated. |
| 299 | + * |
| 300 | + * @param nodeList jQuery object, or array of elements |
| 301 | + */ |
| 302 | + 'updateTooltipAccessKeys' : function( nodeList ) { |
| 303 | + var $nodes; |
| 304 | + if ( nodeList instanceof jQuery ) { |
| 305 | + $nodes = nodeList; |
| 306 | + } else if ( nodeList ) { |
| 307 | + $nodes = $( nodeList ); |
| 308 | + } else { |
| 309 | + // Rather than scanning all links, just the elements that |
| 310 | + // contain the relevant links |
| 311 | + this.updateTooltipAccessKeys( |
| 312 | + $( '#column-one a, #mw-head a, #mw-panel a, #p-logo a' ) ); |
| 313 | + |
| 314 | + // these are rare enough that no such optimization is needed |
| 315 | + this.updateTooltipAccessKeys( $( 'input' ) ); |
| 316 | + this.updateTooltipAccessKeys( $( 'label' ) ); |
| 317 | + return; |
| 318 | + } |
| 319 | + |
| 320 | + $nodes.each( function ( i ) { |
| 321 | + var tip = $(this).attr( 'title' ); |
| 322 | + if ( !!tip && mw.util.tooltipAccessKeyRegexp.exec( tip ) ) { |
| 323 | + tip = tip.replace( mw.util.tooltipAccessKeyRegexp, |
| 324 | + '[' + mw.util.tooltipAccessKeyPrefix + "$5]" ); |
| 325 | + $(this).attr( 'title', tip ); |
| 326 | + } |
| 327 | + } ); |
| 328 | + }, |
| 329 | + |
| 330 | + // jQuery object that refers to the page-content element |
| 331 | + // Populated by init() |
| 332 | + '$content' : null, |
| 333 | + |
| 334 | + /** |
| 335 | + * Add a link to a portlet menu on the page, such as: |
| 336 | + * |
| 337 | + * p-cactions (Content actions), p-personal (Personal tools), |
| 338 | + * p-navigation (Navigation), p-tb (Toolbox) |
| 339 | + * |
| 340 | + * The first three paramters are required, others are optionals. Though |
| 341 | + * providing an id and tooltip is recommended. |
| 342 | + * |
| 343 | + * By default the new link will be added to the end of the list. To |
| 344 | + * add the link before a given existing item, pass the DOM node |
| 345 | + * (document.getElementById( 'foobar' )) or the jQuery-selector |
| 346 | + * ( '#foobar' ) of that item. |
| 347 | + * |
| 348 | + * @example mw.util.addPortletLink( |
| 349 | + * 'p-tb', 'http://mediawiki.org/', |
| 350 | + * 'MediaWiki.org', 't-mworg', 'Go to MediaWiki.org ', 'm', '#t-print' |
| 351 | + * ) |
| 352 | + * |
| 353 | + * @param portlet ID of the target portlet ( 'p-cactions' or 'p-personal' etc.) |
| 354 | + * @param href Link URL |
| 355 | + * @param text Link text |
| 356 | + * @param id ID of the new item, should be unique and preferably have |
| 357 | + * the appropriate prefix ( 'ca-', 'pt-', 'n-' or 't-' ) |
| 358 | + * @param tooltip Text to show when hovering over the link, without accesskey suffix |
| 359 | + * @param accesskey Access key to activate this link (one character, try |
| 360 | + * to avoid conflicts. Use $( '[accesskey=x]' ).get() in the console to |
| 361 | + * see if 'x' is already used. |
| 362 | + * @param nextnode DOM node or jQuery-selector string of the item that the new |
| 363 | + * item should be added before, should be another item in the same |
| 364 | + * list, it will be ignored otherwise |
| 365 | + * |
| 366 | + * @return The DOM node of the new item (a LI element, or A element for |
| 367 | + * older skins) or null. |
| 368 | + */ |
| 369 | + 'addPortletLink' : function( portlet, href, text, id, tooltip, accesskey, nextnode ) { |
| 370 | + |
| 371 | + // Check if there's atleast 3 arguments to prevent a TypeError |
| 372 | + if ( arguments.length < 3 ) { |
| 373 | + return null; |
| 374 | + } |
| 375 | + // Setup the anchor tag |
| 376 | + var $link = $( '<a></a>' ).attr( 'href', href ).text( text ); |
| 377 | + if ( tooltip ) { |
| 378 | + $link.attr( 'title', tooltip ); |
| 379 | + } |
| 380 | + |
| 381 | + // Some skins don't have any portlets |
| 382 | + // just add it to the bottom of their 'sidebar' element as a fallback |
| 383 | + switch ( mw.config.get( 'skin' ) ) { |
| 384 | + case 'standard' : |
| 385 | + case 'cologneblue' : |
| 386 | + $( '#quickbar' ).append( $link.after( '<br />' ) ); |
| 387 | + return $link[0]; |
| 388 | + case 'nostalgia' : |
| 389 | + $( '#searchform' ).before( $link).before( ' | ' ); |
| 390 | + return $link[0]; |
| 391 | + default : // Skins like chick, modern, monobook, myskin, simple, vector... |
| 392 | + |
| 393 | + // Select the specified portlet |
| 394 | + var $portlet = $( '#' + portlet ); |
| 395 | + if ( $portlet.length === 0 ) { |
| 396 | + return null; |
| 397 | + } |
| 398 | + // Select the first (most likely only) unordered list inside the portlet |
| 399 | + var $ul = $portlet.find( 'ul' ); |
| 400 | + |
| 401 | + // If it didn't have an unordered list yet, create it |
| 402 | + if ( $ul.length === 0 ) { |
| 403 | + // If there's no <div> inside, append it to the portlet directly |
| 404 | + if ( $portlet.find( 'div:first' ).length === 0 ) { |
| 405 | + $portlet.append( '<ul></ul>' ); |
| 406 | + } else { |
| 407 | + // otherwise if there's a div (such as div.body or div.pBody) |
| 408 | + // append the <ul> to last (most likely only) div |
| 409 | + $portlet.find( 'div' ).eq( -1 ).append( '<ul></ul>' ); |
| 410 | + } |
| 411 | + // Select the created element |
| 412 | + $ul = $portlet.find( 'ul' ).eq( 0 ); |
| 413 | + } |
| 414 | + // Just in case.. |
| 415 | + if ( $ul.length === 0 ) { |
| 416 | + return null; |
| 417 | + } |
| 418 | + |
| 419 | + // Unhide portlet if it was hidden before |
| 420 | + $portlet.removeClass( 'emptyPortlet' ); |
| 421 | + |
| 422 | + // Wrap the anchor tag in a <span> and create a list item for it |
| 423 | + // and back up the selector to the list item |
| 424 | + var $item = $link.wrap( '<li><span></span></li>' ).parent().parent(); |
| 425 | + |
| 426 | + // Implement the properties passed to the function |
| 427 | + if ( id ) { |
| 428 | + $item.attr( 'id', id ); |
| 429 | + } |
| 430 | + if ( accesskey ) { |
| 431 | + $link.attr( 'accesskey', accesskey ); |
| 432 | + tooltip += ' [' + accesskey + ']'; |
| 433 | + $link.attr( 'title', tooltip ); |
| 434 | + } |
| 435 | + if ( accesskey && tooltip ) { |
| 436 | + this.updateTooltipAccessKeys( $link ); |
| 437 | + } |
| 438 | + |
| 439 | + // Where to put our node ? |
| 440 | + // - nextnode is a DOM element (before MW 1.17, in wikibits.js, this was the only option) |
| 441 | + if ( nextnode && nextnode.parentNode == $ul[0] ) { |
| 442 | + $(nextnode).before( $item ); |
| 443 | + |
| 444 | + // - nextnode is a CSS selector for jQuery |
| 445 | + } else if ( typeof nextnode == 'string' && $ul.find( nextnode ).length !== 0 ) { |
| 446 | + $ul.find( nextnode ).eq( 0 ).before( $item ); |
| 447 | + |
| 448 | + |
| 449 | + // If the jQuery selector isn't found within the <ul>, |
| 450 | + // or if nextnode was invalid or not passed at all, |
| 451 | + // then just append it at the end of the <ul> (this is the default behaviour) |
| 452 | + } else { |
| 453 | + $ul.append( $item ); |
| 454 | + } |
| 455 | + |
| 456 | + |
| 457 | + return $item[0]; |
| 458 | + } |
| 459 | + }, |
| 460 | + |
| 461 | + /** |
| 462 | + * Add a little box at the top of the screen to inform the user of |
| 463 | + * something, replacing any previous message. |
| 464 | + * Calling with no arguments, with an empty string or null will hide the message |
| 465 | + * |
| 466 | + * @param message mixed The DOM-element or HTML-string to be put inside the message box. |
| 467 | + * @param className string Used in adding a class; should be different for each call |
| 468 | + * to allow CSS/JS to hide different boxes. null = no class used. |
| 469 | + * @return boolean True on success, false on failure |
| 470 | + */ |
| 471 | + 'jsMessage' : function( message, className ) { |
| 472 | + |
| 473 | + if ( !arguments.length || message === '' || message === null ) { |
| 474 | + |
| 475 | + $( '#mw-js-message' ).empty().hide(); |
| 476 | + return true; // Emptying and hiding message is intended behaviour, return true |
| 477 | + |
| 478 | + } else { |
| 479 | + // We special-case skin structures provided by the software. Skins that |
| 480 | + // choose to abandon or significantly modify our formatting can just define |
| 481 | + // an mw-js-message div to start with. |
| 482 | + var $messageDiv = $( '#mw-js-message' ); |
| 483 | + if ( !$messageDiv.length ) { |
| 484 | + $messageDiv = $( '<div id="mw-js-message">' ); |
| 485 | + if ( mw.util.$content.parent().length ) { |
| 486 | + mw.util.$content.parent().prepend( $messageDiv ); |
| 487 | + } else { |
| 488 | + return false; |
| 489 | + } |
| 490 | + } |
| 491 | + |
| 492 | + if ( className ) { |
| 493 | + $messageDiv.attr( 'class', 'mw-js-message-' + className ); |
| 494 | + } |
| 495 | + |
| 496 | + if ( typeof message === 'object' ) { |
| 497 | + $messageDiv.empty(); |
| 498 | + $messageDiv.append( message ); // Append new content |
| 499 | + } else { |
| 500 | + $messageDiv.html( message ); |
| 501 | + } |
| 502 | + |
| 503 | + $messageDiv.slideDown(); |
| 504 | + return true; |
| 505 | + } |
| 506 | + }, |
| 507 | + |
| 508 | + /** |
| 509 | + * Validate a string as representing a valid e-mail address |
| 510 | + * according to HTML5 specification. Please note the specification |
| 511 | + * does not validate a domain with one character. |
| 512 | + * |
| 513 | + * FIXME: should be moved to or replaced by a JavaScript validation module. |
| 514 | + */ |
| 515 | + 'validateEmail' : function( mailtxt ) { |
| 516 | + if( mailtxt === '' ) { |
| 517 | + return null; |
| 518 | + } |
| 519 | + |
| 520 | + /** |
| 521 | + * HTML5 defines a string as valid e-mail address if it matches |
| 522 | + * the ABNF: |
| 523 | + * 1 * ( atext / "." ) "@" ldh-str 1*( "." ldh-str ) |
| 524 | + * With: |
| 525 | + * - atext : defined in RFC 5322 section 3.2.3 |
| 526 | + * - ldh-str : defined in RFC 1034 section 3.5 |
| 527 | + * |
| 528 | + * (see STD 68 / RFC 5234 http://tools.ietf.org/html/std68): |
| 529 | + */ |
| 530 | + |
| 531 | + /** |
| 532 | + * First, define the RFC 5322 'atext' which is pretty easy : |
| 533 | + * atext = ALPHA / DIGIT / ; Printable US-ASCII |
| 534 | + "!" / "#" / ; characters not including |
| 535 | + "$" / "%" / ; specials. Used for atoms. |
| 536 | + "&" / "'" / |
| 537 | + "*" / "+" / |
| 538 | + "-" / "/" / |
| 539 | + "=" / "?" / |
| 540 | + "^" / "_" / |
| 541 | + "`" / "{" / |
| 542 | + "|" / "}" / |
| 543 | + "~" |
| 544 | + */ |
| 545 | + var rfc5322_atext = "a-z0-9!#$%&'*+\\-/=?^_`{|}~", |
| 546 | + |
| 547 | + /** |
| 548 | + * Next define the RFC 1034 'ldh-str' |
| 549 | + * <domain> ::= <subdomain> | " " |
| 550 | + * <subdomain> ::= <label> | <subdomain> "." <label> |
| 551 | + * <label> ::= <letter> [ [ <ldh-str> ] <let-dig> ] |
| 552 | + * <ldh-str> ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str> |
| 553 | + * <let-dig-hyp> ::= <let-dig> | "-" |
| 554 | + * <let-dig> ::= <letter> | <digit> |
| 555 | + */ |
| 556 | + rfc1034_ldh_str = "a-z0-9\\-", |
| 557 | + |
| 558 | + HTML5_email_regexp = new RegExp( |
| 559 | + // start of string |
| 560 | + '^' |
| 561 | + + |
| 562 | + // User part which is liberal :p |
| 563 | + '[' + rfc5322_atext + '\\.]+' |
| 564 | + + |
| 565 | + // 'at' |
| 566 | + '@' |
| 567 | + + |
| 568 | + // Domain first part |
| 569 | + '[' + rfc1034_ldh_str + ']+' |
| 570 | + + |
| 571 | + // Optional second part and following are separated by a dot |
| 572 | + '(?:\\.[' + rfc1034_ldh_str + ']+)*' |
| 573 | + + |
| 574 | + // End of string |
| 575 | + '$', |
| 576 | + // RegExp is case insensitive |
| 577 | + 'i' |
| 578 | + ); |
| 579 | + return (null !== mailtxt.match( HTML5_email_regexp ) ); |
| 580 | + }, |
| 581 | + // Note: borrows from IP::isIPv4 |
| 582 | + 'isIPv4Address' : function( address, allowBlock ) { |
| 583 | + var block = allowBlock ? '(?:\\/(?:3[0-2]|[12]?\\d))?' : ''; |
| 584 | + var RE_IP_BYTE = '(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|0?[0-9]?[0-9])'; |
| 585 | + var RE_IP_ADD = '(?:' + RE_IP_BYTE + '\\.){3}' + RE_IP_BYTE; |
| 586 | + return address.search( new RegExp( '^' + RE_IP_ADD + block + '$' ) ) != -1; |
| 587 | + }, |
| 588 | + // Note: borrows from IP::isIPv6 |
| 589 | + 'isIPv6Address' : function( address, allowBlock ) { |
| 590 | + var block = allowBlock ? '(?:\\/(?:12[0-8]|1[01][0-9]|[1-9]?\\d))?' : ''; |
| 591 | + var RE_IPV6_ADD = |
| 592 | + '(?:' + // starts with "::" (including "::") |
| 593 | + ':(?::|(?::' + '[0-9A-Fa-f]{1,4}' + '){1,7})' + |
| 594 | + '|' + // ends with "::" (except "::") |
| 595 | + '[0-9A-Fa-f]{1,4}' + '(?::' + '[0-9A-Fa-f]{1,4}' + '){0,6}::' + |
| 596 | + '|' + // contains no "::" |
| 597 | + '[0-9A-Fa-f]{1,4}' + '(?::' + '[0-9A-Fa-f]{1,4}' + '){7}' + |
| 598 | + ')'; |
| 599 | + if ( address.search( new RegExp( '^' + RE_IPV6_ADD + block + '$' ) ) != -1 ) { |
| 600 | + return true; |
| 601 | + } |
| 602 | + RE_IPV6_ADD = // contains one "::" in the middle (single '::' check below) |
| 603 | + '[0-9A-Fa-f]{1,4}' + '(?:::?' + '[0-9A-Fa-f]{1,4}' + '){1,6}'; |
| 604 | + return address.search( new RegExp( '^' + RE_IPV6_ADD + block + '$' ) ) != -1 |
| 605 | + && address.search( /::/ ) != -1 && address.search( /::.*::/ ) == -1; |
| 606 | + } |
| 607 | + |
| 608 | + }; |
| 609 | + |
| 610 | + mw.util.init(); |
| 611 | + |
| 612 | +} )( jQuery, mediaWiki ); |
Property changes on: trunk/phase3/resources/mediawiki.util/mediawiki.util.js |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 613 | + native |
Index: trunk/phase3/resources/Resources.php |
— | — | @@ -444,6 +444,7 @@ |
445 | 445 | 'jquery.makeCollapsible', |
446 | 446 | 'jquery.placeholder', |
447 | 447 | ), |
| 448 | + 'debugScripts' => 'resources/mediawiki.util/mediawiki.util.test.js', |
448 | 449 | ), |
449 | 450 | 'mediawiki.util.jpegmeta' => array( |
450 | 451 | 'scripts' => 'resources/mediawiki.util/mediawiki.util.jpegmeta.js', |