XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFESystem

【Rust】XFE系统,基于Rust和汇编

公开
关注 0 Fork 0 Star 0
UTF-8
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("<style>body{margin:0}" + css + "</style>" + body);

    [Fact]
    public void UnmatchedPseudoDoesNotInheritAnAncestorsGeneratedContent()
    {
        var page = Page("<section id=Host><span id=Plain>Body</span></section>", "#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("<div id=Parent><i id=Icon data-name=Suffix></i></div>", "#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("<ol start=3><li id=Item>Body</li></ol>", "#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("<i class=Icon id=A></i><i class=Icon id=B></i>","*,::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("<i id=A></i><i id=B></i>","*,::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);
    }
}