Index: trunk/extensions/WebFonts/tests/qunit/ext.webfonts.tests.js |
— | — | @@ -44,11 +44,10 @@ |
45 | 45 | assertFalse( $testElement.hasClass( 'webfonts-lang-attr' ), 'The element has no webfonts-lang-attr class since en lang has no fonts available' ) ; |
46 | 46 | |
47 | 47 | ok( $testElement.attr( 'lang' , 'ta' ) , 'Set lang attribute as Tamil' ); |
48 | | - var cssRulesLength = document.styleSheets.length; |
49 | 48 | ok( mw.webfonts.loadFontsForLangAttr() ); |
50 | 49 | assertTrue( $testElement.hasClass( 'webfonts-lang-attr' ), 'The element has webfonts-lang-attr class' ) ; |
51 | 50 | 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' ); |
53 | 52 | |
54 | 53 | ok( mw.webfonts.reset() ); |
55 | 54 | assertFalse( $testElement.hasClass( 'webfonts-lang-attr' ), 'The element has no webfonts-lang-attr since we reset it' ) ; |
— | — | @@ -65,26 +64,36 @@ |
66 | 65 | assertTrue( $testElement !== [], 'Test element added' ) ; |
67 | 66 | |
68 | 67 | $testElement.attr( 'style','font-family: RufScript, Arial, Helvetica, sans' ); |
69 | | - var cssRulesLength = document.styleSheets.length; |
70 | 68 | assertTrue( $.inArray( 'RufScript', mw.webfonts.fonts ) === -1 , 'RufScript Font not loaded yet' ); |
71 | 69 | ok( mw.webfonts.loadFontsForFontFamilyStyle() ); |
72 | 70 | 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' ); |
74 | 72 | |
75 | 73 | $testElement.attr( 'style','font-family: NonExistingFont, Arial, Helvetica, sans' ); |
76 | | - cssRulesLength = document.styleSheets.length; |
77 | 74 | ok( mw.webfonts.loadFontsForFontFamilyStyle() ); |
78 | 75 | 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 | + |
81 | 78 | $testElement.attr( 'style','font-family: NonExistingFont, AnjaliOldLipi, Arial, Helvetica, sans' ); |
82 | | - cssRulesLength = document.styleSheets.length; |
83 | 79 | assertTrue( $.inArray( 'AnjaliOldLipi', mw.webfonts.fonts ) === -1 , 'Fallback font AnjaliOldLipi not loaded yet' ); |
84 | 80 | ok( mw.webfonts.loadFontsForFontFamilyStyle() ); |
85 | 81 | 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' ); |
87 | 83 | |
88 | 84 | ok( $testElement.remove() ); |
89 | 85 | } ); |
90 | 86 | |
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 | +} |