7f20504fd0c303548571104ab8e457f2b0a95311
2 * JavaScript backwards-compatibility alternatives and other convenience functions
6 trimLeft : function( str
) {
7 return str
== null ? '' : str
.toString().replace( /^\s+/, '' );
9 trimRight : function( str
) {
11 '' : str
.toString().replace( /\s+$/, '' );
13 ucFirst : function( str
) {
14 return str
.substr( 0, 1 ).toUpperCase() + str
.substr( 1, str
.length
);
16 escapeRE : function( str
) {
17 return str
.replace ( /([\\{}()|.?*+^$\[\]])/g, "\\$1" );
19 isEmpty : function( v
) {
21 if ( v
=== "" || v
=== 0 || v
=== "0" || v
=== null
22 || v
=== false || typeof v
=== 'undefined' )
26 // the for-loop could potentially contain prototypes
27 // to avoid that we check it's length first
28 if ( v
.length
=== 0 ) {
31 if ( typeof v
=== 'object' ) {
39 compareArray : function( arrThis
, arrAgainst
) {
40 if ( arrThis
.length
!= arrAgainst
.length
) {
43 for ( var i
= 0; i
< arrThis
.length
; i
++ ) {
44 if ( arrThis
[i
] instanceof Array
) {
45 if ( !$.compareArray( arrThis
[i
], arrAgainst
[i
] ) ) {
48 } else if ( arrThis
[i
] !== arrAgainst
[i
] ) {
57 * Core MediaWiki JavaScript Library
61 window
.mediaWiki
= new ( function( $ ) {
65 // This will not change until we are 100% ready to turn off legacy globals
66 var LEGACY_GLOBALS
= true;
70 // List of messages that have been requested to be loaded
71 var messageQueue
= {};
76 * An object which allows single and multiple get/set/exists functionality
77 * on a list of key / value pairs.
79 * @param {boolean} global Whether to get/set/exists values on the window
80 * object or a private object
82 function Map( global
) {
83 this.values
= ( global
=== true ) ? window
: {};
87 * Gets the value of a key, or a list of key/value pairs for an array of keys.
89 * If called with no arguments, all values will be returned.
91 * @param selection mixed Key or array of keys to get values for
92 * @param fallback mixed Value to use in case key(s) do not exist (optional)
94 Map
.prototype.get = function( selection
, fallback
) {
95 if ( typeof selection
=== 'object' ) {
96 selection
= $.makeArray( selection
);
98 for ( var i
= 0; i
< selection
.length
; i
++ ) {
99 results
[selection
[i
]] = this.get( selection
[i
], fallback
);
102 } else if ( typeof selection
=== 'string' ) {
103 if ( typeof this.values
[selection
] === 'undefined' ) {
104 if ( typeof fallback
!== 'undefined' ) {
109 return this.values
[selection
];
115 * Sets one or multiple key/value pairs.
117 * @param selection mixed Key or object of key/value pairs to set
118 * @param value mixed Value to set (optional, only in use when key is a string)
120 Map
.prototype.set = function( selection
, value
) {
121 if ( typeof selection
=== 'object' ) {
122 for ( var s
in selection
) {
123 this.values
[s
] = selection
[s
];
125 } else if ( typeof selection
=== 'string' && typeof value
!== 'undefined' ) {
126 this.values
[selection
] = value
;
131 * Checks if one or multiple keys exist.
133 * @param selection mixed Key or array of keys to check
134 * @return boolean Existence of key(s)
136 Map
.prototype.exists = function( selection
) {
137 if ( typeof keys
=== 'object' ) {
138 for ( var s
= 0; s
< selection
.length
; s
++ ) {
139 if ( !( selection
[s
] in this.values
) ) {
145 return selection
in this.values
;
150 * Message object, similar to Message in PHP
152 function Message( map
, key
, parameters
) {
153 this.format
= 'parse';
156 this.parameters
= typeof parameters
=== 'undefined' ? [] : $.makeArray( parameters
);
160 * Appends parameters for replacement
162 * @param parameters mixed First in a list of variadic arguments to append as message parameters
164 Message
.prototype.params = function( parameters
) {
165 for ( var i
= 0; i
< parameters
.length
; i
++ ) {
166 this.parameters
[this.parameters
.length
] = parameters
[i
];
172 * Converts message object to it's string form based on the state of format
174 * @return {string} String form of message
176 Message
.prototype.toString = function() {
177 if ( !this.map
.exists( this.key
) ) {
178 // Return <key> if key does not exist
179 return '<' + this.key
+ '>';
181 var text
= this.map
.get( this.key
);
182 var parameters
= this.parameters
;
183 text
= text
.replace( /\$(\d+)/g, function( string
, match
) {
184 var index
= parseInt( match
, 10 ) - 1;
185 return index
in parameters
? parameters
[index
] : '$' + match
;
187 /* This should be fixed up when we have a parser
188 if ( this.format === 'parse' && 'language' in mediaWiki ) {
189 text = mediaWiki.language.parse( text );
196 * Changes format to parse and converts message to string
198 * @return {string} String form of parsed message
200 Message
.prototype.parse = function() {
201 this.format
= 'parse';
202 return this.toString();
206 * Changes format to plain and converts message to string
208 * @return {string} String form of plain message
210 Message
.prototype.plain = function() {
211 this.format
= 'plain';
212 return this.toString();
216 * Checks if message exists
218 * @return {string} String form of parsed message
220 Message
.prototype.exists = function() {
221 return this.map
.exists( this.key
);
229 /* Private Members */
235 this.options
= new Map();
240 * Generates a random user session ID (32 alpha-numeric characters).
242 * This information would potentially be stored in a cookie to identify a user during a
243 * session. It's uniqueness should not be depended on.
245 * @return string random set of 32 alpha-numeric characters
247 function generateSessionId() {
249 var seed
= '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz';
250 for ( var i
= 0, r
; i
< 32; i
++ ) {
251 r
= Math
.floor( Math
.random() * seed
.length
);
252 id
+= seed
.substring( r
, r
+ 1 );
258 * Gets the current user's name.
260 * @return mixed user name string or null if users is anonymous
262 this.name = function() {
263 return mediaWiki
.config
.get( 'wgUserName' );
267 * Gets the current user's name or a random session ID automatically generated and kept in
270 * @return string user name or random session ID
272 this.sessionId = function () {
273 var name
= that
.name();
277 var sessionId
= $.cookie( 'mediaWiki.user.sessionId' );
278 if ( typeof sessionId
== 'undefined' || sessionId
== null ) {
279 sessionId
= generateSessionId();
280 $.cookie( 'mediaWiki.user.sessionId', sessionId
, { 'expires': 30, 'path': '/' } );
289 * Dummy function which in debug mode can be replaced with a function that
290 * does something clever
292 this.log = function() { };
295 * Make the Map-class publicly available
300 * List of configuration values
302 * In legacy mode the values this object wraps will be in the global space
304 this.config
= new this.Map( LEGACY_GLOBALS
);
307 * Information about the current user
309 this.user
= new User();
312 * Localization system
314 this.messages
= new this.Map();
319 * Gets a message object, similar to wfMessage()
321 * @param key string Key of message to get
322 * @param parameters mixed First argument in a list of variadic arguments, each a parameter for $
325 this.message = function( key
, parameters
) {
326 // Support variadic arguments
327 if ( typeof parameters
!== 'undefined' ) {
328 parameters
= $.makeArray( arguments
);
333 return new Message( mediaWiki
.messages
, key
, parameters
);
337 * Gets a message string, similar to wfMsg()
339 * @param key string Key of message to get
340 * @param parameters mixed First argument in a list of variadic arguments, each a parameter for $
343 this.msg = function( key
, parameters
) {
344 return mediaWiki
.message
.apply( mediaWiki
.message
, arguments
).toString();
348 * Client-side module loader which integrates with the MediaWiki ResourceLoader
350 this.loader
= new ( function() {
352 /* Private Members */
355 * Mapping of registered modules
357 * The jquery module is pre-registered, because it must have already
358 * been provided for this object to have been built, and in debug mode
359 * jquery would have been provided through a unique loader request,
360 * making it impossible to hold back registration of jquery until after
366 * 'dependencies': ['required module', 'required module', ...], (or) function() {}
367 * 'state': 'registered', 'loading', 'loaded', 'ready', or 'error'
368 * 'script': function() {},
369 * 'style': 'css code string',
370 * 'messages': { 'key': 'value' },
371 * 'version': ############## (unix timestamp)
376 // List of modules which will be loaded as when ready
378 // List of modules to be loaded
380 // List of callback functions waiting for modules to be ready to be called
382 // Flag indicating that requests should be suspended
383 var suspended
= true;
384 // Flag inidicating that document ready has occured
387 /* Private Methods */
389 function compare( a
, b
) {
390 if ( a
.length
!= b
.length
) {
393 for ( var i
= 0; i
< b
.length
; i
++ ) {
394 if ( $.isArray( a
[i
] ) ) {
395 if ( !compare( a
[i
], b
[i
] ) ) {
399 if ( a
[i
] !== b
[i
] ) {
407 * Generates an ISO8601 "basic" string from a UNIX timestamp
409 function formatVersionNumber( timestamp
) {
410 function pad( a
, b
, c
) {
411 return [a
< 10 ? '0' + a
: a
, b
< 10 ? '0' + b
: b
, c
< 10 ? '0' + c
: c
].join( '' );
414 d
.setTime( timestamp
* 1000 );
416 pad( d
.getUTCFullYear(), d
.getUTCMonth() + 1, d
.getUTCDate() ), 'T',
417 pad( d
.getUTCHours(), d
.getUTCMinutes(), d
.getUTCSeconds() ), 'Z'
422 * Recursively resolves dependencies and detects circular references
424 function recurse( module
, resolved
, unresolved
) {
425 if ( typeof registry
[module
] === 'undefined' ) {
426 throw new Error( 'Unknown dependency: ' + module
);
428 // Resolves dynamic loader function and replaces it with its own results
429 if ( typeof registry
[module
].dependencies
=== 'function' ) {
430 registry
[module
].dependencies
= registry
[module
].dependencies();
431 // Ensures the module's dependencies are always in an array
432 if ( typeof registry
[module
].dependencies
!== 'object' ) {
433 registry
[module
].dependencies
= [registry
[module
].dependencies
];
436 // Tracks down dependencies
437 for ( var n
= 0; n
< registry
[module
].dependencies
.length
; n
++ ) {
438 if ( $.inArray( registry
[module
].dependencies
[n
], resolved
) === -1 ) {
439 if ( $.inArray( registry
[module
].dependencies
[n
], unresolved
) !== -1 ) {
441 'Circular reference detected: ' + module
+
442 ' -> ' + registry
[module
].dependencies
[n
]
445 recurse( registry
[module
].dependencies
[n
], resolved
, unresolved
);
448 resolved
[resolved
.length
] = module
;
449 unresolved
.splice( $.inArray( module
, unresolved
), 1 );
453 * Gets a list of module names that a module depends on in their proper dependency order
455 * @param module string module name or array of string module names
456 * @return list of dependencies
457 * @throws Error if circular reference is detected
459 function resolve( module
, resolved
, unresolved
) {
460 // Allow calling with an array of module names
461 if ( typeof module
=== 'object' ) {
463 for ( var m
= 0; m
< module
.length
; m
++ ) {
464 var dependencies
= resolve( module
[m
] );
465 for ( var n
= 0; n
< dependencies
.length
; n
++ ) {
466 modules
[modules
.length
] = dependencies
[n
];
470 } else if ( typeof module
=== 'string' ) {
471 // Undefined modules have no dependencies
472 if ( !( module
in registry
) ) {
476 recurse( module
, resolved
, [] );
479 throw new Error( 'Invalid module argument: ' + module
);
483 * Narrows a list of module names down to those matching a specific
484 * state. Possible states are 'undefined', 'registered', 'loading',
485 * 'loaded', or 'ready'
487 * @param states string or array of strings of module states to filter by
488 * @param modules array list of module names to filter (optional, all modules
489 * will be used by default)
490 * @return array list of filtered module names
492 function filter( states
, modules
) {
493 // Allow states to be given as a string
494 if ( typeof states
=== 'string' ) {
497 // If called without a list of modules, build and use a list of all modules
499 if ( typeof modules
=== 'undefined' ) {
501 for ( module
in registry
) {
502 modules
[modules
.length
] = module
;
505 // Build a list of modules which are in one of the specified states
506 for ( var s
= 0; s
< states
.length
; s
++ ) {
507 for ( var m
= 0; m
< modules
.length
; m
++ ) {
508 if ( typeof registry
[modules
[m
]] === 'undefined' ) {
509 // Module does not exist
510 if ( states
[s
] == 'undefined' ) {
512 list
[list
.length
] = modules
[m
];
515 // Module exists, check state
516 if ( registry
[modules
[m
]].state
=== states
[s
] ) {
518 list
[list
.length
] = modules
[m
];
527 * Executes a loaded module, making it ready to use
529 * @param module string module name to execute
531 function execute( module
) {
532 if ( typeof registry
[module
] === 'undefined' ) {
533 throw new Error( 'Module has not been registered yet: ' + module
);
534 } else if ( registry
[module
].state
=== 'registered' ) {
535 throw new Error( 'Module has not been requested from the server yet: ' + module
);
536 } else if ( registry
[module
].state
=== 'loading' ) {
537 throw new Error( 'Module has not completed loading yet: ' + module
);
538 } else if ( registry
[module
].state
=== 'ready' ) {
539 throw new Error( 'Module has already been loaded: ' + module
);
541 // Add style sheet to document
542 if ( typeof registry
[module
].style
=== 'string' && registry
[module
].style
.length
) {
544 .append( mediaWiki
.html
.element( 'style',
545 { type
: "text/css" },
546 new mediaWiki
.html
.Cdata( registry
[module
].style
)
548 } else if ( typeof registry
[module
].style
=== 'object'
549 && !( registry
[module
].style
instanceof Array
) )
551 for ( var media
in registry
[module
].style
) {
552 $( 'head' ).append( mediaWiki
.html
.element( 'style',
553 { type
: 'text/css', media
: media
},
554 new mediaWiki
.html
.Cdata( registry
[module
].style
[media
] )
558 // Add localizations to message system
559 if ( typeof registry
[module
].messages
=== 'object' ) {
560 mediaWiki
.messages
.set( registry
[module
].messages
);
564 registry
[module
].script();
565 registry
[module
].state
= 'ready';
566 // Run jobs who's dependencies have just been met
567 for ( var j
= 0; j
< jobs
.length
; j
++ ) {
569 filter( 'ready', jobs
[j
].dependencies
),
570 jobs
[j
].dependencies
) )
572 if ( typeof jobs
[j
].ready
=== 'function' ) {
579 // Execute modules who's dependencies have just been met
580 for ( r
in registry
) {
581 if ( registry
[r
].state
== 'loaded' ) {
583 filter( ['ready'], registry
[r
].dependencies
),
584 registry
[r
].dependencies
) )
591 mediaWiki
.log( 'Exception thrown by ' + module
+ ': ' + e
.message
);
593 registry
[module
].state
= 'error';
594 // Run error callbacks of jobs affected by this condition
595 for ( var j
= 0; j
< jobs
.length
; j
++ ) {
596 if ( $.inArray( module
, jobs
[j
].dependencies
) !== -1 ) {
597 if ( typeof jobs
[j
].error
=== 'function' ) {
608 * Adds a dependencies to the queue with optional callbacks to be run
609 * when the dependencies are ready or fail
611 * @param dependencies string module name or array of string module names
612 * @param ready function callback to execute when all dependencies are ready
613 * @param error function callback to execute when any dependency fails
615 function request( dependencies
, ready
, error
) {
616 // Allow calling by single module name
617 if ( typeof dependencies
=== 'string' ) {
618 dependencies
= [dependencies
];
619 if ( dependencies
[0] in registry
) {
620 for ( var n
= 0; n
< registry
[dependencies
[0]].dependencies
.length
; n
++ ) {
621 dependencies
[dependencies
.length
] =
622 registry
[dependencies
[0]].dependencies
[n
];
626 // Add ready and error callbacks if they were given
627 if ( arguments
.length
> 1 ) {
628 jobs
[jobs
.length
] = {
629 'dependencies': filter(
630 ['undefined', 'registered', 'loading', 'loaded'],
636 // Queue up any dependencies that are undefined or registered
637 dependencies
= filter( ['undefined', 'registered'], dependencies
);
638 for ( var n
= 0; n
< dependencies
.length
; n
++ ) {
639 if ( $.inArray( dependencies
[n
], queue
) === -1 ) {
640 queue
[queue
.length
] = dependencies
[n
];
644 mediaWiki
.loader
.work();
647 function sortQuery(o
) {
648 var sorted
= {}, key
, a
= [];
650 if ( o
.hasOwnProperty( key
) ) {
655 for ( key
= 0; key
< a
.length
; key
++ ) {
656 sorted
[a
[key
]] = o
[a
[key
]];
664 * Requests dependencies from server, loading and executing when things when ready.
666 this.work = function() {
667 // Appends a list of modules to the batch
668 for ( var q
= 0; q
< queue
.length
; q
++ ) {
669 // Only request modules which are undefined or registered
670 if ( !( queue
[q
] in registry
) || registry
[queue
[q
]].state
== 'registered' ) {
671 // Prevent duplicate entries
672 if ( $.inArray( queue
[q
], batch
) === -1 ) {
673 batch
[batch
.length
] = queue
[q
];
674 // Mark registered modules as loading
675 if ( queue
[q
] in registry
) {
676 registry
[queue
[q
]].state
= 'loading';
681 // Clean up the queue
683 // After document ready, handle the batch
684 if ( !suspended
&& batch
.length
) {
685 // Always order modules alphabetically to help reduce cache
686 // misses for otherwise identical content
688 // Build a list of request parameters
690 'skin': mediaWiki
.config
.get( 'skin' ),
691 'lang': mediaWiki
.config
.get( 'wgUserLanguage' ),
692 'debug': mediaWiki
.config
.get( 'debug' )
694 // Extend request parameters with a list of modules in the batch
698 for ( var b
= 0; b
< batch
.length
; b
++ ) {
699 var group
= registry
[batch
[b
]].group
;
700 if ( !( group
in groups
) ) {
703 groups
[group
][groups
[group
].length
] = batch
[b
];
705 for ( var group
in groups
) {
706 // Calculate the highest timestamp
708 for ( var g
= 0; g
< groups
[group
].length
; g
++ ) {
709 if ( registry
[groups
[group
][g
]].version
> version
) {
710 version
= registry
[groups
[group
][g
]].version
;
713 requests
[requests
.length
] = $.extend(
714 { 'modules': groups
[group
].join( '|' ), 'version': formatVersionNumber( version
) }, base
717 // Clear the batch - this MUST happen before we append the
718 // script element to the body or it's possible that the script
719 // will be locally cached, instantly load, and work the batch
720 // again, all before we've cleared it causing each request to
721 // include modules which are already loaded
723 // Asynchronously append a script tag to the end of the body
726 for ( var r
= 0; r
< requests
.length
; r
++ ) {
727 requests
[r
] = sortQuery( requests
[r
] );
728 // Build out the HTML
729 var src
= mediaWiki
.config
.get( 'wgLoadScript' ) + '?' + $.param( requests
[r
] );
730 html
+= mediaWiki
.html
.element( 'script',
731 { type
: 'text/javascript', src
: src
}, '' );
735 // Load asynchronously after doumument ready
737 setTimeout( function() { $( 'body' ).append( request() ); }, 0 )
739 document
.write( request() );
745 * Registers a module, letting the system know about it and its
746 * dependencies. loader.js files contain calls to this function.
748 this.register = function( module
, version
, dependencies
, group
) {
749 // Allow multiple registration
750 if ( typeof module
=== 'object' ) {
751 for ( var m
= 0; m
< module
.length
; m
++ ) {
752 if ( typeof module
[m
] === 'string' ) {
753 mediaWiki
.loader
.register( module
[m
] );
754 } else if ( typeof module
[m
] === 'object' ) {
755 mediaWiki
.loader
.register
.apply( mediaWiki
.loader
, module
[m
] );
761 if ( typeof module
!== 'string' ) {
762 throw new Error( 'module must be a string, not a ' + typeof module
);
764 if ( typeof registry
[module
] !== 'undefined' ) {
765 throw new Error( 'module already implemeneted: ' + module
);
767 // List the module as registered
769 'state': 'registered',
770 'group': typeof group
=== 'string' ? group
: null,
772 'version': typeof version
!== 'undefined' ? parseInt( version
) : 0
774 if ( typeof dependencies
=== 'string' ) {
775 // Allow dependencies to be given as a single module name
776 registry
[module
].dependencies
= [dependencies
];
777 } else if ( typeof dependencies
=== 'object' || typeof dependencies
=== 'function' ) {
778 // Allow dependencies to be given as an array of module names
779 // or a function which returns an array
780 registry
[module
].dependencies
= dependencies
;
785 * Implements a module, giving the system a course of action to take
786 * upon loading. Results of a request for one or more modules contain
787 * calls to this function.
789 this.implement = function( module
, script
, style
, localization
) {
790 // Automatically register module
791 if ( typeof registry
[module
] === 'undefined' ) {
792 mediaWiki
.loader
.register( module
);
795 if ( typeof script
!== 'function' ) {
796 throw new Error( 'script must be a function, not a ' + typeof script
);
798 if ( typeof style
!== 'undefined'
799 && typeof style
!== 'string'
800 && typeof style
!== 'object' )
802 throw new Error( 'style must be a string or object, not a ' + typeof style
);
804 if ( typeof localization
!== 'undefined'
805 && typeof localization
!== 'object' )
807 throw new Error( 'localization must be an object, not a ' + typeof localization
);
809 if ( typeof registry
[module
] !== 'undefined'
810 && typeof registry
[module
].script
!== 'undefined' )
812 throw new Error( 'module already implemeneted: ' + module
);
814 // Mark module as loaded
815 registry
[module
].state
= 'loaded';
817 registry
[module
].script
= script
;
818 if ( typeof style
=== 'string'
819 || typeof style
=== 'object' && !( style
instanceof Array
) )
821 registry
[module
].style
= style
;
823 if ( typeof localization
=== 'object' ) {
824 registry
[module
].messages
= localization
;
826 // Execute or queue callback
828 filter( ['ready'], registry
[module
].dependencies
),
829 registry
[module
].dependencies
) )
838 * Executes a function as soon as one or more required modules are ready
840 * @param dependencies string or array of strings of modules names the callback
841 * dependencies to be ready before
843 * @param ready function callback to execute when all dependencies are ready (optional)
844 * @param error function callback to execute when if dependencies have a errors (optional)
846 this.using = function( dependencies
, ready
, error
) {
848 if ( typeof dependencies
!== 'object' && typeof dependencies
!== 'string' ) {
849 throw new Error( 'dependencies must be a string or an array, not a ' +
850 typeof dependencies
)
852 // Allow calling with a single dependency as a string
853 if ( typeof dependencies
=== 'string' ) {
854 dependencies
= [dependencies
];
856 // Resolve entire dependency map
857 dependencies
= resolve( dependencies
);
858 // If all dependencies are met, execute ready immediately
859 if ( compare( filter( ['ready'], dependencies
), dependencies
) ) {
860 if ( typeof ready
=== 'function' ) {
864 // If any dependencies have errors execute error immediately
865 else if ( filter( ['error'], dependencies
).length
) {
866 if ( typeof error
=== 'function' ) {
870 // Since some dependencies are not yet ready, queue up a request
872 request( dependencies
, ready
, error
);
877 * Loads an external script or one or more modules for future use
879 * @param modules mixed either the name of a module, array of modules,
880 * or a URL of an external script or style
881 * @param type string mime-type to use if calling with a URL of an
882 * external script or style; acceptable values are "text/css" and
883 * "text/javascript"; if no type is provided, text/javascript is
886 this.load = function( modules
, type
) {
888 if ( typeof modules
!== 'object' && typeof modules
!== 'string' ) {
889 throw new Error( 'dependencies must be a string or an array, not a ' +
890 typeof dependencies
)
892 // Allow calling with an external script or single dependency as a string
893 if ( typeof modules
=== 'string' ) {
894 // Support adding arbitrary external scripts
895 if ( modules
.substr( 0, 7 ) == 'http://'
896 || modules
.substr( 0, 8 ) == 'https://' )
898 if ( type
=== 'text/css' ) {
900 .append( $( '<link rel="stylesheet" type="text/css" />' )
901 .attr( 'href', modules
) );
903 } else if ( type
=== 'text/javascript' || typeof type
=== 'undefined' ) {
904 var script
= mediaWiki
.html
.element( 'script',
905 { type
: 'text/javascript', src
: modules
}, '' );
907 $( 'body' ).append( script
);
909 document
.write( script
);
916 // Called with single module
919 // Resolve entire dependency map
920 modules
= resolve( modules
);
921 // If all modules are ready, nothing dependency be done
922 if ( compare( filter( ['ready'], modules
), modules
) ) {
925 // If any modules have errors return false
926 else if ( filter( ['error'], modules
).length
) {
929 // Since some modules are not yet ready, queue up a request
937 * Flushes the request queue and begin executing load requests on demand
939 this.go = function() {
941 mediaWiki
.loader
.work();
945 * Changes the state of a module
947 * @param module string module name or object of module name/state pairs
948 * @param state string state name
950 this.state = function( module
, state
) {
951 if ( typeof module
=== 'object' ) {
952 for ( var m
in module
) {
953 mediaWiki
.loader
.state( m
, module
[m
] );
957 if ( !( module
in registry
) ) {
958 mediaWiki
.loader
.register( module
);
960 registry
[module
].state
= state
;
964 * Gets the version of a module
966 * @param module string name of module to get version for
968 this.version = function( module
) {
969 if ( module
in registry
&& 'version' in registry
[module
] ) {
970 return formatVersionNumber( registry
[module
].version
);
975 /* Cache document ready status */
977 $(document
).ready( function() { ready
= true; } );
980 /** HTML construction helper functions */
981 this.html
= new ( function () {
982 function escapeCallback( s
) {
998 * Escape a string for HTML. Converts special characters to HTML entities.
999 * @param s The string to escape
1001 this.escape = function( s
) {
1002 return s
.replace( /['"<>&]/g, escapeCallback
);
1006 * Wrapper object for raw HTML passed to mediaWiki.html.element().
1008 this.Raw = function( value
) {
1013 * Wrapper object for CDATA element contents passed to mediaWiki.html.element()
1015 this.Cdata = function( value
) {
1020 * Create an HTML element string, with safe escaping.
1022 * @param name The tag name.
1023 * @param attrs An object with members mapping element names to values
1024 * @param contents The contents of the element. May be either:
1025 * - string: The string is escaped.
1026 * - null or undefined: The short closing form is used, e.g. <br/>.
1027 * - this.Raw: The value attribute is included without escaping.
1028 * - this.Cdata: The value attribute is included, and an exception is
1029 * thrown if it contains an illegal ETAGO delimiter.
1030 * See http://www.w3.org/TR/1999/REC-html401-19991224/appendix/notes.html#h-B.3.2
1033 * var h = mediaWiki.html;
1034 * return h.element( 'div', {},
1035 * new h.Raw( h.element( 'img', {src: '<'} ) ) );
1036 * Returns <div><img src="<"/></div>
1038 this.element = function( name
, attrs
, contents
) {
1040 for ( attrName
in attrs
) {
1041 s
+= ' ' + attrName
+ '="' + this.escape( attrs
[attrName
] ) + '"';
1043 if ( typeof contents
== 'undefined' || contents
=== null ) {
1050 if (typeof contents
=== 'string') {
1052 s
+= this.escape( contents
);
1053 } else if ( contents
instanceof this.Raw
) {
1054 // Raw HTML inclusion
1055 s
+= contents
.value
;
1056 } else if ( contents
instanceof this.Cdata
) {
1058 if ( /<\/[a-zA-z]/.test( contents
.value
) ) {
1059 throw new Error( 'mw.html.element: Illegal end tag found in CDATA' );
1061 s
+= contents
.value
;
1063 throw new Error( 'mw.html.element: Invalid type of contents' );
1065 s
+= '</' + name
+ '>';
1071 /* Extension points */
1077 /* Auto-register from pre-loaded startup scripts */
1079 if ( typeof startUp
=== 'function' ) {
1084 // Alias $j to jQuery for backwards compatibility
1086 window
.mw
= mediaWiki
;