Skip to content

Commit

Permalink
Fixed issue with longer data URIs #76
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Aug 10, 2021
1 parent 8834af6 commit d94f8f8
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 1 deletion.
26 changes: 26 additions & 0 deletions src/AngleSharp.Css.Tests/Declarations/CssFontProperty.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace AngleSharp.Css.Tests.Declarations
{
using AngleSharp.Css.Dom;
using AngleSharp.Css.Parser;
using AngleSharp.Html.Parser;
using NUnit.Framework;
using static CssConstructionFunctions;

Expand Down Expand Up @@ -689,5 +691,29 @@ public void CssFontStyleNumericWeightSizeFamiliesLegal()
Assert.IsTrue(property.HasValue);
Assert.AreEqual("400 12px Georgia, serif", property.Value);
}

[Test]
public void LongDataUrisShouldNotBeDisappearing_Issue76()
{
var url = "data-uri.txt".LoadFromResources();
var html = $@"<style>@font-face {{
font-family: ""MyFont"";
src: url(""{url}"") format('woff');
font-weight: normal;
font-style: normal;
font-display: swap;
}}</style>";

var parser = new HtmlParser(new HtmlParserOptions(), BrowsingContext.New(Configuration.Default.WithCss(new CssParserOptions
{
IsIncludingUnknownDeclarations = true,
IsIncludingUnknownRules = true,
IsToleratingInvalidSelectors = true,
})));

var dom = parser.ParseDocument(html);
var fontFace = ((ICssStyleSheet)dom.StyleSheets[0]).Rules[0] as ICssFontFaceRule;
Assert.IsNotEmpty(fontFace.Source);
}
}
}
1 change: 1 addition & 0 deletions src/AngleSharp.Css.Tests/Resources/data-uri.txt

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions src/AngleSharp.Css.Tests/TestExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ namespace AngleSharp.Css.Tests
using AngleSharp.Dom;
using AngleSharp.Html.Parser;
using AngleSharp.Io;
using AngleSharp.Text;
using NUnit.Framework;
using System;
using System.IO;
using System.Reflection;

static class TestExtensions
{
Expand Down Expand Up @@ -43,5 +46,25 @@ public static IDocument ToHtmlDocument(this String sourceCode, IConfiguration co
var htmlParser = context.GetService<IHtmlParser>();
return htmlParser.ParseDocument(sourceCode);
}

private static String GetResourceDirectory()
{
var baseDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var path = new DirectoryInfo(baseDir);

while (!path.Name.Is("AngleSharp.Css.Tests"))
{
path = path.Parent;
}

return Path.Combine(path.FullName, "Resources");
}

public static String LoadFromResources(this String fileName)
{
var directory = GetResourceDirectory();
var path = Path.Combine(directory, fileName);
return File.ReadAllText(path);
}
}
}
1 change: 0 additions & 1 deletion src/AngleSharp.Css/Converters/ListValueConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public ICssValue Convert(StringSource source)

while (!source.IsDone)
{
var index = source.Index;
var value = _converter.Convert(source);
var current = source.SkipSpacesAndComments();

Expand Down
3 changes: 3 additions & 0 deletions src/AngleSharp.Css/Parser/Micro/FunctionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public static CssAttrValue ParseAttr(this StringSource source)
public static CssReferenceValue ParseVars(this StringSource source)
{
var index = source.Index;
var start = index;
var length = FunctionNames.Var.Length;
var refs = default(List<Tuple<TextRange, CssVarValue>>);

Expand Down Expand Up @@ -68,6 +69,8 @@ public static CssReferenceValue ParseVars(this StringSource source)
break;
}

source.BackTo(start);

if (refs != null)
{
return new CssReferenceValue(source.Content, refs);
Expand Down

0 comments on commit d94f8f8

Please sign in to comment.