-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfqpolyfill.min.js
12 lines (12 loc) · 12.1 KB
/
fqpolyfill.min.js
1
2
3
4
5
6
7
8
9
10
11
12
/*
* Feature Queries (@supports) polyfill
*
*
* Drop-in polyfill for window.CSS.supports and @supports CSS rules
* Licensed MIT
*
* @author Krystian Jarmicki
* @version 0.8.1
* @preserve
*/
!function(e){"use strict";var t=function(){var e,t,n=Array.prototype.slice.call(arguments),i=n[0];for(e=1;e<n.length;e++)for(t in n[e])i[t]=n[e][t];return i},n=function(t){return Array.prototype.slice.call(e.document.querySelectorAll(t))},i=e.fqPolyfill={};i.TERMINALS={_END:0,OR:1,AND:2,NOT:3,OPEN_BRACKET:4,CLOSE_BRACKET:5,SINGLE_QUOTED_CONTENT:6,DOUBLE_QUOTED_CONTENT:7,DOUBLE_COLON:8,RULENAME:9,ARITHMETIC:10},i.CHARACTER_SETS={NEWLINE_N:/\n+/g,NEWLINE_R:/\r+/g,COMMENT:""},i.EXPRESSION_NODES={CONDITION_NODE:0,CONJUNCTION_NODE:1,DISJUNCTION_NODE:2},i.init=function(){this.tokenizer.init(),this.supportChecker.init(),this.checker.supportsJS()||this.checker.augmentJS(),this.checker.supportsCSS()||this.checker.watchCSS()},i.checker={supportsJS:function(){return"object"==typeof e.CSS&&"function"==typeof e.CSS.supports},augmentJS:function(){"undefined"==typeof e.CSS&&(e.CSS={}),e.CSS.supports=function(e,t){var n="string"==typeof t||"number"==typeof t?e+": "+t:e;return i.parser.parse(i.tokenizer.tokenize(n)).getValue()}},supportsCSS:function(){var t,n=e.document.createElement("style"),i=e.document.createElement("div"),o=e.document.body,s="#fqPolyfillTestElem { display: block; } @supports (display: inline) { #fqPolyfillTestElem { display: inline; } }";return i.id="fqPolyfillTestElem",n.appendChild(e.document.createTextNode(s)),o.appendChild(n),o.appendChild(i),t="inline"===e.getComputedStyle(i).display,o.removeChild(n),o.removeChild(i),t},watchCSS:function(){i.styleFix.register(this.augmentCSS.bind(this)),setTimeout(function(){n('link[rel="stylesheet"]').forEach(i.styleFix.link)},10),e.document.addEventListener("DOMContentLoaded",i.styleFix.process,!1)},augmentCSS:function(e){var t;return e=this.deleteCSSComments(e),t=this.findSupportsParts(e),t.length>0&&t.forEach(function(t){e=e.replace(t,this.substituteSupportsPart(t))}.bind(this)),e},findSupportsParts:function(e){for(var t,n,i=[];-1!==(t=e.indexOf("@supports"));)n=this.getRulesInParens(e.slice(t)),i.push(n),e=e.replace(n,"");return i},getRulesInParens:function(e){var t,n,i=0,o="";do t=e.indexOf("{"),n=e.indexOf("}"),n>t&&-1!==t?(i++,o+=e.slice(0,t+1),e=e.slice(t+1)):-1!==n&&(i--,o+=e.slice(0,n+1),e=e.slice(n+1));while(i>0&&(-1!==t||-1!==n)&&5>i);return o},substituteSupportsPart:function(e){var t,n=e.replace("@supports","");return n=n.slice(0,n.indexOf("{")).trim(),i.parser.parse(i.tokenizer.tokenize(n)).getValue()?t=e.slice(e.indexOf("{")+1,e.length-1):""},deleteCSSComments:function(e){var t=e.indexOf("/*"),n=e.indexOf("*/")+2;if(t>-1){if(-1===n||t>n)throw"Parse error: unterminated comment";return e=e.replace(e.substr(t,n-t),""),this.deleteCSSComments(e)}return e}},i.styleFix={postfix:"fqpolyfill",link:function(t){try{if("stylesheet"!==t.rel||t.hasAttribute("data-"+i.postfix))return}catch(n){return}var o,s=t.href||t.getAttribute("data-href"),r=s.replace(/[^\/]+$/,""),a=(/^[a-z]{3,10}:/.exec(r)||[""])[0],l=(/^[a-z]{3,10}:\/\/[^\/]+/.exec(r)||[""])[0],u=/^([^?]*)\??/.exec(s)[1],c=t.parentNode,d=new XMLHttpRequest;d.onreadystatechange=function(){4===d.readyState&&o()},o=function(){var n=d.responseText;if(n&&t.parentNode&&(!d.status||d.status<400||d.status>600)){if(n=i.styleFix.fix(n,!0,t),r){n=n.replace(/url\(\s*?((?:"|')?)(.+?)\1\s*?\)/gi,function(e,t,n){return/^([a-z]{3,10}:|#)/i.test(n)?e:/^\/\//.test(n)?'url("'+a+n+'")':/^\//.test(n)?'url("'+l+n+'")':/^\?/.test(n)?'url("'+u+n+'")':'url("'+r+n+'")'});var o=r.replace(/([\\\^\$*+[\]?{}.=!:(|)])/g,"\\$1");n=n.replace(new RegExp("\\b(behavior:\\s*?url\\('?\"?)"+o,"gi"),"$1")}var s=e.document.createElement("style");s.textContent=n,s.media=t.media,s.disabled=t.disabled,s.setAttribute("data-href",t.getAttribute("href")),c.insertBefore(s,t),c.removeChild(t),s.media=t.media}};try{d.open("GET",s),d.send(null)}catch(n){"undefined"!=typeof XDomainRequest&&(d=new XDomainRequest,d.onerror=d.onprogress=function(){},d.onload=o,d.open("GET",s),d.send(null))}t.setAttribute("data-"+i.styleFix.postfix+"progress","")},styleElement:function(e){if(!e.hasAttribute("data-"+i.styleFix.postfix)){var t=e.disabled;e.textContent=i.styleFix.fix(e.textContent,!0,e),e.disabled=t}},styleAttribute:function(e){var t=e.getAttribute("style");t=i.styleFix.fix(t,!1,e),e.setAttribute("style",t)},process:function(){n('link[rel="stylesheet"]:not([data-'+i.styleFix.postfix+"progress])").forEach(i.styleFix.link),n("style").forEach(i.styleFix.styleElement),n("[style]").forEach(i.styleFix.styleAttribute)},register:function(e,t){(i.styleFix.fixers=i.styleFix.fixers||[]).splice(void 0===t?i.styleFix.fixers.length:t,0,e)},fix:function(e,t,n){for(var o=0;o<i.styleFix.fixers.length;o++)e=i.styleFix.fixers[o](e,t,n)||e;return e},camelCase:function(e){return e.replace(/-([a-z])/g,function(e,t){return t.toUpperCase()}).replace("-","")},deCamelCase:function(e){return e.replace(/[A-Z]/g,function(e){return"-"+e.toLowerCase()})}},i.tokenizer={definitions:{},init:function(){this.definitions[i.TERMINALS.OR]=/^or/i,this.definitions[i.TERMINALS.AND]=/^and/i,this.definitions[i.TERMINALS.NOT]=/^not/i,this.definitions[i.TERMINALS.OPEN_BRACKET]=/^\(/,this.definitions[i.TERMINALS.CLOSE_BRACKET]=/^\)/,this.definitions[i.TERMINALS.SINGLE_QUOTED_CONTENT]=/^\'.+',?/,this.definitions[i.TERMINALS.DOUBLE_QUOTED_CONTENT]=/^\".+",?/,this.definitions[i.TERMINALS.DOUBLE_COLON]=/^:/,this.definitions[i.TERMINALS.RULENAME]=/^-?[a-zA-Z0-9%\.!\/#,]+[a-zA-Z0-9-%\.,\/#]*/,this.definitions[i.TERMINALS.ARITHMETIC]=/^(\+|\-|\/|\*)/},sanitize:function(e){return e=e.replace(i.CHARACTER_SETS.NEWLINE_N," ").replace(i.CHARACTER_SETS.NEWLINE_R," ").trim()},tokenize:function(e){var t,n,i=[];for(e=this.sanitize(e);""!==e;){for(n in this.definitions)if(this.definitions.hasOwnProperty(n)&&(t=this.definitions[n].exec(e))){i.push({token:parseInt(n,10),sequence:t[0]}),e=e.replace(t[0],"").trim();break}if(!t)throw"Parse error: Unexpected input: "+e}return i}},i.supportChecker={tests:[],cachedElem:e.document.createElement("div"),init:function(){this.addTest(function(e){return-1!==e.declarationValue.indexOf("rgba(")?-1!==e.testElem.style[e.camelDeclarationName].indexOf("rgba("):void 0}),this.addTest(function(e){return-1!==e.declarationValue.indexOf("hsla(")?-1!==e.testElem.style[e.camelDeclarationName].indexOf("rgba(")||-1!==e.testElem.style[e.camelDeclarationName].indexOf("hsla("):void 0}),this.addTest(function(e){return-1!==e.declarationValue.indexOf("-gradient(")?-1!==e.testElem.style[e.camelDeclarationName].indexOf("gradient"):void 0}),this.addTest(function(e){return-1!==e.declarationName.indexOf("background")&&-1!==e.declarationValue.indexOf(",")&&-1===e.declarationValue.indexOf("-gradient(")?(e.testElem.style.background="url(https://),url(https://),red url(https://)",/(url\s*\(.*?){3}/.test(e.testElem.style.background)):void 0}),this.addTest(function(e){return-1!==e.declarationValue.indexOf("calc(")?-1!==e.testElem.style[e.camelDeclarationName].indexOf("calc("):void 0}),this.addTest(function(e){return-1!==e.declarationName.indexOf("transform-style")&&"preserve-3d"===e.declarationValue?"preserve-3d"===e.testElem.style[e.camelDeclarationName]:void 0})},addTest:function(e){this.tests.push(e)},inject:function(t,n){var i,o=e.document.createElement("div"),s=e.document.body;return o.style.cssText=t,s.appendChild(o),i=n(o),o.parentNode.removeChild(o),!!i},styleExists:function(e){return"string"==typeof this.cachedElem.style[e]},camelize:function(e){return 0===e.indexOf("-ms-")&&(e=e.replace("-ms","ms")),e.replace(/-[a-zA-Z]/g,function(e,t,n){return n[t+1].toUpperCase()})},declaration:function(e,t){var n,i,o=e+": "+t,s=this.camelize(e);for(i=0;i<this.tests.length;i++)"undefined"==typeof n&&this.inject(o,function(o){n=this.tests[i]({testElem:o,declarationName:e,declarationValue:t,camelDeclarationName:s})}.bind(this));return"undefined"==typeof n&&(n=this.styleExists(s)),n}},i.addTest=i.supportChecker.addTest.bind(i.supportChecker);var o=function(){this.isNegated=!1};o.prototype.negate=function(){this.isNegated=!this.isNegated},o.prototype.includeNegation=function(e){return this.isNegated?!e:e};var s=function(e,t){o.apply(this,arguments),this.declarationName=e,this.declarationValue=t};s.prototype=Object.create(o.prototype),s.prototype.getType=function(){return i.EXPRESSION_NODES.CONDITION_NODE},s.prototype.getValue=function(){var e=i.supportChecker.declaration(this.declarationName,this.declarationValue);return this.includeNegation(e)};var r=function(){var e,t=Array.prototype.slice.call(arguments);for(this.conditions=[],e=0;e<t.length;e++)this.add(t[e])};r.prototype.add=function(e){this.conditions.push(e)};var a=function(){o.apply(this,arguments),r.apply(this,arguments)};a.prototype=t(Object.create(o.prototype),Object.create(r.prototype)),a.prototype.getType=function(){return i.EXPRESSION_NODES.CONJUNCTION_NODE},a.prototype.getValue=function(){var e,t=!0;for(e=0;e<this.conditions.length;e++)if(this.conditions[e].getValue()===!1){t=!1;break}return this.includeNegation(t)};var l=function(){o.apply(this,arguments),r.apply(this,arguments)};l.prototype=t(Object.create(o.prototype),Object.create(r.prototype)),l.prototype.getType=function(){return i.EXPRESSION_NODES.DISJUNCTION_NODE},l.prototype.getValue=function(){var e,t=!1;for(e=0;e<this.conditions.length;e++)if(this.conditions[e].getValue()===!0){t=!0;break}return this.includeNegation(t)},i.parser={tokens:null,lookahead:null,parse:function(e){var t;if(this.tokens=e.slice(),this.lookahead=this.tokens[0],t=this.supportsCondition(),this.lookahead.token!==i.TERMINALS._END)throw'Parse error: unexpected sequence at the end of input: "'+this.lookahead.sequence+'"';return t},nextToken:function(){this.tokens.shift(),this.lookahead=0===this.tokens.length?{token:i.TERMINALS._END,sequence:""}:this.tokens[0]},supportsCondition:function(){var e,t,n;if(this.lookahead.token===i.TERMINALS.NOT)return this.nextToken(),e=this.supportsConditionInParens(),e.negate(),e;if(e=this.supportsConditionInParens(),this.lookahead.token===i.TERMINALS.AND){for(t=new a(e);this.lookahead.token===i.TERMINALS.AND;)this.nextToken(),t.add(this.supportsConditionInParens());return t}if(this.lookahead.token===i.TERMINALS.OR){for(n=new l(e);this.lookahead.token===i.TERMINALS.OR;)this.nextToken(),n.add(this.supportsConditionInParens());return n}return e},supportsConditionInParens:function(){var e;if(this.lookahead.token===i.TERMINALS.OPEN_BRACKET){if(this.nextToken(),e=this.lookahead.token===i.TERMINALS.RULENAME?this.declaration():this.supportsCondition(),this.lookahead.token!==i.TERMINALS.CLOSE_BRACKET)throw'Parse error: unexpected sequence "'+this.lookahead.sequence+'", expecting ")"';return this.nextToken(),e}throw'Parse error: unexpected sequence "'+this.lookahead.sequence+'", expecting "("'},declaration:function(){var e=this.lookahead.sequence,t="";if(this.nextToken(),this.lookahead.token===i.TERMINALS.DOUBLE_COLON){if(this.nextToken(),t=this.declarationValue(),!t)throw'Parse error: unexpected sequence "'+this.lookahead.sequence+'", expecting declaration value';return new s(e,t)}throw'Parse error: unexpected sequence "'+this.lookahead.sequence+'", expecting ":"'},declarationValue:function(){var e="";if(this.lookahead.token===i.TERMINALS.RULENAME&&(e+=this.lookahead.sequence,this.nextToken()),this.lookahead.token===i.TERMINALS.ARITHMETIC&&(e+=" "+this.lookahead.sequence+" ",this.nextToken(),e+=this.declarationValue()),this.lookahead.token===i.TERMINALS.OPEN_BRACKET){if(e+=this.lookahead.sequence,this.nextToken(),e+=this.declarationValue(),this.lookahead.token!==i.TERMINALS.CLOSE_BRACKET)throw'Parse error: unexpected sequence "'+this.lookahead.sequence+'", expecting ")"';e+=this.lookahead.sequence,this.nextToken()}return this.lookahead.token===i.TERMINALS.SINGLE_QUOTED_CONTENT&&(e+=this.lookahead.sequence,this.nextToken()),this.lookahead.token===i.TERMINALS.DOUBLE_QUOTED_CONTENT&&(e+=this.lookahead.sequence,this.nextToken()),this.lookahead.token===i.TERMINALS.RULENAME||this.lookahead.token===i.TERMINALS.ARITHMETIC||this.lookahead.token===i.TERMINALS.OPEN_BRACKET||this.lookahead.token===i.TERMINALS.SINGLE_QUOTED_CONTENT||this.lookahead.token===i.TERMINALS.DOUBLE_QUOTED_CONTENT?e+" "+this.declarationValue():e}},i.init()}(this);