Skip to content

Commit

Permalink
Started working on nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Jan 21, 2024
1 parent b755de0 commit 281d795
Show file tree
Hide file tree
Showing 297 changed files with 2,362 additions and 2,961 deletions.
2 changes: 1 addition & 1 deletion src/AngleSharp.Css.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<icon>logo.png</icon>
<readme>README.md</readme>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Extends the CSSOM from the core AngleSharp library.</description>
<description>Extends the CSSOM from the core AngleSharp library. It parses CSS3 and is capable of performing a headless rendering evaluating the styles.</description>
<releaseNotes>https://github.com/AngleSharp/AngleSharp.Css/blob/main/CHANGELOG.md</releaseNotes>
<copyright>Copyright 2016-2024, AngleSharp</copyright>
<tags>html html5 css css3 dom styling library anglesharp angle</tags>
Expand Down
4 changes: 1 addition & 3 deletions src/AngleSharp.Css/AngleSharp.Css.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
<RootNamespace>AngleSharp.Css</RootNamespace>
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">netstandard2.0;net6.0;net7.0;net8.0</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">netstandard2.0;net461;net472;net6.0;net7.0;net8.0</TargetFrameworks>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>Key.snk</AssemblyOriginatorKeyFile>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<LangVersion>9</LangVersion>
<Nullable>enable</Nullable>
<RepositoryUrl>https://github.com/AngleSharp/AngleSharp.Css</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
Expand Down
23 changes: 11 additions & 12 deletions src/AngleSharp.Css/BrowsingContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static class BrowsingContextExtensions
/// <param name="address">The address of the resource.</param>
/// <param name="element">The hosting element.</param>
/// <returns>The async task.</returns>
public static Task<IStyleSheet> OpenStyleSheetAsync(this IBrowsingContext context, Url address, IElement element) =>
public static Task<IStyleSheet?> OpenStyleSheetAsync(this IBrowsingContext context, Url address, IElement element) =>
context.OpenStyleSheetAsync(address, element, CancellationToken.None);

/// <summary>
Expand All @@ -31,21 +31,20 @@ public static Task<IStyleSheet> OpenStyleSheetAsync(this IBrowsingContext contex
/// <param name="element">The hosting element.</param>
/// <param name="cancel">The cancellation token.</param>
/// <returns>The async task.</returns>
public static async Task<IStyleSheet> OpenStyleSheetAsync(this IBrowsingContext context, Url address, IElement element, CancellationToken cancel)
public static async Task<IStyleSheet?> OpenStyleSheetAsync(this IBrowsingContext context, Url address, IElement element, CancellationToken cancel)
{
var loader = context.GetService<IResourceLoader>();
var service = context.GetCssStyling();

if (loader != null && service != null)
if (loader is not null && service is not null)
{
var request = element.CreateRequestFor(address);
var download = loader.FetchAsync(request);

using (var response = await download.Task.ConfigureAwait(false))
{
var options = new StyleOptions(element?.Owner ?? context.Active) { Element = element };
return await service.ParseStylesheetAsync(response, options, cancel).ConfigureAwait(false);
}
using var response = await download.Task.ConfigureAwait(false);
var document = element.Owner ?? context.Active ?? throw new InvalidOperationException("Could not find a related documented.");
var options = new StyleOptions(document) { Element = element };
return await service.ParseStylesheetAsync(response, options, cancel).ConfigureAwait(false);
}

return null;
Expand All @@ -57,7 +56,7 @@ internal static DeclarationInfo GetDeclarationInfo(this IBrowsingContext context
return factory.Create(propertyName);
}

internal static ICssProperty CreateShorthand(this IBrowsingContext context, String name, ICssValue[] longhands, Boolean important)
internal static ICssProperty? CreateShorthand(this IBrowsingContext context, String name, ICssValue[] longhands, Boolean important)
{
var factory = context.GetFactory<IDeclarationFactory>();
var info = factory.Create(name);
Expand All @@ -79,7 +78,7 @@ internal static ICssProperty[] CreateLonghands(this IBrowsingContext context, IC
return factory.CreateProperties(info.Longhands, values, shorthand.IsImportant);
}

internal static CssProperty CreateProperty(this IBrowsingContext context, String propertyName)
internal static CssProperty? CreateProperty(this IBrowsingContext context, String propertyName)
{
var info = context.GetDeclarationInfo(propertyName);

Expand All @@ -102,7 +101,7 @@ private static Boolean IsAllowingUnknownDeclarations(this IBrowsingContext conte

private static ICssProperty[] CreateProperties(this IDeclarationFactory factory, String[] names, ICssValue[] values, Boolean important)
{
if (values != null && values.Length == names.Length)
if (values.Length == names.Length)
{
var properties = new ICssProperty[names.Length];

Expand All @@ -115,7 +114,7 @@ private static ICssProperty[] CreateProperties(this IDeclarationFactory factory,
return properties;
}

return null;
return [];
}
}
}
14 changes: 7 additions & 7 deletions src/AngleSharp.Css/Constants/InitialValues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static class InitialValues
public static readonly ICssValue BackgroundRepeatDecl = new CssImageRepeatsValue(BackgroundRepeatHorizontalDecl, BackgroundRepeatVerticalDecl);
public static readonly ICssValue BackgroundPositionXDecl = new CssLengthValue(0, CssLengthValue.Unit.Percent);
public static readonly ICssValue BackgroundPositionYDecl = new CssLengthValue(0, CssLengthValue.Unit.Percent);
public static readonly ICssValue BackgroundPositionDecl = new CssTupleValue(new [] { BackgroundPositionXDecl, BackgroundPositionYDecl });
public static readonly ICssValue BackgroundPositionDecl = new CssTupleValue([BackgroundPositionXDecl, BackgroundPositionYDecl]);
public static readonly ICssValue BackgroundSizeDecl = new CssBackgroundSizeValue(new CssConstantValue<CssLengthValue>(CssKeywords.Auto, CssLengthValue.Auto), new CssConstantValue<CssLengthValue>(CssKeywords.Auto, CssLengthValue.Auto));
public static readonly ICssValue BackgroundOriginDecl = new CssConstantValue<BoxModel>(CssKeywords.BorderBox, BoxModel.PaddingBox);
public static readonly ICssValue BackgroundClipDecl = new CssConstantValue<BoxModel>(CssKeywords.BorderBox, BoxModel.BorderBox);
Expand Down Expand Up @@ -204,13 +204,13 @@ static class InitialValues
public static readonly ICssValue ZIndexDecl = new CssConstantValue<CssLengthValue>(CssKeywords.Auto, CssLengthValue.Auto);
public static readonly ICssValue WidthDecl = CssLengthValue.Auto;
public static readonly ICssValue HeightDecl = CssLengthValue.Auto;
public static readonly ICssValue ScrollbarTrackColorDecl = CssColors.GetColor("scrollbar");
public static readonly ICssValue ScrollbarShadowColorDecl = CssColors.GetColor("threeddarkshadow");
public static readonly ICssValue ScrollbarHighlightColorDecl = CssColors.GetColor("threedhighlight");
public static readonly ICssValue ScrollbarFaceColorDecl = CssColors.GetColor("threedface");
public static readonly ICssValue ScrollbarDarkshadowColorDecl = CssColors.GetColor("threeddarkshadow");
public static readonly ICssValue ScrollbarTrackColorDecl = CssColors.GetColor("scrollbar")!;
public static readonly ICssValue ScrollbarShadowColorDecl = CssColors.GetColor("threeddarkshadow")!;
public static readonly ICssValue ScrollbarHighlightColorDecl = CssColors.GetColor("threedhighlight")!;
public static readonly ICssValue ScrollbarFaceColorDecl = CssColors.GetColor("threedface")!;
public static readonly ICssValue ScrollbarDarkshadowColorDecl = CssColors.GetColor("threeddarkshadow")!;
public static readonly ICssValue ScrollbarBaseColorDecl = CssColorValue.Transparent;
public static readonly ICssValue ScrollbarArrowColorDecl = CssColors.GetColor("buttontext");
public static readonly ICssValue ScrollbarArrowColorDecl = CssColors.GetColor("buttontext")!;
public static readonly ICssValue Scrollbar3dLightColorDecl = CssColorValue.White;
public static readonly ICssValue LetterSpacingDecl = CssLengthValue.Normal;
public static readonly ICssValue FontSizeAdjustDecl = new CssLengthValue(1.0, CssLengthValue.Unit.Em);
Expand Down
9 changes: 2 additions & 7 deletions src/AngleSharp.Css/Converters/ClassValueConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@
using AngleSharp.Text;
using System;

sealed class ClassValueConverter<T> : IValueConverter
sealed class ClassValueConverter<T>(Func<StringSource, T> converter) : IValueConverter
where T : class, ICssValue
{
private readonly Func<StringSource, T> _converter;

public ClassValueConverter(Func<StringSource, T> converter)
{
_converter = converter;
}
private readonly Func<StringSource, T> _converter = converter;

public ICssValue Convert(StringSource source)
{
Expand Down
18 changes: 6 additions & 12 deletions src/AngleSharp.Css/Converters/CounterValueConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,17 @@ namespace AngleSharp.Css.Converters
using AngleSharp.Css.Parser;
using AngleSharp.Css.Values;
using AngleSharp.Text;
using System;
using System.Collections.Generic;

sealed class CounterValueConverter : IValueConverter
sealed class CounterValueConverter(ICssValue defaultValue) : IValueConverter
{
private static readonly CssCounterValue[] NoneValue = Array.Empty<CssCounterValue>();
private static readonly CssCounterValue[] NoneValue = [];

private readonly ICssValue _defaultValue;
private readonly ICssValue _defaultValue = defaultValue;

public CounterValueConverter(ICssValue defaultValue)
public ICssValue? Convert(StringSource source)
{
_defaultValue = defaultValue;
}

public ICssValue Convert(StringSource source)
{
var counters = new List<CssCounterValue>();
var counters = new List<ICssValue>();

if (!source.IsIdentifier(CssKeywords.None))
{
Expand All @@ -39,7 +33,7 @@ public ICssValue Convert(StringSource source)
counters.Add(new CssCounterValue(name, value));
}

return new CssTupleValue<CssCounterValue>(counters.ToArray());
return new CssTupleValue([.. counters]);
}

return new CssConstantValue<CssCounterValue[]>(CssKeywords.None, NoneValue);
Expand Down
14 changes: 4 additions & 10 deletions src/AngleSharp.Css/Converters/DictionaryValueConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,16 @@ namespace AngleSharp.Css.Converters
using System;
using System.Collections.Generic;

sealed class DictionaryValueConverter<T> : IValueConverter
sealed class DictionaryValueConverter<T>(IDictionary<String, T> values) : IValueConverter
{
private readonly IDictionary<String, T> _values;
private readonly IDictionary<String, T> _values = values;

public DictionaryValueConverter(IDictionary<String, T> values)
{
_values = values;
}

public ICssValue Convert(StringSource source)
public ICssValue? Convert(StringSource source)
{
var pos = source.Index;
var ident = source.ParseIdent();
var mode = default(T);

if (ident != null && _values.TryGetValue(ident, out mode))
if (ident is not null && _values.TryGetValue(ident, out var mode))
{
return new CssConstantValue<T>(ident.ToLowerInvariant(), mode);
}
Expand Down
17 changes: 6 additions & 11 deletions src/AngleSharp.Css/Converters/FlowRelativeValueConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,23 @@ namespace AngleSharp.Css.Converters
using AngleSharp.Text;
using System;

sealed class FlowRelativeValueConverter : IValueConverter
sealed class FlowRelativeValueConverter(IValueConverter converter) : IValueConverter
{
private readonly IValueConverter _converter;
private readonly IValueConverter _converter = converter;

public FlowRelativeValueConverter(IValueConverter converter)
{
_converter = converter;
}

public ICssValue Convert(StringSource source)
public ICssValue? Convert(StringSource source)
{
var options = new ICssValue[2];
var length = 0;

for (var i = 0; i < options.Length; i++)
{
options[i] = _converter.Convert(source);
var option = _converter.Convert(source);
source.SkipSpacesAndComments();

if (options[length] != null)
if (option is not null)
{
length++;
options[length++] = option;
}
}

Expand Down
27 changes: 8 additions & 19 deletions src/AngleSharp.Css/Converters/IdentifierValueConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,15 @@ namespace AngleSharp.Css.Converters
using AngleSharp.Text;
using System;

sealed class IdentifierValueConverter : IValueConverter
sealed class IdentifierValueConverter(Func<StringSource, String> check) : IValueConverter
{
private readonly Func<StringSource, String> _check;
private readonly Func<StringSource, String> _check = check;

public IdentifierValueConverter(Func<StringSource, String> check)
{
_check = check;
}

public ICssValue Convert(StringSource source)
public ICssValue? Convert(StringSource source)
{
var result = _check.Invoke(source);

if (result != null)
if (result is not null)
{
return new CssIdentifierValue(result);
}
Expand All @@ -28,18 +23,12 @@ public ICssValue Convert(StringSource source)
}
}

sealed class IdentifierValueConverter<T> : IValueConverter
sealed class IdentifierValueConverter<T>(String identifier, T result) : IValueConverter
{
private readonly String _identifier;
private readonly T _result;

public IdentifierValueConverter(String identifier, T result)
{
_identifier = identifier;
_result = result;
}
private readonly String _identifier = identifier;
private readonly T _result = result;

public ICssValue Convert(StringSource source)
public ICssValue? Convert(StringSource source)
{
if (source.IsIdentifier(_identifier))
{
Expand Down
13 changes: 4 additions & 9 deletions src/AngleSharp.Css/Converters/ListValueConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@ namespace AngleSharp.Css.Converters
using AngleSharp.Text;
using System.Collections.Generic;

sealed class ListValueConverter : IValueConverter
sealed class ListValueConverter(IValueConverter converter) : IValueConverter
{
private readonly IValueConverter _converter;
private readonly IValueConverter _converter = converter;

public ListValueConverter(IValueConverter converter)
{
_converter = converter;
}

public ICssValue Convert(StringSource source)
public ICssValue? Convert(StringSource source)
{
var values = new List<ICssValue>();

Expand All @@ -33,7 +28,7 @@ public ICssValue Convert(StringSource source)
values.Add(value);
}

return values.Count > 0 ? new CssListValue(values.ToArray()) : null;
return values.Count > 0 ? new CssListValue([.. values]) : null;
}
}
}
22 changes: 7 additions & 15 deletions src/AngleSharp.Css/Converters/OneOrMoreValueConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,14 @@ namespace AngleSharp.Css.Converters
using System;
using System.Collections.Generic;

sealed class OneOrMoreValueConverter : IValueConverter
sealed class OneOrMoreValueConverter(IValueConverter converter, Int32 minimum, Int32 maximum, String separator) : IValueConverter
{
private readonly IValueConverter _converter;
private readonly Int32 _minimum;
private readonly Int32 _maximum;
private readonly String _separator;
private readonly IValueConverter _converter = converter;
private readonly Int32 _minimum = minimum;
private readonly Int32 _maximum = maximum;
private readonly String _separator = separator;

public OneOrMoreValueConverter(IValueConverter converter, Int32 minimum, Int32 maximum, String separator)
{
_converter = converter;
_minimum = minimum;
_maximum = maximum;
_separator = separator;
}

public ICssValue Convert(StringSource source)
public ICssValue? Convert(StringSource source)
{
var values = new List<ICssValue>();

Expand All @@ -37,7 +29,7 @@ public ICssValue Convert(StringSource source)
values.Add(value);
}

return values.Count >= _minimum ? new CssTupleValue(values.ToArray(), _separator) : null;
return values.Count >= _minimum ? new CssTupleValue([.. values], _separator) : null;
}
}
}
14 changes: 4 additions & 10 deletions src/AngleSharp.Css/Converters/OptionValueConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,12 @@ namespace AngleSharp.Css.Converters
using AngleSharp.Css.Values;
using AngleSharp.Text;

sealed class OptionValueConverter : IValueConverter
sealed class OptionValueConverter(IValueConverter converter, ICssValue defaultValue) : IValueConverter
{
private readonly IValueConverter _converter;
private readonly ICssValue _defaultValue;
private readonly IValueConverter _converter = converter;
private readonly ICssValue _defaultValue = defaultValue;

public OptionValueConverter(IValueConverter converter, ICssValue defaultValue)
{
_converter = converter;
_defaultValue = defaultValue;
}

public ICssValue Convert(StringSource source)
public ICssValue? Convert(StringSource source)
{
if (source.IsDone || source.Current == Symbols.Comma)
{
Expand Down
Loading

0 comments on commit 281d795

Please sign in to comment.