Truly independent web browser
/*
* Copyright (c) 2020, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/CSS/CountersSet.h>
#include <LibWeb/CSS/PropertyID.h>
#include <LibWeb/CSS/StyleValues/CounterDefinitionsStyleValue.h>
#include <LibWeb/CSS/StyleValues/CounterStyleStyleValue.h>
#include <LibWeb/CSS/StyleValues/IntegerStyleValue.h>
#include <LibWeb/CSS/StyleValues/KeywordStyleValue.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::attribute_changed(Utf16FlyString const& local_name, Optional<Utf16String> const& old_value, Optional<Utf16String> const& value, Optional<Utf16FlyString> const& namespace_)
{
Base::attribute_changed(local_name, old_value, value, namespace_);
if (local_name == HTML::AttributeNames::value)
schedule_list_item_renumber_for_list_owner();
}
// 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"_utf16);
if (auto maybe_number = HTML::parse_integer(content_attribute_value); maybe_number.has_value())
return *maybe_number;
return 0;
}
void HTMLLIElement::set_value(WebIDL::Long value)
{
set_attribute_value(AttributeNames::value, Utf16String::number(value));
}
bool HTMLLIElement::is_presentational_hint(Utf16FlyString const& name) const
{
if (Base::is_presentational_hint(name))
return true;
return name.is_one_of(HTML::AttributeNames::type, HTML::AttributeNames::value);
}
void HTMLLIElement::apply_presentational_hints(Vector<CSS::StyleProperty>& properties) const
{
Base::apply_presentational_hints(properties);
// https://html.spec.whatwg.org/multipage/rendering.html#lists
// "When an li element has a value attribute, and parsing that attribute's value using the rules
// for parsing integers doesn't generate an error, the user agent is expected to use the parsed
// value value as a presentational hint for the 'counter-set' property of the form
// list-item value."
if (auto value = get_attribute(AttributeNames::value); value.has_value()) {
if (auto parsed = parse_integer(*value); parsed.has_value()) {
CSS::CounterDefinition definition { CSS::list_item_counter_name(), false, CSS::IntegerStyleValue::create(*parsed) };
properties.append({ .property_id = CSS::PropertyID::CounterSet, .value = CSS::CounterDefinitionsStyleValue::create({ move(definition) }) });
}
}
for_each_attribute([&](Utf16FlyString const& name, Utf16View value) {
if (name == HTML::AttributeNames::type) {
if (value == u"1"sv) {
properties.append({ .property_id = CSS::PropertyID::ListStyleType, .value = CSS::CounterStyleStyleValue::create("decimal"_utf16_fly_string) });
} else if (value == u"a"sv) {
properties.append({ .property_id = CSS::PropertyID::ListStyleType, .value = CSS::CounterStyleStyleValue::create("lower-alpha"_utf16_fly_string) });
} else if (value == u"A"sv) {
properties.append({ .property_id = CSS::PropertyID::ListStyleType, .value = CSS::CounterStyleStyleValue::create("upper-alpha"_utf16_fly_string) });
} else if (value == u"i"sv) {
properties.append({ .property_id = CSS::PropertyID::ListStyleType, .value = CSS::CounterStyleStyleValue::create("lower-roman"_utf16_fly_string) });
} else if (value == u"I"sv) {
properties.append({ .property_id = CSS::PropertyID::ListStyleType, .value = CSS::CounterStyleStyleValue::create("upper-roman"_utf16_fly_string) });
} else if (value.equals_ignoring_ascii_case(u"none"sv)) {
properties.append({ .property_id = CSS::PropertyID::ListStyleType, .value = CSS::KeywordStyleValue::create(CSS::Keyword::None) });
} else if (value.equals_ignoring_ascii_case(u"disc"sv)) {
properties.append({ .property_id = CSS::PropertyID::ListStyleType, .value = CSS::CounterStyleStyleValue::create("disc"_utf16_fly_string) });
} else if (value.equals_ignoring_ascii_case(u"circle"sv)) {
properties.append({ .property_id = CSS::PropertyID::ListStyleType, .value = CSS::CounterStyleStyleValue::create("circle"_utf16_fly_string) });
} else if (value.equals_ignoring_ascii_case(u"square"sv)) {
properties.append({ .property_id = CSS::PropertyID::ListStyleType, .value = CSS::CounterStyleStyleValue::create("square"_utf16_fly_string) });
}
}
});
}
}