using AngleSharp.Dom;
using AngleSharp.Html.Parser;
namespace XFESystem.Browser.Tests;
public sealed class PseudoEarlyReturnTests
{
private static IDocument Page(string body, string css) => new HtmlParser().ParseDocument("" + body);
[Fact]
public void UnmatchedPseudoDoesNotInheritAnAncestorsGeneratedContent()
{
var page = Page("", "#Host{--ink:#123456;--word:'Ancestor';color:var(--ink)}#Host::before{content:var(--word)}.unrelated::before{content:'Wrong'}");
var list = new BrowserLayoutEngine().Layout(page, new(800,600));
var generated = Assert.Single(list.Boxes,box => box.GeneratedPseudo is not null);
Assert.Same(page.GetElementById("Host"), generated.Node);
Assert.Equal(new[] { "Ancestor", "Body" }, list.Commands.Where(command => command.Kind == BrowserDrawKind.Text).Select(command => command.Text));
Assert.Equal(0x123456u, list.GetComputedStyle(page.GetElementById("Plain")!)!.Color.Rgb);
}
[Fact]
public void MatchingPseudoStillResolvesInheritedCustomPropertiesFontAndColor()
{
var page = Page("
", "#Parent{--label:'Prefix';--ink:#123456;color:var(--ink);font:700 20px/1.5 Lato}#Icon::before{content:var(--label);color:inherit}#Icon::after{content:attr(data-name);font-size:.5em;color:var(--ink)}");
var commands = new BrowserLayoutEngine().Layout(page,new(800,600)).Commands.Where(command => command.Kind == BrowserDrawKind.Text).ToArray();
Assert.Equal(new[] { "Prefix", "Suffix" }, commands.Select(command => command.Text));
Assert.All(commands, command => { Assert.Equal(0x123456u, command.Color); Assert.Equal("Lato", command.Font.Family); Assert.Equal(700, command.Font.Weight); });
Assert.Equal(20, commands[0].Font.Size); Assert.Equal(10, commands[1].Font.Size);
}
[Fact]
public void UnmatchedMarkerFallsBackToTheOriginalResolvedListStyle()
{
var page = Page("- Body
", "#Item{--ink:#123456;color:var(--ink);font:700 20px/1.5 Lato}.other::marker{color:red}");
var list = new BrowserLayoutEngine().Layout(page,new(800,600));
var marker = Assert.Single(list.Commands,command => command.Kind == BrowserDrawKind.Text && command.Text == "3.");
var style = list.GetComputedStyle(page.GetElementById("Item")!)!;
Assert.Equal(style.Color.Rgb,marker.Color); Assert.Equal(style.Font,marker.Font); Assert.Equal(style.LineHeight,marker.Bounds.Height);
var cascade = new BrowserCssCascade(new(),new(800,600),[]); cascade.Load(page,null);
Assert.Same(style,cascade.ComputeMarker(page.GetElementById("Item")!,style));
Assert.Null(cascade.ComputePseudo(page.GetElementById("Item")!,"before",style));
Assert.Null(cascade.ComputePseudo(page.GetElementById("Item")!,"after",style));
}
[Fact]
public void ContentNoneAndMatchedStyleWithoutContentStillProduceNoGeneratedBoxes()
{
var page=Page("","*,::before,::after{box-sizing:border-box}.Icon{--word:'Wrong';content:'Base content does not inherit'}#A::before{color:red}#B::after{content:none;color:blue}");
var list=new BrowserLayoutEngine().Layout(page,new(800,600));
Assert.DoesNotContain(list.Boxes,box=>box.GeneratedPseudo is not null);
Assert.DoesNotContain(list.Commands,command=>command.Kind==BrowserDrawKind.Text);
}
[Fact]
public void ExplicitContentInheritanceAndEmptyContentSurviveTheUniversalReset()
{
var page=Page("","*,::before,::after{box-sizing:border-box}#A{content:'Inherited';--empty:''}#A::before{content:inherit}#A::after{content:var(--empty);display:inline-block;width:12px;height:9px;background:red}#B::before{box-sizing:content-box}");
var list=new BrowserLayoutEngine().Layout(page,new(800,600));
var before=Assert.Single(list.Boxes,box=>box.GeneratedPseudo=="before");
Assert.Same(page.GetElementById("A"),before.Node);
Assert.Contains(list.Commands,command=>command.Kind==BrowserDrawKind.Text&&command.Text=="Inherited");
var after=Assert.Single(list.Boxes,box=>box.GeneratedPseudo=="after");
Assert.Equal(12,after.ContentBox.Width);Assert.Equal(9,after.ContentBox.Height);
}
}