Skip to content

Commit

Permalink
LibWeb: Support reversed ordered lists
Browse files Browse the repository at this point in the history
This PR adds support for the `reversed` attribute of
ordered lists.
  • Loading branch information
skyz1 committed Jan 28, 2025
1 parent 8b5a25e commit 0f988bc
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 18 deletions.
105 changes: 105 additions & 0 deletions Libraries/LibWeb/DOM/Element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
#include <LibWeb/HTML/HTMLFrameSetElement.h>
#include <LibWeb/HTML/HTMLHtmlElement.h>
#include <LibWeb/HTML/HTMLInputElement.h>
#include <LibWeb/HTML/HTMLLIElement.h>
#include <LibWeb/HTML/HTMLMenuElement.h>
#include <LibWeb/HTML/HTMLOListElement.h>
#include <LibWeb/HTML/HTMLOptGroupElement.h>
#include <LibWeb/HTML/HTMLOptionElement.h>
#include <LibWeb/HTML/HTMLSelectElement.h>
Expand All @@ -53,6 +56,7 @@
#include <LibWeb/HTML/HTMLTableElement.h>
#include <LibWeb/HTML/HTMLTemplateElement.h>
#include <LibWeb/HTML/HTMLTextAreaElement.h>
#include <LibWeb/HTML/HTMLUListElement.h>
#include <LibWeb/HTML/Numbers.h>
#include <LibWeb/HTML/Parser/HTMLParser.h>
#include <LibWeb/HTML/Scripting/Agent.h>
Expand Down Expand Up @@ -2883,6 +2887,107 @@ bool Element::has_paint_containment() const
return false;
}

int Element::number_of_owned_list_items()
{
auto number_of_owned_li_elements = 0;
for_each_child_of_type<DOM::Element>([&](auto& child) {
if (child.list_owner() == this) {
number_of_owned_li_elements++;
}
return IterationDecision::Continue;
});
return number_of_owned_li_elements;
}

// https://html.spec.whatwg.org/#list-owner
Element* Element::list_owner()
{
// Any element whose computed value of 'display' is 'list-item' has a list owner, which is determined as follows:
if (!computed_properties()->display().is_list_item())
return nullptr;

// FIXME: 1. If the element is not being rendered, return null; the element has no list owner.

// 2. Let ancestor be the element's parent.
auto* ancestor = parent();

// 3. If the element has an ol, ul, or menu ancestor, set ancestor to the closest such ancestor element.
for (auto* node = ancestor; node; node = node->parent()) {
if (is<HTML::HTMLOListElement>(node) || is<HTML::HTMLUListElement>(node) || is<HTML::HTMLMenuElement>(node)) {
ancestor = node;
break;
}
}

// 4. Return the closest inclusive ancestor of ancestor that produces a CSS box.
for (; ancestor; ancestor = ancestor->parent()) {
if (is<Element>(ancestor)) {
return static_cast<Element*>(ancestor);
}
}

// Spec-Note: Such an element will always exist, as at the very least the document element will always produce a CSS box.
ASSERT_NOT_REACHED();
}

