Пример #1
0
		/// <summary>
		/// Generates a &lt;link &gt; 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);
		}
Пример #2
0
		public HtmlString Render(Element element)
		{
			if (element == null)
			{
				throw new ArgumentNullException(nameof(element));
			}

			return new HtmlString(RenderCore(element));
		}
Пример #3
0
		/// <summary>
		/// Generates a &lt;script&gt;&lt;/script&gt; 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);
		}
Пример #4
0
		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();
		}
Пример #5
0
		public void Add(Element element)
		{
			_elements.Add(element);
		}