Skip to content

Commit

Permalink
Made parser API public #111
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Jun 2, 2022
1 parent fc0ad92 commit bd83c45
Show file tree
Hide file tree
Showing 56 changed files with 576 additions and 97 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ Released on Tuesday, May 31 2022.

- Dropped .NET Framework 4.6
- Updated to use AngleSharp 0.17
- Updated micro parser API to be public (#111)
- Fixed casing issue with color, timing, and gradient functions (#109)
- Fixed parsing of `skew` (#101)
- Fixed shorthand properties using `inherit` being omitted (#100)
- Added support for `@counter-style` (#102)
- Added support for `@font-feature-values` (#102)
- Added support for `conic-gradient` (#101)

# 0.16.4
Expand Down
10 changes: 10 additions & 0 deletions src/AngleSharp.Css/Constants/RuleNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,15 @@ public static class RuleNames
/// The @page rule.
/// </summary>
public static readonly String Page = "@page";

/// <summary>
/// The @counter-style rule.
/// </summary>
public static readonly String CounterStyle = "@counter-style";

/// <summary>
/// The @font-feature-values rule.
/// </summary>
public static readonly String FontFeatureValues = "@font-feature-values";
}
}
6 changes: 3 additions & 3 deletions src/AngleSharp.Css/Dom/CssRuleType.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AngleSharp.Css.Dom
namespace AngleSharp.Css.Dom
{
using AngleSharp.Attributes;

Expand All @@ -7,7 +7,7 @@
/// </summary>
[DomName("CSSRule")]
public enum CssRuleType : byte
{
{
/// <summary>
/// The rule is not known and cannot be used.
/// </summary>
Expand Down Expand Up @@ -86,6 +86,6 @@ public enum CssRuleType : byte
/// Creating a CSS region with @region.
/// </summary>
[DomName("REGION_STYLE_RULE")]
RegionStyle = 16
RegionStyle = 16,
}
}
35 changes: 34 additions & 1 deletion src/AngleSharp.Css/Dom/Internal/CssMedium.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace AngleSharp.Css.Dom
/// Represents a medium rule. More information available at:
/// http://www.w3.org/TR/css3-mediaqueries/
/// </summary>
sealed class CssMedium : ICssMedium
public sealed class CssMedium : ICssMedium
{
#region Fields

Expand All @@ -23,11 +23,24 @@ sealed class CssMedium : ICssMedium

#region ctor

/// <summary>
/// Creates a new CSS medium.
/// </summary>
/// <param name="type">The type of the media rule.</param>
/// <param name="inverse">Specifies if it should be inverted.</param>
/// <param name="exclusive">Specifies if the rule is exclusive.</param>
public CssMedium(String type, Boolean inverse, Boolean exclusive)
: this(type, inverse, exclusive, Enumerable.Empty<IMediaFeature>())
{
}

/// <summary>
/// Creates a new CSS medium.
/// </summary>
/// <param name="type">The type of the media rule.</param>
/// <param name="inverse">Specifies if it should be inverted.</param>
/// <param name="exclusive">Specifies if the rule is exclusive.</param>
/// <param name="features">The features of the medium.</param>
public CssMedium(String type, Boolean inverse, Boolean exclusive, IEnumerable<IMediaFeature> features)
{
_features = new List<IMediaFeature>(features);
Expand All @@ -40,20 +53,36 @@ public CssMedium(String type, Boolean inverse, Boolean exclusive, IEnumerable<IM

#region Properties

/// <summary>
/// Gets the feature demands (constraints) of the medium.
/// </summary>
public IEnumerable<IMediaFeature> Features => _features;

/// <summary>
/// Gets the type of the medium.
/// </summary>
public String Type => _type;

/// <summary>
/// Gets if the medium is exclusive to other media.
/// </summary>
public Boolean IsExclusive => _exclusive;

/// <summary>
/// Gets if the medium should be inverted.
/// </summary>
public Boolean IsInverse => _inverse;

/// <summary>
/// Gets the constraints - i.e., the stringified features.
/// </summary>
public String Constraints => String.Join(" and ", Features.Select(m => m.ToCss()));

#endregion

#region Methods

/// <inheritdoc />
public override Boolean Equals(Object obj)
{
var other = obj as CssMedium;
Expand All @@ -80,8 +109,12 @@ public override Boolean Equals(Object obj)
return false;
}

/// <inheritdoc />
public override Int32 GetHashCode() => base.GetHashCode();

/// <summary>
/// Writes the medium as CSS.
/// </summary>
public void ToCss(TextWriter writer, IStyleFormatter formatter)
{
var offset = 0;
Expand Down
40 changes: 40 additions & 0 deletions src/AngleSharp.Css/Dom/Internal/Rules/CssCounterStyleRule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
namespace AngleSharp.Css.Dom
{
using AngleSharp.Css;
using System;
using System.Collections.Generic;
using System.Diagnostics;

/// <summary>
/// Represents the @counter-style rule.
/// </summary>
[DebuggerDisplay(null, Name = "CssCounterStyleRule ({StyleName})")]
sealed class CssCounterStyleRule : CssDeclarationRule, ICssRule
{
#region ctor

internal CssCounterStyleRule(ICssStyleSheet owner)
: base(owner, CssRuleType.CounterStyle, RuleNames.CounterStyle, new HashSet<String>())
{
}

#endregion

#region Properties

/// <summary>
/// Gets or sets the counter style name argument.
/// </summary>
public String StyleName { get; set; }

#endregion

#region Methods

protected override void ReplaceWith(ICssRule rule)
{
}

#endregion
}
}
41 changes: 41 additions & 0 deletions src/AngleSharp.Css/Dom/Internal/Rules/CssFontFeatureValuesRule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace AngleSharp.Css.Dom
{
using AngleSharp.Css;
using System;
using System.Collections.Generic;
using System.Diagnostics;

/// <summary>
/// Represents the @font-feature-values rule.
/// </summary>
[DebuggerDisplay(null, Name = "CssFontFeatureValuesRule ({FamilyName})")]
sealed class CssFontFeatureValuesRule : CssDeclarationRule, ICssRule
{
#region ctor

internal CssFontFeatureValuesRule(ICssStyleSheet owner)
: base(owner, CssRuleType.FontFeatureValues, RuleNames.FontFeatureValues, new HashSet<String>())
{
// character-variant, styleset, stylistic, ornaments, annotation, swash
}

#endregion

#region Properties

/// <summary>
/// Gets or sets the family name argument.
/// </summary>
public String FamilyName { get; set; }

#endregion

#region Methods

protected override void ReplaceWith(ICssRule rule)
{
}

#endregion
}
}
27 changes: 25 additions & 2 deletions src/AngleSharp.Css/Parser/CssBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ private ICssRule CreateAtRule(ICssStyleSheet sheet, CssToken token)
var rule = new CssDocumentRule(sheet);
return CreateDocument(rule, token);
}
else if (token.Data.Is(RuleNames.CounterStyle))
{
var rule = new CssCounterStyleRule(sheet);
return CreateCounterStyle(rule, token);
}
else if (token.Data.Is(RuleNames.FontFeatureValues))
{
var rule = new CssFontFeatureValuesRule(sheet);
return CreateFontFeatureValues(rule, token);
}
else if (_options.IsIncludingUnknownRules)
{
return CreateUnknownAtRule(sheet, token);
Expand Down Expand Up @@ -279,7 +289,7 @@ private CssPageRule CreatePage(CssPageRule rule, CssToken current)
{
rule.SetInvalidSelector(selectorText);
}

CollectTrivia(ref current);

if (current.Type != CssTokenType.CurlyBracketOpen)
Expand Down Expand Up @@ -312,6 +322,19 @@ private CssSupportsRule CreateSupports(CssSupportsRule rule, CssToken current)
return null;
}

private CssFontFeatureValuesRule CreateFontFeatureValues(CssFontFeatureValuesRule rule, CssToken current)
{
CollectTrivia(ref current);
//rule.FamilyName =
return null;
}

private CssCounterStyleRule CreateCounterStyle(CssCounterStyleRule rule, CssToken current)
{
CollectTrivia(ref current);
return null;
}

public CssStyleRule CreateStyle(CssStyleRule rule, CssToken current)
{
CollectTrivia(ref current);
Expand Down Expand Up @@ -556,7 +579,7 @@ private void JumpToRuleEnd(ref CssToken current)
{
scopes--;
}

if (scopes <= 0 && (current.Is(CssTokenType.CurlyBracketClose, CssTokenType.Semicolon)))
{
break;
Expand Down
8 changes: 7 additions & 1 deletion src/AngleSharp.Css/Parser/Micro/CalcParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ namespace AngleSharp.Css.Parser
using System;
using System.Globalization;

static class CalcParser
/// <summary>
/// Represents extensions to for calc values.
/// </summary>
public static class CalcParser
{
/// <summary>
/// Parses a calc value, if any.
/// </summary>
public static CssCalcValue ParseCalc(this StringSource source)
{
var pos = source.Index;
Expand Down
9 changes: 9 additions & 0 deletions src/AngleSharp.Css/Parser/Micro/ColorParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ namespace AngleSharp.Css.Parser
using System.Collections.Generic;
using System.Globalization;

/// <summary>
/// Represents extensions to for color values.
/// </summary>
static class ColorParser
{
private static readonly Dictionary<String, Func<StringSource, Color?>> ColorFunctions = new Dictionary<String, Func<StringSource, Color?>>(StringComparer.OrdinalIgnoreCase)
Expand All @@ -19,6 +22,9 @@ static class ColorParser
{ FunctionNames.Hwba, ParseHwba },
};

/// <summary>
/// Parses a color value, if any.
/// </summary>
public static Color? ParseColor(this StringSource source)
{
var pos = source.Index;
Expand All @@ -32,6 +38,9 @@ static class ColorParser
return result;
}

/// <summary>
/// Parses a the current color value, if any.
/// </summary>
public static Color? ParseCurrentColor(this StringSource source) =>
source.IsIdentifier(CssKeywords.CurrentColor) ? Color.CurrentColor : ColorParser.ParseColor(source);

Expand Down
20 changes: 19 additions & 1 deletion src/AngleSharp.Css/Parser/Micro/CompoundParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ namespace AngleSharp.Css.Parser
using System;
using System.Collections.Generic;

static class CompoundParser
/// <summary>
/// Represents extensions to for general CSS compount values.
/// </summary>
public static class CompoundParser
{
/// <summary>
/// Parse for a CSS quote value.
/// </summary>
public static CssTupleValue ParseQuotes(this StringSource source)
{
var quotes = new List<ICssValue>();
Expand All @@ -30,6 +36,9 @@ public static CssTupleValue ParseQuotes(this StringSource source)
return new CssTupleValue(quotes.ToArray());
}

/// <summary>
/// Parse for a CSS border image slice.
/// </summary>
public static CssBorderImageSliceValue ParseBorderImageSlice(this StringSource source)
{
var lengths = new Length[4];
Expand Down Expand Up @@ -73,6 +82,9 @@ public static CssBorderImageSliceValue ParseBorderImageSlice(this StringSource s
return null;
}

/// <summary>
/// Parse for a CSS background repeat.
/// </summary>
public static CssImageRepeatsValue ParseBackgroundRepeat(this StringSource source)
{
if (source.IsIdentifier(CssKeywords.RepeatX))
Expand Down Expand Up @@ -106,6 +118,9 @@ public static CssImageRepeatsValue ParseBackgroundRepeat(this StringSource sourc
return null;
}

/// <summary>
/// Parse for a CSS image source.
/// </summary>
public static ICssImageValue ParseImageSource(this StringSource source)
{
var url = source.ParseUri();
Expand All @@ -118,6 +133,9 @@ public static ICssImageValue ParseImageSource(this StringSource source)
return url;
}

/// <summary>
/// Parse for a generic periodic [u, y, d, x] value.
/// </summary>
public static CssPeriodicValue<T> ParsePeriodic<T>(this StringSource source, Func<StringSource, T> converter)
where T : ICssValue
{
Expand Down
Loading

0 comments on commit bd83c45

Please sign in to comment.