Skip to content

Commit

Permalink
Int parsing modified; code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerBrinks committed Jan 21, 2024
1 parent 370dcae commit 60af5b8
Show file tree
Hide file tree
Showing 27 changed files with 300 additions and 325 deletions.
2 changes: 1 addition & 1 deletion license.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) [year] [fullname]
Copyright (c) 2024 Tyler Brinks

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion src/ExCSS.Tests/ExCSS.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<AssemblyName>ExCSS.Tests</AssemblyName>
<PackageId>ExCSS.Tests</PackageId>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
Expand Down
6 changes: 5 additions & 1 deletion src/ExCSS.Tests/Sheet.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Threading;

namespace ExCSS.Tests
{
Expand Down Expand Up @@ -1261,5 +1260,10 @@ public void ShouldBeAbleToPreserveDuplicateProperties()
Assert.Equal("some-invalid-color", props[1].Value);
}

[Fact]
public void Parse_ZIndex_Out_Of_Range()
{
var sheet = ParseStyleSheet(".style{ z-index: 99999999999999999;}");
}
}
}
2 changes: 1 addition & 1 deletion src/ExCSS/ExCSS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<LangVersion>9.0</LangVersion>
<TargetFrameworks>net7.0;net6.0;netcoreapp3.1;net48;netstandard2.1;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net8.0;net7.0;net6.0;netcoreapp3.1;net48;netstandard2.1;netstandard2.0</TargetFrameworks>
<AssemblyName>ExCSS</AssemblyName>
<PackageId>Anateus.ExCSS</PackageId>
<Title>ExCSS .NET Stylesheet Parser</Title>
Expand Down
23 changes: 11 additions & 12 deletions src/ExCSS/Extensions/CharExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ public static bool IsNormalPathCharacter(this char c)

public static bool IsUppercaseAscii(this char c)
{
return c >= Symbols.CapitalA && c <= Symbols.CapitalZ;
return c is >= Symbols.CapitalA and <= Symbols.CapitalZ;
}

public static bool IsLowercaseAscii(this char c)
{
return c >= Symbols.LowerA && c <= Symbols.LowerZ;
return c is >= Symbols.LowerA and <= Symbols.LowerZ;
}

public static bool IsAlphanumericAscii(this char c)
Expand All @@ -54,8 +54,8 @@ public static bool IsAlphanumericAscii(this char c)

public static bool IsHex(this char c)
{
return c.IsDigit() || c >= Symbols.CapitalA && c <= Symbols.CapitalF ||
c >= Symbols.LowerA && c <= Symbols.LowerF;
return c.IsDigit() || c is >= Symbols.CapitalA and <= Symbols.CapitalF ||
c is >= Symbols.LowerA and <= Symbols.LowerF;
}

public static bool IsNonAscii(this char c)
Expand All @@ -65,9 +65,9 @@ public static bool IsNonAscii(this char c)

public static bool IsNonPrintable(this char c)
{
return c >= Symbols.Null && c <= Symbols.Backspace ||
c >= Symbols.ShiftOut && c <= Symbols.UnitSeparator ||
c >= Symbols.Delete && c < Symbols.NonBreakingSpace;
return c is /*>= Symbols.Null and*/ <= Symbols.Backspace
or >= Symbols.ShiftOut and <= Symbols.UnitSeparator
or >= Symbols.Delete and < Symbols.NonBreakingSpace;
}

public static bool IsLetter(this char c)
Expand All @@ -87,18 +87,17 @@ public static bool IsNameStart(this char c)

public static bool IsLineBreak(this char c)
{
return c == Symbols.LineFeed || c == Symbols.CarriageReturn;
return c is Symbols.LineFeed or Symbols.CarriageReturn;
}

public static bool IsSpaceCharacter(this char c)
{
return c == Symbols.Space || c == Symbols.Tab || c == Symbols.LineFeed ||
c == Symbols.CarriageReturn || c == Symbols.FormFeed;
return c is Symbols.Space or Symbols.Tab or Symbols.LineFeed or Symbols.CarriageReturn or Symbols.FormFeed;
}

