AngleSharp / AngleSharp.Css

:angel: Library to enable support for cascading stylesheets in AngleSharp.
https://anglesharp.github.io
MIT License
72 stars 34 forks source link

ElementExtensions.GetInnerText removes whitespace before span #32

Closed mattwTS closed 5 years ago

mattwTS commented 5 years ago

Bug Report

Description

The GetInnerText() extension method in v0.12.1 of AngleSharp.Css removes whitespace before a <span> element; that isn't desired behavior.

Steps to Reproduce

using AngleSharp;
using AngleSharp.Dom;
using AngleSharp.Html.Parser;

static class Program
{
    static void Main()
    {
        var ctx = BrowsingContext.New(Configuration.Default.WithCss());
        var parser = ctx.GetService<IHtmlParser>();
        var nodeList = parser.ParseFragment("<div><div>Div with <span>a span</span> in it.</div></div>", null);
        var element = (IElement)nodeList[0];
        Console.WriteLine(element.GetInnerText());
        Console.ReadKey();
    }
}

Expected behavior: Outputs "Div with a span in it.".

In all of Firefox, Chrome, Edge, and IE, pasting var wrapper = document.createElement("div"); wrapper.innerHTML = "<div>Div with <span>a span</span> in it.</div>"; wrapper.innerText into the developer console produces this output.

Actual behavior: Outputs "Div witha span in it."

Environment details: .NET Framework v4.6.2

Possible Solution

The problem appears to lie at the end of ProcessText. I'm not sure I fully understand the intent there, it appears to remove a single space after the last non-whitespace character of a text node with trailing white-space. I think that might not be correct when the subsequent node is an inline element, the text node is at the end of an inline element, or if the style for the text node had white-space pre or pre-wrap.

FlorianRappl commented 5 years ago

Landed in devel.