Created
July 18, 2018 12:51
-
-
Save ukcoderj/ec0d15016a813d445900344a96818cff to your computer and use it in GitHub Desktop.
ExCSS getting and setting CSS Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void WorkWithExCss(string path) | |
{ | |
// With nuget package (2.0.6) ref: https://github.com/TylerBrinks/ExCSS | |
// read an existing stylesheet | |
var css = System.IO.File.ReadAllText(path); | |
var stylesheet = new ExCSS.Parser().Parse(css); | |
// Assuming the stylesheet has a selector for 'html' | |
var info = stylesheet.StyleRules.First(i => i.Selector.ToString() == "html"); | |
var selector = info.Selector; | |
var firstCssProperty = info.Declarations.First(); | |
var valueObj = firstCssProperty.Term; | |
var value = valueObj.ToString(); | |
// Create a new stylesheet | |
var stylesheetNew = new ExCSS.Parser().Parse(".fake{ color:none; }"); | |
while(stylesheetNew.Rules.Count() > 0) | |
{ | |
stylesheetNew.Rules.RemoveAt(0); | |
} | |
ExCSS.StyleRule r = new ExCSS.StyleRule(); | |
r.Selector = new ExCSS.SimpleSelector("h2"); | |
var prop1 = new ExCSS.Property("background-color"); | |
prop1.Term = new ExCSS.PrimitiveTerm(ExCSS.UnitType.Unknown, "red"); | |
r.Declarations.Add(prop1); | |
stylesheetNew.Rules.Add(r); | |
var newSheet = stylesheetNew.ToString(friendlyFormat: true, indentation: 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment