AngleSharp.Css 1.6.0

logo

AngleSharp.Css

CI GitHub Tag NuGet Count Issues Open CLA Assistant

AngleSharp.Css extends the core AngleSharp library with some more powerful CSS capabilities. This repository is the home of the source for the AngleSharp.Css NuGet package.

Basic Configuration

If you just want a configuration that works (as close as possible to real browsers) you should use the following code:

var config = Configuration.Default
    .WithCss(); // from AngleSharp.Css

This will register a parser for CSS related content. The CSS parsing options and more could be set with parameters of the WithCss method. Alternatively, all the (desired) parts may be registered individually as well. That mostly boils down to three elementary parts:

  • A CSS parser (implementing the ICssParser interface, e.g., CssParser)
  • A factory for creating CSS declarations (IDeclarationFactory)
  • The styling service that can handle CSS documents, see CssStylingService

For an interactive DOM (i.e., to handle style attribute changes in the HTML document) an observer needs to be registered as well.

Furthermore, for some CSSOM features (e.g., media queries) a render device is required.

var config = Configuration.Default
    .WithCss()
    .WithRenderDevice(new DefaultRenderDevice
    {
        DeviceHeight = 768,
        DeviceWidth = 1024,
    });

If no specific IRenderDevice (e.g., via creating an DefaultRenderDevice object) instance is created a default implementation will be set.

The render device also carries the user preferences of the Media Queries Level 5 (and Level 4) user-preference features. DefaultRenderDevice implements IRenderDevicePreferences for that, and any custom IRenderDevice can implement it as well. The dictionary is keyed by the media feature name and holds the keyword that the feature should answer with:

var config = Configuration.Default
    .WithCss()
    .WithRenderDevice(new DefaultRenderDevice
    {
        Preferences = new Dictionary<String, String>
        {
            { "prefers-color-scheme", "dark" },
            { "prefers-reduced-motion", "reduce" },
        },
    });

With this device @media (prefers-color-scheme: dark) applies in the cascade and window.MatchMedia("(prefers-color-scheme: dark)").IsMatched is true. A key that is not set leaves its media feature unknown, i.e., a query using it never matches. The keys a browser would set are:

Key Keywords
prefers-color-scheme light, dark
prefers-reduced-motion no-preference, reduce
prefers-reduced-transparency no-preference, reduce
prefers-contrast no-preference, more, less, custom
prefers-reduced-data no-preference, reduce
forced-colors none, active
hover, any-hover none, hover
pointer, any-pointer none, coarse, fine
display-mode fullscreen, standalone, minimal-ui, browser

The value is compared to the queried keyword case insensitively, so a keyword that is newer than this library works as well. Used without a value, e.g., @media (prefers-reduced-motion), the feature evaluates in a boolean context, where no-preference (and none for forced-colors, hover, any-hover, pointer and any-pointer) is false. Without a preference hover and pointer keep answering as they did before, i.e., as a device with no input mechanism.

Going a bit further it is possible to Render the current document. This render tree information can then be used to retrieve or other information, e.g.,

var tree = document.DefaultView.Render();
var node = tree.Find(document.QuerySelector("div"));
await node.DownloadResources();

The previous snippet renders the current document. Afterwards it retrieves a particular render tree node, which is related to the first found div. Then all (CSS introduced) resources are downloaded for the node, if visible.

Advantages of AngleSharp.Css

The core library already contains the CSS selector parser and the most basic classes and interfaces for dealing with the CSSOM. AngleSharp.Css brings the following advantages and use cases to life:

  • Correct identification of edge cases
  • A live CSSOM model, i.e., callbacks and everything
  • Computing the styling of certain elements
  • Cascades of stylesheets
  • Validation (and property-based exposure) of CSS declarations
  • Responsive design considerations
  • Full access to the value with different converters

The main idea behind AngleSharp.Css is to expose the CSSOM as it would be in the browser (and potentially beyond, i.e., useful for being used by editors). Originally, most of the code found here was embedded in the AngleSharp.Core library, however, due to the overhead for HTML use cases it was decided to transfer the code into its own repository.

Features

  • Feature validators (e.g., for @supports)
  • Document functions (e.g., for domain)
  • Pseudo elements (e.g., ::before)
  • Declarations (e.g., display) incl. knowledge of their values
  • Dynamic DOM coupling (i.e., to react to style attribute changes)
  • CSS custom properties (also known as CSS variables) with extensibility
  • Media queries and all other commonly implemented rules
  • Calculated values (i.e., calc(20px + 50%))
  • Window-based declaration calculations, see window.GetComputedStyle

