LibWeb/CSS: Parse the & nesting selector

This commit is contained in:
Sam Atkins 2024-10-15 12:00:29 +01:00 committed by Andreas Kling
commit 5b4d1b5b05
Notes: github-actions[bot] 2024-10-17 18:57:46 +00:00
5 changed files with 23 additions and 9 deletions

View file

@ -1,7 +1,7 @@
/*
* Copyright (c) 2018-2022, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2020-2021, the SerenityOS developers.
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2021-2024, Sam Atkins <sam@ladybird.org>
* Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
* Copyright (c) 2022, MacDue <macdue@dueutil.tech>
*
@ -662,6 +662,10 @@ Parser::ParseErrorOr<Optional<Selector::SimpleSelector>> Parser::parse_simple_se
case '*':
// Handled already
VERIFY_NOT_REACHED();
case '&':
return Selector::SimpleSelector {
.type = Selector::SimpleSelector::Type::Nesting,
};
case '.': {
if (peek_token_ends_selector())
return ParseError::SyntaxError;

View file

@ -1,6 +1,6 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2021-2024, Sam Atkins <sam@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -153,6 +153,9 @@ u32 Selector::specificity() const
case SimpleSelector::Type::Universal:
// ignore the universal selector
break;
case SimpleSelector::Type::Nesting:
// We should have replaced this already
VERIFY_NOT_REACHED();
}
}
}
@ -325,8 +328,9 @@ String Selector::SimpleSelector::serialize() const
case Selector::SimpleSelector::Type::PseudoElement:
// Note: Pseudo-elements are dealt with in Selector::serialize()
break;
default:
dbgln("FIXME: Unsupported simple selector serialization for type {}", to_underlying(type));
case Type::Nesting:
// AD-HOC: Not in spec yet.
s.append('&');
break;
}
return MUST(s.to_string());

View file

@ -1,6 +1,6 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2021-2024, Sam Atkins <sam@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -23,7 +23,7 @@ class Selector : public RefCounted<Selector> {
public:
class PseudoElement {
public:
enum class Type {
enum class Type : u8 {
Before,
After,
FirstLine,
@ -83,7 +83,7 @@ public:
};
struct SimpleSelector {
enum class Type {
enum class Type : u8 {
Universal,
TagName,
Id,
@ -91,6 +91,7 @@ public:
Attribute,
PseudoClass,
PseudoElement,
Nesting,
};
struct ANPlusBPattern {

View file

@ -1,6 +1,6 @@
/*
* Copyright (c) 2018-2024, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2021-2024, Sam Atkins <sam@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -742,9 +742,11 @@ static inline bool matches(CSS::Selector::SimpleSelector const& component, Optio
case CSS::Selector::SimpleSelector::Type::PseudoElement:
// Pseudo-element matching/not-matching is handled in the top level matches().
return true;
default:
case CSS::Selector::SimpleSelector::Type::Nesting:
// We should only try to match selectors that have been absolutized!
VERIFY_NOT_REACHED();
}
VERIFY_NOT_REACHED();
}
static inline bool matches(CSS::Selector const& selector, Optional<CSS::CSSStyleSheet const&> style_sheet_for_rule, int component_list_index, DOM::Element const& element, JS::GCPtr<DOM::Element const> shadow_host, JS::GCPtr<DOM::ParentNode const> scope, SelectorKind selector_kind)

View file

@ -503,6 +503,9 @@ void dump_selector(StringBuilder& builder, CSS::Selector const& selector, int in
case CSS::Selector::SimpleSelector::Type::PseudoElement:
type_description = "PseudoElement";
break;
case CSS::Selector::SimpleSelector::Type::Nesting:
type_description = "Nesting";
break;
}
builder.appendff("{}:", type_description);