From 81e75530d9e7127978acc24562c66739b971239b Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 6 Nov 2024 19:52:49 +0100 Subject: [PATCH] LibWeb: Make :nth-* selectors match children of non-elements This was covered by WPT, which caught us not allowing :nth-child(1) to match the root HTML element, and other similar issues. --- .../selectors/child-indexed-pseudo-class.txt | 36 +++++++++---------- .../Libraries/LibWeb/CSS/SelectorEngine.cpp | 2 +- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/selectors/child-indexed-pseudo-class.txt b/Tests/LibWeb/Text/expected/wpt-import/css/selectors/child-indexed-pseudo-class.txt index d9749da30ed..3eca40d5050 100644 --- a/Tests/LibWeb/Text/expected/wpt-import/css/selectors/child-indexed-pseudo-class.txt +++ b/Tests/LibWeb/Text/expected/wpt-import/css/selectors/child-indexed-pseudo-class.txt @@ -6,8 +6,8 @@ Rerun Found 54 tests -30 Pass -24 Fail +46 Pass +8 Fail Details Result Test Name MessagePass Expected HTML element to match :first-child with matches, querySelector(), and querySelectorAll() Pass Expected HTML element to match :last-child with matches, querySelector(), and querySelectorAll() @@ -15,14 +15,14 @@ Pass Expected HTML element to match :only-child with matches, querySelector(), a Pass Expected HTML element to match :first-of-type with matches, querySelector(), and querySelectorAll() Pass Expected HTML element to match :last-of-type with matches, querySelector(), and querySelectorAll() Pass Expected HTML element to match :only-of-type with matches, querySelector(), and querySelectorAll() -Fail Expected HTML element to match :nth-child(1) with matches, querySelector(), and querySelectorAll() -Fail Expected HTML element to match :nth-child(n) with matches, querySelector(), and querySelectorAll() -Fail Expected HTML element to match :nth-last-child(1) with matches, querySelector(), and querySelectorAll() -Fail Expected HTML element to match :nth-last-child(n) with matches, querySelector(), and querySelectorAll() -Fail Expected HTML element to match :nth-of-type(1) with matches, querySelector(), and querySelectorAll() -Fail Expected HTML element to match :nth-of-type(n) with matches, querySelector(), and querySelectorAll() -Fail Expected HTML element to match :nth-last-of-type(1) with matches, querySelector(), and querySelectorAll() -Fail Expected HTML element to match :nth-last-of-type(n) with matches, querySelector(), and querySelectorAll() +Pass Expected HTML element to match :nth-child(1) with matches, querySelector(), and querySelectorAll() +Pass Expected HTML element to match :nth-child(n) with matches, querySelector(), and querySelectorAll() +Pass Expected HTML element to match :nth-last-child(1) with matches, querySelector(), and querySelectorAll() +Pass Expected HTML element to match :nth-last-child(n) with matches, querySelector(), and querySelectorAll() +Pass Expected HTML element to match :nth-of-type(1) with matches, querySelector(), and querySelectorAll() +Pass Expected HTML element to match :nth-of-type(n) with matches, querySelector(), and querySelectorAll() +Pass Expected HTML element to match :nth-last-of-type(1) with matches, querySelector(), and querySelectorAll() +Pass Expected HTML element to match :nth-last-of-type(n) with matches, querySelector(), and querySelectorAll() Pass Expected HTML element to not match :nth-child(2) with matches, querySelector(), and querySelectorAll() Pass Expected HTML element to not match :nth-last-child(2) with matches, querySelector(), and querySelectorAll() Pass Expected HTML element to not match :nth-of-type(2) with matches, querySelector(), and querySelectorAll() @@ -51,14 +51,14 @@ Pass Expected DIV element to match :only-child with matches, querySelector(), an Pass Expected DIV element to match :first-of-type with matches, querySelector(), and querySelectorAll() Pass Expected DIV element to match :last-of-type with matches, querySelector(), and querySelectorAll() Pass Expected DIV element to match :only-of-type with matches, querySelector(), and querySelectorAll() -Fail Expected DIV element to match :nth-child(1) with matches, querySelector(), and querySelectorAll() -Fail Expected DIV element to match :nth-child(n) with matches, querySelector(), and querySelectorAll() -Fail Expected DIV element to match :nth-last-child(1) with matches, querySelector(), and querySelectorAll() -Fail Expected DIV element to match :nth-last-child(n) with matches, querySelector(), and querySelectorAll() -Fail Expected DIV element to match :nth-of-type(1) with matches, querySelector(), and querySelectorAll() -Fail Expected DIV element to match :nth-of-type(n) with matches, querySelector(), and querySelectorAll() -Fail Expected DIV element to match :nth-last-of-type(1) with matches, querySelector(), and querySelectorAll() -Fail Expected DIV element to match :nth-last-of-type(n) with matches, querySelector(), and querySelectorAll() +Pass Expected DIV element to match :nth-child(1) with matches, querySelector(), and querySelectorAll() +Pass Expected DIV element to match :nth-child(n) with matches, querySelector(), and querySelectorAll() +Pass Expected DIV element to match :nth-last-child(1) with matches, querySelector(), and querySelectorAll() +Pass Expected DIV element to match :nth-last-child(n) with matches, querySelector(), and querySelectorAll() +Pass Expected DIV element to match :nth-of-type(1) with matches, querySelector(), and querySelectorAll() +Pass Expected DIV element to match :nth-of-type(n) with matches, querySelector(), and querySelectorAll() +Pass Expected DIV element to match :nth-last-of-type(1) with matches, querySelector(), and querySelectorAll() +Pass Expected DIV element to match :nth-last-of-type(n) with matches, querySelector(), and querySelectorAll() Pass Expected DIV element to not match :nth-child(2) with matches, querySelector(), and querySelectorAll() Pass Expected DIV element to not match :nth-last-child(2) with matches, querySelector(), and querySelectorAll() Pass Expected DIV element to not match :nth-of-type(2) with matches, querySelector(), and querySelectorAll() diff --git a/Userland/Libraries/LibWeb/CSS/SelectorEngine.cpp b/Userland/Libraries/LibWeb/CSS/SelectorEngine.cpp index 375e3b34745..eb537f59aa1 100644 --- a/Userland/Libraries/LibWeb/CSS/SelectorEngine.cpp +++ b/Userland/Libraries/LibWeb/CSS/SelectorEngine.cpp @@ -517,7 +517,7 @@ static inline bool matches_pseudo_class(CSS::Selector::SimpleSelector::PseudoCla if (step_size == 0 && offset == 0) return false; // "If both a and b are equal to zero, the pseudo-class represents no element in the document tree." - auto const* parent = element.parent_element(); + auto const* parent = element.parent(); if (!parent) return false;