Esempio n. 1
0
 private static void CopyAttributes(TagHelperOutput output, IHtmlContentBuilder destination, bool move = false)
 {
     if (output.Attributes.Count > 0)
     {
         foreach (var attr in output.Attributes)
         {
             destination.Append(" ");
             if (move)
             {
                 attr.MoveTo(destination);
             }
             else
             {
                 attr.CopyTo(destination);
             }
         }
     }
 }
Esempio n. 2
0
        private void CopyAttributesTo(IHtmlContentBuilder destination)
        {
            StringWriter stringWriter = null;

            // Perf: Avoid allocating enumerator
            for (var i = 0; i < (Attributes.Count); i++)
            {
                var attribute = Attributes[i];
                destination.AppendHtml(" ");
                destination.AppendHtml(attribute.Name);

                if (attribute.Minimized)
                {
                    continue;
                }

                destination.AppendHtml("=\"");
                var value       = attribute.Value;
                var htmlContent = value as IHtmlContent;
                if (htmlContent != null)
                {
                    // Perf: static text in a bound attribute go down this path. Avoid allocating if possible (common case).
                    var htmlEncodedString = value as HtmlEncodedString;
                    if (htmlEncodedString != null && !htmlEncodedString.Value.Contains("\""))
                    {
                        destination.AppendHtml(htmlEncodedString);
                    }
                    else
                    {
                        // Perf: We'll share this writer implementation for all attributes since
                        // they can't nest.
                        stringWriter = stringWriter ?? new StringWriter();

                        destination.AppendHtml(new AttributeContent(htmlContent, stringWriter));
                    }
                }
                else if (value != null)
                {
                    destination.Append(value.ToString());
                }

                destination.AppendHtml("\"");
            }
        }
 /// <summary>
 /// Sets the content to the <see cref="IHtmlContent"/> value.
 /// </summary>
 /// <param name="builder">The <see cref="IHtmlContentBuilder"/>.</param>
 /// <param name="value">The <see cref="IHtmlContent"/> value that replaces the content.</param>
 /// <returns>The <see cref="IHtmlContentBuilder"/>.</returns>
 public static IHtmlContentBuilder SetContent(this IHtmlContentBuilder builder, IHtmlContent content)
 {
     builder.Clear();
     builder.Append(content);
     return(builder);
 }
 /// <summary>
 /// Sets the content to the <see cref="string"/> value. The value is treated as unencoded as-provided,
 /// and will be HTML encoded before writing to output.
 /// </summary>
 /// <param name="builder">The <see cref="IHtmlContentBuilder"/>.</param>
 /// <param name="value">The <see cref="string"/> value that replaces the content.</param>
 /// <returns>The <see cref="IHtmlContentBuilder"/>.</returns>
 public static IHtmlContentBuilder SetContent(this IHtmlContentBuilder builder, string unencoded)
 {
     builder.Clear();
     builder.Append(unencoded);
     return(builder);
 }
 /// <summary>
 /// Appends an <see cref="Environment.NewLine"/> after appending the <see cref="string"/> value.
 /// The value is treated as HTML encoded as-provided, and no further encoding will be performed.
 /// </summary>
 /// <param name="builder">The <see cref="IHtmlContentBuilder"/>.</param>
 /// <param name="content">The HTML encoded <see cref="string"/> to append.</param>
 /// <returns>The <see cref="IHtmlContentBuilder"/>.</returns>
 public static IHtmlContentBuilder AppendLineEncoded(this IHtmlContentBuilder builder, string encoded)
 {
     builder.AppendEncoded(encoded);
     builder.Append(HtmlEncodedString.NewLine);
     return(builder);
 }
Esempio n. 6
0
        private void MoveToCore(object entry, IHtmlContentBuilder destination)
        {
            if (entry == null)
            {
                return;
            }

            string entryAsString;
            IHtmlContentContainer entryAsContainer;
            if ((entryAsString = entry as string) != null)
            {
                destination.Append(entryAsString);
            }
            else if ((entryAsContainer = entry as IHtmlContentContainer) != null)
            {
                entryAsContainer.MoveTo(destination);
            }
            else
            {
                destination.AppendHtml((IHtmlContent)entry);
            }
        }
        /// <inheritdoc />
        public void MoveTo(IHtmlContentBuilder destination)
        {
            if (destination == null)
            {
                throw new ArgumentNullException(nameof(destination));
            }

            for (var i = 0; i < Entries.Count; i++)
            {
                var entry = Entries[i];

                string entryAsString;
                IHtmlContentContainer entryAsContainer;
                if ((entryAsString = entry as string) != null)
                {
                    destination.Append(entryAsString);
                }
                else if ((entryAsContainer = entry as IHtmlContentContainer) != null)
                {
                    // Since we're moving, do a deep flatten.
                    entryAsContainer.MoveTo(destination);
                }
                else
                {
                    // Only string, IHtmlContent values can be added to the buffer.
                    destination.AppendHtml((IHtmlContent)entry);
                }
            }

            Entries.Clear();
        }
Esempio n. 8
0
        /// <inheritdoc />
        public void CopyTo(IHtmlContentBuilder destination)
        {
            if (destination == null)
            {
                throw new ArgumentNullException(nameof(destination));
            }

            destination.AppendHtml(Name);

            if (ValueStyle == HtmlAttributeValueStyle.Minimized)
            {
                return;
            }

            var valuePrefix = GetAttributeValuePrefix(ValueStyle);
            if (valuePrefix != null)
            {
                destination.AppendHtml(valuePrefix);
            }

            string valueAsString;
            IHtmlContentContainer valueAsHtmlContainer;
            IHtmlContent valueAsHtmlContent;
            if ((valueAsString = Value as string) != null)
            {
                destination.Append(valueAsString);
            }
            else if ((valueAsHtmlContainer = Value as IHtmlContentContainer) != null)
            {
                valueAsHtmlContainer.CopyTo(destination);
            }
            else if ((valueAsHtmlContent = Value as IHtmlContent) != null)
            {
                destination.AppendHtml(valueAsHtmlContent);
            }
            else if (Value != null)
            {
                destination.Append(Value.ToString());
            }

            var valueSuffix = GetAttributeValueSuffix(ValueStyle);
            if (valueSuffix != null)
            {
                destination.AppendHtml(valueSuffix);
            }
        }
Esempio n. 9
0
 public void AppendText(string unencoded)
 {
     _builder.Append(unencoded);
 }