Skip to content

Commit d94f8f8

Browse files
committed
Fixed issue with longer data URIs #76
1 parent 8834af6 commit d94f8f8

File tree

5 files changed

+53
-1
lines changed

5 files changed

+53
-1
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
namespace AngleSharp.Css.Tests.Declarations
22
{
33
using AngleSharp.Css.Dom;
4+
using AngleSharp.Css.Parser;
5+
using AngleSharp.Html.Parser;
46
using NUnit.Framework;
57
using static CssConstructionFunctions;
68

@@ -689,5 +691,29 @@ public void CssFontStyleNumericWeightSizeFamiliesLegal()
689691
Assert.IsTrue(property.HasValue);
690692
Assert.AreEqual("400 12px Georgia, serif", property.Value);
691693
}
694+
695+
[Test]
696+
public void LongDataUrisShouldNotBeDisappearing_Issue76()
697+
{
698+
var url = "data-uri.txt".LoadFromResources();
699+
var html = $@"<style>@font-face {{
700+
font-family: ""MyFont"";
701+
src: url(""{url}"") format('woff');
702+
font-weight: normal;
703+
font-style: normal;
704+
font-display: swap;
705+
}}</style>";
706+
707+
var parser = new HtmlParser(new HtmlParserOptions(), BrowsingContext.New(Configuration.Default.WithCss(new CssParserOptions
708+
{
709+
IsIncludingUnknownDeclarations = true,
710+
IsIncludingUnknownRules = true,
711+
IsToleratingInvalidSelectors = true,
712+
})));
713+
714+
var dom = parser.ParseDocument(html);
715+
var fontFace = ((ICssStyleSheet)dom.StyleSheets[0]).Rules[0] as ICssFontFaceRule;
716+
Assert.IsNotEmpty(fontFace.Source);
717+
}
692718
}
693719
}

src/AngleSharp.Css.Tests/Resources/data-uri.txt

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

src/AngleSharp.Css.Tests/TestExtensions.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ namespace AngleSharp.Css.Tests
44
using AngleSharp.Dom;
55
using AngleSharp.Html.Parser;
66
using AngleSharp.Io;
7+
using AngleSharp.Text;
78
using NUnit.Framework;
89
using System;
10+
using System.IO;
11+
using System.Reflection;
912

1013
static class TestExtensions
1114
{
@@ -43,5 +46,25 @@ public static IDocument ToHtmlDocument(this String sourceCode, IConfiguration co
4346
var htmlParser = context.GetService<IHtmlParser>();
4447
return htmlParser.ParseDocument(sourceCode);
4548
}
49+
50+
private static String GetResourceDirectory()
51+
{
52+
var baseDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
53+
var path = new DirectoryInfo(baseDir);
54+
55+
while (!path.Name.Is("AngleSharp.Css.Tests"))
56+
{
57+
path = path.Parent;
58+
}
59+
60+
return Path.Combine(path.FullName, "Resources");
61+
}
62+
63+
public static String LoadFromResources(this String fileName)
64+
{
65+
var directory = GetResourceDirectory();
66+
var path = Path.Combine(directory, fileName);
67+
return File.ReadAllText(path);
68+
}
4669
}
4770
}

src/AngleSharp.Css/Converters/ListValueConverter.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ public ICssValue Convert(StringSource source)
2121

2222
while (!source.IsDone)
2323
{
24-
var index = source.Index;
2524
var value = _converter.Convert(source);
2625
var current = source.SkipSpacesAndComments();
2726

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public static CssAttrValue ParseAttr(this StringSource source)
3030
public static CssReferenceValue ParseVars(this StringSource source)
3131
{
3232
var index = source.Index;
33+
var start = index;
3334
var length = FunctionNames.Var.Length;
3435
var refs = default(List<Tuple<TextRange, CssVarValue>>);
3536

@@ -68,6 +69,8 @@ public static CssReferenceValue ParseVars(this StringSource source)
6869
break;
6970
}
7071

72+
source.BackTo(start);
73+
7174
if (refs != null)
7275
{
7376
return new CssReferenceValue(source.Content, refs);

0 commit comments

Comments
 (0)