Skip to content

Commit edd46ba

Browse files
authored
Merge pull request #70 from meziantou/issue69
Add support for min-content, max-content and fit-content
2 parents 8f959a7 + 018ff38 commit edd46ba

File tree

6 files changed

+91
-1
lines changed

6 files changed

+91
-1
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
namespace AngleSharp.Css.Tests.Declarations
2+
{
3+
using NUnit.Framework;
4+
using static CssConstructionFunctions;
5+
6+
[TestFixture]
7+
public class CssWidthPropertyTests
8+
{
9+
[Test]
10+
public void CssWidthPropertyAutoLegal()
11+
{
12+
var snippet = "width: auto";
13+
var property = ParseDeclaration(snippet);
14+
Assert.AreEqual("width", property.Name);
15+
Assert.IsFalse(property.IsImportant);
16+
Assert.IsFalse(property.IsInherited);
17+
Assert.IsTrue(property.HasValue);
18+
Assert.AreEqual("auto", property.Value);
19+
}
20+
21+
[Test]
22+
public void CssWidthPropertyFitContentLegal()
23+
{
24+
var snippet = "width: fit-content";
25+
var property = ParseDeclaration(snippet);
26+
Assert.AreEqual("width", property.Name);
27+
Assert.IsFalse(property.IsImportant);
28+
Assert.IsFalse(property.IsInherited);
29+
Assert.IsTrue(property.HasValue);
30+
Assert.AreEqual("fit-content", property.Value);
31+
}
32+
33+
[Test]
34+
public void CssWidthPropertyValueInPxLegal()
35+
{
36+
var snippet = "width: 42px";
37+
var property = ParseDeclaration(snippet);
38+
Assert.AreEqual("width", property.Name);
39+
Assert.IsFalse(property.IsImportant);
40+
Assert.IsFalse(property.IsInherited);
41+
Assert.IsTrue(property.HasValue);
42+
Assert.AreEqual("42px", property.Value);
43+
}
44+
}
45+
}

src/AngleSharp.Css/Constants/CssKeywords.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,5 +1566,10 @@ public static class CssKeywords
15661566
/// The closed keyword.
15671567
/// </summary>
15681568
public static readonly String Closed = "closed";
1569+
1570+
/// <summary>
1571+
/// The fit-content keyword.
1572+
/// </summary>
1573+
public static readonly String FitContent = "fit-content";
15691574
}
15701575
}

src/AngleSharp.Css/Constants/Map.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,5 +883,15 @@ static class Map
883883
{ CssKeywords.Compact, FootnoteDisplay.Compact },
884884
{ CssKeywords.Inline, FootnoteDisplay.Inline },
885885
};
886+
887+
/// <summary>
888+
/// Contains the string-length mapping.
889+
/// </summary>
890+
public static readonly Dictionary<String, Sizing> Sizings = new Dictionary<String, Width>(StringComparer.OrdinalIgnoreCase)
891+
{
892+
{ CssKeywords.FitContent, Sizing.FitContent },
893+
{ CssKeywords.MinContent, Sizing.MinContent},
894+
{ CssKeywords.MaxContent, Sizing.MaxContent },
895+
};
886896
}
887897
}

src/AngleSharp.Css/Declarations/WidthDeclaration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ static class WidthDeclaration
88
{
99
public static String Name = PropertyNames.Width;
1010

11-
public static IValueConverter Converter = AutoLengthOrPercentConverter;
11+
public static IValueConverter Converter = WidthConverter;
1212

1313
public static ICssValue InitialValue = InitialValues.WidthDecl;
1414

src/AngleSharp.Css/Dom/Sizing.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace AngleSharp.Css.Dom
2+
{
3+
/// <summary>
4+
/// Represents the selected Width display mode.
5+
/// </summary>
6+
public enum Sizing
7+
{
8+
/// <summary>
9+
/// Essentially fit-content(stretch) i.e. min(max-content, max(min-content, stretch)).
10+
/// <seealso href="https://drafts.csswg.org/css-sizing-4/#valdef-width-fit-content"/>
11+
/// </summary>
12+
FitContent,
13+
/// <summary>
14+
/// The intrinsic preferred width.
15+
/// </summary>
16+
MaxContent,
17+
/// <summary>
18+
/// The intrinsic minimum width.
19+
/// </summary>
20+
MinContent,
21+
}
22+
}

src/AngleSharp.Css/ValueConverters.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,14 @@ static class ValueConverters
612612
public static readonly IValueConverter AutoLengthOrPercentConverter = Or(
613613
LengthOrPercentConverter,
614614
Auto);
615+
616+
/// <summary>
617+
/// Represents a value for a width.
618+
/// </summary>
619+
public static readonly IValueConverter WidthConverter = Or(
620+
LengthOrPercentConverter,
621+
Auto,
622+
Map.Sizings.ToConverter());
615623

616624
/// <summary>
617625
/// Represents a length for a font size.

0 commit comments

Comments
 (0)