Coder Social home page Coder Social logo

athari / cscss Goto Github PK

View Code? Open in Web Editor NEW
77.0 13.0 9.0 1.34 MB

CSS parsing library for C# based on Mozilla Firefox code [MPL]

License: Mozilla Public License 2.0

C# 51.62% CSS 0.02% C++ 48.15% C 0.20% Batchfile 0.01%
c-sharp css-parser css mozilla-firefox library dotnet

cscss's Introduction

GitHub created at GitHub followers GitHub following Time zone: UTC+3 (MSK) Native language: C#
Spoken language: C, C++, JavaScript, PHP, Python, Java, Visual Basic, SQL, HTML, CSS, Delphi, PowerShell, Lua
Human created: English, Russian Human language: English, Russian Gender: male Pronouns: взаимное ты, он, his
Education: specialist, KPFU, IoCMaIT Alignment: chaotic neutral Political views: civil libertarianism
E-mail: athari.p (cat with a curly tail) gmail.com Telegram: @Athari_P Skype: @Athari_P Stack Overflow: Athari Хабрахабр: Athari
Flairs: 1 Badges: 20

StackExchange flair

cscss's People

Contributors

athari avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cscss's Issues

Broken resource strings

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

Don't work or I don't understand.
p.pastoral.marine { color: green }

Invite you to try FslexFsyacc

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

Documentation references to nsCSSKeywords

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 :)

Unable to parse URL from google font face

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();

Pseudo elements parsing and case sensitivity

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.