1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-30 18:08:12 +00:00

LibWeb: Implement :nth-child pseudo-class

This commit is contained in:
miere43 2021-05-08 23:19:25 +03:00 committed by Andreas Kling
parent 1e0e8b27c0
commit aa83539d7b
6 changed files with 161 additions and 36 deletions

View file

@ -11,6 +11,7 @@
#include <LibWeb/CSS/CSSStyleRule.h>
#include <LibWeb/CSS/Parser/DeprecatedCSSParser.h>
#include <LibWeb/CSS/PropertyID.h>
#include <LibWeb/CSS/Selector.h>
#include <LibWeb/DOM/Document.h>
#include <ctype.h>
#include <stdlib.h>
@ -384,6 +385,17 @@ public:
return ch == '~' || ch == '>' || ch == '+';
}
static StringView capture_selector_args(const String& pseudo_name)
{
if (const auto start_pos = pseudo_name.find('('); start_pos.has_value()) {
const auto start = start_pos.value() + 1;
if (const auto end_pos = pseudo_name.index_of(")", start); end_pos.has_value()) {
return pseudo_name.substring_view(start, end_pos.value() - start).trim_whitespace();
}
}
return {};
}
Optional<CSS::Selector::SimpleSelector> parse_simple_selector()
{
auto index_at_start = index;
@ -399,15 +411,9 @@ public:
if (peek() == '*') {
type = CSS::Selector::SimpleSelector::Type::Universal;
consume_one();
return CSS::Selector::SimpleSelector {
type,
CSS::Selector::SimpleSelector::PseudoClass::None,
CSS::Selector::SimpleSelector::PseudoElement::None,
String(),
CSS::Selector::SimpleSelector::AttributeMatchType::None,
String(),
String()
};
CSS::Selector::SimpleSelector result;
result.type = type;
return result;
}
if (peek() == '.') {
@ -435,15 +441,9 @@ public:
value = value.to_lowercase();
}
CSS::Selector::SimpleSelector simple_selector {
type,
CSS::Selector::SimpleSelector::PseudoClass::None,
CSS::Selector::SimpleSelector::PseudoElement::None,
value,
CSS::Selector::SimpleSelector::AttributeMatchType::None,
String(),
String()
};
CSS::Selector::SimpleSelector simple_selector;
simple_selector.type = type;
simple_selector.value = value;
buffer.clear();
if (peek() == '[') {
@ -563,6 +563,9 @@ public:
simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::FirstOfType;
} else if (pseudo_name.equals_ignoring_case("last-of-type")) {
simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::LastOfType;
} else if (pseudo_name.starts_with("nth-child", CaseSensitivity::CaseInsensitive)) {
simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::NthChild;
simple_selector.nth_child_pattern = CSS::Selector::SimpleSelector::NthChildPattern::parse(capture_selector_args(pseudo_name));
} else if (pseudo_name.equals_ignoring_case("before")) {
simple_selector.pseudo_element = CSS::Selector::SimpleSelector::PseudoElement::Before;
} else if (pseudo_name.equals_ignoring_case("after")) {