From: Krinkle Date: Fri, 24 Feb 2012 23:22:30 +0000 (+0000) Subject: [mw.loader] Refactor addInlineCSS's logic, fixing various bugs X-Git-Tag: 1.31.0-rc.0~24527 X-Git-Url: http://git.cyclocoop.org/%27.parametre_url%28%20%20%20generer_action_auteur%28%27charger_plugin%27%2C%20%27update_flux%27%29%2C%27update_flux%27%2C%20%27oui%27%29.%27?a=commitdiff_plain;h=57dda2b95e0997165dcc6eba83e5aa101bd10b51;p=lhc%2Fweb%2Fwiklou.git [mw.loader] Refactor addInlineCSS's logic, fixing various bugs * Using mw.util.addCSS as base to instance of bug 33305 automatically * Expose as mw.loader.addStyleTag * Re-use the code in mw.util.addCSS * Drop the "text > Cdata > element > jQuery object > innerHTML", in favor of setting cssText or appending a text node (html escapement isn't a problem when working with text nodes directly). * Appending this way also works in IE, no need for the dispose/re-create style-tag logic in the try-catch(). * Follows-up r110988 (fixme) --- diff --git a/resources/mediawiki/mediawiki.js b/resources/mediawiki/mediawiki.js index 2ed894e19b..c0e2fdd4e1 100644 --- a/resources/mediawiki/mediawiki.js +++ b/resources/mediawiki/mediawiki.js @@ -389,41 +389,57 @@ var mw = ( function ( $, undefined ) { return $marker; } } - + + /** + * Create a new style tag and add it to the DOM. + * + * @param text String: CSS text + * @param $nextnode mixed: [optional] An Element or jQuery object for an element where + * the style tag should be inserted before. Otherwise appended to the . + * @return HTMLStyleElement + */ + function addStyleTag( text, $nextnode ) { + var s = document.createElement( 'style' ); + s.type = 'text/css'; + s.rel = 'stylesheet'; + // Insert into document before setting cssText (bug 33305) + if ( $nextnode ) { + // If a raw element, create a jQuery object, otherwise use directly + if ( $nextnode.nodeType ) { + $nextnode = $( $nextnode ); + } + $nextnode.before( s ); + } else { + document.getElementsByTagName('head')[0].appendChild( s ); + } + if ( s.styleSheet ) { + s.styleSheet.cssText = text; // IE + } else { + // Safari sometimes borks on null + s.appendChild( document.createTextNode( String( text ) ) ); + } + return s; + } + function addInlineCSS( css, media ) { - var $style = getMarker().prev(), - $newStyle, - attrs = { 'type': 'text/css', 'media': media }; + var $style, style, $newStyle; + $style = getMarker().prev(); if ( $style.is( 'style' ) && $style.data( 'ResourceLoaderDynamicStyleTag' ) === true ) { - // There's already a dynamic