コード例 #1
0
        /// <summary> Renders the resource element described by resource. </summary>
        /// <exception cref="InvalidOperationException"> Thrown if the resource type is invalid. </exception>
        /// <param name="resource"> The resource to render. </param>
        /// <returns> The rendered resource as a string. </returns>
        public virtual string RenderResourceElement(ResourceInfo resource)
        {
            var    text             = string.Empty;
            var    fallbackTestExpr = resource.FallbackTestExpression;
            string fallbackElement  = null;

            switch (resource.Type)
            {
            case ResourceTypes.CSS:
                text = _RenderCSS(resource.GetURI(_UrlHelper));

                if (!string.IsNullOrWhiteSpace(resource.FallbackPath))
                {
                    if (!string.IsNullOrWhiteSpace(resource.FallbackTestClass))
                    {
                        text += Environment.NewLine + "<meta name=\"x-stylesheet-fallback-test\" class=\"" + resource.FallbackTestClass + "\" />";
                    }

                    if (fallbackTestExpr == null)
                    {
                        fallbackTestExpr = @"(function (a, b, c) { var e = _d.getElementsByTagName('meta'), meta = e[e.length-1]; return _d.defaultView.getComputedStyle(meta)[a] === b;})('" + resource.FallbackTestProperty + @"', '" + resource.FallbackTestPropertyValue + @"', '" + resource.FallbackPath + @"')";
                    }

                    fallbackElement = _RenderCSS(resource.GetFallbackURI(_UrlHelper));
                }
                break;

            case ResourceTypes.Script:     // (or Scripts.Render() can be used)
                text = _RenderScript(resource.GetURI(_UrlHelper));

                if (!string.IsNullOrWhiteSpace(resource.FallbackPath))
                {
                    fallbackElement = _RenderScript(resource.GetFallbackURI(_UrlHelper));
                }
                break;

            default:
                throw new InvalidOperationException("ResourceInfo: Invalid resource type value for URI '" + resource.Path + "'.");
            }

            if (!string.IsNullOrWhiteSpace(text))
            {
                text += Environment.NewLine;

                if (!string.IsNullOrEmpty(fallbackTestExpr) && !string.IsNullOrEmpty(fallbackElement))
                {
                    text += @"
<script>
    (function () {
        var _d = document, test = (" + fallbackTestExpr + @");
        (test || _d.write('" + fallbackElement.Replace("<", "\\u003c").Replace(">", "\\u003e") + @"'));
    })();
</script>
" + Environment.NewLine;
                }
            }

            return(text ?? string.Empty);
        }