/// <summary> /// Generates a <link > tag. /// </summary> public static void GenerateLink(this OutputHelper @this, string href) { var e = new Element("link", ClosingTagKind.None); e.Attributes.Add(new ElementAttribute("rel", "stylesheet")); e.Attributes.Add(new ElementAttribute("href", href)); @this.Add(e); }
public HtmlString Render(Element element) { if (element == null) { throw new ArgumentNullException(nameof(element)); } return new HtmlString(RenderCore(element)); }
/// <summary> /// Generates a <script></script> tag. /// </summary> public static void GenerateScript(this OutputHelper @this, string src, string type = null) { var e = new Element("script", ClosingTagKind.Normal); e.Attributes.Add(new ElementAttribute("src", src)); if (type != null) { e.Attributes.Add(new ElementAttribute("type", type)); } @this.Add(e); }
private string RenderCore(Element element) { var sb = new StringBuilder(); sb.Append($"<{element.TagName}"); if (element.Attributes.Any()) { sb.Append(" "); } for (int i = 0; i < element.Attributes.Count; i++) { var attribute = element.Attributes[i]; sb.Append($"{attribute.Name}=\"{attribute.Value}\""); if (i != element.Attributes.Count - 1) { sb.Append(" "); } } if (!string.IsNullOrWhiteSpace(element.Content) && element.ClosingTagKind != ClosingTagKind.Normal) { throw new InvalidOperationException( "Elements with a non-normal closing tag kind cannot have content."); } switch (element.ClosingTagKind) { case ClosingTagKind.None: sb.Append(" >"); break; case ClosingTagKind.SelfClosing: sb.Append(" />"); break; case ClosingTagKind.Normal: sb.Append(">"); if (!string.IsNullOrWhiteSpace(element.Content)) { sb.Append(element.Content); } sb.Append($"</{element.TagName}>"); break; } return sb.ToString(); }
public void Add(Element element) { _elements.Add(element); }