/// <summary>
        /// Initializes a new instance of the <see cref="TagHelperContentWrapperTextWriter"/> class.
        /// </summary>
        /// <param name="encoding">The <see cref="Encoding"/> in which output is written.</param>
        /// <param name="content">The <see cref="TagHelperContent"/> to write to.</param>
        public TagHelperContentWrapperTextWriter(Encoding encoding, TagHelperContent content)
        {
            if (encoding == null)
            {
                throw new ArgumentNullException(nameof(encoding));
            }

            if (content == null)
            {
                throw new ArgumentNullException(nameof(content));
            }

            Content = content;
            Encoding = encoding;
        }
示例#2
0
 private void AppendAttribute(TagHelperContent content, string key, object value, bool escapeQuotes)
 {
     content
         .AppendHtml(" ")
         .AppendHtml(key);
     if (escapeQuotes)
     {
         // Passed only JavaScript-encoded strings in this case. Do not perform HTML-encoding as well.
         content
             .AppendHtml("=\\\"")
             .AppendHtml((string)value)
             .AppendHtml("\\\"");
     }
     else
     {
         // HTML-encoded the given value if necessary.
         content
             .AppendHtml("=\"")
             .Append(HtmlEncoder, ViewContext.Writer.Encoding, value)
             .AppendHtml("\"");
     }
 }
示例#3
0
        private void BuildScriptTag(
            TagHelperAttributeList attributes,
            TagHelperContent builder)
        {
            builder.AppendHtml("<script");

            foreach (var attribute in attributes)
            {
                var attributeValue = attribute.Value;
                if (AppendVersion == 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.AppendHtml("></script>");
        }
示例#4
0
        private void BuildGlobbedScriptTags(
            TagHelperAttributeList attributes,
            TagHelperContent builder)
        {
            EnsureGlobbingUrlBuilder();

            // Build a <script> tag for each matched src as well as the original one in the source file
            var urls = GlobbingUrlBuilder.BuildUrlList(null, SrcInclude, SrcExclude);
            foreach (var url in urls)
            {
                // "url" values come from bound attributes and globbing. Must always be non-null.
                Debug.Assert(url != null);

                if (string.Equals(url, Src, StringComparison.OrdinalIgnoreCase))
                {
                    // Don't build duplicate script tag for the original source url.
                    continue;
                }

                attributes[SrcAttributeName] = url;
                BuildScriptTag(attributes, builder);
            }
        }
        private void BuildLinkTag(TagHelperAttributeList attributes, TagHelperContent builder)
        {
            builder.AppendHtml("<link ");

            foreach (var attribute in attributes)
            {
                var attributeValue = attribute.Value;

                builder
                    .AppendHtml(attribute.Name)
                    .AppendHtml("=\"")
                    .Append(HtmlEncoder, ViewContext.Writer.Encoding, attribute.Value)
                    .AppendHtml("\" ");
            }

            builder.AppendHtml("/>");
        }
        private void BuildGlobbedLinkTags(TagHelperAttributeList attributes, TagHelperContent builder)
        {
            EnsureGlobbingUrlBuilder();

            // Build a <link /> tag for each matched href.
            var staticUrl = HostingEnvironment.IsDevelopment() ? String.Format("http://localhost:{0}{1}", devSettings.WebpackDevServerPort, HrefInclude) : "";
            var includePattern = HostingEnvironment.IsDevelopment() ? "" : HrefInclude;

            var urls = GlobbingUrlBuilder.BuildUrlList(staticUrl, includePattern, "");
            foreach (var url in urls)
            {
                // "url" values come from bound attributes and globbing. Must always be non-null.
                Debug.Assert(url != null);

                if (string.Equals(Href, url, StringComparison.OrdinalIgnoreCase))
                {
                    // Don't build duplicate link tag for the original href url.
                    continue;
                }

                attributes[HrefAttributeName] = url;
                BuildLinkTag(attributes, builder);
            }
        }
示例#7
0
        private void BuildLinkTag(TagHelperAttributeList attributes, TagHelperContent builder)
        {
            builder.AppendHtml("<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
                    .AppendHtml(attribute.Name)
                    .AppendHtml("=\"")
                    .Append(HtmlEncoder, ViewContext.Writer.Encoding, attributeValue)
                    .AppendHtml("\" ");
            }

            builder.AppendHtml("/>");
        }
示例#8
0
        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(HtmlString.NewLine);

                // Build the <meta /> tag that's used to test for the presence of the stylesheet
                builder
                    .AppendHtml("<meta name=\"x-stylesheet-fallback-test\" class=\"")
                    .Append(FallbackTestClass)
                    .AppendHtml("\" />");

                // 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
                    .AppendHtml("<script>")
                    .AppendHtml(
                        string.Format(
                            CultureInfo.InvariantCulture,
                            JavaScriptResources.GetEmbeddedJavaScript(FallbackJavaScriptResourceName),
                            JavaScriptEncoder.JavaScriptStringEncode(FallbackTestProperty),
                            JavaScriptEncoder.JavaScriptStringEncode(FallbackTestValue),
                            JavaScriptStringArrayEncoder.Encode(JavaScriptEncoder, fallbackHrefs)))
                    .AppendHtml("</script>");
            }
        }
示例#9
0
        private void BuildGlobbedLinkTags(TagHelperAttributeList attributes, TagHelperContent builder)
        {
            EnsureGlobbingUrlBuilder();

            // Build a <link /> tag for each matched href.
            var urls = GlobbingUrlBuilder.BuildUrlList(null, HrefInclude, HrefExclude);
            foreach (var url in urls)
            {
                // "url" values come from bound attributes and globbing. Must always be non-null.
                Debug.Assert(url != null);

                if (string.Equals(Href, url, StringComparison.OrdinalIgnoreCase))
                {
                    // Don't build duplicate link tag for the original href url.
                    continue;
                }

                attributes[HrefAttributeName] = url;
                BuildLinkTag(attributes, builder);
            }
        }
        private void BuildScriptTag(
            TagHelperAttributeList attributes,
            TagHelperContent builder)
        {
            builder.AppendHtml("<script");

            foreach (var attribute in attributes)
            {
                AppendAttribute(builder, attribute.Name, attribute.Value, escapeQuotes: false);
            }

            builder.AppendHtml("></script>");
        }