AngleSharp.Css 1.2.2

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)
286
HtmlSanitizer
Cleans HTML from constructs that can be used for cross site scripting (XSS)
202
HtmlSanitizer
Cleans HTML from constructs that can be used for cross-site scripting (XSS)
172
HtmlSanitizer
Cleans HTML from constructs that can be used for cross-site scripting (XSS)
35
HtmlSanitizer
Cleans HTML from constructs that can be used for cross-site scripting (XSS)
28
HtmlSanitizer
Cleans HTML from constructs that can be used for cross site scripting (XSS)
27
HtmlSanitizer
Cleans HTML from constructs that can be used for cross site scripting (XSS)
26
HtmlSanitizer
Cleans HTML from constructs that can be used for cross-site scripting (XSS)
26
HtmlSanitizer
Cleans HTML from constructs that can be used for cross-site scripting (XSS)
25
HtmlSanitizer
Cleans HTML from constructs that can be used for cross-site scripting (XSS)
23
HtmlSanitizer
Cleans HTML from constructs that can be used for cross site scripting (XSS)
23
HtmlSanitizer
Cleans HTML from constructs that can be used for cross site scripting (XSS)
22

.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.5.1-beta.457 0 11/19/2026
1.5.0 0 11/18/2026
1.5.0-beta.453 0 11/18/2026
1.5.0-beta.452 0 11/17/2026
1.5.0-beta.447 0 11/16/2026
1.4.2 0 11/13/2026
1.4.2-beta.438 0 11/13/2026
1.4.2-beta.436 0 11/13/2026
1.4.2-beta.434 0 11/13/2026
1.4.2-beta.432 0 11/13/2026
1.4.2-beta.431 1 11/13/2026
1.4.1 2 11/10/2026
1.4.1-beta.430 1 11/13/2026
1.4.1-beta.426 5 11/10/2026
1.4.1-beta.425 3 11/08/2026
1.4.1-beta.424 3 11/08/2026
1.4.0 3 11/08/2026
1.4.0-beta.420 3 11/08/2026
1.4.0-beta.418 6 11/06/2026
1.4.0-beta.416 5 11/06/2026
1.4.0-beta.414 3 11/06/2026
1.4.0-beta.413 2 11/06/2026
1.4.0-beta.408 3 11/06/2026
1.4.0-beta.404 3 11/05/2026
1.4.0-beta.403 6 11/06/2026
1.4.0-beta.402 2 11/06/2026
1.3.2 4 11/06/2026
1.3.2-beta.397 3 11/06/2026
1.3.2-beta.394 5 11/05/2026
1.3.2-beta.390 4 11/05/2026
1.3.2-beta.387 0 11/02/2026
1.3.1 6 11/02/2026
1.3.1-beta.386 1 11/04/2026
1.3.1-beta.381 5 11/02/2026
1.3.1-beta.375 4 11/02/2026
1.3.0 4 11/02/2026
1.3.0-beta.371 1 11/02/2026
1.3.0-beta.369 3 10/31/2026
1.3.0-beta.367 3 10/31/2026
1.3.0-beta.365 3 10/27/2026
1.3.0-beta.364 9 10/27/2026
1.3.0-beta.363 5 11/01/2026
1.2.2 2 11/06/2026
1.2.2-beta.359 6 10/31/2026
1.2.1 5 10/27/2026
1.2.1-beta.353 4 10/26/2026
1.2.0 6 10/28/2026
1.2.0-beta.349 3 11/01/2026
1.2.0-beta.347 6 10/25/2026
1.2.0-beta.345 3 10/25/2026
1.1.4 5 10/26/2026
1.1.4-beta.341 5 10/26/2026
1.1.4-beta.340 4 10/26/2026
1.1.3 12 10/19/2026
1.1.3-beta.335 4 10/20/2026
1.1.3-beta.334 10 10/24/2026
1.1.3-beta.330 10 09/14/2026
1.1.2 9 09/13/2026
1.1.2-beta.323 8 09/13/2026
1.1.2-beta.322 11 09/24/2026
1.1.2-beta.321 7 09/12/2026
1.1.1 5 09/25/2026
1.1.1-beta.317 8 09/13/2026
1.1.1-beta.316 11 09/13/2026
1.1.1-beta.311 11 09/11/2026
1.1.1-beta.310 10 09/13/2026
1.1.1-beta.309 6 09/12/2026
1.1.1-beta.308 7 09/10/2026
1.1.1-beta.307 8 09/09/2026
1.1.0 7 09/07/2026
1.1.0-beta.302 10 09/07/2026
1.1.0-beta.297 10 09/07/2026
1.1.0-beta.295 8 09/25/2026
1.1.0-beta.293 8 09/07/2026
1.1.0-beta.291 6 09/08/2026
1.1.0-beta.283 5 09/25/2026
1.1.0-beta.281 7 09/19/2026
1.1.0-beta.277 7 09/05/2026
1.1.0-beta.276 7 09/06/2026
1.1.0-beta.273 6 09/25/2026
1.0.2 13 08/24/2026
1.0.2-beta.272 10 09/06/2026
1.0.2-beta.271 7 09/21/2026
1.0.2-beta.265 12 08/22/2026
1.0.1 10 08/08/2026
1.0.1-beta.264 20 08/23/2026
1.0.1-beta.262 11 08/23/2026
1.0.1-beta.260 13 08/12/2026
1.0.1-beta.258 14 08/11/2026
1.0.1-beta.256 13 08/09/2026
1.0.1-beta.252 12 08/12/2026
1.0.1-beta.250 11 08/09/2026
1.0.1-beta.247 15 08/11/2026
1.0.1-beta.245 12 08/10/2026
1.0.0 14 07/29/2026
1.0.0-beta.244 10 07/31/2026
1.0.0-beta.243 12 07/29/2026
1.0.0-beta.239 13 07/28/2026
1.0.0-beta.237 14 07/28/2026
1.0.0-beta.231 13 07/27/2026
1.0.0-beta.228 11 07/24/2026
1.0.0-beta.226 10 07/25/2026
1.0.0-beta.224 17 07/25/2026
1.0.0-beta.222 13 07/25/2026
1.0.0-beta.216 36 06/20/2026
1.0.0-beta.213 14 06/29/2026
1.0.0-beta.211 15 06/30/2026
1.0.0-beta.209 10 07/02/2026
1.0.0-beta.204 20 06/25/2026
1.0.0-beta.202 14 06/20/2026
1.0.0-beta.199 14 06/20/2026
1.0.0-beta.197 6 06/26/2026
1.0.0-beta.195 18 06/25/2026
1.0.0-beta.189 13 06/28/2026
1.0.0-beta.186 14 06/28/2026
1.0.0-beta.184 15 07/05/2026
1.0.0-beta.178 11 07/07/2026
1.0.0-beta.176 9 06/29/2026
1.0.0-beta.167 14 06/25/2026
1.0.0-beta.164 12 06/25/2026
1.0.0-beta.159 10 06/29/2026
1.0.0-beta.157 11 06/29/2026
1.0.0-beta.154 10 06/20/2026
1.0.0-beta.151 19 06/26/2026
1.0.0-beta.149 10 06/26/2026
1.0.0-beta.147 11 07/19/2026
1.0.0-beta.144 13 03/11/2025
1.0.0-beta.139 16 06/30/2026
1.0.0-beta.137 12 06/20/2026
1.0.0-beta.130 7 07/17/2026
1.0.0-beta.127 13 06/25/2026
1.0.0-beta.122 11 06/28/2026
1.0.0-alpha-99 17 06/17/2024
1.0.0-alpha-98 17 05/07/2025
1.0.0-alpha-97 13 10/09/2024
1.0.0-alpha-117 20 06/27/2026
1.0.0-alpha-113 16 06/27/2026
1.0.0-alpha-111 12 06/25/2026
1.0.0-alpha-109 16 06/25/2026
1.0.0-alpha-108 14 06/27/2026
1.0.0-alpha-107 10 06/25/2026
1.0.0-alpha-106 13 06/27/2026
1.0.0-alpha-104 18 06/20/2026
1.0.0-alpha-103 11 06/27/2026
1.0.0-alpha-102 11 06/20/2026
1.0.0-alpha-101 14 06/27/2026
1.0.0-alpha-100 13 06/29/2026
0.18.0 15 07/28/2026
0.18.0-beta.233 11 07/28/2026
0.17.0 70 02/11/2024
0.17.0-alpha-87 16 03/04/2024
0.17.0-alpha-86 16 01/12/2024
0.17.0-alpha-80 16 03/03/2024
0.17.0-alpha-78 13 03/04/2024
0.17.0-alpha-77 21 04/10/2024
0.17.0-alpha-76 16 03/04/2024
0.17.0-alpha-75 14 03/04/2024
0.16.4 13 03/30/2022
0.16.4-alpha-72 14 08/13/2024
0.16.4-alpha-68 17 01/08/2024
0.16.3 13 06/26/2026
0.16.3-alpha-65 12 04/25/2025
0.16.2 13 03/30/2024
0.16.2-alpha-62 17 10/22/2024
0.16.2-alpha-61 12 03/04/2024
0.16.2-alpha-60 14 03/03/2024
0.16.2-alpha-42 15 03/21/2025
0.16.2-alpha-35 19 03/04/2024
0.16.2-alpha-30 10 06/29/2026
0.16.2-alpha-29 12 01/20/2024
0.16.2-alpha-28 13 05/26/2024
0.16.1 201 08/12/2021
0.16.1-alpha-27 10 10/02/2024
0.16.1-alpha-24 16 08/25/2024
0.16.0 18 01/23/2024
0.16.0-alpha-23 15 03/04/2024
0.16.0-alpha-19 17 11/09/2024
0.15.1 15 12/21/2024
0.15.1-alpha-13 13 01/10/2024
0.15.1-alpha-11 13 06/11/2024
0.15.0 396 04/26/2021
0.15.0-alpha-7 14 02/28/2024
0.14.3-alpha-5 20 02/29/2024
0.14.3-alpha-4 12 11/17/2025
0.14.3-alpha-166 21 11/03/2024
0.14.3-alpha-165 16 11/14/2024
0.14.2 26 03/04/2024
0.14.2-alpha-163 16 01/30/2024
0.14.2-alpha-162 9 10/25/2024
0.14.1 15 03/04/2024
0.14.1-alpha-161 16 06/11/2024
0.14.0 14 03/04/2024
0.14.0-alpha-160 17 06/26/2026
0.14.0-alpha-154 17 03/04/2024
0.14.0-alpha-152 15 03/03/2024
0.14.0-alpha-149 16 03/04/2024
0.14.0-alpha-147 16 03/03/2024
0.14.0-alpha-145 21 03/04/2024
0.14.0-alpha-143 15 03/04/2024
0.14.0-alpha-139 15 03/19/2024
0.14.0-alpha-138 14 09/21/2024
0.13.0 14 06/25/2026
0.13.0-alpha-135 13 06/26/2026
0.13.0-alpha-131 12 03/08/2024
0.13.0-alpha-123 11 06/20/2026
0.13.0-alpha-121 15 12/22/2023
0.13.0-alpha-119 15 01/27/2024
0.13.0-alpha-117 22 03/04/2024
0.13.0-alpha-116 16 03/04/2024
0.13.0-alpha-115 21 12/16/2023
0.13.0-alpha-114 14 02/26/2024
0.13.0-alpha-113 14 02/24/2025
0.12.1 11 03/04/2024
0.12.0 16 03/04/2024
0.10.0 14 01/23/2024