Inlining CSS in C#

不问归期 提交于 2019-11-28 04:23:01
Richard Cook

Since you're already 90% of the way there with your current implementation, why don't you use your existing framework but replace the XML parsing with an HTML parser instead? One of the more popular ones out there is the HTML Agility Pack. It supports XPath queries and even has a LINQ interface similar to the standard .NET interface provided for XML so it should be a fairly straightforward replacement.

I have a project on Github that makes CSS inline. It's very simple, and support mobile styles. Read more on my blog: http://martinnormark.com/move-css-inline-premailer-net

Excellent question.

I have no idea if there is a .NET solution, but I found a Ruby program called Premailer that claims to inline CSS. If you want to use it you have a couple options:

  1. Rewrite Premailer in C# (or any .NET language you are familiar with)
  2. Use IronRuby to run Ruby in .NET

I'd recommend using an actual CSS parser rather than Regexes. You don't need to parse the full language since you're interested mostly in reproduction, but in any case such parsers are available (and for .NET too). For example, take a look at antlr's list of grammars, specifically a CSS 2.1 grammar or a CSS3 grammar. You can possibly strip large parts of both grammars if you don't mind sub-optimal results wherein inline styles may include duplicate definitions, but to do this well, you'll need some idea of internal CSS logic to be able to resolve shorthand attributes.

In the long run, however, this will certainly be a lot less work than a neverending series of adhoc regex fixes.

As this option is not very clear in the other replies, I think it deserves a straightforward answer.

Use PreMailer.Net.

All you have to do is:

  1. Install PreMailer.NET via nuget.
  2. Type this:

    var inlineStyles = PreMailer.Net.PreMailer.MoveCssInline(htmlSource, false);
    destination = inlineStyles.Html;
    

And you are done!

BTW, you may want to add a using directive to shorten that line.

More usage info in the link above, of course.

Here is an idea, why dont you make a post call to http://www.mailchimp.com/labs/inlinecss.php using c#. from analysis using firebug it looks like the post call needs 2 params html and strip which takes values (on/off) the result is in a param called text.

here is a sample on how to make a post call using c#

Chad, do you necessarily have to add the CSS inline? Or could you maybe be better off by adding a <style> block to your <head>? This will in essence replace the need for a reference to a CSS file as well plus maintain the rule that the actual inline rules override the ones set in the header/referenced css file.

(sorry, forgot to add the quotes for code)

I would recommend a dictonary like this:

private Dictionary<string, Dictionary<string, string>> cssDictionary = new Dictionary<string, Dictionary<string, string>();

I would parse the css to fill this cssDictionary.

(Adding 'style-type', 'style-property', 'value'. In example:

Dictionary<string,string> bodyStyleDictionary = new Dictionary<string, string();
    bodyStyleDictionary.Add("background", "#000000");
    cssDictionary.Add("body", bodyStyleDictionary);

After that I would preferably convert the HTML to an XmlDocument.

You can recursively run through the documents nodes by it's children and also look up it's parents (This would even enable you being able to use selectors).

On each element you check for the element type, the id and the class. You then browse through the cssDictionary to add any styles for this element to the style attribute (Granted, you might want to place them in order of occurrence if they have overlapping properties (And add the existing inline styles the last).

When you're done, you emit the xmlDocument as a string and remove the first line (<?xml version="1.0"?>) This should leave you with a valid html document with inline css.

Sure, it might half look like a hack, but in the end I think it's a pretty solid solution that ensures stability and quite does what you seem to be looking for.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!