How can I programmatically expand CSS shorthand properties?

Is there a .Net CSS parser that will allow me to parse css shorthand properties in the form of their long form?

For example, I would like to do the following:

#somediv{ margin: 10px; padding: 10px 20px; border:5px solid #FFF; } 

And translate it into:

  #somediv{ margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; padding-top: 10px; padding-right: 20px; padding-bottom: 10px; padding-left: 20px; border-width: 5px; border-style: solid; border-color: #FFF; } 

Here is a pretty good list of all the different properties that I will need to handle this way: http://www.dustindiaz.com/css-shorthand/

Ideally, I would like something in .Net, but if there is something in another open source language, I can probably adapt it.

Update

Without going into details about exactly what I'm trying to do here, is a basic premise:

I need to programmatically take multiple CSS documents and combine them to create one final CSS set.

So, if doc 1 has:

 p { padding: 10px;} 

And then add to doc 2:

 p { padding-left:20px;} 

The resulting CSS should be:

 p { padding-top: 10px; padding-right:10px; padding-bottom:10px; padding-left:20px;} 

Since a later added document overwrites one property. To do this for sure, I would need to take each CSS and first break each property into its bottom element.

+4
source share
3 answers

For regular CSS parsing, I found this the easiest to use:

http://www.codeproject.com/KB/recipes/CSSParser.aspx

To destroy the shorthand properties in the form of a long form, I found two that can do this:

On .Net: http://www.modeltext.com/css/index.aspx

In JavaScript: http://www.glazman.org/JSCSSP/

+1
source

The easiest approach is to use the .NET WebBrowserControl with MsHTML (IE Renderer), and this is also the most reliable approach!

 //Create the instance of new webbrowser control. WebBrowser browser = new WebBrowser(); //Navigate to the specified URL. browser.Navigate(@"test.html"); //Wait until the webpage gets loaded completely. while (browser.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); } foreach (object divElement in (browser.Document.GetElementsByTagName("div"))) { IHTMLCurrentStyle currentStyle = ((divElement as HtmlElement) .DomElement as IHTMLElement2).currentStyle; Console.WriteLine(currentStyle.marginLeft); Console.WriteLine(currentStyle.marginRight); } 

Note:

To get this code, you need to add a link to the Microsoft.MSHTML.dll file, which can be found in the following location.

c: {Program Files} \ microsoft.net \ Primary Interop Assemblies \

+1
source

Could you give more details on why you want to do this?

And you are looking for it to properly parse things like:

padding: 10px 15px;

in

padding top: 10px; padding to the right: 15px; padding bottom: 10px; padding to the left: 15px;

0
source

All Articles