r109157 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r109156‎ | r109157 | r109158 >
Date:16:11, 17 January 2012
Author:santhosh
Status:resolved (Comments)
Tags:
Comment:
Better way to check whether @font-face rule is added to the document for the font.
Also helps in case font was loaded using uselang=ta and not by tests.
Modified paths:
  • /trunk/extensions/WebFonts/tests/qunit/ext.webfonts.tests.js (modified) (history)

Diff [purge]

Index: trunk/extensions/WebFonts/tests/qunit/ext.webfonts.tests.js
@@ -44,11 +44,10 @@
4545 assertFalse( $testElement.hasClass( 'webfonts-lang-attr' ), 'The element has no webfonts-lang-attr class since en lang has no fonts available' ) ;
4646
4747 ok( $testElement.attr( 'lang' , 'ta' ) , 'Set lang attribute as Tamil' );
48 - var cssRulesLength = document.styleSheets.length;
4948 ok( mw.webfonts.loadFontsForLangAttr() );
5049 assertTrue( $testElement.hasClass( 'webfonts-lang-attr' ), 'The element has webfonts-lang-attr class' ) ;
5150 assertTrue( $.inArray( 'Lohit Tamil', mw.webfonts.fonts ) >= 0 , 'Font loaded' );
52 - assertTrue( cssRulesLength + 1 === document.styleSheets.length, 'New css rule added to the document' );
 51+ assertTrue( isFontFaceLoaded( 'Lohit Tamil' ), 'New css rule added to the document for font Lohit Tamil' );
5352
5453 ok( mw.webfonts.reset() );
5554 assertFalse( $testElement.hasClass( 'webfonts-lang-attr' ), 'The element has no webfonts-lang-attr since we reset it' ) ;
@@ -65,26 +64,36 @@
6665 assertTrue( $testElement !== [], 'Test element added' ) ;
6766
6867 $testElement.attr( 'style','font-family: RufScript, Arial, Helvetica, sans' );
69 - var cssRulesLength = document.styleSheets.length;
7068 assertTrue( $.inArray( 'RufScript', mw.webfonts.fonts ) === -1 , 'RufScript Font not loaded yet' );
7169 ok( mw.webfonts.loadFontsForFontFamilyStyle() );
7270 assertTrue( $.inArray( 'RufScript', mw.webfonts.fonts ) >= 0 , 'Font loaded' );
73 - assertTrue( cssRulesLength +1 === document.styleSheets.length, 'New css rule added to the document' );
 71+ assertTrue( isFontFaceLoaded('RufScript'), 'New css rule added to the document for RufScript' );
7472
7573 $testElement.attr( 'style','font-family: NonExistingFont, Arial, Helvetica, sans' );
76 - cssRulesLength = document.styleSheets.length;
7774 ok( mw.webfonts.loadFontsForFontFamilyStyle() );
7875 assertTrue( $.inArray( 'NonExistingFont', mw.webfonts.fonts ) === -1 , 'Font not loaded since it is not existing, including fallback fonts' );
79 - assertTrue( cssRulesLength === document.styleSheets.length, 'No new css rule added to the document' );
80 -
 76+ assertFalse( isFontFaceLoaded( 'NonExistingFont' ), 'No new css rule added to the document' );
 77+
8178 $testElement.attr( 'style','font-family: NonExistingFont, AnjaliOldLipi, Arial, Helvetica, sans' );
82 - cssRulesLength = document.styleSheets.length;
8379 assertTrue( $.inArray( 'AnjaliOldLipi', mw.webfonts.fonts ) === -1 , 'Fallback font AnjaliOldLipi not loaded yet' );
8480 ok( mw.webfonts.loadFontsForFontFamilyStyle() );
8581 assertTrue( $.inArray( 'AnjaliOldLipi', mw.webfonts.fonts ) >= 0 , 'Fallback font AnjaliOldLipi loaded' );
86 - assertTrue( cssRulesLength + 1 === document.styleSheets.length, 'New css rule added to the document for fallbackfont' );
 82+ assertTrue( isFontFaceLoaded('AnjaliOldLipi') , 'New css rule added to the document for fallbackfont AnjaliOldLipi' );
8783
8884 ok( $testElement.remove() );
8985 } );
9086
91 -
 87+isFontFaceLoaded = function(fontFamilyName){
 88+ var lastStyleIndex = document.styleSheets.length-1;
 89+ // Iterate from last.
 90+ for( var styleIndex = lastStyleIndex; styleIndex > 0 ; styleIndex-- ){
 91+ var lastStyleSheet = document.styleSheets[styleIndex];
 92+ if ( !lastStyleSheet ) continue ;
 93+ if ( !lastStyleSheet.cssRules[0] ) continue ;
 94+ var cssText = lastStyleSheet.cssRules[0].cssText;
 95+ if ( cssText.indexOf( '@font-face' ) >= 0 && cssText.indexOf( fontFamilyName ) >= 0 ){
 96+ return true;
 97+ }
 98+ }
 99+ return false;
 100+}

Follow-up revisions

RevisionCommit summaryAuthorDate
r109677[WebFonts.tests] Fixes...krinkle06:21, 21 January 2012

Comments

#Comment by Krinkle (talk | contribs)   06:38, 21 January 2012
isFontFaceLoaded = function(fontFamilyName){

implied global, spotted in r109677. Just noting here for reference

Status & tagging log