Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ Released on tbd.
- Fixed the background showing through where two border sides of different colors meet on a diagonal
- Fixed `data:` images whose base64 is percent-encoded (e.g. `%2F` for `/`) not being shown
- Added `object` elements showing an image instead of their fallback content
- Fixed an `object` with data that failed to load (e.g. a 404) showing it instead of its fallback content
- Fixed an `object` showing a page (e.g. `text/html`) showing its fallback content; it is an empty box of the size of an iframe
- Fixed the document of an empty iframe (e.g. 0 by 0) keeping the viewport of 300 by 150
- Updated the document of an iframe to keep the render device of its browsing context, if it has one of its own

# 0.12.0

Expand Down
109 changes: 109 additions & 0 deletions src/AngleSharp.Renderer.Tests/Acid1ConformanceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
namespace AngleSharp.Renderer.Tests;

using AngleSharp;
using AngleSharp.Css;
using AngleSharp.Dom;
using AngleSharp.Renderer.Rendering;

/// <summary>
/// The Acid1 test (<c>cases/test15-acid1.html</c>, the CSS1 test "display/box/float/clear"):
/// every box above the paragraph has a size fixed in ems, so the reference rendering
/// (sec5526c.gif) pins each of them to the pixel.
/// </summary>
[Trait("Category", "Visual")]
public sealed class Acid1ConformanceTests
{
private const int ViewportWidth = 960;
private const int ViewportHeight = 1420;

[Fact]
public async Task Acid1_LaysOutTheBoxesOfTheReferenceRendering()
{
var document = await OpenAsync();

// html at 10px: body has a 15px margin and a 5px border, the dl a 5px padding
var body = Box(document, "body");
Assert.Equal(15f, body.X, precision: 1);
Assert.Equal(15f, body.Y, precision: 1);
Assert.Equal(490f, body.Width, precision: 1);
AssertBox(document, "dt", 25f, 25f, 80f, 310f);
AssertBox(document, "dd", 115f, 25f, 380f, 310f);
AssertBox(document, "li:nth-child(1)", 135f, 45f, 80f, 120f);
AssertBox(document, "#bar", 225f, 45f, 160f, 110f);
AssertBox(document, "li:nth-child(3)", 395f, 45f, 80f, 120f);
// below "i grow old" (CSS 1 float rule 2), not beside it under #bar
AssertBox(document, "#baz", 135f, 175f, 120f, 120f);
AssertBox(document, "blockquote", 275f, 175f, 70f, 140f);
AssertBox(document, "h1", 355f, 175f, 120f, 120f);

// the paragraph clears both floats
var paragraph = Box(document, "body > p");
Assert.Equal(20f, paragraph.X, precision: 1);
Assert.Equal(335f, paragraph.Y, precision: 1);
Assert.Equal(480f, paragraph.Width, precision: 1);
}

[Fact]
public async Task Acid1_MatchesTheSnapshot()
{
var document = await OpenAsync();
var image = new HtmlRenderer().RenderToPng(document, CreateDevice());

VisualSnapshotVerifier.VerifyOrCreate(
snapshotName: "acid1.png",
actualPng: image.Data,
perChannelTolerance: 12,
maxDifferentPixels: 20);
}

private static void AssertBox(IDocument document, string selector, float x, float y, float width, float height)
{
var box = Box(document, selector);
Assert.Equal(x, box.X, precision: 1);
Assert.Equal(y, box.Y, precision: 1);
Assert.Equal(width, box.Width, precision: 1);
Assert.Equal(height, box.Height, precision: 1);
}

private static RenderRect Box(IDocument document, string selector)
{
var rect = document.QuerySelector(selector)!.GetBoundingClientRect();
return new RenderRect((float)rect.Left, (float)rect.Top, (float)rect.Width, (float)rect.Height);
}

private static DefaultRenderDevice CreateDevice() => new()
{
ViewPortWidth = ViewportWidth,
ViewPortHeight = ViewportHeight,
FontSize = 16f,
};

private static async Task<IDocument> OpenAsync()
{
var html = await File.ReadAllTextAsync(Path.Combine(FindProjectRoot(), "cases", "test15-acid1.html"));
// Verdana is installed on some hosts only; snapshots use the bundled fonts
html = html.Replace("Verdana, sans-serif", "sans-serif", StringComparison.Ordinal);

var context = BrowsingContext.New(Configuration.Default.WithCss().WithRenderDevice(CreateDevice()));
var document = await context.OpenAsync(request => request.Content(html));
context.GetDomHarness();
return document;
}

private static string FindProjectRoot()
{
var directory = new DirectoryInfo(AppContext.BaseDirectory);

while (directory is not null)
{
if (File.Exists(Path.Combine(directory.FullName, "AngleSharp.Renderer.Tests.csproj")))
{
return directory.FullName;
}

directory = directory.Parent;
}

throw new InvalidOperationException("Could not locate AngleSharp.Renderer.Tests project root.");
}
}
101 changes: 101 additions & 0 deletions src/AngleSharp.Renderer.Tests/Acid3RegressionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
namespace AngleSharp.Renderer.Tests;

