athari / cscss Goto Github PK
View Code? Open in Web Editor NEWCSS parsing library for C# based on Mozilla Firefox code [MPL]
License: Mozilla Public License 2.0
CSS parsing library for C# based on Mozilla Firefox code [MPL]
License: Mozilla Public License 2.0
Hi.
I use Alba.CssCss library in my project for parsing css blocks and found some problems within it related to css error handling.
ErrorReporter class is used for formatting error messages and uses resource strings as format parameter with String.Format call.
Some resource strings have format text incompatible with .net String.Format call.
The problem is with unescaped characters '{' or '}' which have special meaning for string format engine.
Here is an example:
key: PEBadDeclBlockStart
value: Expected '{' to begin declaration block but found '{0}'.
If you call String.Format with such format string FormatException will be thrown.
Correct value would be like this:
Expected '{{' to begin declaration block but found '{0}'.
String.Format replaces "{{" and "}}" with "{" and "}" ...
So i've used pretty simple code to find all similar entries.
var rm = new ResourceManager("Alba.CsCss.Globalization.CssResources", typeof(CssStyleSheet).Assembly);
var resources = rm.GetResourceSet(CultureInfo.CurrentCulture, true, true);
var badEntries = resources.Cast<DictionaryEntry>().Where(e => e.Key is string && e.Value is string && ((string)e.Value).Contains("{0}") &&
((string)e.Value).ToCharArray().Where(c => c == '{').Count() != ((string)e.Value).ToCharArray().Where(c => c == '}').Count()).ToArray();
var builder = new StringBuilder();
foreach (var e in badEntries)
{
builder.AppendFormat("{0}={1}", e.Key, e.Value).AppendLine();
}
Console.WriteLine(builder.ToString());
Here are the entries displayed
PESelectorListExtra=Expected ',' or '{' but found '{0}'.
PESupportsGroupRuleStart=Expected '{' to begin @supports rule but found '{0}'.
PEBadDeclOrRuleEnd2=Expected ';' or '}' to terminate declaration but found '{0}'.
PEBadDeclBlockStart=Expected '{' to begin declaration block but found '{0}'.
PEBadFontBlockEnd=Expected '}' to end @font-face rule but found '{0}'.
PEBadFontBlockStart=Expected '{' to begin @font-face rule but found '{0}'.
Don't work or I don't understand.
p.pastoral.marine { color: green }
Having the library parse color values such as "red" or "green" to convert them to CssColor objects would be nice. Version 1.0.0.3 does not seem to support this. I think the System.Drawing.Color methods could be used to achieve this.
Lex/Yacc programming enthusiasts, I am very pleased to invite you to try a new series of lex/yacc tools.
Fslex is a code generator that uses regular expression syntax as a rule to generate a function, which divides the input token sequence into groups at a higher level. The Fslex is often used to remove redundant delimiters, add omitted delimiters or other syntax components, and so on. The Fslex is also used to determine context somewhere in the stream.
Fsyacc is a code generator that use BNF productions and precedences as a rule to generate a function, which resolves the input token sequence to an abstract syntax tree.
Why use this package?
You can use your existing handwriting tokenizer.
This package uses standard lex/yacc syntax to minimize your learning costs.
fslex/fsyacc generates respectively an independent, side-effect-free function that can be called flexibly.
The method of generating code is simple, without command lines and without the need to configure projects.
The result code is data-driven and highly readable.
Flexiblely compose of tokenize, regular expressions, BNF technology.
You can check it by following the link: https://github.com/xp44mm/FslexFsyacc
Too much freedom is bad. Less API compatibility issues is good.
Not sure if this is an XML documentation or implementation issue (or only a misunderstanding on my part), but many snippets of documentation refer to Alba.CsCss.Style.nsCSSKeywords
(along with CssUnit.Enumerated
) which does not seem to be accessible.
For instance, the current implementation of the TextAlign (css text-align) property indicates it contains a single value of one of the following types :
inherit
(CssUnit.Inherit
, CssUnit.Initial
)nsCssKeywords
(CssUnit.Enumerated
)nsCssKeywords
seems to be an internal class, and the CssKeywords
enumeration which is visible does not seem to cast correctly to an expected value for this property (https://developer.mozilla.org/docs/Web/CSS/text-align).
Trying to parse td { text-align:right; }
results to a CssValue with TextAlign.Int = 2
and TextAlign.Unit = Enumerated
. With theses values and the current documentation, I can't find a way to retrieve a "Right" enumeration value of any sort.
If this is a misunderstaning on my part because I'm in the learning process of the API, I'd be glad to edit a wiki page to quickly explain how to use these properties :)
Error when parsing this CSS Url https://cdn.sstatic.net/Sites/stackoverflow/primary.css?v=a4a1216f167f
{"Object reference not set to an instance of an object."}
I need to parse URLs from this CSS content but it returns nothing:
@font-face {
font-family: 'Montserrat';
font-style: normal;
font-weight: 400;
src: local('Montserrat Regular'), local('Montserrat-Regular'), url(https://fonts.gstatic.com/s/montserrat/v12/rBHvpRWBkgyW99dXT88n7yEAvth_LlrfE80CYdSH47w.woff2) format('woff2');
unicode-range: U+0460-052F, U+1C80-1C88, U+20B4, U+2DE0-2DFF, U+A640-A69F, U+FE2E-FE2F;
}
I have used this code to get URLs:
var uris = _cssLoader.AllStyleRules
.SelectMany(styleRule => styleRule.Declaration.AllData)
.SelectMany(prop => prop.Value.Unit == CssUnit.List ? prop.Value.List : new[] { prop.Value })
.Where(val => val.Unit == CssUnit.Url)
// Get unresolved URLs (you can use Uri property to get resolved URLs)
.Select(val => val.OriginalUri)
.ToList();
According to the CSS docs (http://www.w3.org/TR/CSS2/selector.html § 5.10), pseudo elements and pseudo classes are case-insensitive. Values like :first-child
, :First-Child
or :FIRST-CHILD
should be parsed in the same way.
The following snippet validates correctly with the w3 CSS validator but fails to be parsed with CsCss 1.0.1.0:
@charset "UTF-8";
div:First-Child {
text-align: center;
}
I've tested it using the project sources by adding the following test case :
[TestMethod]
public void CssFirstChild_20140527()
{
var loader = new CssLoader();
var css = loader.ParseSheet(Css20140527_pseudo_element_case, SheetUri, BaseUri);
Assert.IsTrue(css.StyleRules.Any(), "No rules found");
}
private string Css20140527_pseudo_element_case
{
get { return GetResourceFile("20140527_pseudo-element-case.css"); }
}
As a temporary / permanent workaround I'll update my CSS files to use lower case pseudo elements.
Do you any plans to support your library running on .NET Core in the near future?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.