// https://html.spec.whatwg.org/#ordinal-value
int Element::ordinal_value()
{
// NOTE: The spec provides an algorithm to determine the ordinal value of each element owned by a given list owner.
// However, we are only interested in the ordinal value of this element.

// FIXME: 1. Let i be 1.

// 2. If owner is an ol element, let numbering be owner's starting value. Otherwise, let numbering be 1.
auto* owner = list_owner();
if (!owner) {
return 1;
}

auto numbering = 1;
auto reversed = false;
if (is<HTML::HTMLOListElement>(owner)) {
auto* ol_element = static_cast<HTML::HTMLOListElement*>(owner);
numbering = ol_element->starting_value();
reversed = ol_element->has_attribute(HTML::AttributeNames::reversed);
}

// FIXME: 3. Loop : If i is greater than the number of list items that owner owns, then return; all of owner's owned list items have been assigned ordinal values.
// FIXME: 4. Let item be the ith of owner's owned list items, in tree order.

owner->for_each_child_of_type<DOM::Element>([&](auto& item) {
if (item.list_owner() == owner) {
// 5. If item is an li element that has a value attribute, then:
auto value_attribute = item.get_attribute(HTML::AttributeNames::value);
if (is<HTML::HTMLLIElement>(item) && value_attribute.has_value()) {
// 1. Let parsed be the result of parsing the value of the attribute as an integer.
auto parsed = HTML::parse_integer(value_attribute.value());

// 2. If parsed is not an error, then set numbering to parsed.
if (parsed.has_value())
numbering = parsed.value();
}

// FIXME: 6. The ordinal value of item is numbering.
if (&item == this)
return IterationDecision::Break;

// 7. If owner is an ol element, and owner has a reversed attribute, decrement numbering by 1; otherwise, increment numbering by 1.
if (reversed) {
numbering--;
} else {
numbering++;
}

// FIXME: 8. Increment i by 1.
}
return IterationDecision::Continue;
});

// FIXME: 9. Go to the step labeled loop.
return numbering;
}

bool Element::id_reference_exists(String const& id_reference) const
{
return document().get_element_by_id(id_reference);
Expand Down
4 changes: 4 additions & 0 deletions Libraries/LibWeb/DOM/Element.h
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,10 @@ class Element
bool has_style_containment() const;
bool has_paint_containment() const;

int number_of_owned_list_items();
Element* list_owner();
int ordinal_value();

protected:
Element(Document&, DOM::QualifiedName);
virtual void initialize(JS::Realm&) override;
Expand Down
24 changes: 24 additions & 0 deletions Libraries/LibWeb/HTML/HTMLOListElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <LibWeb/Bindings/HTMLOListElementPrototype.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/HTML/HTMLOListElement.h>
#include <LibWeb/HTML/Numbers.h>

Expand Down Expand Up @@ -36,4 +37,27 @@ WebIDL::Long HTMLOListElement::start()
return 1;
}

// https://html.spec.whatwg.org/#concept-ol-start
int HTMLOListElement::starting_value()
{
// 1. If the ol element has a start attribute, then:
auto start = get_attribute(AttributeNames::start);
if (start.has_value()) {
// 1. Let parsed be the result of parsing the value of the attribute as an integer.
auto parsed = parse_integer(start.value());

// 2. If parsed is not an error, then return parsed.
if (parsed.has_value())
return parsed.value();
}

// 2. If the ol element has a reversed attribute, then return the number of owned li elements.
if (has_attribute(AttributeNames::reversed)) {
return number_of_owned_list_items();
}

// 3. Return 1.
return 1;
}

}
2 changes: 2 additions & 0 deletions Libraries/LibWeb/HTML/HTMLOListElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class HTMLOListElement final : public HTMLElement {
MUST(set_attribute(AttributeNames::start, String::number(start)));
}

int starting_value();

private:
HTMLOListElement(DOM::Document&, DOM::QualifiedName);

Expand Down
20 changes: 2 additions & 18 deletions Libraries/LibWeb/Layout/TreeBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,25 +428,9 @@ static bool is_ignorable_whitespace(Layout::Node const& node)

i32 TreeBuilder::calculate_list_item_index(DOM::Node& dom_node)
{
if (is<HTML::HTMLLIElement>(dom_node)) {
auto& li = static_cast<HTML::HTMLLIElement&>(dom_node);
if (li.value() != 0)
return li.value();
}
if (is<DOM::Element>(dom_node))
return static_cast<DOM::Element*>(&dom_node)->ordinal_value();

if (dom_node.previous_sibling() != nullptr) {
DOM::Node* current = dom_node.previous_sibling();
while (current != nullptr) {
if (is<HTML::HTMLLIElement>(*current))
return calculate_list_item_index(*current) + 1;
current = current->previous_sibling();
}
}

if (is<HTML::HTMLOListElement>(*dom_node.parent())) {
auto& ol = static_cast<HTML::HTMLOListElement&>(*dom_node.parent());
return ol.start();
}
return 1;
}

Expand Down

0 comments on commit 0f988bc

Please sign in to comment.