AngleSharp by AngleSharp

<PackageReference Include="AngleSharp" Version="0.9.11" />

 CssStyleFormatter

public sealed class CssStyleFormatter : IStyleFormatter
Represents the standard CSS3 style formatter.
using System; using System.Collections.Generic; using System.IO; using System.Text; namespace AngleSharp.Css { public sealed class CssStyleFormatter : IStyleFormatter { public static readonly IStyleFormatter Instance = new CssStyleFormatter(); string IStyleFormatter.Sheet(IEnumerable<IStyleFormattable> rules) { StringBuilder sb = Pool.NewStringBuilder(); WriteJoined(sb, rules, Environment.NewLine, true); return sb.ToPool(); } string IStyleFormatter.Block(IEnumerable<IStyleFormattable> rules) { StringBuilder stringBuilder = Pool.NewStringBuilder().Append('{'); using (StringWriter stringWriter = new StringWriter(stringBuilder)) { foreach (IStyleFormattable rule in rules) { stringWriter.Write(' '); rule.ToCss(stringWriter, this); } } return stringBuilder.Append(' ').Append('}').ToPool(); } string IStyleFormatter.Declaration(string name, string value, bool important) { string str = value + (important ? " !important" : string.Empty); return name + ": " + str; } string IStyleFormatter.Declarations(IEnumerable<string> declarations) { return string.Join("; ", declarations); } string IStyleFormatter.Medium(bool exclusive, bool inverse, string type, IEnumerable<IStyleFormattable> constraints) { StringBuilder stringBuilder = Pool.NewStringBuilder(); bool first = true; if (exclusive) stringBuilder.Append("only "); else if (inverse) { stringBuilder.Append("not "); } if (!string.IsNullOrEmpty(type)) { stringBuilder.Append(type); first = false; } WriteJoined(stringBuilder, constraints, " and ", first); return stringBuilder.ToPool(); } string IStyleFormatter.Constraint(string name, string value) { string str = (value != null) ? (": " + value) : string.Empty; return "(" + name + str + ")"; } string IStyleFormatter.Rule(string name, string value) { return name + " " + value + ";"; } string IStyleFormatter.Rule(string name, string prelude, string rules) { string str = string.IsNullOrEmpty(prelude) ? string.Empty : (prelude + " "); return name + " " + str + rules; } string IStyleFormatter.Style(string selector, IStyleFormattable rules) { StringBuilder stringBuilder = Pool.NewStringBuilder().Append(selector).Append(" { "); int length = stringBuilder.Length; using (StringWriter writer = new StringWriter(stringBuilder)) rules.ToCss(writer, this); if (stringBuilder.Length > length) stringBuilder.Append(' '); return stringBuilder.Append('}').ToPool(); } string IStyleFormatter.Comment(string data) { return string.Join("/* ", data, " */"); } private void WriteJoined(StringBuilder sb, IEnumerable<IStyleFormattable> elements, string separator, bool first = true) { using (StringWriter stringWriter = new StringWriter(sb)) { foreach (IStyleFormattable element in elements) { if (first) first = false; else stringWriter.Write(separator); element.ToCss(stringWriter, this); } } } } }