From 140dc95e6769a41542f98abef333d5bc32b86e39 Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Tue, 1 Oct 2024 17:45:10 +0100 Subject: [PATCH] LibWeb: Map dimension attributes for table elements --- .../expected/table/colgroup-with-two-cols.txt | 8 +++---- .../expected/HTML/dimension-attributes.txt | 6 ++++++ .../Text/input/HTML/dimension-attributes.html | 2 ++ .../LibWeb/HTML/HTMLTableColElement.cpp | 14 +++++++++++++ .../LibWeb/HTML/HTMLTableColElement.h | 2 ++ .../LibWeb/HTML/HTMLTableElement.cpp | 8 ++++++- .../LibWeb/HTML/HTMLTableRowElement.cpp | 6 +++--- .../LibWeb/HTML/HTMLTableSectionElement.cpp | 21 +++++++++++++++++++ .../LibWeb/HTML/HTMLTableSectionElement.h | 2 ++ 9 files changed, 61 insertions(+), 8 deletions(-) diff --git a/Tests/LibWeb/Layout/expected/table/colgroup-with-two-cols.txt b/Tests/LibWeb/Layout/expected/table/colgroup-with-two-cols.txt index d4e8504c6cc..00eb5d5b57d 100644 --- a/Tests/LibWeb/Layout/expected/table/colgroup-with-two-cols.txt +++ b/Tests/LibWeb/Layout/expected/table/colgroup-with-two-cols.txt @@ -1,8 +1,8 @@ Viewport <#document> at (0,0) content-size 800x600 children: not-inline BlockContainer at (0,0) content-size 800x600 [BFC] children: not-inline BlockContainer at (8,8) content-size 784x2 children: not-inline - TableWrapper <(anonymous)> at (8,8) content-size 6x2 [BFC] children: not-inline - Box at (8,8) content-size 6x2 table-box [TFC] children: not-inline + TableWrapper <(anonymous)> at (8,8) content-size 46x2 [BFC] children: not-inline + Box
at (8,8) content-size 46x2 table-box [TFC] children: not-inline BlockContainer (not painted) table-column-group children: not-inline BlockContainer (not painted) children: not-inline BlockContainer (not painted) children: not-inline @@ -11,6 +11,6 @@ Viewport <#document> at (0,0) content-size 800x600 children: not-inline ViewportPaintable (Viewport<#document>) [0,0 800x600] PaintableWithLines (BlockContainer) [0,0 800x600] PaintableWithLines (BlockContainer) [8,8 784x2] - PaintableWithLines (TableWrapper(anonymous)) [8,8 6x2] - PaintableBox (Box
) [8,8 6x2] + PaintableWithLines (TableWrapper(anonymous)) [8,8 46x2] + PaintableBox (Box
) [8,8 46x2] PaintableBox (Box) [8,8 0x0] diff --git a/Tests/LibWeb/Text/expected/HTML/dimension-attributes.txt b/Tests/LibWeb/Text/expected/HTML/dimension-attributes.txt index c98be015578..d7243653d2c 100644 --- a/Tests/LibWeb/Text/expected/HTML/dimension-attributes.txt +++ b/Tests/LibWeb/Text/expected/HTML/dimension-attributes.txt @@ -55,3 +55,9 @@ Test embed.width = "120." maps to width: 120px Test embed.height = "100" maps to height: 100px Test embed.height = " 00110 " maps to height: 110px Test embed.height = "120." maps to height: 120px +Test tr.height = "100" maps to height: 100px +Test tr.height = " 00110 " maps to height: 110px +Test tr.height = "120." maps to height: 120px +Test col.width = "100" maps to width: 100px +Test col.width = " 00110 " maps to width: 110px +Test col.width = "120." maps to width: 120px diff --git a/Tests/LibWeb/Text/input/HTML/dimension-attributes.html b/Tests/LibWeb/Text/input/HTML/dimension-attributes.html index bcd257fa2b7..8e77c1a1a09 100644 --- a/Tests/LibWeb/Text/input/HTML/dimension-attributes.html +++ b/Tests/LibWeb/Text/input/HTML/dimension-attributes.html @@ -22,6 +22,8 @@ { elementName: "embed", attribute: "vspace", mappedProperty: "marginBottom" }, { elementName: "embed", attribute: "width", mappedProperty: "width" }, { elementName: "embed", attribute: "height", mappedProperty: "height" }, + { elementName: "tr", attribute: "height", mappedProperty: "height" }, + { elementName: "col", attribute: "width", mappedProperty: "width" }, ]; const values = ["100", " 00110 ", "120."]; diff --git a/Userland/Libraries/LibWeb/HTML/HTMLTableColElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLTableColElement.cpp index 3834ffb5b85..b9ea4a6feb1 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLTableColElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLTableColElement.cpp @@ -6,8 +6,10 @@ #include #include +#include #include #include +#include namespace Web::HTML { @@ -42,4 +44,16 @@ WebIDL::ExceptionOr HTMLTableColElement::set_span(unsigned int value) return set_attribute(HTML::AttributeNames::span, MUST(String::number(value))); } +void HTMLTableColElement::apply_presentational_hints(CSS::StyleProperties& style) const +{ + for_each_attribute([&](auto& name, auto& value) { + // https://html.spec.whatwg.org/multipage/rendering.html#tables-2:maps-to-the-dimension-property-2 + if (name == HTML::AttributeNames::width) { + if (auto parsed_value = parse_dimension_value(value)) { + style.set_property(CSS::PropertyID::Width, *parsed_value); + } + } + }); +} + } diff --git a/Userland/Libraries/LibWeb/HTML/HTMLTableColElement.h b/Userland/Libraries/LibWeb/HTML/HTMLTableColElement.h index 178248213a9..8638f8396f5 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLTableColElement.h +++ b/Userland/Libraries/LibWeb/HTML/HTMLTableColElement.h @@ -24,6 +24,8 @@ private: HTMLTableColElement(DOM::Document&, DOM::QualifiedName); virtual void initialize(JS::Realm&) override; + + virtual void apply_presentational_hints(CSS::StyleProperties&) const override; }; } diff --git a/Userland/Libraries/LibWeb/HTML/HTMLTableElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLTableElement.cpp index 6f32d9ba77b..b06e231dfc8 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLTableElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLTableElement.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -59,7 +60,7 @@ void HTMLTableElement::apply_presentational_hints(CSS::StyleProperties& style) c return; } if (name == HTML::AttributeNames::height) { - if (auto parsed_value = parse_nonzero_dimension_value(value)) + if (auto parsed_value = parse_dimension_value(value)) style.set_property(CSS::PropertyID::Height, parsed_value.release_nonnull()); return; } @@ -72,6 +73,11 @@ void HTMLTableElement::apply_presentational_hints(CSS::StyleProperties& style) c } return; } + if (name == HTML::AttributeNames::background) { + if (auto parsed_value = document().parse_url(value); parsed_value.is_valid()) + style.set_property(CSS::PropertyID::BackgroundImage, CSS::ImageStyleValue::create(parsed_value)); + return; + } if (name == HTML::AttributeNames::bgcolor) { // https://html.spec.whatwg.org/multipage/rendering.html#tables-2:rules-for-parsing-a-legacy-colour-value auto color = parse_legacy_color_value(value); diff --git a/Userland/Libraries/LibWeb/HTML/HTMLTableRowElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLTableRowElement.cpp index 8d6c5b23922..99e76a81f8c 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLTableRowElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLTableRowElement.cpp @@ -48,15 +48,15 @@ void HTMLTableRowElement::apply_presentational_hints(CSS::StyleProperties& style auto color = parse_legacy_color_value(value); if (color.has_value()) style.set_property(CSS::PropertyID::BackgroundColor, CSS::CSSColorValue::create_from_color(color.value())); - return; } else if (name == HTML::AttributeNames::background) { if (auto parsed_value = document().parse_url(value); parsed_value.is_valid()) style.set_property(CSS::PropertyID::BackgroundImage, CSS::ImageStyleValue::create(parsed_value)); - return; + } else if (name == HTML::AttributeNames::height) { + if (auto parsed_value = parse_dimension_value(value)) + style.set_property(CSS::PropertyID::Height, *parsed_value); } else if (name == HTML::AttributeNames::valign) { if (auto parsed_value = parse_css_value(CSS::Parser::ParsingContext { document() }, value, CSS::PropertyID::VerticalAlign)) style.set_property(CSS::PropertyID::VerticalAlign, parsed_value.release_nonnull()); - return; } }); } diff --git a/Userland/Libraries/LibWeb/HTML/HTMLTableSectionElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLTableSectionElement.cpp index c4ae4e309f5..6ac4064bda6 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLTableSectionElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLTableSectionElement.cpp @@ -7,10 +7,15 @@ #include #include +#include +#include +#include +#include #include #include #include #include +#include #include namespace Web::HTML { @@ -95,4 +100,20 @@ WebIDL::ExceptionOr HTMLTableSectionElement::delete_row(WebIDL::Long index return {}; } +void HTMLTableSectionElement::apply_presentational_hints(CSS::StyleProperties& style) const +{ + for_each_attribute([&](auto& name, auto& value) { + // https://html.spec.whatwg.org/multipage/rendering.html#tables-2:encoding-parsing-and-serializing-a-url + if (name == HTML::AttributeNames::background) { + if (auto parsed_value = document().parse_url(value); parsed_value.is_valid()) + style.set_property(CSS::PropertyID::BackgroundImage, CSS::ImageStyleValue::create(parsed_value)); + } + // https://html.spec.whatwg.org/multipage/rendering.html#tables-2:rules-for-parsing-a-legacy-colour-value + else if (name == HTML::AttributeNames::bgcolor) { + if (auto color = parse_legacy_color_value(value); color.has_value()) + style.set_property(CSS::PropertyID::BackgroundColor, CSS::CSSColorValue::create_from_color(color.value())); + } + }); +} + } diff --git a/Userland/Libraries/LibWeb/HTML/HTMLTableSectionElement.h b/Userland/Libraries/LibWeb/HTML/HTMLTableSectionElement.h index 0d8eeda1506..c1503104d79 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLTableSectionElement.h +++ b/Userland/Libraries/LibWeb/HTML/HTMLTableSectionElement.h @@ -37,6 +37,8 @@ private: virtual void initialize(JS::Realm&) override; virtual void visit_edges(Cell::Visitor&) override; + virtual void apply_presentational_hints(CSS::StyleProperties&) const override; + JS::GCPtr mutable m_rows; };