Benchmarks

The AngleSharp.Performance.Css project uses BenchmarkDotNet to compare CSS parsing performance across libraries. Run the benchmarks in Release mode:

dotnet run --project src/AngleSharp.Performance.Css/AngleSharp.Performance.Css.csproj -c Release --framework net10.0

To run a quick smoke test instead of a full benchmark:

dotnet run --project src/AngleSharp.Performance.Css/AngleSharp.Performance.Css.csproj -c Release --framework net10.0 -- --job short

Participating

Participation in the project is highly welcome. For this project the same rules as for the AngleSharp core project may be applied.

If you have any question, concern, or spot an issue then please report it before opening a pull request. An initial discussion is appreciated regardless of the nature of the problem.

Live discussions can take place in our Gitter chat, which supports using GitHub accounts.

This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behavior in our community.

For more information see the .NET Foundation Code of Conduct.

.NET Foundation

This project is supported by the .NET Foundation.

License

AngleSharp.Css is released using the MIT license. For more information see the license file.

Showing the top 20 packages that depend on AngleSharp.Css.

Packages Downloads
HtmlSanitizer
Cleans HTML from constructs that can be used for cross-site scripting (XSS)
59
HtmlSanitizer
Cleans HTML from constructs that can be used for cross-site scripting (XSS)
56
HtmlSanitizer
Cleans HTML from constructs that can be used for cross-site scripting (XSS)
55
HtmlSanitizer
Cleans HTML from constructs that can be used for cross-site scripting (XSS)
54
HtmlSanitizer
Cleans HTML from constructs that can be used for cross-site scripting (XSS)
52
HtmlSanitizer
Cleans HTML from constructs that can be used for cross site scripting (XSS)
49
HtmlSanitizer
Cleans HTML from constructs that can be used for cross-site scripting (XSS)
48
HtmlSanitizer
Cleans HTML from constructs that can be used for cross site scripting (XSS)
47
HtmlSanitizer
Cleans HTML from constructs that can be used for cross-site scripting (XSS)
45
HtmlSanitizer
Cleans HTML from constructs that can be used for cross site scripting (XSS)
44
HtmlSanitizer
Cleans HTML from constructs that can be used for cross-site scripting (XSS)
44
HtmlSanitizer
Cleans HTML from constructs that can be used for cross-site scripting (XSS)
43

.NET 10.0

.NET Framework 4.6.2

.NET Framework 4.7.2

.NET 8.0

.NET Standard 2.0

