CssStyleFormatter
Represents the standard CSS3 style formatter.
using AngleSharp.Text;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
namespace AngleSharp.Css
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public sealed class CssStyleFormatter : IStyleFormatter
{
public static readonly IStyleFormatter Instance = new CssStyleFormatter();
string IStyleFormatter.Sheet(IEnumerable<IStyleFormattable> rules)
{
StringBuilder stringBuilder = StringBuilderPool.Obtain();
string newLine = Environment.NewLine;
using (StringWriter stringWriter = new StringWriter(stringBuilder)) {
foreach (IStyleFormattable rule in rules) {
rule.ToCss(stringWriter, this);
stringWriter.Write(newLine);
}
if (stringBuilder.Length > 0)
stringBuilder.Remove(stringBuilder.Length - newLine.Length, newLine.Length);
}
return stringBuilder.ToPool();
}
string IStyleFormatter.BlockRules(IEnumerable<IStyleFormattable> rules)
{
StringBuilder stringBuilder = StringBuilderPool.Obtain().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)
{
return name + ": " + (value + (important ? " !important" : string.Empty));
}
string IStyleFormatter.BlockDeclarations(IEnumerable<IStyleFormattable> declarations)
{
StringBuilder stringBuilder = StringBuilderPool.Obtain().Append('{');
using (StringWriter stringWriter = new StringWriter(stringBuilder)) {
foreach (IStyleFormattable declaration in declarations) {
stringWriter.Write(' ');
declaration.ToCss(stringWriter, this);
stringWriter.Write(';');
}
if (stringBuilder.Length > 1)
stringBuilder.Remove(stringBuilder.Length - 1, 1);
}
return stringBuilder.Append(' ').Append('}').ToPool();
}
string IStyleFormatter.Rule(string name, string value)
{
return name + " " + value + ";";
}
string IStyleFormatter.Rule(string name, string prelude, string rules)
{
return name + " " + (string.IsNullOrEmpty(prelude) ? string.Empty : (prelude + " ")) + rules;
}
string IStyleFormatter.Comment(string data)
{
return string.Join("/*", data, "*/");
}
}
}