mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-30 20:59:16 +00:00
LibWeb: Support counter-* properties on pseudo-elements
There are multiple things happening here which are interconnected: - We now use AbstractElement to refer to the source of a counter, which means we also need to pass that around to compute `content`. - Give AbstractElement new helper methods that are needed by CountersSet, so it doesn't have to care whether it's dealing with a true Element or PseudoElement. - The CountersSet algorithms now walk the layout tree instead of DOM tree, so TreeBuilder needs to wait until the layout node exists before it resolves counters for it. - Resolve counters when creating a pseudo-element's layout node. We awkwardly compute the `content` value up to twice: Once to figure out what kind of node we need to make, and then if it's a string, we do so again after counters are resolved so we can get the true value of any `counter()` functions. This will need adjusting in the future but it works for now.
This commit is contained in:
parent
a6df9e1bac
commit
c1d4323cf7
Notes:
github-actions[bot]
2025-06-19 11:36:57 +00:00
Author: https://github.com/AtkinsSJ
Commit: c1d4323cf7
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/5132
Reviewed-by: https://github.com/tcl3
14 changed files with 247 additions and 60 deletions
|
@ -6,6 +6,7 @@
|
|||
|
||||
#include <LibWeb/DOM/AbstractElement.h>
|
||||
#include <LibWeb/DOM/Element.h>
|
||||
#include <LibWeb/Layout/Node.h>
|
||||
|
||||
namespace Web::DOM {
|
||||
|
||||
|
@ -20,6 +21,13 @@ void AbstractElement::visit(GC::Cell::Visitor& visitor) const
|
|||
visitor.visit(m_element);
|
||||
}
|
||||
|
||||
GC::Ptr<Layout::NodeWithStyle> AbstractElement::layout_node()
|
||||
{
|
||||
if (m_pseudo_element.has_value())
|
||||
return m_element->get_pseudo_element_node(*m_pseudo_element);
|
||||
return m_element->layout_node();
|
||||
}
|
||||
|
||||
GC::Ptr<Element const> AbstractElement::parent_element() const
|
||||
{
|
||||
if (m_pseudo_element.has_value())
|
||||
|
@ -27,6 +35,39 @@ GC::Ptr<Element const> AbstractElement::parent_element() const
|
|||
return m_element->parent_element();
|
||||
}
|
||||
|
||||
Optional<AbstractElement> AbstractElement::walk_layout_tree(WalkMethod walk_method)
|
||||
{
|
||||
GC::Ptr<Layout::Node> node = layout_node();
|
||||
if (!node)
|
||||
return OptionalNone {};
|
||||
|
||||
while (true) {
|
||||
switch (walk_method) {
|
||||
case WalkMethod::Previous:
|
||||
node = node->previous_in_pre_order();
|
||||
break;
|
||||
case WalkMethod::PreviousSibling:
|
||||
node = node->previous_sibling();
|
||||
break;
|
||||
}
|
||||
if (!node)
|
||||
return OptionalNone {};
|
||||
|
||||
if (auto* previous_element = as_if<Element>(node->dom_node()))
|
||||
return AbstractElement { *previous_element };
|
||||
|
||||
if (node->is_generated())
|
||||
return AbstractElement { *node->pseudo_element_generator(), node->generated_for_pseudo_element() };
|
||||
}
|
||||
}
|
||||
|
||||
bool AbstractElement::is_before(AbstractElement const& other) const
|
||||
{
|
||||
auto this_node = layout_node();
|
||||
auto other_node = other.layout_node();
|
||||
return this_node && other_node && this_node->is_before(*other_node);
|
||||
}
|
||||
|
||||
GC::Ptr<CSS::ComputedProperties const> AbstractElement::computed_properties() const
|
||||
{
|
||||
if (m_pseudo_element.has_value())
|
||||
|
@ -34,16 +75,46 @@ GC::Ptr<CSS::ComputedProperties const> AbstractElement::computed_properties() co
|
|||
return m_element->computed_properties();
|
||||
}
|
||||
|
||||
bool AbstractElement::has_non_empty_counters_set() const
|
||||
{
|
||||
if (m_pseudo_element.has_value())
|
||||
return m_element->get_pseudo_element(*m_pseudo_element)->has_non_empty_counters_set();
|
||||
return m_element->has_non_empty_counters_set();
|
||||
}
|
||||
|
||||
Optional<CSS::CountersSet const&> AbstractElement::counters_set() const
|
||||
{
|
||||
if (m_pseudo_element.has_value())
|
||||
return m_element->get_pseudo_element(*m_pseudo_element)->counters_set();
|
||||
return m_element->counters_set();
|
||||
}
|
||||
|
||||
CSS::CountersSet& AbstractElement::ensure_counters_set()
|
||||
{
|
||||
// FIXME: Handle pseudo-elements
|
||||
if (m_pseudo_element.has_value())
|
||||
return m_element->get_pseudo_element(*m_pseudo_element)->ensure_counters_set();
|
||||
return m_element->ensure_counters_set();
|
||||
}
|
||||
|
||||
void AbstractElement::set_counters_set(OwnPtr<CSS::CountersSet>&& counters_set)
|
||||
{
|
||||
// FIXME: Handle pseudo-elements
|
||||
m_element->set_counters_set(move(counters_set));
|
||||
if (m_pseudo_element.has_value()) {
|
||||
m_element->get_pseudo_element(*m_pseudo_element)->set_counters_set(move(counters_set));
|
||||
} else {
|
||||
m_element->set_counters_set(move(counters_set));
|
||||
}
|
||||
}
|
||||
|
||||
String AbstractElement::debug_description() const
|
||||
{
|
||||
if (m_pseudo_element.has_value()) {
|
||||
StringBuilder builder;
|
||||
builder.append(m_element->debug_description());
|
||||
builder.append("::"sv);
|
||||
builder.append(CSS::pseudo_element_name(*m_pseudo_element));
|
||||
return builder.to_string_without_validation();
|
||||
}
|
||||
return m_element->debug_description();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue