LibWeb: Generalize support for dimension attributes

Rather than each element which supports dimension attributes needing to
implement parsing the attributes and setting the appropriate style, we
can generalize this functionality. This will also make each element more
closely resemble the spec text, as we will be effectively declaring, for
example, "The img element supports dimension attributes" in code.
This commit is contained in:
Timothy Flynn 2024-04-10 21:44:11 -04:00 committed by Andreas Kling
commit 4b1abcf61d
Notes: sideshowbarker 2024-07-17 00:59:43 +09:00
8 changed files with 32 additions and 42 deletions

View file

@ -68,6 +68,7 @@
#include <LibWeb/DOM/ShadowRoot.h>
#include <LibWeb/HTML/HTMLBRElement.h>
#include <LibWeb/HTML/HTMLHtmlElement.h>
#include <LibWeb/HTML/Parser/HTMLParser.h>
#include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
#include <LibWeb/HighResolutionTime/TimeOrigin.h>
#include <LibWeb/Layout/Node.h>
@ -1507,6 +1508,19 @@ static void apply_animation_properties(DOM::Document& document, StyleProperties&
}
}
static void apply_dimension_attribute(StyleProperties& style, DOM::Element const& element, FlyString const& attribute_name, CSS::PropertyID property_id)
{
auto attribute = element.attribute(attribute_name);
if (!attribute.has_value())
return;
auto parsed_value = HTML::parse_dimension_value(*attribute);
if (!parsed_value)
return;
style.set_property(property_id, parsed_value.release_nonnull());
}
// https://www.w3.org/TR/css-cascade/#cascading
void StyleComputer::compute_cascaded_values(StyleProperties& style, DOM::Element& element, Optional<CSS::Selector::PseudoElement::Type> pseudo_element, bool& did_match_any_pseudo_element_rules, ComputeStyleMode mode) const
{
@ -1543,6 +1557,11 @@ void StyleComputer::compute_cascaded_values(StyleProperties& style, DOM::Element
if (!pseudo_element.has_value()) {
element.apply_presentational_hints(style);
if (element.supports_dimension_attributes()) {
apply_dimension_attribute(style, element, HTML::AttributeNames::width, CSS::PropertyID::Width);
apply_dimension_attribute(style, element, HTML::AttributeNames::height, CSS::PropertyID::Height);
}
// SVG presentation attributes are parsed as CSS values, so we need to handle potential custom properties here.
if (element.is_svg_element()) {
// FIXME: This is not very efficient, we should only resolve the custom properties that are actually used.