Added default style sheet provider fixes #21

This commit is contained in:
Florian Rappl 2019-05-05 19:59:13 +02:00
Родитель 406e3b49a8
Коммит a136533add
41 изменённых файлов: 322 добавлений и 379 удалений

Просмотреть файл

@ -5,6 +5,7 @@ Released on Thursday, May 5 2019.
- Reference latest AngleSharp
- Returns `null` in `GetStyle` if CSS not configured (#15)
- Added `pointer-events` and fixed border recombination (#16)
- Fixed missing `CssStylingService.Default` in cascade (#21)
- Added extension helper `SetStyle` to modify all styles of many elements (#22)
- Introduced special converter for the `src` declaration (#25)
- Fixed bug regarding CSS grid serialization (#27)

Просмотреть файл

@ -73,7 +73,9 @@ namespace AngleSharp.Css.Tests.Extensions
[TestCase("<style>test</style>", "")]
public void GetInnerText(String fixture, String expected)
{
var config = Configuration.Default.WithCss();
var defaultSheet = new CssDefaultStyleSheetProvider();
defaultSheet.SetDefault("");
var config = Configuration.Default.With(defaultSheet).WithCss();
var doc = fixture.ToHtmlDocument(config);
Assert.AreEqual(expected, doc.Body.GetInnerText());

Просмотреть файл

@ -35,8 +35,8 @@
public override Int64 Position
{
get { return _stream.Position; }
set { _stream.Position = value; }
get => _stream.Position;
set => _stream.Position = value;
}
public override async Task CopyToAsync(Stream destination, Int32 bufferSize, CancellationToken cancellationToken)

Просмотреть файл

@ -1,4 +1,4 @@
namespace AngleSharp.Css.Tests.Styling
namespace AngleSharp.Css.Tests.Styling
{
using AngleSharp.Css.Dom;
using AngleSharp.Io;
@ -30,10 +30,9 @@
[Test]
public void ObtainDefaultSheet()
{
var service = new CssStylingService();
var service = new CssDefaultStyleSheetProvider();
Assert.IsNotNull(service.Default);
Assert.IsTrue((service as IStylingService).SupportsType(MimeTypeNames.Css));
var sheet = service.Default as ICssStyleSheet;
var sheet = service.Default;
Assert.IsNotNull(sheet);
Assert.AreEqual(49, sheet.Rules.Length);
}

Просмотреть файл

@ -5,6 +5,7 @@ namespace AngleSharp.Css.Tests.Styling
using AngleSharp.Css.Tests.Mocks;
using AngleSharp.Dom;
using AngleSharp.Html.Dom;
using AngleSharp.Html.Parser;
using AngleSharp.Io;
using NUnit.Framework;
using System;
@ -429,5 +430,15 @@ namespace AngleSharp.Css.Tests.Styling
element.GetStyle().SetBorderColor("black");
Assert.AreEqual(expected, element.ToHtml());
}
[Test]
public static void DefaultStyleSheetTest_Issue21()
{
var browsingContext = BrowsingContext.New(Configuration.Default.WithCss());
var htmlParser = browsingContext.GetService<IHtmlParser>();
var document = htmlParser.ParseDocument("<html><body><b>Hello, World!</b></body></html>");
var boldStyle = document.Body.FirstElementChild.ComputeCurrentStyle();
Assert.AreEqual("bolder", boldStyle.GetFontWeight());
}
}
}

Просмотреть файл

@ -15,15 +15,18 @@ namespace AngleSharp
/// </summary>
/// <param name="configuration">The configuration to extend.</param>
/// <param name="options">Optional options for the parser.</param>
/// <param name="setup">Optional setup for the style engine.</param>
/// <returns>The new instance with the service.</returns>
public static IConfiguration WithCss(this IConfiguration configuration, CssParserOptions options = default(CssParserOptions), Action<CssStylingService> setup = null)
public static IConfiguration WithCss(this IConfiguration configuration, CssParserOptions options = default(CssParserOptions))
{
if (configuration == null)
throw new ArgumentNullException(nameof(configuration));
var service = new CssStylingService();
setup?.Invoke(service);
if (!configuration.Has<ICssDefaultStyleSheetProvider>())
{
configuration = configuration.With(new CssDefaultStyleSheetProvider());
}
if (!configuration.Has<IFeatureValidatorFactory>())
{
@ -50,7 +53,9 @@ namespace AngleSharp
configuration = configuration.With<ICssParser>(context => new CssParser(options, context));
}
return configuration.WithOnly(Factory.Observer).With(service);
return configuration
.WithOnly(Factory.Observer)
.WithOnly<IStylingService>(service);
}
}
}

Просмотреть файл

@ -0,0 +1,125 @@
namespace AngleSharp.Css
{
using AngleSharp.Css.Dom;
using AngleSharp.Css.Parser;
using System;
sealed class CssDefaultStyleSheetProvider : ICssDefaultStyleSheetProvider
{
#region Fields
private ICssStyleSheet _default;
#endregion
#region Properties
public ICssStyleSheet Default => _default ?? (_default = Parse(DefaultSource));
#endregion
#region Methods
public void SetDefault(ICssStyleSheet sheet) => _default = sheet;
public void SetDefault(String source) => SetDefault(Parse(source));
public void AppendDefault(String source) => SetDefault(Parse($"{DefaultSource}\r\n{source}"));
#endregion
#region Default Stylesheet
private static ICssStyleSheet Parse(String source)
{
var parser = new CssParser();
return parser.ParseStyleSheet(source);
}
/// <summary>
/// Gets the source code for the by default used base stylesheet.
/// Taken from https://www.w3.org/TR/CSS22/sample.html.
/// </summary>
public static readonly String DefaultSource = @"
html, address,
blockquote,
body, dd, div,
dl, dt, fieldset, form,
frame, frameset,
h1, h2, h3, h4,
h5, h6, noframes,
ol, p, ul, center,
dir, hr, menu, pre { display: block; unicode-bidi: embed }
li { display: list-item }
head { display: none }
table { display: table }
tr { display: table-row }
thead { display: table-header-group }
tbody { display: table-row-group }
tfoot { display: table-footer-group }
col { display: table-column }
colgroup { display: table-column-group }
td, th { display: table-cell }
caption { display: table-caption }
th { font-weight: bolder; text-align: center }
caption { text-align: center }
body { margin: 8px }
h1 { font-size: 2em; margin: .67em 0 }
h2 { font-size: 1.5em; margin: .75em 0 }
h3 { font-size: 1.17em; margin: .83em 0 }
h4, p,
blockquote, ul,
fieldset, form,
ol, dl, dir,
menu { margin: 1.12em 0 }
h5 { font-size: .83em; margin: 1.5em 0 }
h6 { font-size: .75em; margin: 1.67em 0 }
h1, h2, h3, h4,
h5, h6, b,
strong { font-weight: bolder }
blockquote { margin-left: 40px; margin-right: 40px }
i, cite, em,
var, address { font-style: italic }
pre, tt, code,
kbd, samp { font-family: monospace }
pre { white-space: pre }
button, textarea,
input, select { display: inline-block }
big { font-size: 1.17em }
small, sub, sup { font-size: .83em }
sub { vertical-align: sub }
sup { vertical-align: super }
table { border-spacing: 2px; }
thead, tbody,
tfoot { vertical-align: middle }
td, th, tr { vertical-align: inherit }
s, strike, del { text-decoration: line-through }
hr { border: 1px inset }
ol, ul, dir,
menu, dd { margin-left: 40px }
ol { list-style-type: decimal }
ol ul, ul ol,
ul ul, ol ol { margin-top: 0; margin-bottom: 0 }
u, ins { text-decoration: underline }
br:before { content: '\A'; white-space: pre-line }
center { text-align: center }
:link, :visited { text-decoration: underline }
:focus { outline: thin dotted invert }
/* Begin bidirectionality settings (do not change) */
BDO[DIR='ltr'] { direction: ltr; unicode-bidi: bidi-override }
BDO[DIR='rtl'] { direction: rtl; unicode-bidi: bidi-override }
*[DIR='ltr'] { direction: ltr; unicode-bidi: embed }
*[DIR='rtl'] { direction: rtl; unicode-bidi: embed }
@media print {
h1 { page-break-before: always }
h1, h2, h3,
h4, h5, h6 { page-break-after: avoid }
ul, ol, dl { page-break-before: avoid }
}";
#endregion
}
}

Просмотреть файл

@ -1,4 +1,4 @@
namespace AngleSharp.Css
namespace AngleSharp.Css
{
using AngleSharp.Css.Dom;
using AngleSharp.Css.Parser;
@ -14,37 +14,9 @@
/// </summary>
public class CssStylingService : IStylingService
{
#region Fields
private ICssStyleSheet _default;
#endregion
#region Properties
/// <summary>
/// Gets the default stylesheet as specified by the W3C:
/// http://www.w3.org/TR/CSS21/sample.html
/// </summary>
public ICssStyleSheet Default => _default ?? (_default = ParseDefault());
#endregion
#region Methods
Boolean IStylingService.SupportsType(String mimeType)
{
return mimeType.Isi(MimeTypeNames.Css);
}
/// <summary>
/// Sets a new default stylesheet to use.
/// </summary>
/// <param name="sheet">The default stylesheet to use.</param>
public void SetDefault(ICssStyleSheet sheet)
{
_default = sheet;
}
Boolean IStylingService.SupportsType(String mimeType) => mimeType.Isi(MimeTypeNames.Css);
/// <summary>
/// Creates a style sheet for the given response asynchronously.
@ -74,98 +46,5 @@
}
#endregion
#region Default Stylesheet
private ICssStyleSheet ParseDefault()
{
var parser = new CssParser();
return parser.ParseStyleSheet(DefaultSource);
}
/// <summary>
/// Gets the source code for the by default used base stylesheet.
/// </summary>
public static readonly String DefaultSource = @"
html, address,
blockquote,
body, dd, div,
dl, dt, fieldset, form,
frame, frameset,
h1, h2, h3, h4,
h5, h6, noframes,
ol, p, ul, center,
dir, hr, menu, pre { display: block; unicode-bidi: embed }
li { display: list-item }
head { display: none }
table { display: table }
tr { display: table-row }
thead { display: table-header-group }
tbody { display: table-row-group }
tfoot { display: table-footer-group }
col { display: table-column }
colgroup { display: table-column-group }
td, th { display: table-cell }
caption { display: table-caption }
th { font-weight: bolder; text-align: center }
caption { text-align: center }
body { margin: 8px }
h1 { font-size: 2em; margin: .67em 0 }
h2 { font-size: 1.5em; margin: .75em 0 }
h3 { font-size: 1.17em; margin: .83em 0 }
h4, p,
blockquote, ul,
fieldset, form,
ol, dl, dir,
menu { margin: 1.12em 0 }
h5 { font-size: .83em; margin: 1.5em 0 }
h6 { font-size: .75em; margin: 1.67em 0 }
h1, h2, h3, h4,
h5, h6, b,
strong { font-weight: bolder }
blockquote { margin-left: 40px; margin-right: 40px }
i, cite, em,
var, address { font-style: italic }
pre, tt, code,
kbd, samp { font-family: monospace }
pre { white-space: pre }
button, textarea,
input, select { display: inline-block }
big { font-size: 1.17em }
small, sub, sup { font-size: .83em }
sub { vertical-align: sub }
sup { vertical-align: super }
table { border-spacing: 2px; }
thead, tbody,
tfoot { vertical-align: middle }
td, th, tr { vertical-align: inherit }
s, strike, del { text-decoration: line-through }
hr { border: 1px inset }
ol, ul, dir,
menu, dd { margin-left: 40px }
ol { list-style-type: decimal }
ol ul, ul ol,
ul ul, ol ol { margin-top: 0; margin-bottom: 0 }
u, ins { text-decoration: underline }
br:before { content: '\A'; white-space: pre-line }
center { text-align: center }
:link, :visited { text-decoration: underline }
:focus { outline: thin dotted invert }
/* Begin bidirectionality settings (do not change) */
BDO[DIR='ltr'] { direction: ltr; unicode-bidi: bidi-override }
BDO[DIR='rtl'] { direction: rtl; unicode-bidi: bidi-override }
*[DIR='ltr'] { direction: ltr; unicode-bidi: embed }
*[DIR='rtl'] { direction: rtl; unicode-bidi: embed }
@media print {
h1 { page-break-before: always }
h1, h2, h3,
h4, h5, h6 { page-break-after: avoid }
ul, ol, dl { page-break-before: avoid }
}";
#endregion
}
}

Просмотреть файл

@ -1,4 +1,4 @@
namespace AngleSharp.Css.Dom
namespace AngleSharp.Css.Dom
{
using System;
using System.IO;
@ -31,10 +31,7 @@
#region String Representation
public void ToCss(TextWriter writer, IStyleFormatter formatter)
{
writer.Write(formatter.Comment(_data));
}
public void ToCss(TextWriter writer, IStyleFormatter formatter) => writer.Write(formatter.Comment(_data));
#endregion
}

Просмотреть файл

@ -1,4 +1,4 @@
namespace AngleSharp.Css.Dom
namespace AngleSharp.Css.Dom
{
using AngleSharp.Css.Dom.Events;
using AngleSharp.Dom;
@ -50,11 +50,8 @@
#region Helpers
private Boolean ComputeMatched(IWindow window)
{
//TODO use Validate with RenderDevice
return false;
}
//TODO use Validate with RenderDevice
private Boolean ComputeMatched(IWindow window) => false;
private void Resized(Object sender, Event ev)
{

Просмотреть файл

@ -1,4 +1,4 @@
namespace AngleSharp.Css.Dom
namespace AngleSharp.Css.Dom
{
using AngleSharp.Text;
using System;
@ -48,14 +48,7 @@
public Boolean IsInverse => _inverse;
public String Constraints
{
get
{
var constraints = Features.Select(m => m.ToCss());
return String.Join(" and ", constraints);
}
}
public String Constraints => String.Join(" and ", Features.Select(m => m.ToCss()));
#endregion
@ -87,10 +80,7 @@
return false;
}
public override Int32 GetHashCode()
{
return base.GetHashCode();
}
public override Int32 GetHashCode() => base.GetHashCode();
public void ToCss(TextWriter writer, IStyleFormatter formatter)
{

Просмотреть файл

@ -40,14 +40,14 @@ namespace AngleSharp.Css.Dom
public ICssValue RawValue
{
get { return _value; }
set { _value = value; }
get => _value;
set => _value = value;
}
public String Value
{
get { return _value?.CssText ?? CssKeywords.Initial; }
set { _value = _converter.Convert(value); }
get => _value?.CssText ?? CssKeywords.Initial;
set => _value = _converter.Convert(value);
}
public Boolean HasValue => _value != null;
@ -66,8 +66,8 @@ namespace AngleSharp.Css.Dom
public Boolean IsImportant
{
get { return _important; }
set { _important = value; }
get => _important;
set => _important = value;
}
public String CssText => this.ToCss();
@ -86,10 +86,7 @@ namespace AngleSharp.Css.Dom
#region String Representation
public void ToCss(TextWriter writer, IStyleFormatter formatter)
{
writer.Write(formatter.Declaration(Name, Value, IsImportant));
}
public void ToCss(TextWriter writer, IStyleFormatter formatter) => writer.Write(formatter.Declaration(Name, Value, IsImportant));
#endregion
}

Просмотреть файл

@ -33,7 +33,7 @@
public String CssText
{
get { return this.ToCss(); }
get => this.ToCss();
set
{
var rule = Parser.ParseRule(Owner, value);
@ -50,7 +50,7 @@
public ICssRule Parent
{
get { return _parent; }
get => _parent;
set
{
_parent = value;
@ -64,8 +64,8 @@
public ICssStyleSheet Owner
{
get { return _owner; }
set { _owner = value; }
get => _owner;
set => _owner = value;
}
public CssRuleType Type => _type;

Просмотреть файл

@ -1,4 +1,4 @@
namespace AngleSharp.Css.Dom
namespace AngleSharp.Css.Dom
{
using AngleSharp.Dom;
using System;
@ -28,10 +28,7 @@
#region Index
public ICssRule this[Int32 index]
{
get { return _rules[index]; }
}
public ICssRule this[Int32 index] => _rules[index];
#endregion
@ -45,10 +42,7 @@
#region Methods
public void Clear()
{
_rules.Clear();
}
public void Clear() => _rules.Clear();
public void RemoveAt(Int32 index)
{
@ -103,24 +97,15 @@
}
}
public void AddRange(IEnumerable<ICssRule> rules)
{
_rules.AddRange(rules);
}
public void AddRange(IEnumerable<ICssRule> rules) => _rules.AddRange(rules);
#endregion
#region Implemented Interface
public IEnumerator<ICssRule> GetEnumerator()
{
return _rules.GetEnumerator();
}
public IEnumerator<ICssRule> GetEnumerator() => _rules.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
#endregion

Просмотреть файл

@ -48,15 +48,9 @@ namespace AngleSharp.Css.Dom
#region Index
public String this[Int32 index]
{
get { return _declarations[index]?.Name; }
}
public String this[Int32 index] => _declarations[index]?.Name;
public String this[String name]
{
get { return GetPropertyValue(name); }
}
public String this[String name] => GetPropertyValue(name);
#endregion
@ -66,7 +60,7 @@ namespace AngleSharp.Css.Dom
public String CssText
{
get { return this.ToCss(); }
get => this.ToCss();
set { Update(value); RaiseChanged(); }
}

Просмотреть файл

@ -1,4 +1,4 @@
namespace AngleSharp.Css.Dom
namespace AngleSharp.Css.Dom
{
using AngleSharp.Css.Parser;
using AngleSharp.Dom;
@ -73,10 +73,7 @@
#region Methods
public void ToCss(TextWriter writer, IStyleFormatter formatter)
{
writer.Write(formatter.Sheet(Rules));
}
public void ToCss(TextWriter writer, IStyleFormatter formatter) => writer.Write(formatter.Sheet(Rules));
public void Add(ICssRule rule)
{

Просмотреть файл

@ -1,4 +1,4 @@
namespace AngleSharp.Css.Dom
namespace AngleSharp.Css.Dom
{
using AngleSharp.Text;
using System;
@ -42,10 +42,7 @@
#region String Representation
public void ToCss(TextWriter writer, IStyleFormatter formatter)
{
writer.Write(_name.CssFunction(_data.CssString()));
}
public void ToCss(TextWriter writer, IStyleFormatter formatter) => writer.Write(_name.CssFunction(_data.CssString()));
#endregion
}

Просмотреть файл

@ -1,4 +1,4 @@
namespace AngleSharp.Css.Dom
namespace AngleSharp.Css.Dom
{
using System;
using System.Collections;
@ -19,42 +19,21 @@
_functions = functions;
}
public IDocumentFunction this[Int32 index]
{
get { return _functions[index]; }
}
public IDocumentFunction this[Int32 index] => _functions[index];
public Int32 Length => _functions.Count;
public IEnumerator<IDocumentFunction> GetEnumerator()
{
return _functions.GetEnumerator();
}
public IEnumerator<IDocumentFunction> GetEnumerator() => _functions.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public void Add(IDocumentFunction function)
{
_functions.Add(function);
}
public void Add(IDocumentFunction function) => _functions.Add(function);
public void Remove(IDocumentFunction function)
{
_functions.Remove(function);
}
public void Remove(IDocumentFunction function) => _functions.Remove(function);
public void Clear()
{
_functions.Clear();
}
public void Clear() => _functions.Clear();
public void AddRange(IEnumerable<IDocumentFunction> functions)
{
_functions.AddRange(functions);
}
public void AddRange(IEnumerable<IDocumentFunction> functions) => _functions.AddRange(functions);
public void ToCss(TextWriter writer, IStyleFormatter formatter)
{

Просмотреть файл

@ -1,4 +1,4 @@
namespace AngleSharp.Css.Dom
namespace AngleSharp.Css.Dom
{
using AngleSharp.Css.Parser;
using AngleSharp.Dom;
@ -31,10 +31,7 @@
#region Index
public String this[Int32 index]
{
get { return _media[index].ToCss(); }
}
public String this[Int32 index] => _media[index].ToCss();
#endregion
@ -46,8 +43,8 @@
public String MediaText
{
get { return this.ToCss(); }
set { SetMediaText(value, throwOnError: true); }
get => this.ToCss();
set => SetMediaText(value, throwOnError: true);
}
#endregion
@ -76,20 +73,13 @@
public void Add(String newMedium)
{
var medium = MediumParser.Parse(newMedium);
if (medium == null)
throw new DomException(DomError.Syntax);
var medium = MediumParser.Parse(newMedium) ?? throw new DomException(DomError.Syntax);
_media.Add(medium);
}
public void Remove(String oldMedium)
{
var medium = MediumParser.Parse(oldMedium);
if (medium == null)
throw new DomException(DomError.Syntax);
var medium = MediumParser.Parse(oldMedium) ?? throw new DomException(DomError.Syntax);
for (var i = 0; i < _media.Count; i++)
{
@ -127,15 +117,9 @@
#region IEnumerable implementation
public IEnumerator<ICssMedium> GetEnumerator()
{
return _media.GetEnumerator();
}
public IEnumerator<ICssMedium> GetEnumerator() => _media.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
#endregion
}

Просмотреть файл

@ -33,7 +33,7 @@ namespace AngleSharp.Css.Dom
public String Slot
{
get { return _host.Slot; }
get => _host.Slot;
set { }
}
@ -55,25 +55,25 @@ namespace AngleSharp.Css.Dom
public String ClassName
{
get { return _host.ClassName; }
get => _host.ClassName;
set { }
}
public String Id
{
get { return _host.Id; }
get => _host.Id;
set { }
}
public String InnerHtml
{
get { return String.Empty; }
get => String.Empty;
set { }
}
public String OuterHtml
{
get { return String.Empty; }
get => String.Empty;
set { }
}
@ -107,14 +107,14 @@ namespace AngleSharp.Css.Dom
public String NodeValue
{
get { return _host.NodeValue; }
get => _host.NodeValue;
set { }
}
public String TextContent
{
get { return String.Empty; }
set { }
get => String.Empty;
set { }
}
public Boolean HasChildNodes => _host.HasChildNodes;

Просмотреть файл

@ -29,8 +29,8 @@
public String CharacterSet
{
get { return _charSet; }
set { _charSet = value ?? String.Empty; }
get => _charSet;
set => _charSet = value ?? String.Empty;
}
#endregion

Просмотреть файл

@ -34,10 +34,7 @@ namespace AngleSharp.Css.Dom
#region Properties
public String this[String propertyName]
{
get { return GetValue(propertyName); }
}
public String this[String propertyName] => GetValue(propertyName);
public Int32 Length => _declarations.Count;

Просмотреть файл

@ -30,8 +30,8 @@
public String ConditionText
{
get { return _conditions.ToCss(); }
set { SetConditionText(value, throwOnError: true); }
get => _conditions.ToCss();
set => SetConditionText(value, throwOnError: true);
}
public IDocumentFunctions Conditions => _conditions;

Просмотреть файл

@ -37,49 +37,49 @@
String ICssFontFaceRule.Family
{
get { return GetValue(PropertyNames.FontFamily); }
set { SetValue(PropertyNames.FontFamily, value); }
get => GetValue(PropertyNames.FontFamily);
set => SetValue(PropertyNames.FontFamily, value);
}
String ICssFontFaceRule.Source
{
get { return GetValue(PropertyNames.Src); }
set { SetValue(PropertyNames.Src, value); }
get => GetValue(PropertyNames.Src);
set => SetValue(PropertyNames.Src, value);
}
String ICssFontFaceRule.Style
{
get { return GetValue(PropertyNames.FontStyle); }
set { SetValue(PropertyNames.FontStyle, value); }
get => GetValue(PropertyNames.FontStyle);
set => SetValue(PropertyNames.FontStyle, value);
}
String ICssFontFaceRule.Weight
{
get { return GetValue(PropertyNames.FontWeight); }
set { SetValue(PropertyNames.FontWeight, value); }
get => GetValue(PropertyNames.FontWeight);
set => SetValue(PropertyNames.FontWeight, value);
}
String ICssFontFaceRule.Stretch
{
get { return GetValue(PropertyNames.FontStretch); }
set { SetValue(PropertyNames.FontStretch, value); }
get => GetValue(PropertyNames.FontStretch);
set => SetValue(PropertyNames.FontStretch, value);
}
String ICssFontFaceRule.Range
{
get { return GetValue(PropertyNames.UnicodeRange); }
set { SetValue(PropertyNames.UnicodeRange, value); }
get => GetValue(PropertyNames.UnicodeRange);
set => SetValue(PropertyNames.UnicodeRange, value);
}
String ICssFontFaceRule.Variant
{
get { return GetValue(PropertyNames.FontVariant); }
set { SetValue(PropertyNames.FontVariant, value); }
get => GetValue(PropertyNames.FontVariant);
set => SetValue(PropertyNames.FontVariant, value);
}
String ICssFontFaceRule.Features
{
get { return String.Empty; }
get => String.Empty;
set { }
}

Просмотреть файл

@ -31,8 +31,8 @@
public String Href
{
get { return _href; }
set { _href = value; }
get => _href;
set => _href = value;
}
IMediaList ICssImportRule.Media => _media;
@ -41,7 +41,7 @@
public ICssStyleSheet Sheet
{
get { return _styleSheet; }
get => _styleSheet;
set { _styleSheet = value; _styleSheet?.SetParent(Owner); }
}

Просмотреть файл

@ -30,8 +30,8 @@ namespace AngleSharp.Css.Dom
public String KeyText
{
get { return _selector?.ToCss(); }
set { _selector = KeyframeParser.Parse(value); }
get => _selector?.ToCss();
set => _selector = KeyframeParser.Parse(value);
}
public IKeyframeSelector Key => _selector;

Просмотреть файл

@ -29,8 +29,8 @@
public String Name
{
get { return _name; }
set { _name = value; }
get => _name;
set => _name = value;
}
#endregion

Просмотреть файл

@ -28,8 +28,8 @@
public String ConditionText
{
get { return _media.MediaText; }
set { _media.MediaText = value; }
get => _media.MediaText;
set => _media.MediaText = value;
}
IMediaList ICssMediaRule.Media => _media;

Просмотреть файл

@ -32,13 +32,13 @@
public String NamespaceUri
{
get { return _namespaceUri; }
get => _namespaceUri;
set { CheckValidity(); _namespaceUri = value ?? String.Empty; }
}
public String Prefix
{
get { return _prefix; }
get => _prefix;
set { CheckValidity(); _prefix = value ?? String.Empty; }
}

Просмотреть файл

@ -29,7 +29,7 @@
public String SelectorText
{
get { return _selector?.Text; }
get => _selector?.Text;
set { _selector = ParseSelector(value); ; }
}

Просмотреть файл

@ -31,8 +31,8 @@ namespace AngleSharp.Css.Dom
public String SelectorText
{
get { return _selector?.Text; }
set { _selector = ParseSelector(value); }
get => _selector?.Text;
set => _selector = ParseSelector(value);
}
ICssStyleDeclaration ICssStyleRule.Style => _style;

Просмотреть файл

@ -29,8 +29,8 @@
public String ConditionText
{
get { return _condition.ToCss(); }
set { SetConditionText(value, throwOnError: true); }
get => _condition.ToCss();
set => SetConditionText(value, throwOnError: true);
}
public IConditionFunction Condition => _condition;

Просмотреть файл

@ -1,6 +1,5 @@
namespace AngleSharp.Css.Dom
namespace AngleSharp.Css.Dom
{
using AngleSharp.Css.Extensions;
using System.Collections;
using System.Collections.Generic;
@ -93,10 +92,7 @@
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
#endregion
}

Просмотреть файл

@ -23,8 +23,12 @@ namespace AngleSharp.Css.Extensions
public static StyleCollection GetStyleCollection(this IWindow window)
{
var document = window.Document;
var device = document.Context.GetService<IRenderDevice>();
var stylesheets = document.GetStyleSheets().OfType<ICssStyleSheet>();
var ctx = document.Context;
var device = ctx.GetService<IRenderDevice>();
var defaultStyleSheetProvider = ctx.GetServices<ICssDefaultStyleSheetProvider>();
var defaultSheets = defaultStyleSheetProvider.Select(m => m.Default).Where(m => m != null);
var currentSheets = document.GetStyleSheets().OfType<ICssStyleSheet>();
var stylesheets = defaultSheets.Concat(currentSheets);
return new StyleCollection(stylesheets, device);
}

Просмотреть файл

@ -19,9 +19,7 @@ namespace AngleSharp.Dom
/// <returns>The computed style declaration if available.</returns>
public static ICssStyleDeclaration ComputeCurrentStyle(this IElement element)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
element = element ?? throw new ArgumentNullException(nameof(element));
var document = element.Owner;
var window = document?.DefaultView;
return window?.GetComputedStyle(element);
@ -39,13 +37,10 @@ namespace AngleSharp.Dom
/// </param>
/// <returns>The collection itself.</returns>
public static T Css<T>(this T elements, String propertyName, String propertyValue)
where T : IEnumerable<IElement>
where T : class, IEnumerable<IElement>
{
if (elements == null)
throw new ArgumentNullException(nameof(elements));
if (propertyName == null)
throw new ArgumentNullException(nameof(propertyName));
elements = elements ?? throw new ArgumentNullException(nameof(elements));
propertyName = propertyName ?? throw new ArgumentNullException(nameof(propertyName));
foreach (var element in elements.OfType<IHtmlElement>())
{
@ -66,13 +61,10 @@ namespace AngleSharp.Dom
/// </param>
/// <returns>The collection itself.</returns>
public static T Css<T>(this T elements, IEnumerable<KeyValuePair<String, String>> properties)
where T : IEnumerable<IElement>
where T : class, IEnumerable<IElement>
{
if (elements == null)
throw new ArgumentNullException(nameof(elements));
if (properties == null)
throw new ArgumentNullException(nameof(properties));
elements = elements ?? throw new ArgumentNullException(nameof(elements));
properties = properties ?? throw new ArgumentNullException(nameof(properties));
foreach (var element in elements.OfType<IHtmlElement>())
{
@ -97,7 +89,7 @@ namespace AngleSharp.Dom
/// </param>
/// <returns>The collection itself.</returns>
public static T Css<T>(this T elements, Object properties)
where T : IEnumerable<IElement>
where T : class, IEnumerable<IElement>
{
var realProperties = properties.ToDictionary();
return elements.Css(realProperties);

Просмотреть файл

@ -7,7 +7,8 @@ namespace AngleSharp.Css.Dom
static class MediaListExtensions
{
private readonly static ConditionalWeakTable<IMediaFeature, IFeatureValidator> AssociatedValidators = new ConditionalWeakTable<IMediaFeature, IFeatureValidator>();
private readonly static ConditionalWeakTable<IMediaFeature, IFeatureValidator> AssociatedValidators =
new ConditionalWeakTable<IMediaFeature, IFeatureValidator>();
private readonly static String[] KnownTypes =
{
@ -21,10 +22,8 @@ namespace AngleSharp.Css.Dom
CssKeywords.All
};
public static void AssociateValidator(this IMediaFeature feature, IFeatureValidator validator)
{
public static void AssociateValidator(this IMediaFeature feature, IFeatureValidator validator) =>
AssociatedValidators.Add(feature, validator);
}
public static Boolean Validate(this IMediaFeature feature, IRenderDevice device)
{
@ -33,10 +32,7 @@ namespace AngleSharp.Css.Dom
return validator?.Validate(feature, device) ?? false;
}
public static Boolean Validate(this IMediaList list, IRenderDevice device)
{
return !list.Any(m => !m.Validate(device));
}
public static Boolean Validate(this IMediaList list, IRenderDevice device) => !list.Any(m => !m.Validate(device));
public static Boolean Validate(this ICssMedium medium, IRenderDevice device)
{
@ -48,16 +44,12 @@ namespace AngleSharp.Css.Dom
return !medium.IsInvalid(device) && !medium.Features.Any(m => m.Validate(device) == medium.IsInverse);
}
private static Boolean IsInvalid(this ICssMedium medium, IRenderDevice device)
{
return medium.IsInvalid(device, CssKeywords.Screen, DeviceCategory.Screen) ||
medium.IsInvalid(device, CssKeywords.Speech, DeviceCategory.Speech) ||
medium.IsInvalid(device, CssKeywords.Print, DeviceCategory.Printer);
}
private static Boolean IsInvalid(this ICssMedium medium, IRenderDevice device) =>
medium.IsInvalid(device, CssKeywords.Screen, DeviceCategory.Screen) ||
medium.IsInvalid(device, CssKeywords.Speech, DeviceCategory.Speech) ||
medium.IsInvalid(device, CssKeywords.Print, DeviceCategory.Printer);
private static Boolean IsInvalid(this ICssMedium medium, IRenderDevice device, String keyword, DeviceCategory category)
{
return keyword.Is(medium.Type) && (device.Category == category) == medium.IsInverse;
}
private static Boolean IsInvalid(this ICssMedium medium, IRenderDevice device, String keyword, DeviceCategory category) =>
device != null && keyword.Is(medium.Type) && device.Category == category == medium.IsInverse;
}
}

Просмотреть файл

@ -14,10 +14,7 @@ namespace AngleSharp.Css.Dom
/// <param name="rule">The rule to extend.</param>
/// <param name="device">The device to check for support.</param>
/// <returns>True if support is given, otherwise false.</returns>
public static Boolean IsValid(this ICssSupportsRule rule, IRenderDevice device)
{
return rule.Condition.Check(device);
}
public static Boolean IsValid(this ICssSupportsRule rule, IRenderDevice device) => rule.Condition.Check(device);
/// <summary>
/// Determines if the rule is valid for a given device.
@ -25,10 +22,7 @@ namespace AngleSharp.Css.Dom
/// <param name="rule">The rule to extend.</param>
/// <param name="device">The device to check for conformance.</param>
/// <returns>True if support is given, otherwise false.</returns>
public static Boolean IsValid(this ICssMediaRule rule, IRenderDevice device)
{
return rule.Media.Validate(device);
}
public static Boolean IsValid(this ICssMediaRule rule, IRenderDevice device) => rule.Media.Validate(device);
/// <summary>
/// Determines if the rule is valid for a given URL.
@ -36,9 +30,6 @@ namespace AngleSharp.Css.Dom
/// <param name="rule">The rule to extend.</param>
/// <param name="url">The URL to check for conformance.</param>
/// <returns>True if the URL is matching, otherwise false.</returns>
public static Boolean IsValid(this ICssDocumentRule rule, Url url)
{
return rule.Conditions.Any(m => m.Matches(url));
}
public static Boolean IsValid(this ICssDocumentRule rule, Url url) => rule.Conditions.Any(m => m.Matches(url));
}
}

Просмотреть файл

@ -0,0 +1,35 @@
namespace AngleSharp.Css
{
using AngleSharp.Css.Dom;
using System;
/// <summary>
/// Handles the presence of a default stylesheet, if any.
/// </summary>
public interface ICssDefaultStyleSheetProvider
{
/// <summary>
/// Gets the default stylesheet for some basic styling.
/// </summary>
ICssStyleSheet Default { get; }
/// <summary>
/// Sets a new default stylesheet to use.
/// </summary>
/// <param name="sheet">The default stylesheet to use.</param>
void SetDefault(ICssStyleSheet sheet);
/// <summary>
/// Sets a new default stylesheet to use.
/// </summary>
/// <param name="source">The source of the default stylesheet.</param>
void SetDefault(String source);
/// <summary>
/// Sets the default stylesheet to the W3C stylesheet together with
/// some custom extensions in form of the given source.
/// </summary>
/// <param name="source">The source of the custom stylesheet part.</param>
void AppendDefault(String source);
}
}

Просмотреть файл

@ -39,8 +39,8 @@
/// </summary>
public String Indentation
{
get { return _intendString; }
set { _intendString = value; }
get => _intendString;
set => _intendString = value;
}
/// <summary>
@ -48,8 +48,8 @@
/// </summary>
public String NewLine
{
get { return _newLineString; }
set { _newLineString = value; }
get => _newLineString;
set => _newLineString = value;
}
#endregion

Просмотреть файл

@ -60,10 +60,7 @@ namespace AngleSharp.Css.Values
/// </summary>
/// <param name="index">The index to look for.</param>
/// <returns>The value.</returns>
public Double this[Int32 index]
{
get { return _values[index]; }
}
public Double this[Int32 index] => _values[index];
#endregion

Просмотреть файл

@ -29,14 +29,14 @@
public Int32 NumberOfRepeats
{
get { return _repeats; }
set { _repeats = value; }
get => _repeats;
set => _repeats = value;
}
public Int32 NumberOfReRuns
{
get { return _reruns; }
set { _reruns = value; }
get => _reruns;
set => _reruns = value;
}
internal IEnumerable<ITest> Tests => _tests;