AngleSharp by AngleSharp

<PackageReference Include="AngleSharp" Version="0.9.7" />

.NET API 1,204,224 bytes

 CssStyleEngine

The CSS style engine for creating CSSStyleSheet instances.
using AngleSharp.Dom.Collections; using AngleSharp.Dom.Events; using AngleSharp.Extensions; using AngleSharp.Network; using AngleSharp.Parser.Css; using AngleSharp.Services.Styling; using System.Threading; using System.Threading.Tasks; namespace AngleSharp.Dom.Css { public class CssStyleEngine : ICssStyleEngine, IStyleEngine { private ICssStyleSheet _default; private CssParserOptions _options; public static readonly string DefaultSource = "\r\nhtml, address,\r\nblockquote,\r\nbody, dd, div,\r\ndl, dt, fieldset, form,\r\nframe, frameset,\r\nh1, h2, h3, h4,\r\nh5, h6, noframes,\r\nol, p, ul, center,\r\ndir, hr, menu, pre { display: block; unicode-bidi: embed }\r\nli { display: list-item }\r\nhead { display: none }\r\ntable { display: table }\r\ntr { display: table-row }\r\nthead { display: table-header-group }\r\ntbody { display: table-row-group }\r\ntfoot { display: table-footer-group }\r\ncol { display: table-column }\r\ncolgroup { display: table-column-group }\r\ntd, th { display: table-cell }\r\ncaption { display: table-caption }\r\nth { font-weight: bolder; text-align: center }\r\ncaption { text-align: center }\r\nbody { margin: 8px }\r\nh1 { font-size: 2em; margin: .67em 0 }\r\nh2 { font-size: 1.5em; margin: .75em 0 }\r\nh3 { font-size: 1.17em; margin: .83em 0 }\r\nh4, p,\r\nblockquote, ul,\r\nfieldset, form,\r\nol, dl, dir,\r\nmenu { margin: 1.12em 0 }\r\nh5 { font-size: .83em; margin: 1.5em 0 }\r\nh6 { font-size: .75em; margin: 1.67em 0 }\r\nh1, h2, h3, h4,\r\nh5, h6, b,\r\nstrong { font-weight: bolder }\r\nblockquote { margin-left: 40px; margin-right: 40px }\r\ni, cite, em,\r\nvar, address { font-style: italic }\r\npre, tt, code,\r\nkbd, samp { font-family: monospace }\r\npre { white-space: pre }\r\nbutton, textarea,\r\ninput, select { display: inline-block }\r\nbig { font-size: 1.17em }\r\nsmall, sub, sup { font-size: .83em }\r\nsub { vertical-align: sub }\r\nsup { vertical-align: super }\r\ntable { border-spacing: 2px; }\r\nthead, tbody,\r\ntfoot { vertical-align: middle }\r\ntd, th, tr { vertical-align: inherit }\r\ns, strike, del { text-decoration: line-through }\r\nhr { border: 1px inset }\r\nol, ul, dir,\r\nmenu, dd { margin-left: 40px }\r\nol { list-style-type: decimal }\r\nol ul, ul ol,\r\nul ul, ol ol { margin-top: 0; margin-bottom: 0 }\r\nu, ins { text-decoration: underline }\r\nbr:before { content: '\\A'; white-space: pre-line }\r\ncenter { text-align: center }\r\n:link, :visited { text-decoration: underline }\r\n:focus { outline: thin dotted invert }\r\n\r\n/* Begin bidirectionality settings (do not change) */\r\nBDO[DIR='ltr'] { direction: ltr; unicode-bidi: bidi-override }\r\nBDO[DIR='rtl'] { direction: rtl; unicode-bidi: bidi-override }\r\n\r\n*[DIR='ltr'] { direction: ltr; unicode-bidi: embed }\r\n*[DIR='rtl'] { direction: rtl; unicode-bidi: embed }\r\n\r\n@media print {\r\n h1 { page-break-before: always }\r\n h1, h2, h3,\r\n h4, h5, h6 { page-break-after: avoid }\r\n ul, ol, dl { page-break-before: avoid }\r\n}"; public string Type => MimeTypeNames.Css; public ICssStyleSheet Default => _default ?? SetDefault(DefaultSource); public CssParserOptions Options { get { return _options; } set { _options = value; } } public CssStyleEngine() { _options = default(CssParserOptions); } public ICssStyleSheet SetDefault(string sourceCode) { return _default = new CssParser(_options, Configuration.Default).ParseStylesheet(sourceCode); } public async Task<IStyleSheet> ParseStylesheetAsync(IResponse response, StyleOptions options, CancellationToken cancel) { IBrowsingContext context = options.Context; IConfiguration configuration = context.Configuration; CssParser cssParser = new CssParser(_options, configuration); string url = (response.Address != null) ? response.Address.Href : null; CssStyleSheet sheet = new CssStyleSheet(cssParser, url, options.Element) { IsDisabled = options.IsDisabled }; TextSource source = new TextSource(response.Content, null); new CssBuilder(new CssTokenizer(source), cssParser).Error += delegate(object s, CssErrorEvent ev) { context.Fire(ev); }; context.Fire(new CssParseEvent(sheet, false)); await cssParser.ParseStylesheetAsync(sheet, source).ConfigureAwait(false); context.Fire(new CssParseEvent(sheet, true)); return sheet; } public ICssStyleDeclaration ParseDeclaration(string source, StyleOptions options) { IConfiguration configuration = options.Context.Configuration; CssStyleDeclaration cssStyleDeclaration = new CssStyleDeclaration(new CssParser(_options, configuration)); cssStyleDeclaration.Update(source); return cssStyleDeclaration; } public IMediaList ParseMedia(string source, StyleOptions options) { IConfiguration configuration = options.Context.Configuration; return new MediaList(new CssParser(_options, configuration)) { MediaText = source }; } } }