r111067 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r111066‎ | r111067 | r111068 >
Date:19:02, 9 February 2012
Author:catrope
Status:ok
Tags:
Comment:
Followup r110988: IE8 throws JS errors if you try to call .append() on a <style> tag (or try to change .innerHTML), so catch the error and fall back to replacing the style tag with a new one
Modified paths:
  • /trunk/phase3/resources/mediawiki/mediawiki.js (modified) (history)

Diff [purge]

Index: trunk/phase3/resources/mediawiki/mediawiki.js
@@ -391,21 +391,34 @@
392392 }
393393
394394 function addInlineCSS( css, media ) {
395 - var $style = getMarker().prev();
 395+ var $style = getMarker().prev(),
 396+ $newStyle,
 397+ attrs = { 'type': 'text/css', 'media': media };
396398 if ( $style.is( 'style' ) && $style.data( 'ResourceLoaderDynamicStyleTag' ) === true ) {
397399 // There's already a dynamic <style> tag present, append to it
398400 // This recycling of <style> tags is for bug 31676 (can't have
399401 // more than 32 <style> tags in IE)
400402
401 - // Do cdata sanitization on the provided CSS, and prepend a double newline
402 - css = $( mw.html.element( 'style', {}, new mw.html.Cdata( "\n\n" + css ) ) ).html();
403 - $style.append( css );
 403+ // Also, calling .append() on a <style> tag explodes with a JS error in IE,
 404+ // so if the .append() fails we fall back to building a new <style> tag and
 405+ // replacing the existing one
 406+ try {
 407+ // Do cdata sanitization on the provided CSS, and prepend a double newline
 408+ css = $( mw.html.element( 'style', {}, new mw.html.Cdata( "\n\n" + css ) ) ).html();
 409+ $style.append( css );
 410+ } catch ( e ) {
 411+ // Generate a new tag with the combined CSS
 412+ css = $style.html() + "\n\n" + css;
 413+ $newStyle = $( mw.html.element( 'style', attrs, new mw.html.Cdata( css ) ) )
 414+ .data( 'ResourceLoaderDynamicStyleTag', true );
 415+ // Prevent a flash of unstyled content by inserting the new tag
 416+ // before removing the old one
 417+ $style.after( $newStyle );
 418+ $style.remove();
 419+ }
404420 } else {
405421 // Create a new <style> tag and insert it
406 - $style = $( mw.html.element( 'style', {
407 - 'type': 'text/css',
408 - 'media': media
409 - }, new mw.html.Cdata( css ) ) );
 422+ $style = $( mw.html.element( 'style', attrs, new mw.html.Cdata( css ) ) );
410423 $style.data( 'ResourceLoaderDynamicStyleTag', true );
411424 getMarker().before( $style );
412425 }

Sign-offs

UserFlagDate
Nikerabbitinspected08:04, 10 February 2012

Follow-up revisions

RevisionCommit summaryAuthorDate
r111175REL1_19: MFT r111029, r111034, r111067, r111076, r111085, r111128, r111144reedy18:37, 10 February 2012

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r110988(bug 31676) Group dynamically inserted CSS into a single <style> tag, because...catrope00:10, 9 February 2012

Status & tagging log