Esempio n. 1
0
        private static System.Web.Optimization.Bundle DoIncludeFallback(this System.Web.Optimization.Bundle bundle, string fallback,
                                                                        string className = null, string ruleName = null, string ruleValue = null)
        {
            if (!(bundle is StyleBundle) && (bundle is System.Web.Optimization.StyleBundle))
            {
                throw new ArgumentException("Not allowed type", nameof(bundle));
            }

            if (String.IsNullOrEmpty(bundle.CdnPath))
            {
                throw new ArgumentException("CdnPath must be provided when specifying a fallback", nameof(fallback));
            }

            if (VirtualPathUtility.IsAppRelative(bundle.CdnPath))
            {
                bundle.DoCdnFallbackExpress(fallback);
            }
            else if (new[] { className, ruleName, ruleValue }.Any(String.IsNullOrEmpty))
            {
                throw new Exception(
                          "IncludeFallback for cross-domain CdnPath must provide values for parameters [className, ruleName, ruleValue].");
            }
            else
            {
                bundle.DoCdnFallbackExpress(fallback, className, ruleName, ruleValue);
            }

            return(bundle);
        }
Esempio n. 2
0
        private static System.Web.Optimization.Bundle DoCdnFallbackExpress(this System.Web.Optimization.Bundle bundle, string fallback,
                                                                           string className = null, string ruleName = null, string ruleValue = null)
        {
            bundle.Include(fallback);

            fallback = VirtualPathUtility.ToAbsolute(fallback);

            bundle.CdnFallbackExpression = String.IsNullOrEmpty(className)

                ? String.Format(@"function() {{
                    var len = document.styleSheets.length;
                    for (var i = 0; i < len; i++) {{
                        var sheet = document.styleSheets[i];
                        if (sheet.href && sheet.href.indexOf('{0}') !== -1) {{
                            var rules = sheet.rules || sheet.cssRules;
                            if (rules.length <= 0) {{
                                document.write('<link href=""{1}"" rel=""stylesheet"" type=""text/css"" />');
                            }}
                        }}
                    }}
                    return true;
                    }}()", bundle.CdnPath, fallback)

                : String.Format(@"function() {{
                    var loadFallback,
                        len = document.styleSheets.length;
                    for (var i = 0; i < len; i++) {{
                        var sheet = document.styleSheets[i];
                        if (sheet.href && sheet.href.indexOf('{0}') !== -1) {{
                            var meta = document.createElement('meta');
                            meta.className = '{2}';
                            document.head.appendChild(meta);
                            var value = window.getComputedStyle(meta).getPropertyValue('{3}');
                            document.head.removeChild(meta);
                            if (value !== '{4}') {{
                                document.write('<link href=""{1}"" rel=""stylesheet"" type=""text/css"" />');
                            }}
                        }}
                    }}
                return true;
            }}()", bundle.CdnPath, fallback, className, ruleName, ruleValue);

            return(bundle);
        }