Index: trunk/extensions/WebFonts/tests/qunit/ext.webfonts.tests.js |
— | — | @@ -6,4 +6,16 @@ |
7 | 7 | ok( mw.webfonts, 'mw.webfonts defined' ); |
8 | 8 | }); |
9 | 9 | |
| 10 | +test( '-- Dynamic font loading', function() { |
| 11 | + expect( 7 ); |
| 12 | + var cssRulesLength = document.styleSheets.length; |
| 13 | + assertTrue( mw.webfonts.addFont( 'Lohit Devanagari' ) , 'Add the Lohit Devanagari font' ); |
| 14 | + assertTrue( $.inArray( 'Lohit Devanagari', mw.webfonts.fonts ) >= 0 , 'Font loaded' ); |
| 15 | + assertTrue( cssRulesLength +1 === document.styleSheets.length, 'New css rule added to the document' ); |
| 16 | + var loadedFontsSize = mw.webfonts.fonts.length; |
| 17 | + assertTrue( mw.webfonts.addFont( 'Lohit Devanagari' ) , 'Add the Lohit Devanagari font again' ); |
| 18 | + assertTrue( loadedFontsSize === mw.webfonts.fonts.length , 'Already loaded fonts not loaded again.' ); |
| 19 | + assertFalse( mw.webfonts.addFont( 'Some Non existing font' ), 'addFont should return false if font not found' ); |
| 20 | + assertTrue( cssRulesLength +1 === document.styleSheets.length, 'No new css rules added at this stage' ); |
| 21 | +}); |
10 | 22 | |
Index: trunk/extensions/WebFonts/resources/ext.webfonts.js |
— | — | @@ -148,6 +148,7 @@ |
149 | 149 | * This method ensures that css are not duplicated and |
150 | 150 | * keep track of added fonts. |
151 | 151 | * @param fontFamilyName {String} The font-family name |
| 152 | + * @return boolean False if font not found. True if loaded. |
152 | 153 | */ |
153 | 154 | addFont: function( fontFamilyName ) { |
154 | 155 | // Avoid duplicate loading |
— | — | @@ -156,8 +157,11 @@ |
157 | 158 | if ( mw.webfonts.config.fonts[fontFamilyName] !== undefined ) { |
158 | 159 | mw.webfonts.loadCSS( fontFamilyName ); |
159 | 160 | mw.webfonts.fonts.push( fontFamilyName ); |
| 161 | + } else { |
| 162 | + return false; |
160 | 163 | } |
161 | 164 | } |
| 165 | + return true; |
162 | 166 | }, |
163 | 167 | |
164 | 168 | /** |