/* ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is CssHackz. * * The Initial Developer of the Original Code is * Disruptive Innovations SARL. * Portions created by the Initial Developer are Copyright (C) 2009 * the Initial Developer. All Rights Reserved. * * Contributor(s): * Daniel Glazman , Original author * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the MPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the MPL, the GPL or the LGPL. * * ***** END LICENSE BLOCK ***** */ var CssHackz = { mStylesheetLinks: [], init: function() { var links = document.getElementsByTagName("link"); for (var i = 0; i < links.length; i++) { var l = links[i]; if (l.getAttribute("rel") == "stylesheet" && l.getAttribute("type") == "text/css") { this.mStylesheetLinks.push( { url: l.href, media: l.getAttribute("media"), elt: l } ); } } var ajaxRequest; var requestCallback = function(aRequest, aLink) { var ENDIF_IMPL_STR = "@endif-implemented;"; // IE does not know const... if (ajaxRequest.readyState == 4) { if (ajaxRequest.status == 200) { var cssText = ajaxRequest.responseText; var newCssText = ""; var index = 0; while (true) { var newIndex = cssText.indexOf("@if-implemented", index); if (newIndex == -1) { if (index < cssText.length) newCssText += cssText.substr(index); break; } newCssText += cssText.substring(index, newIndex); index = newIndex; var parametersRegexp = /@if-implemented\s*{([^}]*)}([^@]*)@/ ; parametersRegexp.lastIndex = index; var match = parametersRegexp.exec(cssText); if (!match) { alert("CssHackz error, cannot parse"); return; } var declarations = match[1]; var ifCases = match[2]; var elseCases = ""; index += match[0].length - 1; var elseIndex = cssText.indexOf("@else-implemented;", index); var endifIndex = cssText.indexOf(ENDIF_IMPL_STR, index); if (endifIndex == -1 || (elseIndex != index && endifIndex != index)) { alert("CssHackz error, cannot parse"); return; } if (endifIndex != index) { var elsecaseRegexp = /@else-implemented\s*;\s*([^@]*)@/ ; elsecaseRegexp.lastIndex = index; match = elsecaseRegexp.exec(cssText); if (!match) { alert("CssHackz error, cannot parse"); return; } elseCases = match[1]; index += match[0].length - 1; } if (cssText.substr(index, ENDIF_IMPL_STR.length) != ENDIF_IMPL_STR) { alert("CssHackz error, cannot parse"); return; } index += ENDIF_IMPL_STR.length; newCssText += CssHackz.applicableDeclarations(declarations) ? ifCases : elseCases; } var style = document.createElement("style"); style.setAttribute("type", "text/css"); if (aLink.media) style.setAttribute("media", aLink.media); if (style.styleSheet) { // IE, big piece of crap, cannot insert a text node as child // of a style element !!! Unbelievable... Even IE8... style.styleSheet.cssText = newCssText; } else { var cdata = document.createTextNode(newCssText); style.appendChild(cdata); } aLink.elt.parentNode.insertBefore(style, aLink.elt); aLink.elt.parentNode.removeChild(aLink.elt); } else { alert("I can't retrieve the source of the stylesheet, sorry"); } } }; for (var i = 0; i < this.mStylesheetLinks.length; i++) { l = this.mStylesheetLinks[i]; var url = l.url; if (window.XMLHttpRequest) { ajaxRequest = new XMLHttpRequest(); ajaxRequest.onreadystatechange = function() { requestCallback(ajaxRequest, l); } ajaxRequest.open("GET", url, true); ajaxRequest.setRequestHeader("Content-type", "type/plain"); ajaxRequest.send(null); } else if (window.ActiveXObject) { // ie ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); if (ajaxRequest) { ajaxRequest.onreadystatechange = function() { requestCallback(ajaxRequest, l); }; ajaxRequest.open("GET", url, true); ajaxRequest.send(); } } } }, applicableDeclarations: function(aDeclarations) { var declArray = aDeclarations.split(";"); var span = document.createElement("span"); for (var i = 0 ; i < declArray.length; i++) { span.removeAttribute("style"); span.setAttribute("style", declArray[i]); var a = declArray[i].split(":")[0].replace( /^\s*/g , "" ).replace( /\s*$/g , "" ); if (span.style.getPropertyValue) { if (!span.style.getPropertyValue(a)) return false; } else { // IE, again big piece of... if (!span.style.cssText) return false; } } return true; } };