CSS Parser

A .NET class library which parses CSS and calculates CSS property values for DOM elements.


Output

The CSS component has 3 APIs, which you can call from your .NET applications:

  • The lowest-level API lets you parse CSS, and then lets you read all the parsed elements in the stylesheet as various property values of an object model.
  • The mid-level API adds functionality to the underlying data:
    • Select media-specific rulesets from a stylesheet,
    • Calculate the specificity of a ruleset for a given DOM element,
    • Calculate the value of the expression in each declaration.
  • The top-level API calculates the specific, absolute property value (including any inherited and/or default values) which apply to an element in a DOM.

For further details, see the APIs for the CSS Component.

Input

The component's parsing is based on the Grammar of CSS 2.1.

Limitations

The component's implementation embeds a formal parser (see the Acknowledgements below). Its processing degrades gracefully when it encounters any unknown CSS properties; but, it does not recover gracefully from malformed declarations and statements: instead it throws an exception (instead of parsing) if it encounters unexpected tokens. It thus obeys only some of the specified Rules for handling parsing errors.

See also: Limitations of the CSS Component.

License

Copyright 2008-2012, Christopher Wells ("Licensor")

Permission to use without fee

Permission to use, copy, and/or distribute this software for any purpose with or without fee is hereby granted to you, provided that you accept all the terms of this license.

Transferable

You may copy and distribute this software to other parties ("third parties"), provided that the above copyright notice and this permission notice appear in all copies, and that third parties are bound by the terms of this license.

Closed source, no modification

This is closed source, proprietary software. The software's source code (except for some sample code) has not been released. Although permission is hereby granted to write software which uses this software component, and to use this software as a component within other software, permission is not granted to modify this software component, nor to use nor to distribute modified copies.

No warranties

THE SOFTWARE IS PROVIDED "AS IS" AND THE LICENSOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE LICENSOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

Installation

After you download the zip file which contains the release, and unzip it, you can simply reference the ModelCssView.dll assembly from your executable: no installation is required.

The ModelCssView.dll executable depends on version 2.0 (or greater) of the .NET framework, which must be installed before you run the utility (it is probably installed on your machine already).

You may (but are not required to) add the assembly to the Global Assembly Cache.

Contact Us

Please contact me (the author) with any suggestions, bug reports or support issues, by sending email to modeltext2018@gmail.com.

For further details, see also the Contact page.

Acknowledgements

I used the GOLD Parsing System to develop the component's lowest-level API: I ported the CSS reference grammar to the format required by the GOLD parser; I then embedded the C# GOLD Parser Engine within the component, using the ILMerge utility. The GOLD parser components have a freeware license agreement. Because they are embedded using ILMerge, they are packaged within the component's DLL and require no separate installation.

Release Notes

The changes in the various releases are as follows (starting with the most recent).

Date and version Notes
2010/12/28 version 1.1.4014.*

New:

  • The calculated borderSpacing property returns a list of two values.
  • New Stylesheets class with enum Origin to support combining optional user stylesheets and user agent stylesheets, together with author stylesheets.

Fixed:

  • Parse font-family identifier with embedded space, for example font-family: Segoe UI, Cambria.
  • Tolerate wrong-case identifiers, for example White instead of white.
  • Inheritance of color and background-color.
2010/11/16 version 1.1.3972.*

New:

  • The mid-level API supports the rect function (e.g. used by the clip property), and the attr, counter, and counters functions (e.g. used by the content property).
  • Unknown (e.g. CSS3) functions are now returned as unknown values, instead of throwing an exception.
2010/10/26 version 1.1.3966.*

Fixed:

  • Support multiple values in an element's class attribute: for example <p class="thisclass thatclass"> will now match a CSS rule selector like p.thisclass or p.thatclass.

    This fix does not implement support for multiple class attributes in a rule selector: for example a rule selector p.thisclass.thatclass.

2010/10/26 version 1.1.3951.*

Fixed:

  • Tolerate unexpected identifiers, e.g. CSS 3 identifiers.
  • Support non-string value in an url, e.g. url(http://example.com) as well as the previously-supported url("http://example.com").
  • Support negative numbers and dimensions per the grammar, e.g. -70px as well as the previously-supported 70px.
2010/09/23 version 1.1.3918.*

Fixed:

  • Return the actual (identity) string or numeric value instead of a null value, if ever an unknown identifier (e.g. a CSS 3 identifier) is detected and StrictException is disabled.
2010/09/22 version 1.1.3917.*

Fixed:

  • Support empty declarations.
  • Support values like url('filename').
  • Support values like rgb(0,0,0) and rgb(50%,50%,50%).
2010/08/10 version 1.1.3874.*

First release.