Skip to content

Instantly share code, notes, and snippets.

@jonbretman
Created May 27, 2015 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonbretman/6281b65f14be6bb72f68 to your computer and use it in GitHub Desktop.
Save jonbretman/6281b65f14be6bb72f68 to your computer and use it in GitHub Desktop.
FontFaceObserver without Promise polyfill
!function(){"use strict";function t(t){function e(){document.body?t():setTimeout(e,0)}e()}function e(t){this.a=document.createElement("div"),this.a.setAttribute("aria-hidden","true"),this.a.appendChild(document.createTextNode(t)),this.b=document.createElement("span"),this.c=document.createElement("span"),this.h=document.createElement("span"),this.g=document.createElement("span"),this.f=-1,this.b.style.cssText="display:inline-block;position:absolute;height:100%;width:100%;overflow:scroll;",this.c.style.cssText="display:inline-block;position:absolute;height:100%;width:100%;overflow:scroll;",this.g.style.cssText="display:inline-block;position:absolute;height:100%;width:100%;overflow:scroll;",this.h.style.cssText="display:inline-block;width:200%;height:200%;",this.b.appendChild(this.h),this.c.appendChild(this.g),this.a.appendChild(this.b),this.a.appendChild(this.c)}function i(t,e,i){t.a.style.cssText="min-width:20px;min-height:20px;display:inline-block;position:absolute;width:auto;margin:0;padding:0;top:-999px;left:-999px;white-space:nowrap;font-size:100px;font-family:"+e+";"+i}function n(t){var e=t.a.offsetWidth,i=e+100;return t.g.style.width=i+"px",t.c.scrollLeft=i,t.b.scrollLeft=t.b.scrollWidth+100,t.f!==e?(t.f=e,!0):!1}function s(t,e){t.b.addEventListener("scroll",function(){n(t)&&null!==t.a.parentNode&&e(t.f)},!1),t.c.addEventListener("scroll",function(){n(t)&&null!==t.a.parentNode&&e(t.f)},!1),n(t)}function o(){for(;r.length;)r[0](),r.shift()}function a(t,e){var i=e||{};this.family=t,this.style=i.style||"normal",this.variant=i.variant||"normal",this.weight=i.weight||"normal",this.stretch=i.stretch||"stretch",this.featureSettings=i.featureSettings||"normal"}var r=[];if(window.MutationObserver){var l=document.createElement("div");new MutationObserver(o).observe(l,{attributes:!0});var h=function(){l.setAttribute("x",0)}}else h=function(){setTimeout(o)};var c=window.Promise,f=null;a.prototype.a=function(n,o){var a=n||"BESbswy",r=o||3e3,l="font-style:"+this.style+";font-variant:"+this.variant+";font-weight:"+this.weight+";font-stretch:"+this.stretch+";font-feature-settings:"+this.featureSettings+";-moz-font-feature-settings:"+this.featureSettings+";-webkit-font-feature-settings:"+this.featureSettings+";",h=document.createElement("div"),d=new e(a),u=new e(a),p=new e(a),m=-1,w=-1,v=-1,b=-1,g=-1,y=-1,x=this;return new c(function(e,n){function o(){null!==h.parentNode&&h.parentNode.removeChild(h)}function a(){if((-1!==m&&-1!==w||-1!==m&&-1!==v||-1!==w&&-1!==v)&&(m===w||m===v||w===v)){if(null===f){var t=/AppleWebKit\/([0-9]+)(?:\.([0-9]+))/.exec(window.navigator.userAgent);f=!!t&&(536>parseInt(t[1],10)||536===parseInt(t[1],10)&&11>=parseInt(t[2],10))}f?m===b&&w===b&&v===b||m===g&&w===g&&v===g||m===y&&w===y&&v===y||(o(),e(x)):(o(),e(x))}}t(function(){function t(){if(Date.now()-e>=r)o(),n(x);else{var i=document.hidden;(!0===i||void 0===i)&&(m=d.a.offsetWidth,w=u.a.offsetWidth,v=p.a.offsetWidth,a()),setTimeout(t,50)}}var e=Date.now();i(d,"sans-serif",l),i(u,"serif",l),i(p,"monospace",l),h.appendChild(d.a),h.appendChild(u.a),h.appendChild(p.a),document.body.appendChild(h),b=d.a.offsetWidth,g=u.a.offsetWidth,y=p.a.offsetWidth,t(),s(d,function(t){m=t,a()}),i(d,x.family+",sans-serif",l),s(u,function(t){w=t,a()}),i(u,x.family+",serif",l),s(p,function(t){v=t,a()}),i(p,x.family+",monospace",l)})})},window.FontFaceObserver=a,window.FontFaceObserver.prototype.check=a.prototype.a}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment