From e781aab274bc7df1119cd857f4bce38299e324c9 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 23 Nov 2024 11:44:06 +0100 Subject: [PATCH] LibWeb: Honor the various margin attributes in HTMLBodyElement This one is particularly weird as there's a priority order, and we even have to look at attributes from the container element if we're inside a subframe. --- Libraries/LibWeb/HTML/AttributeNames.h | 4 + Libraries/LibWeb/HTML/HTMLBodyElement.cpp | 36 +- .../rendering/pixel-length-attributes.txt | 516 +++++++++--------- 3 files changed, 297 insertions(+), 259 deletions(-) diff --git a/Libraries/LibWeb/HTML/AttributeNames.h b/Libraries/LibWeb/HTML/AttributeNames.h index f37822047db..0b74b3498f5 100644 --- a/Libraries/LibWeb/HTML/AttributeNames.h +++ b/Libraries/LibWeb/HTML/AttributeNames.h @@ -32,6 +32,7 @@ namespace AttributeNames { __ENUMERATE_HTML_ATTRIBUTE(axis) \ __ENUMERATE_HTML_ATTRIBUTE(background) \ __ENUMERATE_HTML_ATTRIBUTE(behavior) \ + __ENUMERATE_HTML_ATTRIBUTE(bottommargin) \ __ENUMERATE_HTML_ATTRIBUTE(bgcolor) \ __ENUMERATE_HTML_ATTRIBUTE(border) \ __ENUMERATE_HTML_ATTRIBUTE(cellpadding) \ @@ -104,6 +105,7 @@ namespace AttributeNames { __ENUMERATE_HTML_ATTRIBUTE(label) \ __ENUMERATE_HTML_ATTRIBUTE(lang) \ __ENUMERATE_HTML_ATTRIBUTE(language) \ + __ENUMERATE_HTML_ATTRIBUTE(leftmargin) \ __ENUMERATE_HTML_ATTRIBUTE(link) \ __ENUMERATE_HTML_ATTRIBUTE(list) \ __ENUMERATE_HTML_ATTRIBUTE(loading) \ @@ -241,6 +243,7 @@ namespace AttributeNames { __ENUMERATE_HTML_ATTRIBUTE(required) \ __ENUMERATE_HTML_ATTRIBUTE(rev) \ __ENUMERATE_HTML_ATTRIBUTE(reversed) \ + __ENUMERATE_HTML_ATTRIBUTE(rightmargin) \ __ENUMERATE_HTML_ATTRIBUTE(rows) \ __ENUMERATE_HTML_ATTRIBUTE(rowspan) \ __ENUMERATE_HTML_ATTRIBUTE(rules) \ @@ -274,6 +277,7 @@ namespace AttributeNames { __ENUMERATE_HTML_ATTRIBUTE(target) \ __ENUMERATE_HTML_ATTRIBUTE(text) \ __ENUMERATE_HTML_ATTRIBUTE(title) \ + __ENUMERATE_HTML_ATTRIBUTE(topmargin) \ __ENUMERATE_HTML_ATTRIBUTE(truespeed) \ __ENUMERATE_HTML_ATTRIBUTE(type) \ __ENUMERATE_HTML_ATTRIBUTE(usemap) \ diff --git a/Libraries/LibWeb/HTML/HTMLBodyElement.cpp b/Libraries/LibWeb/HTML/HTMLBodyElement.cpp index 4055bc77762..0326afb764e 100644 --- a/Libraries/LibWeb/HTML/HTMLBodyElement.cpp +++ b/Libraries/LibWeb/HTML/HTMLBodyElement.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2018-2024, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ @@ -8,8 +8,10 @@ #include #include #include +#include #include #include +#include #include #include #include @@ -57,6 +59,38 @@ void HTMLBodyElement::apply_presentational_hints(CSS::StyleProperties& style) co style.set_property(CSS::PropertyID::BackgroundImage, *m_background_style_value); } }); + + auto get_margin_value = [&](auto const& first_body_attr_name, auto const& second_body_attr_name, auto const& container_frame_attr_name) -> Optional { + if (auto value = get_attribute(first_body_attr_name); value.has_value()) + return value.value(); + if (auto value = get_attribute(second_body_attr_name); value.has_value()) + return value.value(); + auto navigable = document().navigable(); + if (!navigable) + return {}; + auto container = navigable->container(); + if (!container) + return {}; + if (auto value = container->get_attribute(container_frame_attr_name); value.has_value()) + return value; + return {}; + }; + auto margin_top_value = get_margin_value(HTML::AttributeNames::marginheight, HTML::AttributeNames::topmargin, HTML::AttributeNames::marginheight); + auto margin_bottom_value = get_margin_value(HTML::AttributeNames::marginheight, HTML::AttributeNames::bottommargin, HTML::AttributeNames::marginheight); + auto margin_left_value = get_margin_value(HTML::AttributeNames::marginwidth, HTML::AttributeNames::leftmargin, HTML::AttributeNames::marginwidth); + auto margin_right_value = get_margin_value(HTML::AttributeNames::marginwidth, HTML::AttributeNames::rightmargin, HTML::AttributeNames::marginwidth); + + auto apply_margin_value = [&](CSS::PropertyID property_id, Optional const& value) { + if (!value.has_value()) + return; + if (auto parsed_value = parse_non_negative_integer(value.value()); parsed_value.has_value()) + style.set_property(property_id, CSS::LengthStyleValue::create(CSS::Length::make_px(*parsed_value))); + }; + + apply_margin_value(CSS::PropertyID::MarginTop, margin_top_value); + apply_margin_value(CSS::PropertyID::MarginBottom, margin_bottom_value); + apply_margin_value(CSS::PropertyID::MarginLeft, margin_left_value); + apply_margin_value(CSS::PropertyID::MarginRight, margin_right_value); } void HTMLBodyElement::attribute_changed(FlyString const& name, Optional const& old_value, Optional const& value, Optional const& namespace_) diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/rendering/pixel-length-attributes.txt b/Tests/LibWeb/Text/expected/wpt-import/html/rendering/pixel-length-attributes.txt index 67777b3e85c..2ed8ee02e90 100644 --- a/Tests/LibWeb/Text/expected/wpt-import/html/rendering/pixel-length-attributes.txt +++ b/Tests/LibWeb/Text/expected/wpt-import/html/rendering/pixel-length-attributes.txt @@ -6,340 +6,340 @@ Rerun Found 588 tests -244 Pass -344 Fail +500 Pass +88 Fail Details -Result Test Name MessageFail