jamietre / CsQuery

CsQuery is a complete CSS selector engine, HTML parser, and jQuery port for C# and .NET 4.
Other
1.16k stars 249 forks source link

ChildMatches returns null for :first-child selector #208

Closed coel closed 8 years ago

coel commented 8 years ago

I seem to get a NullReferenceException on a document using and matching the :first-child selector.

Debugging into it, it seems like the cause is ChildMatches() in FirstChild.cs returning null.

    public override IEnumerable<IDomObject> ChildMatches(IDomContainer element)
    {
        IDomObject child = element.FirstChild;
        if (child != null)
        {
            yield return element.FirstElementChild;
        }
    }

For example, null is returned on a p tag with text where element.FirstChild returns the text, but element.FirstElementChild is null.

Looking at LastChild.cs, it uses element.LastElementChild for both the check and to return.

Is the handling for FirstChild intentionally different, or is this likely a bug?

jamietre commented 8 years ago

Looks like a bug. IIRC ChildMatches should never return null.