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
);
228 this.options
= new Map();
234 * Dummy function which in debug mode can be replaced with a function that
235 * does something clever
237 this.log = function() { };
240 * List of configuration values
242 * In legacy mode the values this object wraps will be in the global space
244 this.config
= new Map( LEGACY_GLOBALS
);
247 * Information about the current user
249 this.user
= new User();
252 * Localization system
254 this.messages
= new Map();
259 * Gets a message object, similar to wfMessage()
261 * @param key string Key of message to get
262 * @param parameters mixed First argument in a list of variadic arguments, each a parameter for $
265 this.message = function( key
, parameters
) {
266 // Support variadic arguments
267 if ( typeof parameters
!== 'undefined' ) {
268 parameters
= $.makeArray( arguments
);
273 return new Message( mediaWiki
.messages
, key
, parameters
);
277 * Gets a message string, similar to wfMsg()
279 * @param key string Key of message to get
280 * @param parameters mixed First argument in a list of variadic arguments, each a parameter for $
283 this.msg = function( key
, parameters
) {
284 return mediaWiki
.message
.apply( mediaWiki
.message
, arguments
).toString();
288 * Client-side module loader which integrates with the MediaWiki ResourceLoader
290 this.loader
= new ( function() {
292 /* Private Members */
295 * Mapping of registered modules
297 * The jquery module is pre-registered, because it must have already
298 * been provided for this object to have been built, and in debug mode
299 * jquery would have been provided through a unique loader request,
300 * making it impossible to hold back registration of jquery until after
306 * 'dependencies': ['required module', 'required module', ...], (or) function() {}
307 * 'state': 'registered', 'loading', 'loaded', 'ready', or 'error'
308 * 'script': function() {},
309 * 'style': 'css code string',
310 * 'messages': { 'key': 'value' },
311 * 'version': ############## (unix timestamp)
316 // List of modules which will be loaded as when ready
318 // List of modules to be loaded
320 // List of callback functions waiting for modules to be ready to be called
322 // Flag indicating that requests should be suspended
323 var suspended
= true;
324 // Flag inidicating that document ready has occured
327 /* Private Methods */
329 function compare( a
, b
) {
330 if ( a
.length
!= b
.length
) {
333 for ( var i
= 0; i
< b
.length
; i
++ ) {
334 if ( $.isArray( a
[i
] ) ) {
335 if ( !compare( a
[i
], b
[i
] ) ) {
339 if ( a
[i
] !== b
[i
] ) {
347 * Generates an ISO8601 "basic" string from a UNIX timestamp
349 function formatVersionNumber( timestamp
) {
350 function pad( a
, b
, c
) {
351 return [a
< 10 ? '0' + a
: a
, b
< 10 ? '0' + b
: b
, c
< 10 ? '0' + c
: c
].join( '' );
354 d
.setTime( timestamp
* 1000 );
356 pad( d
.getUTCFullYear(), d
.getUTCMonth() + 1, d
.getUTCDate() ), 'T',
357 pad( d
.getUTCHours(), d
.getUTCMinutes(), d
.getUTCSeconds() ), 'Z'
362 * Recursively resolves dependencies and detects circular references
364 function recurse( module
, resolved
, unresolved
) {
365 if ( typeof registry
[module
] === 'undefined' ) {
366 throw new Error( 'Unknown dependency: ' + module
);
368 // Resolves dynamic loader function and replaces it with its own results
369 if ( typeof registry
[module
].dependencies
=== 'function' ) {
370 registry
[module
].dependencies
= registry
[module
].dependencies();
371 // Ensures the module's dependencies are always in an array
372 if ( typeof registry
[module
].dependencies
!== 'object' ) {
373 registry
[module
].dependencies
= [registry
[module
].dependencies
];
376 // Tracks down dependencies
377 for ( var n
= 0; n
< registry
[module
].dependencies
.length
; n
++ ) {
378 if ( $.inArray( registry
[module
].dependencies
[n
], resolved
) === -1 ) {
379 if ( $.inArray( registry
[module
].dependencies
[n
], unresolved
) !== -1 ) {
381 'Circular reference detected: ' + module
+
382 ' -> ' + registry
[module
].dependencies
[n
]
385 recurse( registry
[module
].dependencies
[n
], resolved
, unresolved
);
388 resolved
[resolved
.length
] = module
;
389 unresolved
.splice( $.inArray( module
, unresolved
), 1 );
393 * Gets a list of module names that a module depends on in their proper dependency order
395 * @param module string module name or array of string module names
396 * @return list of dependencies
397 * @throws Error if circular reference is detected
399 function resolve( module
, resolved
, unresolved
) {
400 // Allow calling with an array of module names
401 if ( typeof module
=== 'object' ) {
403 for ( var m
= 0; m
< module
.length
; m
++ ) {
404 var dependencies
= resolve( module
[m
] );
405 for ( var n
= 0; n
< dependencies
.length
; n
++ ) {
406 modules
[modules
.length
] = dependencies
[n
];
410 } else if ( typeof module
=== 'string' ) {
411 // Undefined modules have no dependencies
412 if ( !( module
in registry
) ) {
416 recurse( module
, resolved
, [] );
419 throw new Error( 'Invalid module argument: ' + module
);
423 * Narrows a list of module names down to those matching a specific
424 * state. Possible states are 'undefined', 'registered', 'loading',
425 * 'loaded', or 'ready'
427 * @param states string or array of strings of module states to filter by
428 * @param modules array list of module names to filter (optional, all modules
429 * will be used by default)
430 * @return array list of filtered module names
432 function filter( states
, modules
) {
433 // Allow states to be given as a string
434 if ( typeof states
=== 'string' ) {
437 // If called without a list of modules, build and use a list of all modules
439 if ( typeof modules
=== 'undefined' ) {
441 for ( module
in registry
) {
442 modules
[modules
.length
] = module
;
445 // Build a list of modules which are in one of the specified states
446 for ( var s
= 0; s
< states
.length
; s
++ ) {
447 for ( var m
= 0; m
< modules
.length
; m
++ ) {
448 if ( typeof registry
[modules
[m
]] === 'undefined' ) {
449 // Module does not exist
450 if ( states
[s
] == 'undefined' ) {
452 list
[list
.length
] = modules
[m
];
455 // Module exists, check state
456 if ( registry
[modules
[m
]].state
=== states
[s
] ) {
458 list
[list
.length
] = modules
[m
];
467 * Executes a loaded module, making it ready to use
469 * @param module string module name to execute
471 function execute( module
) {
472 if ( typeof registry
[module
] === 'undefined' ) {
473 throw new Error( 'Module has not been registered yet: ' + module
);
474 } else if ( registry
[module
].state
=== 'registered' ) {
475 throw new Error( 'Module has not been requested from the server yet: ' + module
);
476 } else if ( registry
[module
].state
=== 'loading' ) {
477 throw new Error( 'Module has not completed loading yet: ' + module
);
478 } else if ( registry
[module
].state
=== 'ready' ) {
479 throw new Error( 'Module has already been loaded: ' + module
);
481 // Add style sheet to document
482 if ( typeof registry
[module
].style
=== 'string' && registry
[module
].style
.length
) {
484 .append( mediaWiki
.html
.element( 'style',
485 { type
: "text/css" },
486 new mediaWiki
.html
.Cdata( registry
[module
].style
)
488 } else if ( typeof registry
[module
].style
=== 'object'
489 && !( registry
[module
].style
instanceof Array
) )
491 for ( var media
in registry
[module
].style
) {
492 $( 'head' ).append( mediaWiki
.html
.element( 'style',
493 { type
: 'text/css', media
: media
},
494 new mediaWiki
.html
.Cdata( registry
[module
].style
[media
] )
498 // Add localizations to message system
499 if ( typeof registry
[module
].messages
=== 'object' ) {
500 mediaWiki
.messages
.set( registry
[module
].messages
);
504 registry
[module
].script();
505 registry
[module
].state
= 'ready';
506 // Run jobs who's dependencies have just been met
507 for ( var j
= 0; j
< jobs
.length
; j
++ ) {
509 filter( 'ready', jobs
[j
].dependencies
),
510 jobs
[j
].dependencies
) )
512 if ( typeof jobs
[j
].ready
=== 'function' ) {
519 // Execute modules who's dependencies have just been met
520 for ( r
in registry
) {
521 if ( registry
[r
].state
== 'loaded' ) {
523 filter( ['ready'], registry
[r
].dependencies
),
524 registry
[r
].dependencies
) )
531 mediaWiki
.log( 'Exception thrown by ' + module
+ ': ' + e
.message
);
533 registry
[module
].state
= 'error';
534 // Run error callbacks of jobs affected by this condition
535 for ( var j
= 0; j
< jobs
.length
; j
++ ) {
536 if ( $.inArray( module
, jobs
[j
].dependencies
) !== -1 ) {
537 if ( typeof jobs
[j
].error
=== 'function' ) {
548 * Adds a dependencies to the queue with optional callbacks to be run
549 * when the dependencies are ready or fail
551 * @param dependencies string module name or array of string module names
552 * @param ready function callback to execute when all dependencies are ready
553 * @param error function callback to execute when any dependency fails
555 function request( dependencies
, ready
, error
) {
556 // Allow calling by single module name
557 if ( typeof dependencies
=== 'string' ) {
558 dependencies
= [dependencies
];
559 if ( dependencies
[0] in registry
) {
560 for ( var n
= 0; n
< registry
[dependencies
[0]].dependencies
.length
; n
++ ) {
561 dependencies
[dependencies
.length
] =
562 registry
[dependencies
[0]].dependencies
[n
];
566 // Add ready and error callbacks if they were given
567 if ( arguments
.length
> 1 ) {
568 jobs
[jobs
.length
] = {
569 'dependencies': filter(
570 ['undefined', 'registered', 'loading', 'loaded'],
576 // Queue up any dependencies that are undefined or registered
577 dependencies
= filter( ['undefined', 'registered'], dependencies
);
578 for ( var n
= 0; n
< dependencies
.length
; n
++ ) {
579 if ( $.inArray( dependencies
[n
], queue
) === -1 ) {
580 queue
[queue
.length
] = dependencies
[n
];
584 mediaWiki
.loader
.work();
587 function sortQuery(o
) {
588 var sorted
= {}, key
, a
= [];
590 if ( o
.hasOwnProperty( key
) ) {
595 for ( key
= 0; key
< a
.length
; key
++ ) {
596 sorted
[a
[key
]] = o
[a
[key
]];
604 * Requests dependencies from server, loading and executing when things when ready.
606 this.work = function() {
607 // Appends a list of modules to the batch
608 for ( var q
= 0; q
< queue
.length
; q
++ ) {
609 // Only request modules which are undefined or registered
610 if ( !( queue
[q
] in registry
) || registry
[queue
[q
]].state
== 'registered' ) {
611 // Prevent duplicate entries
612 if ( $.inArray( queue
[q
], batch
) === -1 ) {
613 batch
[batch
.length
] = queue
[q
];
614 // Mark registered modules as loading
615 if ( queue
[q
] in registry
) {
616 registry
[queue
[q
]].state
= 'loading';
621 // Clean up the queue
623 // After document ready, handle the batch
624 if ( !suspended
&& batch
.length
) {
625 // Always order modules alphabetically to help reduce cache
626 // misses for otherwise identical content
628 // Build a list of request parameters
630 'skin': mediaWiki
.config
.get( 'skin' ),
631 'lang': mediaWiki
.config
.get( 'wgUserLanguage' ),
632 'debug': mediaWiki
.config
.get( 'debug' )
634 // Extend request parameters with a list of modules in the batch
638 for ( var b
= 0; b
< batch
.length
; b
++ ) {
639 var group
= registry
[batch
[b
]].group
;
640 if ( !( group
in groups
) ) {
643 groups
[group
][groups
[group
].length
] = batch
[b
];
645 for ( var group
in groups
) {
646 // Calculate the highest timestamp
648 for ( var g
= 0; g
< groups
[group
].length
; g
++ ) {
649 if ( registry
[groups
[group
][g
]].version
> version
) {
650 version
= registry
[groups
[group
][g
]].version
;
653 requests
[requests
.length
] = $.extend(
654 { 'modules': groups
[group
].join( '|' ), 'version': formatVersionNumber( version
) }, base
657 // Clear the batch - this MUST happen before we append the
658 // script element to the body or it's possible that the script
659 // will be locally cached, instantly load, and work the batch
660 // again, all before we've cleared it causing each request to
661 // include modules which are already loaded
663 // Asynchronously append a script tag to the end of the body
666 for ( var r
= 0; r
< requests
.length
; r
++ ) {
667 requests
[r
] = sortQuery( requests
[r
] );
668 // Build out the HTML
669 var src
= mediaWiki
.config
.get( 'wgLoadScript' ) + '?' + $.param( requests
[r
] );
670 html
+= mediaWiki
.html
.element( 'script',
671 { type
: 'text/javascript', src
: src
}, '' );
675 // Load asynchronously after doumument ready
677 setTimeout( function() { $( 'body' ).append( request() ); }, 0 )
679 document
.write( request() );
685 * Registers a module, letting the system know about it and its
686 * dependencies. loader.js files contain calls to this function.
688 this.register = function( module
, version
, dependencies
, group
) {
689 // Allow multiple registration
690 if ( typeof module
=== 'object' ) {
691 for ( var m
= 0; m
< module
.length
; m
++ ) {
692 if ( typeof module
[m
] === 'string' ) {
693 mediaWiki
.loader
.register( module
[m
] );
694 } else if ( typeof module
[m
] === 'object' ) {
695 mediaWiki
.loader
.register
.apply( mediaWiki
.loader
, module
[m
] );
701 if ( typeof module
!== 'string' ) {
702 throw new Error( 'module must be a string, not a ' + typeof module
);
704 if ( typeof registry
[module
] !== 'undefined' ) {
705 throw new Error( 'module already implemeneted: ' + module
);
707 // List the module as registered
709 'state': 'registered',
710 'group': typeof group
=== 'string' ? group
: null,
712 'version': typeof version
!== 'undefined' ? parseInt( version
) : 0
714 if ( typeof dependencies
=== 'string' ) {
715 // Allow dependencies to be given as a single module name
716 registry
[module
].dependencies
= [dependencies
];
717 } else if ( typeof dependencies
=== 'object' || typeof dependencies
=== 'function' ) {
718 // Allow dependencies to be given as an array of module names
719 // or a function which returns an array
720 registry
[module
].dependencies
= dependencies
;
725 * Implements a module, giving the system a course of action to take
726 * upon loading. Results of a request for one or more modules contain
727 * calls to this function.
729 this.implement = function( module
, script
, style
, localization
) {
730 // Automatically register module
731 if ( typeof registry
[module
] === 'undefined' ) {
732 mediaWiki
.loader
.register( module
);
735 if ( typeof script
!== 'function' ) {
736 throw new Error( 'script must be a function, not a ' + typeof script
);
738 if ( typeof style
!== 'undefined'
739 && typeof style
!== 'string'
740 && typeof style
!== 'object' )
742 throw new Error( 'style must be a string or object, not a ' + typeof style
);
744 if ( typeof localization
!== 'undefined'
745 && typeof localization
!== 'object' )
747 throw new Error( 'localization must be an object, not a ' + typeof localization
);
749 if ( typeof registry
[module
] !== 'undefined'
750 && typeof registry
[module
].script
!== 'undefined' )
752 throw new Error( 'module already implemeneted: ' + module
);
754 // Mark module as loaded
755 registry
[module
].state
= 'loaded';
757 registry
[module
].script
= script
;
758 if ( typeof style
=== 'string'
759 || typeof style
=== 'object' && !( style
instanceof Array
) )
761 registry
[module
].style
= style
;
763 if ( typeof localization
=== 'object' ) {
764 registry
[module
].messages
= localization
;
766 // Execute or queue callback
768 filter( ['ready'], registry
[module
].dependencies
),
769 registry
[module
].dependencies
) )
778 * Executes a function as soon as one or more required modules are ready
780 * @param dependencies string or array of strings of modules names the callback
781 * dependencies to be ready before
783 * @param ready function callback to execute when all dependencies are ready (optional)
784 * @param error function callback to execute when if dependencies have a errors (optional)
786 this.using = function( dependencies
, ready
, error
) {
788 if ( typeof dependencies
!== 'object' && typeof dependencies
!== 'string' ) {
789 throw new Error( 'dependencies must be a string or an array, not a ' +
790 typeof dependencies
)
792 // Allow calling with a single dependency as a string
793 if ( typeof dependencies
=== 'string' ) {
794 dependencies
= [dependencies
];
796 // Resolve entire dependency map
797 dependencies
= resolve( dependencies
);
798 // If all dependencies are met, execute ready immediately
799 if ( compare( filter( ['ready'], dependencies
), dependencies
) ) {
800 if ( typeof ready
=== 'function' ) {
804 // If any dependencies have errors execute error immediately
805 else if ( filter( ['error'], dependencies
).length
) {
806 if ( typeof error
=== 'function' ) {
810 // Since some dependencies are not yet ready, queue up a request
812 request( dependencies
, ready
, error
);
817 * Loads an external script or one or more modules for future use
819 * @param modules mixed either the name of a module, array of modules,
820 * or a URL of an external script or style
821 * @param type string mime-type to use if calling with a URL of an
822 * external script or style; acceptable values are "text/css" and
823 * "text/javascript"; if no type is provided, text/javascript is
826 this.load = function( modules
, type
) {
828 if ( typeof modules
!== 'object' && typeof modules
!== 'string' ) {
829 throw new Error( 'dependencies must be a string or an array, not a ' +
830 typeof dependencies
)
832 // Allow calling with an external script or single dependency as a string
833 if ( typeof modules
=== 'string' ) {
834 // Support adding arbitrary external scripts
835 if ( modules
.substr( 0, 7 ) == 'http://'
836 || modules
.substr( 0, 8 ) == 'https://' )
838 if ( type
=== 'text/css' ) {
840 .append( $( '<link rel="stylesheet" type="text/css" />' )
841 .attr( 'href', modules
) );
843 } else if ( type
=== 'text/javascript' || typeof type
=== 'undefined' ) {
844 var script
= mediaWiki
.html
.element( 'script',
845 { type
: 'text/javascript', src
: modules
}, '' );
847 $( 'body' ).append( script
);
849 document
.write( script
);
856 // Called with single module
859 // Resolve entire dependency map
860 modules
= resolve( modules
);
861 // If all modules are ready, nothing dependency be done
862 if ( compare( filter( ['ready'], modules
), modules
) ) {
865 // If any modules have errors return false
866 else if ( filter( ['error'], modules
).length
) {
869 // Since some modules are not yet ready, queue up a request
877 * Flushes the request queue and begin executing load requests on demand
879 this.go = function() {
881 mediaWiki
.loader
.work();
885 * Changes the state of a module
887 * @param module string module name or object of module name/state pairs
888 * @param state string state name
890 this.state = function( module
, state
) {
891 if ( typeof module
=== 'object' ) {
892 for ( var m
in module
) {
893 mediaWiki
.loader
.state( m
, module
[m
] );
897 if ( !( module
in registry
) ) {
898 mediaWiki
.loader
.register( module
);
900 registry
[module
].state
= state
;
904 * Gets the version of a module
906 * @param module string name of module to get version for
908 this.version = function( module
) {
909 if ( module
in registry
&& 'version' in registry
[module
] ) {
910 return formatVersionNumber( registry
[module
].version
);
915 /* Cache document ready status */
917 $(document
).ready( function() { ready
= true; } );
920 /** HTML construction helper functions */
921 this.html
= new ( function () {
922 function escapeCallback( s
) {
938 * Escape a string for HTML. Converts special characters to HTML entities.
939 * @param s The string to escape
941 this.escape = function( s
) {
942 return s
.replace( /['"<>&]/g, escapeCallback
);
946 * Wrapper object for raw HTML passed to mediaWiki.html.element().
948 this.Raw = function( value
) {
953 * Wrapper object for CDATA element contents passed to mediaWiki.html.element()
955 this.Cdata = function( value
) {
960 * Create an HTML element string, with safe escaping.
962 * @param name The tag name.
963 * @param attrs An object with members mapping element names to values
964 * @param contents The contents of the element. May be either:
965 * - string: The string is escaped.
966 * - null or undefined: The short closing form is used, e.g. <br/>.
967 * - this.Raw: The value attribute is included without escaping.
968 * - this.Cdata: The value attribute is included, and an exception is
969 * thrown if it contains an illegal ETAGO delimiter.
970 * See http://www.w3.org/TR/1999/REC-html401-19991224/appendix/notes.html#h-B.3.2
973 * var h = mediaWiki.html;
974 * return h.element( 'div', {},
975 * new h.Raw( h.element( 'img', {src: '<'} ) ) );
976 * Returns <div><img src="<"/></div>
978 this.element = function( name
, attrs
, contents
) {
980 for ( attrName
in attrs
) {
981 s
+= ' ' + attrName
+ '="' + this.escape( attrs
[attrName
] ) + '"';
983 if ( typeof contents
== 'undefined' || contents
=== null ) {
990 if (typeof contents
=== 'string') {
992 s
+= this.escape( contents
);
993 } else if ( contents
instanceof this.Raw
) {
994 // Raw HTML inclusion
996 } else if ( contents
instanceof this.Cdata
) {
998 if ( /<\/[a-zA-z]/.test( contents
.value
) ) {
999 throw new Error( 'mw.html.element: Illegal end tag found in CDATA' );
1001 s
+= contents
.value
;
1003 throw new Error( 'mw.html.element: Invalid type of contents' );
1005 s
+= '</' + name
+ '>';
1011 /* Extension points */
1017 /* Auto-register from pre-loaded startup scripts */
1019 if ( typeof startUp
=== 'function' ) {
1024 // Alias $j to jQuery for backwards compatibility
1026 window
.mw
= mediaWiki
;