Is There a CSS Parser For C#

c# parse css using ExCSS

Because you're passing, not only the CSS but also the parent style tag to the parser. You should've pass just the content of style by using InnerHtml instead of OuterHtml. Then you can do as follows to get the target font-style :

// find among the CSS rules
var fontStyle = stylesheet.StyleRules
// rule for class `csFC2BB1D1`
.Where(r => r.Selector.ToString() == ".csFC2BB1D1")
// then select the value for `font-style`
.Select(o => o.Declarations.First(p => p.Name == "font-style").Term.ToString())
.First();

I prefer query syntax for in this scenario though :

var query = from rule in stylesheet.StyleRules
where rule.Selector.ToString() == ".csFC2BB1D1"
from declaration in rule.Declarations
where declaration.Name == "font-style"
select declaration.Term.ToString();

var fontStyle = query.FirstOrDefault();

dotnetfiddle demo

Parsing CSS with AngleSharp

Thanks for asking this question and sorry for some outdated documentation that is still flying around.

AngleSharp does not come with CSS; instead, AngleSharp can be extended with functionality from other libraries. One of these libraries is AngleSharp.Css.

As the name suggests this library brings CSS capabilities.

Assuming you installed both (AngleSharp and AngleSharp.Css) via NuGet you can do:

  var config = Configuration.Default.WithDefaultLoader(new LoaderOptions { IsResourceLoadingEnabled = true }).WithCss();
var context = BrowsingContext.New(config);
var address = "http://www.example.com"; // any reason for dropping the protocol?
var document = await context.OpenAsync(address);
var sheet = document.QuerySelector<IHtmlLinkElement>("link[rel=stylesheet]")?.Sheet;

I used the following namespace imports.

    AngleSharp
AngleSharp.Dom
AngleSharp.Html.Dom
AngleSharp.Io

Hope that helps!

Parse CSS out from style elements

Try Regex.

goto:http://gskinner.com/RegExr/
paste html with css, and use this expression at the top:

<style type=\"text/css\">(.*?)</style>

here is the c# version:

using System.Text.RegularExpressions;

Match m = Regex.Match(this.textBox1.Text, "<style type=\"text/css\">(.*?)</style>", RegexOptions.Singleline);

if (m.Success)
{
string css = m.Groups[1].Value;
//do stuff
}

Simple css parsing guidance needed

Specifications for syntax and parsing rules:

  • http://www.w3.org/TR/CSS21/syndata.html
  • http://www.w3.org/TR/css3-syntax/#syntax

As for IE10, as far as I know, final version of it will be available for Windows Vista/7 too.

Parsing CSS in C#: extracting all URLs

Finally got Alba.CsCss, my port of CSS parser from Mozilla Firefox, working.

First and foremost, the question contains two errors:

  1. url (img) syntax is incorrect, because space is not allowed between url and ( in CSS grammar. Therefore, "img6", "img7" and "img8" should not be returned as URLs.

  2. An unclosed quote in url function (url('img)) is a serious syntax error; web browsers, including Firefox, do not seem to recover from it and simply skip the rest of the CSS file. Therefore, requiring the parser to return "img9" and "img10" is unnecessary (but necessary if the two problematic lines are removed).

With CsCss, there are two solutions.

The first solution is to rely just on the tokenizer CssScanner.

List<string> uris = new CssLoader().GetUris(source).ToList();

This will return all "img" URLs (except mentioned in the error #1 above), but will also include "noimg3" as property names are not checked.

The second solution is to properly parse the CSS file. This will most closely mimic the behavior of browsers (including stopping parsing after an unclosed quote).

var css = new CssLoader().ParseSheet(source, SheetUri, BaseUri);
List<string> uris = css.AllStyleRules
.SelectMany(styleRule => styleRule.Declaration.AllData)
.SelectMany(prop => prop.Value.Unit == CssUnit.List
? prop.Value.List : new[] { prop.Value })
.Where(value => value.Unit == CssUnit.Url)
.Select(value => value.OriginalUri)
.ToList();

If the two problematic lines are removed, this will return all correct "img" URLs.

(The LINQ query is complex, because background-image property in CSS3 can contain a list of URLs.)

Parsing nested CSS-styled text in C#

You can try the solution below, based on a regular expression :

string myLine = "Sentence 1<style styles='B;fg:Green'>STYLED SENTENCE</style>Sentence 2";
const string splitLinesRegex = @"((?<Styled>\<style[^\>]*\>[^\<\>]*\</style\>)|(?<NoStyle>[^\<\>]*))";

var splitLinesMatch = Regex.Matches(myLine, splitLinesRegex, RegexOptions.Compiled);
List<string> styledLinesBis = new List<string>();

foreach (Match item in splitLinesMatch)
{
if (item.Length > 0)
{
if (!string.IsNullOrEmpty(item.Groups["Styled"].Value))
styledLinesBis.Add(string.Format("<style styles='B'>{0}</style> ", item.Groups["Styled"].Value));

if (!string.IsNullOrEmpty(item.Groups["NoStyle"].Value))
styledLinesBis.Add(string.Format("<style styles='B;fg:Red'>{0}</style> ", item.Groups["NoStyle"].Value));
}
}

You just have to join the strings, using a string.Join statement for instance.

How to get css properties from a parsed html element using AngleSharp

First of all the C# code is wrong. The string you pass on to parser.Parse is an HTML code, not an URI. Also your code does not do any CSS parsing - it is only using an HTML parser. Thus let's use a browsing context to get all you need.

var config = Configuration.Default.WithDefaultLoader().WithCss();
var context = BrowsingContext.New(config);
var document = context.OpenAsync("http://localhost/test.html").Result;
var element = document.QuerySelector("span.foo");
var style = document.DefaultView.GetComputedStyle(element);
var size = style.BackgroundSize;

Keep in mind that element may be null if no such element exists (none matching the query) and that the GetComputedStyle method only works in a limited way. Also if your CSS is defined in an external style sheet make sure to activate resource loading.

Hope this helps!



Related Topics



Leave a reply



Submit