Version Downloads Last updated
1.6.1-beta.474 1 11/25/2026
1.6.0 1 11/24/2026
1.6.0-beta.470 1 11/24/2026
1.6.0-beta.466 5 11/23/2026
1.6.0-beta.464 8 11/23/2026
1.6.0-beta.463 5 11/23/2026
1.6.0-beta.462 11 11/22/2026
1.5.1-beta.461 11 11/21/2026
1.5.1-beta.460 13 11/21/2026
1.5.1-beta.457 12 11/19/2026
1.5.0 10 11/18/2026
1.5.0-beta.453 10 11/18/2026
1.5.0-beta.452 19 11/17/2026
1.5.0-beta.447 16 11/17/2026
1.4.2 16 11/14/2026
1.4.2-beta.438 17 11/13/2026
1.4.2-beta.436 24 11/13/2026
1.4.2-beta.434 24 11/13/2026
1.4.2-beta.432 15 11/13/2026
1.4.2-beta.431 15 11/13/2026
1.4.1 15 11/09/2026
1.4.1-beta.430 15 11/12/2026
1.4.1-beta.426 18 11/08/2026
1.4.1-beta.425 15 11/08/2026
1.4.1-beta.424 26 11/07/2026
1.4.0 13 11/06/2026
1.4.0-beta.420 20 11/06/2026
1.4.0-beta.418 16 11/06/2026
1.4.0-beta.416 16 11/05/2026
1.4.0-beta.414 18 11/05/2026
1.4.0-beta.413 26 11/05/2026
1.4.0-beta.408 16 11/04/2026
1.4.0-beta.404 21 11/04/2026
1.4.0-beta.403 22 11/04/2026
1.4.0-beta.402 22 11/04/2026
1.3.2 22 11/03/2026
1.3.2-beta.397 22 11/03/2026
1.3.2-beta.394 20 11/02/2026
1.3.2-beta.390 25 11/02/2026
1.3.2-beta.387 22 11/02/2026
1.3.1 23 11/02/2026
1.3.1-beta.386 22 11/02/2026
1.3.1-beta.381 21 11/02/2026
1.3.1-beta.375 21 10/31/2026
1.3.0 24 10/31/2026
1.3.0-beta.371 21 10/31/2026
1.3.0-beta.369 25 10/28/2026
1.3.0-beta.367 23 10/28/2026
1.3.0-beta.365 24 10/25/2026
1.3.0-beta.364 21 10/25/2026
1.3.0-beta.363 20 10/25/2026
1.2.2 17 10/25/2026
1.2.2-beta.359 20 10/25/2026
1.2.1 24 10/24/2026
1.2.1-beta.353 21 10/23/2026
1.2.0 28 10/21/2026
1.2.0-beta.349 22 10/21/2026
1.2.0-beta.347 22 10/20/2026
1.2.0-beta.345 28 10/20/2026
1.1.4 30 10/20/2026
1.1.4-beta.341 23 10/20/2026
1.1.4-beta.340 21 10/19/2026
1.1.3 27 10/15/2026
1.1.3-beta.335 22 10/15/2026
1.1.3-beta.334 25 10/15/2026
1.1.3-beta.330 29 09/13/2026
1.1.2 27 09/11/2026
1.1.2-beta.323 37 09/11/2026
1.1.2-beta.322 26 09/11/2026
1.1.2-beta.321 29 09/11/2026
1.1.1 31 09/10/2026
1.1.1-beta.317 30 09/10/2026
1.1.1-beta.316 29 09/10/2026
1.1.1-beta.311 26 09/10/2026
1.1.1-beta.310 26 09/09/2026
1.1.1-beta.309 25 09/09/2026
1.1.1-beta.308 29 09/07/2026
1.1.1-beta.307 27 09/07/2026
1.1.0 27 09/06/2026
1.1.0-beta.302 24 09/06/2026
1.1.0-beta.297 23 09/06/2026
1.1.0-beta.295 28 09/05/2026
1.1.0-beta.293 30 09/05/2026
1.1.0-beta.291 29 09/06/2026
1.1.0-beta.283 32 09/04/2026
1.1.0-beta.281 29 09/04/2026
1.1.0-beta.277 30 09/04/2026
1.1.0-beta.276 31 09/04/2026
1.1.0-beta.273 28 09/03/2026
1.0.2 34 08/23/2026
1.0.2-beta.272 34 09/02/2026
1.0.2-beta.271 32 09/02/2026
1.0.2-beta.265 29 08/20/2026
1.0.1 28 07/31/2026
1.0.1-beta.264 31 08/20/2026
1.0.1-beta.262 33 08/13/2026
1.0.1-beta.260 26 08/08/2026
1.0.1-beta.258 24 08/05/2026
1.0.1-beta.256 23 08/01/2026
1.0.1-beta.252 25 07/31/2026
1.0.1-beta.250 28 07/31/2026
1.0.1-beta.247 28 07/31/2026
1.0.1-beta.245 29 07/31/2026
1.0.0 34 07/27/2026
1.0.0-beta.244 25 07/29/2026
1.0.0-beta.243 29 07/28/2026
1.0.0-beta.239 21 07/25/2026
1.0.0-beta.237 29 07/25/2026
1.0.0-beta.231 32 07/25/2026
1.0.0-beta.228 28 07/22/2026
1.0.0-beta.226 28 07/22/2026
1.0.0-beta.224 31 07/20/2026
1.0.0-beta.222 30 07/20/2026
1.0.0-beta.216 65 05/14/2026
1.0.0-beta.213 56 04/01/2026
1.0.0-beta.211 56 03/29/2026
1.0.0-beta.209 54 03/29/2026
1.0.0-beta.204 57 03/29/2026
1.0.0-beta.202 55 03/29/2026
1.0.0-beta.199 59 03/29/2026
1.0.0-beta.197 60 03/29/2026
1.0.0-beta.195 44 03/29/2026
1.0.0-beta.189 53 03/29/2026
1.0.0-beta.186 58 03/29/2026
1.0.0-beta.184 53 03/29/2026
1.0.0-beta.178 68 03/28/2026
1.0.0-beta.176 38 03/29/2026
1.0.0-beta.167 71 01/17/2026
1.0.0-beta.164 15 10/02/2026
1.0.0-beta.159 12 10/01/2026
1.0.0-beta.157 57 12/13/2025
1.0.0-beta.154 7 10/02/2026
1.0.0-beta.151 68 12/13/2025
1.0.0-beta.149 66 12/12/2025
1.0.0-beta.147 14 10/01/2026
1.0.0-beta.144 57 12/13/2025
1.0.0-beta.139 11 09/28/2026
1.0.0-beta.137 67 12/13/2025
1.0.0-beta.130 61 01/01/2026
1.0.0-beta.127 7 10/02/2026
1.0.0-beta.122 62 12/22/2025
1.0.0-alpha-99 10 10/01/2026
1.0.0-alpha-98 62 12/20/2025
1.0.0-alpha-97 53 12/12/2025
1.0.0-alpha-117 65 12/13/2025
1.0.0-alpha-113 9 10/01/2026
1.0.0-alpha-111 68 12/15/2025
1.0.0-alpha-109 65 01/09/2026
1.0.0-alpha-108 9 10/02/2026
1.0.0-alpha-107 17 09/28/2026
1.0.0-alpha-106 59 12/15/2025
1.0.0-alpha-104 65 12/16/2025
1.0.0-alpha-103 11 10/01/2026
1.0.0-alpha-102 62 12/18/2025
1.0.0-alpha-101 54 12/16/2025
1.0.0-alpha-100 83 12/15/2025
0.18.0 30 07/25/2026
0.18.0-beta.233 29 07/25/2026
0.17.0 69 12/15/2025
0.17.0-alpha-87 24 09/28/2026
0.17.0-alpha-86 9 10/02/2026
0.17.0-alpha-80 66 12/13/2025
0.17.0-alpha-78 63 12/21/2025
0.17.0-alpha-77 18 10/02/2026
0.17.0-alpha-76 63 12/24/2025
0.17.0-alpha-75 17 09/30/2026
0.16.4 56 12/11/2025
0.16.4-alpha-72 63 12/14/2025
0.16.4-alpha-68 46 01/01/2026
0.16.3 17 10/01/2026
0.16.3-alpha-65 23 10/02/2026
0.16.2 68 01/02/2026
0.16.2-alpha-62 19 09/28/2026
0.16.2-alpha-61 51 12/24/2025
0.16.2-alpha-60 66 01/01/2026
0.16.2-alpha-42 18 10/02/2026
0.16.2-alpha-35 40 12/14/2025
0.16.2-alpha-30 42 01/17/2026
0.16.2-alpha-29 59 12/11/2025
0.16.2-alpha-28 49 01/01/2026
0.16.1 67 12/12/2025
0.16.1-alpha-27 16 09/30/2026
0.16.1-alpha-24 58 12/21/2025
0.16.0 56 01/09/2026
0.16.0-alpha-23 67 12/12/2025
0.16.0-alpha-19 73 12/11/2025
0.15.1 64 01/17/2026
0.15.1-alpha-13 68 01/01/2026
0.15.1-alpha-11 53 12/15/2025
0.15.0 62 12/19/2025
0.15.0-alpha-7 62 12/15/2025
0.14.3-alpha-5 45 12/12/2025
0.14.3-alpha-4 52 12/20/2025
0.14.3-alpha-166 57 12/15/2025
0.14.3-alpha-165 10 10/02/2026
0.14.2 52 12/24/2025
0.14.2-alpha-163 12 10/02/2026
0.14.2-alpha-162 54 12/20/2025
0.14.1 56 01/01/2026
0.14.1-alpha-161 63 12/15/2025
0.14.0 56 12/13/2025
0.14.0-alpha-160 59 12/14/2025
0.14.0-alpha-154 55 01/09/2026
0.14.0-alpha-152 59 01/01/2026
0.14.0-alpha-149 64 01/01/2026
0.14.0-alpha-147 52 12/14/2025
0.14.0-alpha-145 68 12/22/2025
0.14.0-alpha-143 14 10/01/2026
0.14.0-alpha-139 52 12/15/2025
0.14.0-alpha-138 64 01/09/2026
0.13.0 70 12/24/2025
0.13.0-alpha-135 65 01/09/2026
0.13.0-alpha-131 14 10/01/2026
0.13.0-alpha-123 15 10/01/2026
0.13.0-alpha-121 63 01/23/2026
0.13.0-alpha-119 59 12/22/2025
0.13.0-alpha-117 50 01/01/2026
0.13.0-alpha-116 56 12/19/2025
0.13.0-alpha-115 65 12/22/2025
0.13.0-alpha-114 65 12/14/2025
0.13.0-alpha-113 19 09/28/2026
0.12.1 65 12/30/2025
0.12.0 22 09/30/2026
0.10.0 19 10/01/2026