Skip to content

Commit 5294c40

Browse files
committed
Fixed exponential handling #79
1 parent 715f038 commit 5294c40

File tree

4 files changed

+56
-6
lines changed

4 files changed

+56
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Released on Thursday, November 4 2021.
44

55
- Fixed issue with unbalanced grid areas and rows (#82)
66
- Fixed small numbers to be transformed into negative exponentials (#80)
7+
- Fixed issue handling exponential unit values (#79)
78

89
# 0.16.1
910

src/AngleSharp.Css.Tests/Declarations/CssMarginProperty.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,42 @@ public void CssMarginLeftTinyValue_Issue80()
3030
Assert.AreEqual("0.00001px", property.Value);
3131
}
3232

33+
[Test]
34+
public void CssMarginLeftExponentialValue_Issue79()
35+
{
36+
var snippet = "margin-left: 1E5px";
37+
var property = ParseDeclaration(snippet);
38+
Assert.AreEqual("margin-left", property.Name);
39+
Assert.IsFalse(property.IsImportant);
40+
Assert.IsFalse(property.IsInherited);
41+
Assert.IsTrue(property.HasValue);
42+
Assert.AreEqual("100000px", property.Value);
43+
}
44+
45+
[Test]
46+
public void CssMarginLeftExponentialValueWithPlus_Issue79()
47+
{
48+
var snippet = "margin-left: 1E+5px";
49+
var property = ParseDeclaration(snippet);
50+
Assert.AreEqual("margin-left", property.Name);
51+
Assert.IsFalse(property.IsImportant);
52+
Assert.IsFalse(property.IsInherited);
53+
Assert.IsTrue(property.HasValue);
54+
Assert.AreEqual("100000px", property.Value);
55+
}
56+
57+
[Test]
58+
public void CssMarginLeftExponentialValueWithMinus_Issue79()
59+
{
60+
var snippet = "margin-left: 1E-2px";
61+
var property = ParseDeclaration(snippet);
62+
Assert.AreEqual("margin-left", property.Name);
63+
Assert.IsFalse(property.IsImportant);
64+
Assert.IsFalse(property.IsInherited);
65+
Assert.IsTrue(property.HasValue);
66+
Assert.AreEqual("0.01px", property.Value);
67+
}
68+
3369
[Test]
3470
public void CssMarginLeftInitialLegal()
3571
{

src/AngleSharp.Css/Parser/CssTokenizer.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
namespace AngleSharp.Css.Parser
22
{
33
using AngleSharp.Common;
4-
using AngleSharp.Dom;
54
using AngleSharp.Css.Dom.Events;
65
using AngleSharp.Css.Parser.Tokens;
6+
using AngleSharp.Dom;
77
using AngleSharp.Text;
88
using System;
99
using System.Globalization;
@@ -807,7 +807,7 @@ private CssToken NumberRest()
807807
{
808808
StringBuffer.Append(current);
809809
}
810-
else if (current.IsNameStart())
810+
else if (current != 'e' && current != 'E' && current.IsNameStart())
811811
{
812812
StringBuffer.Append(current);
813813
return Dimension();
@@ -871,7 +871,7 @@ private CssToken NumberFraction()
871871
{
872872
StringBuffer.Append(current);
873873
}
874-
else if (current.IsNameStart())
874+
else if (current != 'e' && current != 'E' && current.IsNameStart())
875875
{
876876
StringBuffer.Append(current);
877877
return Dimension();
@@ -949,6 +949,11 @@ private CssToken SciNotation()
949949
{
950950
StringBuffer.Append(current);
951951
}
952+
else if (current.IsNameStart() || IsValidEscape(current))
953+
{
954+
StringBuffer.Append(current);
955+
return Dimension();
956+
}
952957
else
953958
{
954959
Back();

src/AngleSharp.Css/Parser/Micro/UnitParser.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,9 @@ private static Unit UnitStart(StringSource source)
316316
return null;
317317
}
318318

319+
private static Boolean IsDimension(StringSource source, Char current) =>
320+
current != 'e' && current != 'E' && (current.IsNameStart() || source.IsValidEscape());
321+
319322
private static Unit UnitRest(StringSource source, StringBuilder buffer)
320323
{
321324
var current = source.Next();
@@ -326,7 +329,7 @@ private static Unit UnitRest(StringSource source, StringBuilder buffer)
326329
{
327330
buffer.Append(current);
328331
}
329-
else if (current.IsNameStart() || source.IsValidEscape())
332+
else if (IsDimension(source, current))
330333
{
331334
var number = buffer.ToString();
332335
return Dimension(source, number, buffer.Clear());
@@ -378,7 +381,7 @@ private static Unit UnitFraction(StringSource source, StringBuilder buffer)
378381
{
379382
buffer.Append(current);
380383
}
381-
else if (current.IsNameStart() || source.IsValidEscape())
384+
else if (IsDimension(source, current))
382385
{
383386
var number = buffer.ToString();
384387
return Dimension(source, number, buffer.Clear());
@@ -421,7 +424,7 @@ private static Unit Dimension(StringSource source, String number, StringBuilder
421424
}
422425
else if (source.IsValidEscape())
423426
{
424-
current = source.Next();
427+
source.Next();
425428
buffer.Append(source.ConsumeEscape());
426429
}
427430
else
@@ -443,6 +446,11 @@ private static Unit SciNotation(StringSource source, StringBuilder buffer)
443446
{
444447
buffer.Append(current);
445448
}
449+
else if (current.IsNameStart() || source.IsValidEscape())
450+
{
451+
var number = buffer.ToString();
452+
return Dimension(source, number, buffer.Clear());
453+
}
446454
else
447455
{
448456
return new Unit(buffer.ToPool(), String.Empty);

0 commit comments

Comments
 (0)