Update jquery.webfonts from upstream

Upstream: https://github.com/wikimedia/jquery.webfonts
Changes:
* Fixes Bug 53203
* More documentation and code cleanup

Bug: 53203
Change-Id: Ieb48ba532cc1d9880d2fde1b185af7cbed1a3eff
This commit is contained in:
Santhosh Thottingal
2013-08-23 13:52:35 +05:30
parent 1b55342049
commit 17956d5af8

View File

@@ -55,6 +55,11 @@
WebFonts.prototype = { WebFonts.prototype = {
constructor: WebFonts, constructor: WebFonts,
/**
* Get the default font family for given language.
* @param {String} language Language code.
* @return {String} Font family name
*/
getFont: function( language ) { getFont: function( language ) {
if ( this.options.fontSelector ) { if ( this.options.fontSelector ) {
return this.options.fontSelector( this.repository, language || this.language ); return this.options.fontSelector( this.repository, language || this.language );
@@ -74,16 +79,19 @@
this.parse(); this.parse();
}, },
/**
* TODO: document
*/
refresh: function() { refresh: function() {
this.reset(); this.reset();
this.init(); this.init();
}, },
/** /**
* Apply a font for the element. * Apply a font for given elements.
* *
* @param fontFamily String: font family name * @param {String} fontFamily Font family name
* @param $element * @param {jQuery} $element One or more jQuery elements
*/ */
apply: function( fontFamily, $element ) { apply: function( fontFamily, $element ) {
var fontStack = this.options.fontStack.slice( 0 ); var fontStack = this.options.fontStack.slice( 0 );
@@ -142,6 +150,8 @@
} }
} }
// In case the list contained only fonts that are already loaded
// or non-existing fonts.
if ( fontFaceRule !== '' ) { if ( fontFaceRule !== '' ) {
injectCSS( fontFaceRule ); injectCSS( fontFaceRule );
} }
@@ -177,8 +187,12 @@
var fontFamilyStyle, fontFamily, var fontFamilyStyle, fontFamily,
$element = $( element ); $element = $( element );
// Note: it depends on the browser whether this returns font names
// which don't exist. In Chrome it does, while in Opera it doesn't.
fontFamilyStyle = $element.css( 'fontFamily' ); fontFamilyStyle = $element.css( 'fontFamily' );
// Note: It is unclear whether this can ever be falsy. Maybe also
// browser specific.
if ( fontFamilyStyle ) { if ( fontFamilyStyle ) {
fontFamily = fontFamilyStyle.split( ',' )[0]; fontFamily = fontFamilyStyle.split( ',' )[0];
@@ -186,10 +200,10 @@
fontFamily = $.trim( fontFamily.replace( /["']/g, '' ) ); fontFamily = $.trim( fontFamily.replace( /["']/g, '' ) );
append( fontQueue, fontFamily ); append( fontQueue, fontFamily );
// Load and apply fonts for other language tagged elements (batched)
} }
if ( element.lang && element.lang !== webfonts.$element.attr( 'lang' ) ) { // Load and apply fonts for other language tagged elements (batched)
if ( element.lang && element.lang !== webfonts.language ) {
fontFamily = webfonts.getFont( element.lang ); fontFamily = webfonts.getFont( element.lang );
// We do not have fonts for all languages // We do not have fonts for all languages
if ( fontFamily !== null ) { if ( fontFamily !== null ) {
@@ -210,8 +224,8 @@
/** /**
* List all fonts for the given language * List all fonts for the given language
* *
* @param language mixed: [optional] language code. If undefined all fonts will be listed * @param {String} [language] Language code. If undefined all fonts will be listed.
* @return Array font names array * @return {Array} List of font family names.
*/ */
list: function( language ) { list: function( language ) {
var fontName, var fontName,
@@ -233,7 +247,7 @@
/** /**
* List all languages supported by the repository * List all languages supported by the repository
* *
* @return Array language codes * @return {Array} List of language codes
*/ */
languages: function() { languages: function() {
var language, var language,
@@ -272,11 +286,11 @@
}, },
/** /**
* Construct the CSS required for the font-family, inject it to the head * Construct the CSS required for the font-family.
* of the body so that it gets loaded.
* *
* @param fontFamily The font-family name * @param {String} fontFamily The font-family name
* @param variant The font variant, eg: bold, italic etc. Default is normal. * @param {String} [variant] The font variant, eg: bold, italic etc. Default is normal.
* @return {String} CSS
*/ */
getCSS: function( fontFamily, variant ) { getCSS: function( fontFamily, variant ) {
var webfonts, base, version, versionSuffix, var webfonts, base, version, versionSuffix,
@@ -317,17 +331,17 @@
if ( fontconfig.woff ) { if ( fontconfig.woff ) {
fontFormats.push( '\t\turl(\'' + base + fontconfig.woff + versionSuffix fontFormats.push( '\t\turl(\'' + base + fontconfig.woff + versionSuffix
+ '\') format(\'woff\')' ); + '\') format(\'woff\')' );
} }
if ( fontconfig.svg ) { if ( fontconfig.svg ) {
fontFormats.push( '\t\turl(\'' + base + fontconfig.svg + versionSuffix + '#' fontFormats.push( '\t\turl(\'' + base + fontconfig.svg + versionSuffix + '#'
+ fontFamily + '\') format(\'svg\')' ); + fontFamily + '\') format(\'svg\')' );
} }
if ( fontconfig.ttf ) { if ( fontconfig.ttf ) {
fontFormats.push( '\t\turl(\'' + base + fontconfig.ttf + versionSuffix fontFormats.push( '\t\turl(\'' + base + fontconfig.ttf + versionSuffix
+ '\') format(\'truetype\')' ); + '\') format(\'truetype\')' );
} }
fontFaceRule += fontFormats.join() + ';\n'; fontFaceRule += fontFormats.join() + ';\n';
@@ -348,7 +362,7 @@
fontFaceRule += '\tfont-style: normal;'; fontFaceRule += '\tfont-style: normal;';
} }
fontFaceRule += '}'; fontFaceRule += '}\n';
webfonts = this; webfonts = this;
if ( fontconfig.variants !== undefined ) { if ( fontconfig.variants !== undefined ) {
@@ -390,15 +404,11 @@
/** /**
* Create a new style tag and add it to the DOM. * Create a new style tag and add it to the DOM.
* *
* @param css String: CSS text * @param {String} css
* @return HTMLStyleElement
*/ */
function injectCSS( css ) { function injectCSS( css ) {
var s = document.createElement( 'style' ); var s = document.createElement( 'style' );
s.type = 'text/css';
s.rel = 'stylesheet';
// Insert into document before setting cssText // Insert into document before setting cssText
document.getElementsByTagName( 'head' )[0].appendChild( s ); document.getElementsByTagName( 'head' )[0].appendChild( s );
@@ -409,7 +419,5 @@
// Safari sometimes borks on null // Safari sometimes borks on null
s.appendChild( document.createTextNode( String( css ) ) ); s.appendChild( document.createTextNode( String( css ) ) );
} }
return s;
} }
} )( jQuery, window, document ); } )( jQuery, window, document );