Edward Wilde
Edward Wilde

Reputation: 26507

Is there a Css writer for .net?

I've noticed that Microsoft has implemented a CssTextWriter which is internal

internal sealed class CssTextWriter : TextWriter
{
  ....
}

Is there a Css writer for .net that?

For example I would like to write code such as:

CssTextWriter writer = new CssTextWriter(textWriter);
writer.WriteBeginCssRule("p");
writer.WriteAttribute("font-family", "Arial,Liberation Sans,DejaVu Sans,sans-serif");
writer.WriteEndCssRule();

The above code would output to the stream as follows:

p { font-family: Arial,Liberation Sans,DejaVu Sans,sans-serif; }

Upvotes: 0

Views: 832

Answers (1)

Edward Wilde
Edward Wilde

Reputation: 26507

dotLess http://www.dotlesscss.org/ looks like it would do the job, but was a bit much I just needed a single clss

I wrapped calls to the internal Microsoft class (yes naughty and it might go away in future releases of .net etc....)

public class CssTextWriter
{
    public CssTextWriter(TextWriter writer)
    {
        if (writer == null)
        {
            throw new ArgumentNullException("writer");
        }

        this.Writer = writer;
        this.Initialize();
    }

    /// <summary>
    /// Gets the writer.
    /// </summary>
    /// <value>
    /// The writer.
    /// </value>
    public TextWriter Writer { get; private set; }

    /// <summary>
    /// Gets or sets the internal CSS text writer.
    /// </summary>
    /// <value>
    /// The internal CSS text writer.
    /// </value>
    private object InternalCssTextWriter
    {
        get;

        set;
    }

     public void WriteBeginCssRule(string selector)
    {
        this.InternalCssTextWriter.InvokeMethod("WriteBeginCssRule", new[] { selector });
    }

    public void WriteEndCssRule()
    {
        this.InternalCssTextWriter.InvokeMethod("WriteEndCssRule");
    }

    public void WriteAttribute(string name, string value)
    {
        this.InternalCssTextWriter.InvokeMethod("WriteAttribute", new[] { name, value }, new Type[] { typeof(string), typeof(string) });
    }

    public void Write(string value)
    {
        this.InternalCssTextWriter.InvokeMethod("Write", new[] { value }, new Type[] { typeof(string) });
    }

    public void WriteAttribute(HtmlTextWriterStyle key, string value)
    {
        this.InternalCssTextWriter.InvokeMethod("WriteAttribute", new object[] { key, value }, new Type[] { typeof(HtmlTextWriterStyle), typeof(string) });
    }

    private void Initialize()
    {
        Type internalType = typeof(System.Web.UI.HtmlTextWriter).Assembly.GetType("System.Web.UI.CssTextWriter");
        ConstructorInfo ctor = internalType.GetConstructors(BindingFlags.Instance | BindingFlags.Public)[0];
        this.InternalCssTextWriter = ctor.Invoke(new[] { this.Writer });
    }
}

Upvotes: 1

Related Questions