public static bool IsDigit(this char c)
{
return c >= Symbols.Zero && c <= Symbols.Nine;
return c is >= Symbols.Zero and <= Symbols.Nine;
}

// HTML forbids the use of Universal Character Set / Unicode code points
Expand All @@ -110,7 +109,7 @@ public static bool IsDigit(this char c)
public static bool IsInvalid(this int c)
{
return c == 0 || c > Symbols.MaximumCodepoint ||
c > Symbols.UTF16SurrogateMin && c < Symbols.UTF16SurrogateMax;
c is > Symbols.UTF16SurrogateMin and < Symbols.UTF16SurrogateMax;
}

public static bool IsOneOf(this char c, char a, char b)
Expand Down
2 changes: 0 additions & 2 deletions src/ExCSS/Extensions/PortableExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;

#if !NET40 && !SL50

Expand Down
8 changes: 4 additions & 4 deletions src/ExCSS/Factories/AttributeSelectorFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@ public IAttrSelector Create(string combinator, string match, string value, strin

if (!string.IsNullOrEmpty(prefix))
{
name = FormFront(prefix, match);
_ = FormMatch(prefix, match);
name = AttributeSelectorFactory.FormFront(prefix, match);
_ = AttributeSelectorFactory.FormMatch(prefix, match);
}

return _types.TryGetValue(combinator, out var type)
? (IAttrSelector)Activator.CreateInstance(type, name, value)
: new AttrAvailableSelector(name, value);
}

private string FormFront(string prefix, string match)
private static string FormFront(string prefix, string match)
{
return string.Concat(prefix, Combinators.Pipe, match);
}

private string FormMatch(string prefix, string match)
private static string FormMatch(string prefix, string match)
{
return prefix.Is(Keywords.Asterisk) ? match : string.Concat(prefix, PseudoClassNames.Separator, match);
}
Expand Down
8 changes: 4 additions & 4 deletions src/ExCSS/Factories/PropertyFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -398,14 +398,14 @@ public bool IsAnimatable(string name)
{
return _longhands.ContainsKey(name)
? _animatables.Contains(name)
: GetLonghands(name).Any(longhand => _animatables.Contains(name));
: GetLonghands(name).Any(_ => _animatables.Contains(name));
}

public string[] GetLonghands(string name)
{
return _mappings.ContainsKey(name)
? _mappings[name]
: new string[0];
return _mappings.TryGetValue(name, out var mapping)
? mapping
: Array.Empty<string>();
}

public IEnumerable<string> GetShorthands(string name)
Expand Down
2 changes: 1 addition & 1 deletion src/ExCSS/MediaFeatures/MediaFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal MediaFeature(string name)

public string Value => HasValue ? _tokenValue.Text : string.Empty;

public bool HasValue => _tokenValue != null && _tokenValue.Count > 0;
public bool HasValue => _tokenValue is {Count: > 0};

public override void ToCss(TextWriter writer, IStyleFormatter formatter)
{
Expand Down
12 changes: 6 additions & 6 deletions src/ExCSS/Model/Converters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,8 @@ public static readonly IValueConverter

public static readonly IValueConverter OptionalNumberConverter = NumberConverter.OrNone();

public static readonly IValueConverter LengthOrNormalConverter =
LengthConverter.Or(Keywords.Normal, new Length(1f, Length.Unit.Em));
//public static readonly IValueConverter LengthOrNormalConverter =
// LengthConverter.Or(Keywords.Normal, new Length(1f, Length.Unit.Em));

public static readonly IValueConverter OptionalLengthConverter = LengthConverter.Or(Keywords.Normal);
public static readonly IValueConverter AutoLengthConverter = LengthConverter.OrAuto();
Expand Down Expand Up @@ -460,10 +460,10 @@ public static IValueConverter Toggle(string on, string off)
return Assign(on, true).Or(off, false);
}

public static IValueConverter WithFallback<T>(T fallbackValue) where T : struct, IFormattable
{
return new StructValueConverter<T>(_ => fallbackValue);
}
//public static IValueConverter WithFallback<T>(T fallbackValue) where T : struct, IFormattable
//{
// return new StructValueConverter<T>(_ => fallbackValue);
//}

