-
Notifications
You must be signed in to change notification settings - Fork 0
Converters (mixins)
Gabriel Scatolin edited this page Sep 25, 2023
·
1 revision
You can define custom converters which converts properties and values into new ones. This is only supported by hard-coding into your assembly and does not interpret instructions inside your input.
Example:
using SimpleCSS;
static void Main(string[] args)
{
string css = """
div {
size: 100px 400px;
}
""";
string pretty = SimpleCSSCompiler.Compile(css, new SimpleCSS.CSSCompilerOptions()
{
Converters = new()
{
new CssSizeConverter()
}
});
Console.WriteLine(pretty);
}
public class CssSizeConverter : CSSConverter
{
public override bool CanConvert(string propertyName, string value)
{
// determines if the property should be converted
return propertyName == "size";
}
public override void Convert(string? value, NameValueCollection outputDeclarations)
{
// get values and remove the default value
string[] values = this.SafeSplit(value);
value = null;
// output the new values
outputDeclarations.Set("width", values[0]);
outputDeclarations.Set("height", values[1]);
}
}
And get the converted output:
div {
width: 100px;
height: 400px;
}
Note: don't use
string.Split()
to split your values. Use the basebase.SafeSplit()
to split values into expressions, which it supports splitting string and expression literals.