Merge "Move section ID fallbacks into headers themselves"
[lhc/web/wiklou.git] / resources / src / mediawiki.legacy / wikibits.js
1 /**
2 * MediaWiki legacy wikibits
3 */
4 ( function ( mw, $ ) {
5 var msg,
6 loadedScripts = {};
7
8 function wikiUrlencode( page ) {
9 return encodeURIComponent( String( page ) )
10 .replace( /'/g, '%27' )
11 .replace( /%20/g, '_' )
12 // wfUrlencode replacements
13 .replace( /%3B/g, ';' )
14 .replace( /%40/g, '@' )
15 .replace( /%24/g, '$' )
16 .replace( /%21/g, '!' )
17 .replace( /%2A/g, '*' )
18 .replace( /%28/g, '(' )
19 .replace( /%29/g, ')' )
20 .replace( /%2C/g, ',' )
21 .replace( /%2F/g, '/' )
22 .replace( /%7E/g, '~' )
23 .replace( /%3A/g, ':' );
24 }
25
26 /**
27 * @deprecated since 1.17 Use jQuery instead
28 */
29 mw.log.deprecate( window, 'addOnloadHook', function ( fn ) {
30 $( function () { fn(); } );
31 }, 'Use jQuery instead.' );
32
33 /**
34 * Wikipage import methods
35 *
36 * See https://www.mediawiki.org/wiki/ResourceLoader/Legacy_JavaScript#wikibits.js
37 */
38
39 /**
40 * @deprecated since 1.17 Use mw.loader instead. Warnings added in 1.25.
41 * @param {string} url
42 * @return {HTMLElement} Script tag
43 */
44 function importScriptURI( url ) {
45 var s;
46 if ( loadedScripts[ url ] ) {
47 return null;
48 }
49 loadedScripts[ url ] = true;
50 s = document.createElement( 'script' );
51 s.setAttribute( 'src', url );
52 document.getElementsByTagName( 'head' )[ 0 ].appendChild( s );
53 return s;
54 }
55
56 function importScript( page ) {
57 var uri = mw.config.get( 'wgScript' ) + '?title=' + wikiUrlencode( page ) +
58 '&action=raw&ctype=text/javascript';
59 return importScriptURI( uri );
60 }
61
62 /**
63 * @deprecated since 1.17 Use mw.loader instead. Warnings added in 1.25.
64 * @param {string} url
65 * @param {string} media
66 * @return {HTMLElement} Link tag
67 */
68 function importStylesheetURI( url, media ) {
69 var l = document.createElement( 'link' );
70 l.rel = 'stylesheet';
71 l.href = url;
72 if ( media ) {
73 l.media = media;
74 }
75 document.getElementsByTagName( 'head' )[ 0 ].appendChild( l );
76 return l;
77 }
78
79 function importStylesheet( page ) {
80 var uri = mw.config.get( 'wgScript' ) + '?title=' + wikiUrlencode( page ) +
81 '&action=raw&ctype=text/css';
82 return importStylesheetURI( uri );
83 }
84
85 msg = 'Use mw.loader instead.';
86 mw.log.deprecate( window, 'loadedScripts', loadedScripts, msg );
87 mw.log.deprecate( window, 'importScriptURI', importScriptURI, msg );
88 mw.log.deprecate( window, 'importStylesheetURI', importStylesheetURI, msg );
89 // Not quite deprecated yet.
90 window.importScript = importScript;
91 window.importStylesheet = importStylesheet;
92
93 /**
94 * Replace document.write/writeln with basic html parsing that appends
95 * to the <body> to avoid blanking pages. Added JavaScript will not run.
96 *
97 * @deprecated since 1.26
98 */
99 $.each( [ 'write', 'writeln' ], function ( idx, method ) {
100 mw.log.deprecate( document, method, function () {
101 $( 'body' ).append( $.parseHTML( Array.prototype.join.call( arguments, '' ) ) );
102 }, 'Use jQuery or mw.loader.load instead.', 'document.' + method );
103 } );
104
105 }( mediaWiki, jQuery ) );