ladybird/Libraries/LibWeb/HTML/HTMLLIElement.cpp
Andreas Kling a6dfc74e93 LibWeb: Only set prototype once for object with IDL interface
Before this change, we were going through the chain of base classes for
each IDL interface object and having them set the prototype to their
prototype.

Instead of doing that, reorder things so that we set the right prototype
immediately in Foo::initialize(), and then don't bother in all the base
class overrides.

This knocks off a ~1% profile item on Speedometer 3.
2025-04-20 18:43:11 +02:00

78 lines
3.7 KiB
C++

/*
* Copyright (c) 2020, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/Bindings/HTMLLIElementPrototype.h>
#include <LibWeb/CSS/StyleValues/CSSKeywordValue.h>
#include <LibWeb/HTML/HTMLLIElement.h>
#include <LibWeb/HTML/Numbers.h>
#include <LibWeb/HTML/Window.h>
namespace Web::HTML {
GC_DEFINE_ALLOCATOR(HTMLLIElement);
HTMLLIElement::HTMLLIElement(DOM::Document& document, DOM::QualifiedName qualified_name)
: HTMLElement(document, move(qualified_name))
{
}
HTMLLIElement::~HTMLLIElement() = default;
void HTMLLIElement::initialize(JS::Realm& realm)
{
WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLLIElement);
Base::initialize(realm);
}
// https://html.spec.whatwg.org/multipage/grouping-content.html#dom-li-value
WebIDL::Long HTMLLIElement::value()
{
// The value IDL attribute must reflect the value of the value content attribute.
// NOTE: This is equivalent to the code that would be generated by the IDL generator if we used [Reflect] on the value attribute.
// We don't do that in this case, since this method is used elsewhere.
auto content_attribute_value = get_attribute(AttributeNames::value).value_or("0"_string);
if (auto maybe_number = HTML::parse_integer(content_attribute_value); maybe_number.has_value())
return *maybe_number;
return 0;
}
bool HTMLLIElement::is_presentational_hint(FlyString const& name) const
{
if (Base::is_presentational_hint(name))
return true;
return name == HTML::AttributeNames::type;
}
void HTMLLIElement::apply_presentational_hints(GC::Ref<CSS::CascadedProperties> cascaded_properties) const
{
// https://html.spec.whatwg.org/multipage/rendering.html#lists
for_each_attribute([&](auto& name, auto& value) {
if (name == HTML::AttributeNames::type) {
if (value == "1"sv) {
cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::ListStyleType, CSS::CSSKeywordValue::create(CSS::Keyword::Decimal));
} else if (value == "a"sv) {
cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::ListStyleType, CSS::CSSKeywordValue::create(CSS::Keyword::LowerAlpha));
} else if (value == "A"sv) {
cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::ListStyleType, CSS::CSSKeywordValue::create(CSS::Keyword::UpperAlpha));
} else if (value == "i"sv) {
cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::ListStyleType, CSS::CSSKeywordValue::create(CSS::Keyword::LowerRoman));
} else if (value == "I"sv) {
cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::ListStyleType, CSS::CSSKeywordValue::create(CSS::Keyword::UpperRoman));
} else if (value.equals_ignoring_ascii_case("none"sv)) {
cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::ListStyleType, CSS::CSSKeywordValue::create(CSS::Keyword::None));
} else if (value.equals_ignoring_ascii_case("disc"sv)) {
cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::ListStyleType, CSS::CSSKeywordValue::create(CSS::Keyword::Disc));
} else if (value.equals_ignoring_ascii_case("circle"sv)) {
cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::ListStyleType, CSS::CSSKeywordValue::create(CSS::Keyword::Circle));
} else if (value.equals_ignoring_ascii_case("square"sv)) {
cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::ListStyleType, CSS::CSSKeywordValue::create(CSS::Keyword::Square));
}
}
});
}
}