CssStyleEngine
The CSS style engine for creating CSSStyleSheet instances.
using AngleSharp.Events;
using AngleSharp.Network;
using AngleSharp.Parser.Css;
using AngleSharp.Services.Styling;
namespace AngleSharp.Dom.Css
{
public class CssStyleEngine : IStyleEngine
{
private IStyleSheet _default;
public string Type => MimeTypes.Css;
public IStyleSheet Default => _default ?? (_default = SetupDefault());
public IStyleSheet Parse(string source, StyleOptions options)
{
CssStyleSheet cssStyleSheet = new CssStyleSheet(options.Configuration, source);
cssStyleSheet.OwnerNode = options.Element;
cssStyleSheet.IsDisabled = options.IsDisabled;
cssStyleSheet.Title = options.Title;
CssStyleSheet style = cssStyleSheet;
return Parse(style, options);
}
public IStyleSheet Parse(IResponse response, StyleOptions options)
{
TextSource source = new TextSource(response.Content, null);
CssStyleSheet cssStyleSheet = new CssStyleSheet(options.Configuration, source);
cssStyleSheet.Href = response.Address.Href;
cssStyleSheet.OwnerNode = options.Element;
cssStyleSheet.IsDisabled = options.IsDisabled;
cssStyleSheet.Title = options.Title;
CssStyleSheet style = cssStyleSheet;
return Parse(style, options);
}
private static IStyleSheet Parse(CssStyleSheet style, StyleOptions options)
{
CssParser cssParser = new CssParser(style);
CssParseStartEvent cssParseStartEvent = new CssParseStartEvent(cssParser);
options.Configuration.Events?.Publish(cssParseStartEvent);
cssParser.Parse();
cssParseStartEvent.SetResult(style);
return style;
}
private static IStyleSheet SetupDefault()
{
CssParser cssParser = new CssParser("\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}", null);
return cssParser.Parse();
}
}
}