using System.Net;
using AngleSharp;
using AngleSharp.Css;
using AngleSharp.Dom;
using AngleSharp.Io;
using AngleSharp.Renderer;
using AngleSharp.Renderer.Rendering;

/// <summary>
/// Reduced versions of what the Acid3 test (acid3.acidtests.org) showed to differ from browsers.
/// </summary>
public sealed class Acid3RegressionTests
{
private static readonly DefaultRenderDevice Device = new() { ViewPortWidth = 800, ViewPortHeight = 600, FontSize = 16 };

// a 1x1 yellow PNG
private static readonly byte[] YellowPixel = Convert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR42mP4/58BAAT/Af9jgNErAAAAAElFTkSuQmCC");

[Fact]
public async Task ObjectWithADocument_HidesItsFallback()
{
// test 16: a 404 (even of an image) shows the fallback, a page is shown instead of it
var document = await OpenAsync("""
<object id="a" data="http://localhost/missing.png"><object id="b" data="http://localhost/page.png"><object id="c" data="http://localhost/c.png">FAIL</object></object></object>
""");

Assert.Equal(300f, Rect(document, "b").Width, precision: 1);
Assert.Equal(150f, Rect(document, "b").Height, precision: 1);
Assert.DoesNotContain(new HtmlRenderer().BuildDisplayList(document, Device).Commands, command => command.GetType().Name.Contains("Text"));
}

[Fact]
public async Task ObjectWithAMissingImage_ShowsItsFallback()
{
var document = await OpenAsync("""
<object id="a" data="http://localhost/missing.png"><span id="fallback">fallback</span></object>
""");

Assert.True(Rect(document, "fallback").Width > 0f);
}

private static RenderRect Rect(IDocument document, string id)
{
var rect = document.GetElementById(id)!.GetBoundingClientRect();
return new RenderRect((float)rect.Left, (float)rect.Top, (float)rect.Width, (float)rect.Height);
}

private static async Task<IDocument> OpenAsync(string body)
{
var context = BrowsingContext.New(Configuration.Default
.WithCss()
.WithRenderDevice(Device)
.WithRendering()
.WithDefaultLoader(new LoaderOptions { IsResourceLoadingEnabled = true })
.With(new SiteRequester()));
var document = await context.OpenAsync(request => request.Address("http://localhost/").Content($$"""
<html><head><style>html, body { margin: 0; padding: 0; font-family: sans-serif }</style></head>
<body>{{body}}</body></html>
"""));

// the objects show what they show once their data is there
foreach (var element in document.QuerySelectorAll("object").OfType<ILoadableElement>())
{
if (element.CurrentDownload?.Task is { } task)
{
await task;
}
}

await Task.Delay(50);
context.GetDomHarness();
return document;
}

// what Acid3's server answers
private sealed class SiteRequester : BaseRequester
{
public override bool SupportsProtocol(string protocol) =>
string.Equals(protocol, "http", StringComparison.OrdinalIgnoreCase);

protected override Task<IResponse?> PerformRequestAsync(Request request, CancellationToken cancel)
{
var (status, type, content) = request.Address.Path switch
{
"missing.png" => (HttpStatusCode.NotFound, "image/png", YellowPixel),
"page.png" => (HttpStatusCode.OK, "text/html; charset=utf-8", "<!DOCTYPE html><title>FAIL</title><p><!-- transparent -->"u8.ToArray()),
_ => (HttpStatusCode.OK, "image/png", YellowPixel),
};
var response = new DefaultResponse
{
Address = request.Address,
StatusCode = status,
Content = new MemoryStream(content),
};
response.Headers[HeaderNames.ContentType] = type;
return Task.FromResult<IResponse?>(response);
}
}
}
Loading
Loading