private void AppendAttribute(TagHelperContent content, string key, object value, bool escapeQuotes) { content .Append(" ") .Append(key); if (escapeQuotes) { // Passed only JavaScript-encoded strings in this case. Do not perform HTML-encoding as well. content .Append("=\\\"") .Append((string)value) .Append("\\\""); } else { // HTML-encoded the given value if necessary. content .Append("=\"") .Append(HtmlEncoder, ViewContext.Writer.Encoding, value) .Append("\""); } }
private void BuildLinkTag(TagHelperAttributeList attributes, TagHelperContent builder) { EnsureFileVersionProvider(); builder.Append("<link "); foreach (var attribute in attributes) { var attributeValue = attribute.Value; if (AppendVersion == true && string.Equals(attribute.Name, HrefAttributeName, StringComparison.OrdinalIgnoreCase)) { // "href" values come from bound attributes and globbing. So anything but a non-null string is // unexpected but could happen if another helper targeting the same element does something odd. // Pass through existing value in that case. var attributeStringValue = attributeValue as string; if (attributeStringValue != null) { attributeValue = _fileVersionProvider.AddFileVersionToPath(attributeStringValue); } } builder .Append(attribute.Name) .Append("=\"") .Append(HtmlEncoder, ViewContext.Writer.Encoding, attributeValue) .Append("\" "); } builder.Append("/>"); }
private void BuildScriptTag( TagHelperContent content, TagHelperAttributeList attributes, TagHelperContent builder) { EnsureFileVersionProvider(); builder.Append("<script"); foreach (var attribute in attributes) { var attributeValue = attribute.Value; if (FileVersion == true && string.Equals(attribute.Name, SrcAttributeName, StringComparison.OrdinalIgnoreCase)) { // "src" values come from bound attributes and globbing. So anything but a non-null string is // unexpected but could happen if another helper targeting the same element does something odd. // Pass through existing value in that case. var attributeStringValue = attributeValue as string; if (attributeStringValue != null) { attributeValue = _fileVersionProvider.AddFileVersionToPath(attributeStringValue); } } AppendAttribute(builder, attribute.Name, attributeValue, escapeQuotes: false); } builder.Append(">") .Append(content) .Append("</script>"); }
private void BuildFallbackBlock(TagHelperContent builder) { EnsureGlobbingUrlBuilder(); var fallbackHrefs = GlobbingUrlBuilder.BuildUrlList(FallbackHref, FallbackHrefInclude, FallbackHrefExclude).ToArray(); if (fallbackHrefs.Length > 0) { if (AppendVersion == true) { for (var i=0; i < fallbackHrefs.Length; i++) { // fallbackHrefs come from bound attributes and globbing. Must always be non-null. Debug.Assert(fallbackHrefs[i] != null); fallbackHrefs[i] = _fileVersionProvider.AddFileVersionToPath(fallbackHrefs[i]); } } builder.Append(Environment.NewLine); // Build the <meta /> tag that's used to test for the presence of the stylesheet builder.AppendFormat( CultureInfo.InvariantCulture, "<meta name=\"x-stylesheet-fallback-test\" class=\"{0}\" />", HtmlEncoder.HtmlEncode(FallbackTestClass)); // Build the <script /> tag that checks the effective style of <meta /> tag above and renders the extra // <link /> tag to load the fallback stylesheet if the test CSS property value is found to be false, // indicating that the primary stylesheet failed to load. builder .Append("<script>") .AppendFormat( CultureInfo.InvariantCulture, JavaScriptResources.GetEmbeddedJavaScript(FallbackJavaScriptResourceName), JavaScriptEncoder.JavaScriptStringEncode(FallbackTestProperty), JavaScriptEncoder.JavaScriptStringEncode(FallbackTestValue), JavaScriptStringArrayEncoder.Encode(JavaScriptEncoder, fallbackHrefs)) .Append("</script>"); } }