#endregion

Expand Down
6 changes: 3 additions & 3 deletions src/ExCSS/Model/ParserExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ private static IConditionFunction CreateOrCondition(IEnumerable<IConditionFuncti

public static TokenType GetTypeFromName(this string functionName)
{
return FunctionTypes.TryGetValue(functionName, out Func<string, DocumentFunction> creator)
return FunctionTypes.TryGetValue(functionName, out _)
? TokenType.Url
: TokenType.Function;
}

public static Func<IEnumerable<IConditionFunction>, IConditionFunction> GetCreator(this string conjunction)
{
GroupCreators.TryGetValue(conjunction, out Func<IEnumerable<IConditionFunction>, IConditionFunction> creator);
GroupCreators.TryGetValue(conjunction, out var creator);
return creator;
}

Expand Down Expand Up @@ -101,7 +101,7 @@ public static DocumentFunction ToDocumentFunction(this Token token)
case TokenType.Url:
{
var functionName = ((UrlToken)token).FunctionName;
FunctionTypes.TryGetValue(functionName, out Func<string, DocumentFunction> creator);
FunctionTypes.TryGetValue(functionName, out var creator);
return creator(token.Data);
}
case TokenType.Function when token.Data.Isi(FunctionNames.Regexp):
Expand Down
33 changes: 18 additions & 15 deletions src/ExCSS/Model/Priority.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ namespace ExCSS
[StructLayout(LayoutKind.Explicit, Pack = 1, CharSet = CharSet.Unicode)]
public struct Priority : IEquatable<Priority>, IComparable<Priority>
{
[FieldOffset(0)] private readonly byte _tags;
[FieldOffset(1)] private readonly byte _classes;
[FieldOffset(2)] private readonly byte _ids;
[FieldOffset(3)] private readonly byte _inlines;
[FieldOffset(0)] private readonly uint _priority;

public static readonly Priority Zero = new (0u);
Expand All @@ -20,23 +16,30 @@ public struct Priority : IEquatable<Priority>, IComparable<Priority>

public Priority(uint priority)
{
_inlines = _ids = _classes = _tags = 0;
Inlines = Ids = Classes = Tags = 0;
_priority = priority;
}

public Priority(byte inlines, byte ids, byte classes, byte tags)
{
_priority = 0;
_inlines = inlines;
_ids = ids;
_classes = classes;
_tags = tags;
Inlines = inlines;
Ids = ids;
Classes = classes;
Tags = tags;
}

public byte Ids => _ids;
public byte Tags => _tags;
public byte Classes => _classes;
public byte Inlines => _inlines;
[field: FieldOffset(2)]
public byte Ids { get; }

[field: FieldOffset(0)]
public byte Tags { get; }

[field: FieldOffset(1)]
public byte Classes { get; }

[field: FieldOffset(3)]
public byte Inlines { get; }

public static Priority operator +(Priority a, Priority b)
{
Expand Down Expand Up @@ -79,7 +82,7 @@ public bool Equals(Priority other)

public override bool Equals(object obj)
{
return obj is Priority && Equals((Priority) obj);
return obj is Priority priority && Equals(priority);
}

public override int GetHashCode()
Expand All @@ -94,7 +97,7 @@ public int CompareTo(Priority other)

public override string ToString()
{
return $"({_inlines}, {_ids}, {_classes}, {_tags})";
return $"({Inlines}, {Ids}, {Classes}, {Tags})";
}
}
}
6 changes: 3 additions & 3 deletions src/ExCSS/Model/StyleDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private void RemovePropertyByName(string propertyName)
public string GetPropertyPriority(string propertyName)
{
var property = GetProperty(propertyName);
if (property != null && property.IsImportant) return Keywords.Important;
if (property is {IsImportant: true}) return Keywords.Important;
if (!IsStrictMode || !PropertyFactory.Instance.IsShorthand(propertyName)) return string.Empty;

var longhands = PropertyFactory.Instance.GetLonghands(propertyName);
Expand Down Expand Up @@ -225,12 +225,12 @@ internal void SetProperty(Property property)

internal void SetDeclarations(IEnumerable<Property> declarations)
{
ChangeDeclarations(declarations, m => false, (o, n) => !o.IsImportant || n.IsImportant);
ChangeDeclarations(declarations, _ => false, (o, n) => !o.IsImportant || n.IsImportant);
}

internal void UpdateDeclarations(IEnumerable<Property> declarations)
{
ChangeDeclarations(declarations, m => !m.CanBeInherited, (o, n) => o.IsInherited);
ChangeDeclarations(declarations, m => !m.CanBeInherited, (o, _) => o.IsInherited);
}

private void ChangeDeclarations(IEnumerable<Property> declarations, Predicate<Property> defaultSkip,
Expand Down
8 changes: 1 addition & 7 deletions src/ExCSS/Model/StylesheetNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@ namespace ExCSS
{
public abstract class StylesheetNode : IStylesheetNode
{
private readonly List<IStylesheetNode> _children;

protected StylesheetNode()
{
_children = new List<IStylesheetNode>();
StylesheetText = null;
}
private readonly List<IStylesheetNode> _children = new();

protected void ReplaceAll(IStylesheetNode node)
{
Expand Down
10 changes: 5 additions & 5 deletions src/ExCSS/Model/Symbols.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ namespace ExCSS

internal static class Symbols
{
public static readonly string[] NewLines = { "\r\n", "\r", "\n" };
//public static readonly string[] NewLines = { "\r\n", "\r", "\n" };

public const char StartOfHeading = (char)0x01; // Non-printable SOH
public const char Backspace = (char)0x08; // Non-printable BS
public const char UnitSeparator = (char)0x1F; // Non-printable US
public const char ShiftOut = (char)0x0E; // Non-printable SO
public const char ShiftIn = (char)0x0F; // Non-printable SI
//public const char ShiftIn = (char)0x0F; // Non-printable SI

public const char Zero = (char)0x30; // 0
public const char Seven = (char)0x37; // 7
Expand All @@ -31,7 +31,7 @@ internal static class Symbols
public const char Tilde = (char) 0x7e; // ~
public const char Pipe = (char) 0x7c; // |
public const char Null = (char) 0x0;
public const char Ampersand = (char) 0x26; // &amp
//public const char Ampersand = (char) 0x26; // &amp
public const char Num = (char) 0x23; // #
public const char Dollar = (char) 0x24; // $
public const char Semicolon = (char) 0x3b; // ;
Expand All @@ -55,7 +55,7 @@ internal static class Symbols
public const char FormFeed = (char) 0x0c; // form feed
public const char Space = (char) 0x20; // space
public const char Solidus = (char) 0x2f; // solidus /
public const char NoBreakSpace = (char) 0xa0; // no breaking space
//public const char NoBreakSpace = (char) 0xa0; // no breaking space
public const char ReverseSolidus = (char) 0x5c; // reverse solidus \
public const char Colon = (char) 0x3a; // :
public const char ExclamationMark = (char) 0x21; // !
Expand All @@ -73,7 +73,7 @@ internal static class Symbols
public const char NonBreakingSpace = (char)0xA0; // 160 start of defined extended set
public const char UTF16SurrogateMin = (char)0xD800; // 55296
public const char UTF16SurrogateMax = (char)0xDFFF; // 57343
public static Dictionary<char, char> Punycode = new Dictionary<char, char>
public static Dictionary<char, char> Punycode = new()
{
{'。', '.'},
{'.', '.'},
Expand Down
10 changes: 8 additions & 2 deletions src/ExCSS/Parser/Lexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -710,8 +710,14 @@ private Token UrlDoubleQuote(string functionName)
return UrlBad(functionName);
}

if (Symbols.EndOfFile == current) return NewUrl(functionName, FlushBuffer());
if (current == Symbols.DoubleQuote) return UrlEnd(functionName);
switch (current)
{
case Symbols.EndOfFile:
return NewUrl(functionName, FlushBuffer());
case Symbols.DoubleQuote:
return UrlEnd(functionName);
}

if (current != Symbols.ReverseSolidus)
{
StringBuffer.Append(current);
Expand Down
Loading

0 comments on commit 60af5b8

Please sign in to comment.