Truly independent web browser
LibWeb: Center align=middle images on their parent's baseline
Jelle Raaijmakerscb24b96e9e39
diff --git a/Libraries/LibWeb/CSS/Enums.json b/Libraries/LibWeb/CSS/Enums.jsonindex 16de58a4daf..17b921303dc 100644--- a/Libraries/LibWeb/CSS/Enums.json+++ b/Libraries/LibWeb/CSS/Enums.json@@ -1243,7 +1243,8 @@"super","text-bottom","text-top",- "top"+ "top",+ "-libweb-baseline-middle"],"visibility": ["collapse",diff --git a/Libraries/LibWeb/CSS/Keywords.json b/Libraries/LibWeb/CSS/Keywords.jsonindex ea1d4eb0e5f..5d9ce3df8c1 100644--- a/Libraries/LibWeb/CSS/Keywords.json+++ b/Libraries/LibWeb/CSS/Keywords.json@@ -1,5 +1,6 @@["-infinity",+ "-libweb-baseline-middle","-libweb-buttonfacedisabled","-libweb-buttonfacehover","-libweb-center",diff --git a/Libraries/LibWeb/HTML/HTMLEmbedElement.cpp b/Libraries/LibWeb/HTML/HTMLEmbedElement.cppindex d61e4b4b6de..748295c4d15 100644--- a/Libraries/LibWeb/HTML/HTMLEmbedElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLEmbedElement.cpp@@ -44,10 +44,8 @@ void HTMLEmbedElement::apply_presentational_hints(Vector<CSS::StyleProperty>& pr// has an align attribute whose value is an ASCII case-insensitive match for the string "center" or the string "middle",// the user agent is expected to act as if the element's 'vertical-align' property was set to a value that aligns the// vertical middle of the element with the parent element's baseline.- // FIXME: This should use legacy baseline-middle alignment instead of CSS vertical-align: middle,- // as Firefox and Chrome do with engine-specific legacy values.if (value.equals_ignoring_ascii_case(u"center"sv) || value.equals_ignoring_ascii_case(u"middle"sv))- properties.append({ .property_id = CSS::PropertyID::VerticalAlign, .value = CSS::KeywordStyleValue::create(CSS::Keyword::Middle) });+ properties.append({ .property_id = CSS::PropertyID::VerticalAlign, .value = CSS::KeywordStyleValue::create(CSS::Keyword::LibwebBaselineMiddle) });} else if (name == HTML::AttributeNames::height) {if (auto parsed_value = parse_dimension_value(value))properties.append({ .property_id = CSS::PropertyID::Height, .value = *parsed_value });diff --git a/Libraries/LibWeb/HTML/HTMLIFrameElement.cpp b/Libraries/LibWeb/HTML/HTMLIFrameElement.cppindex 6b60390211e..98cc9571551 100644--- a/Libraries/LibWeb/HTML/HTMLIFrameElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLIFrameElement.cpp@@ -5,6 +5,7 @@* SPDX-License-Identifier: BSD-2-Clause*/+#include <AK/GenericShorthands.h>#include <LibGC/Heap.h>#include <LibWeb/CSS/ElementBoxKind.h>#include <LibWeb/CSS/Invalidation/EmbeddedContentInvalidator.h>@@ -12,6 +13,7 @@#include <LibWeb/CSS/StyleComputer.h>#include <LibWeb/CSS/StyleEngineInput.h>#include <LibWeb/CSS/StyleValues/DisplayStyleValue.h>+#include <LibWeb/CSS/StyleValues/KeywordStyleValue.h>#include <LibWeb/CSS/StyleValues/LengthStyleValue.h>#include <LibWeb/DOM/DOMTokenList.h>#include <LibWeb/DOM/Document.h>@@ -324,13 +326,23 @@ bool HTMLIFrameElement::is_presentational_hint(Utf16FlyString const& name) constif (Base::is_presentational_hint(name))return true;- return name == HTML::AttributeNames::frameborder;+ return first_is_one_of(name, HTML::AttributeNames::align, HTML::AttributeNames::frameborder);}void HTMLIFrameElement::apply_presentational_hints(Vector<CSS::StyleProperty>& properties) const{Base::apply_presentational_hints(properties);+ // https://html.spec.whatwg.org/multipage/rendering.html#attributes-for-embedded-content-and-images+ // When an embed, iframe, img, or object element, or an input element whose type attribute is in the Image Button state,+ // has an align attribute whose value is an ASCII case-insensitive match for the string "center" or the string "middle",+ // the user agent is expected to act as if the element's 'vertical-align' property was set to a value that aligns the+ // vertical middle of the element with the parent element's baseline.+ if (auto align = get_attribute(HTML::AttributeNames::align); align.has_value()) {+ if (align->equals_ignoring_ascii_case(u"center"sv) || align->equals_ignoring_ascii_case(u"middle"sv))+ properties.append({ .property_id = CSS::PropertyID::VerticalAlign, .value = CSS::KeywordStyleValue::create(CSS::Keyword::LibwebBaselineMiddle) });+ }+// https://html.spec.whatwg.org/multipage/rendering.html#attributes-for-embedded-content-and-images:attr-iframe-frameborder// When an iframe element has a frameborder attribute whose value, when parsed using the rules for parsing integers,// is zero or an error, the user agent is expected to have presentational hints setting the element'sdiff --git a/Libraries/LibWeb/HTML/HTMLImageElement.cpp b/Libraries/LibWeb/HTML/HTMLImageElement.cppindex 5d3470a217d..53519d80575 100644--- a/Libraries/LibWeb/HTML/HTMLImageElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLImageElement.cpp@@ -207,6 +207,7 @@ bool HTMLImageElement::is_presentational_hint(Utf16FlyString const& name) constreturn true;return first_is_one_of(name,+ HTML::AttributeNames::align,HTML::AttributeNames::hspace,HTML::AttributeNames::vspace,HTML::AttributeNames::border);@@ -216,7 +217,15 @@ void HTMLImageElement::apply_presentational_hints(Vector<CSS::StyleProperty>& pr{Base::apply_presentational_hints(properties);for_each_attribute([&](Utf16FlyString const& name, Utf16View value) {- if (name == HTML::AttributeNames::hspace) {+ if (name == HTML::AttributeNames::align) {+ // https://html.spec.whatwg.org/multipage/rendering.html#attributes-for-embedded-content-and-images+ // When an embed, iframe, img, or object element, or an input element whose type attribute is in the Image Button state,+ // has an align attribute whose value is an ASCII case-insensitive match for the string "center" or the string "middle",+ // the user agent is expected to act as if the element's 'vertical-align' property was set to a value that aligns the+ // vertical middle of the element with the parent element's baseline.+ if (value.equals_ignoring_ascii_case(u"center"sv) || value.equals_ignoring_ascii_case(u"middle"sv))+ properties.append({ .property_id = CSS::PropertyID::VerticalAlign, .value = CSS::KeywordStyleValue::create(CSS::Keyword::LibwebBaselineMiddle) });+ } else if (name == HTML::AttributeNames::hspace) {if (auto parsed_value = parse_dimension_value(value)) {properties.append({ .property_id = CSS::PropertyID::MarginLeft, .value = *parsed_value });properties.append({ .property_id = CSS::PropertyID::MarginRight, .value = *parsed_value });diff --git a/Libraries/LibWeb/HTML/HTMLInputElement.cpp b/Libraries/LibWeb/HTML/HTMLInputElement.cppindex c0e9b0b2a52..83e45a871ca 100644--- a/Libraries/LibWeb/HTML/HTMLInputElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLInputElement.cpp@@ -2315,10 +2315,8 @@ void HTMLInputElement::apply_presentational_hints(Vector<CSS::StyleProperty>& pr// has an align attribute whose value is an ASCII case-insensitive match for the string "center" or the string "middle",// the user agent is expected to act as if the element's 'vertical-align' property was set to a value that aligns the// vertical middle of the element with the parent element's baseline.- // FIXME: This should use legacy baseline-middle alignment instead of CSS vertical-align: middle,- // as Firefox and Chrome do with engine-specific legacy values.if (value.equals_ignoring_ascii_case(u"center"sv) || value.equals_ignoring_ascii_case(u"middle"sv))- properties.append({ .property_id = CSS::PropertyID::VerticalAlign, .value = CSS::KeywordStyleValue::create(CSS::Keyword::Middle) });+ properties.append({ .property_id = CSS::PropertyID::VerticalAlign, .value = CSS::KeywordStyleValue::create(CSS::Keyword::LibwebBaselineMiddle) });} else if (name == HTML::AttributeNames::border) {if (auto parsed_value = parse_non_negative_integer(value); parsed_value.has_value()) {auto width_style_value = CSS::LengthStyleValue::create(CSS::Length::make_px(*parsed_value));diff --git a/Libraries/LibWeb/HTML/HTMLObjectElement.cpp b/Libraries/LibWeb/HTML/HTMLObjectElement.cppindex 19785456745..a46c57a128b 100644--- a/Libraries/LibWeb/HTML/HTMLObjectElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLObjectElement.cpp@@ -145,10 +145,8 @@ void HTMLObjectElement::apply_presentational_hints(Vector<CSS::StyleProperty>& p// has an align attribute whose value is an ASCII case-insensitive match for the string "center" or the string "middle",// the user agent is expected to act as if the element's 'vertical-align' property was set to a value that aligns the// vertical middle of the element with the parent element's baseline.- // FIXME: This should use legacy baseline-middle alignment instead of CSS vertical-align: middle,- // as Firefox and Chrome do with engine-specific legacy values.if (value.equals_ignoring_ascii_case(u"center"sv) || value.equals_ignoring_ascii_case(u"middle"sv))- properties.append({ .property_id = CSS::PropertyID::VerticalAlign, .value = CSS::KeywordStyleValue::create(CSS::Keyword::Middle) });+ properties.append({ .property_id = CSS::PropertyID::VerticalAlign, .value = CSS::KeywordStyleValue::create(CSS::Keyword::LibwebBaselineMiddle) });} else if (name == HTML::AttributeNames::border) {if (auto parsed_value = parse_non_negative_integer(value); parsed_value.has_value()) {auto width_style_value = CSS::LengthStyleValue::create(CSS::Length::make_px(*parsed_value));diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rsindex 8f26983dff0..954f59dfda7 100644--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs@@ -606,6 +606,10 @@ pub(crate) fn box_baseline_with_content_baselines(return used.margin_box_block_size(collapsed) / 2+ CssPixels::nearest_value_for_f32(containing_style.font_x_height() / 2.0);}+ vertical_align::_LIBWEB_BASELINE_MIDDLE => {+ // NB: The vertical midpoint of the box is aligned with the baseline of the parent box.+ return used.margin_box_block_size(collapsed) / 2;+ }vertical_align::BOTTOM => {// Bottom: Align the bottom of the aligned subtree with the bottom of the line box.return used.content_block_size.get() + used.margin_box_top(collapsed);diff --git a/Libraries/LibWeb/Rust/src/layout/line_builder.rs b/Libraries/LibWeb/Rust/src/layout/line_builder.rsindex da8e55cc1f7..0013af63f72 100644--- a/Libraries/LibWeb/Rust/src/layout/line_builder.rs+++ b/Libraries/LibWeb/Rust/src/layout/line_builder.rs@@ -686,6 +686,14 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {- metrics.block_size)/ 2}+ vertical_align::_LIBWEB_BASELINE_MIDDLE => {+ self.current_block_offset+ + line_box_baseline+ + (metrics.effective_box_block_start_offset+ - metrics.effective_box_block_end_offset+ - metrics.block_size)+ / 2+ }vertical_align::SUB => alphabetic + self.containing_style().font_size() / 5,vertical_align::SUPER => alphabetic - self.containing_style().font_size() / 3,// A top- or bottom-aligned box is the root of an aligned subtree, laid out on the baseline here anddiff --git a/Tests/LibWeb/Text/expected/HTML/align-presentational-hint.txt b/Tests/LibWeb/Text/expected/HTML/align-presentational-hint.txtindex 79c5584df4b..03d927d674a 100644--- a/Tests/LibWeb/Text/expected/HTML/align-presentational-hint.txt+++ b/Tests/LibWeb/Text/expected/HTML/align-presentational-hint.txt@@ -1,6 +1,22 @@-object align=center: middle-embed align=center: middle-input image align=center: middle-object align=middle: middle-embed align=middle: middle-input image align=middle: middle+Without a user style:+object align=center: -libweb-baseline-middle+embed align=center: -libweb-baseline-middle+input image align=center: -libweb-baseline-middle+iframe align=center: -libweb-baseline-middle+img align=center: -libweb-baseline-middle+object align=middle: -libweb-baseline-middle+embed align=middle: -libweb-baseline-middle+input image align=middle: -libweb-baseline-middle+iframe align=middle: -libweb-baseline-middle+img align=middle: -libweb-baseline-middle+With a user style:+object align=center: -libweb-baseline-middle+embed align=center: -libweb-baseline-middle+input image align=center: -libweb-baseline-middle+iframe align=center: -libweb-baseline-middle+img align=center: -libweb-baseline-middle+object align=middle: -libweb-baseline-middle+embed align=middle: -libweb-baseline-middle+input image align=middle: -libweb-baseline-middle+iframe align=middle: -libweb-baseline-middle+img align=middle: -libweb-baseline-middlediff --git a/Tests/LibWeb/Text/expected/wpt-import/html/rendering/replaced-elements/attributes-for-embedded-content-and-images/align.txt b/Tests/LibWeb/Text/expected/wpt-import/html/rendering/replaced-elements/attributes-for-embedded-content-and-images/align.txtnew file mode 100644index 00000000000..925426844b1--- /dev/null+++ b/Tests/LibWeb/Text/expected/wpt-import/html/rendering/replaced-elements/attributes-for-embedded-content-and-images/align.txt@@ -0,0 +1,29 @@+Harness status: OK++Found 23 tests++21 Pass+2 Fail+Pass align attribute mapping+Pass align=left on replaced elements+Pass align=left on non-replaced elements+Pass align=right on replaced elements+Pass align=right on non-replaced elements+Pass align=top on replaced elements+Pass align=top on non-replaced elements+Pass align=middle on replaced elements+Pass align=middle on non-replaced elements+Pass align=center on replaced elements+Pass align=center on non-replaced elements+Pass align=baseline on replaced elements+Pass align=baseline on non-replaced elements+Fail align=bottom on replaced elements+Pass align=bottom on non-replaced elements+Pass align=texttop on replaced elements+Pass align=texttop on non-replaced elements+Pass align=absmiddle on replaced elements+Pass align=absmiddle on non-replaced elements+Pass align=abscenter on replaced elements+Pass align=abscenter on non-replaced elements+Fail align=absbottom on replaced elements+Pass align=absbottom on non-replaced elements\ No newline at end of filediff --git a/Tests/LibWeb/Text/input/HTML/align-presentational-hint.html b/Tests/LibWeb/Text/input/HTML/align-presentational-hint.htmlindex 9a5468a5a4d..151cf7bf95c 100644--- a/Tests/LibWeb/Text/input/HTML/align-presentational-hint.html+++ b/Tests/LibWeb/Text/input/HTML/align-presentational-hint.html@@ -2,21 +2,34 @@<script src="../include.js"></script><script>test(() => {+ const elements = [];for (const align of ["center", "middle"]) {for (const [label, element] of [["object", document.createElement("object")],["embed", document.createElement("embed")],["input image", document.createElement("input")],+ ["iframe", document.createElement("iframe")],+ ["img", document.createElement("img")],]) {element.setAttribute("align", align);if (element.localName === "input")element.type = "image";document.body.appendChild(element);-- const style = getComputedStyle(element);- println(`${label} align=${align}: ${style.verticalAlign}`);+ elements.push([`${label} align=${align}`, element]);}}++ const printVerticalAlign = () => {+ for (const [label, element] of elements)+ println(`${label}: ${getComputedStyle(element).verticalAlign}`);+ };++ println("Without a user style:");+ printVerticalAlign();++ internals.setUserStyle("embed, iframe, img, input, object { vertical-align: top; }");+ println("With a user style:");+ printVerticalAlign();});</script>diff --git a/Tests/LibWeb/Text/input/wpt-import/html/rendering/replaced-elements/attributes-for-embedded-content-and-images/align.html b/Tests/LibWeb/Text/input/wpt-import/html/rendering/replaced-elements/attributes-for-embedded-content-and-images/align.htmlnew file mode 100644index 00000000000..e1728a3a49c--- /dev/null+++ b/Tests/LibWeb/Text/input/wpt-import/html/rendering/replaced-elements/attributes-for-embedded-content-and-images/align.html@@ -0,0 +1,71 @@+<!doctype html>+<title>align attribute mapping on replaced elements</title>+<script src="../../../../resources/testharness.js"></script>+<script src="../../../../resources/testharnessreport.js"></script>+<img id="replaced" src="../../../../images/green.png">+<something id="non-replaced"></something>+<script>+const kMapping = {+ "left": ["float", "left"],+ "right": ["float", "right"],++ "top": ["vertical-align", "top"],++ // These two require a magic value (`-moz-middle-with-baseline` on Gecko,+ // `-webkit-baseline-middle` on WebKit-based browsers).+ "middle": ["vertical-align", undefined],+ "center": ["vertical-align", undefined],++ "baseline": ["vertical-align", "baseline"],+ "bottom": ["vertical-align", "baseline"], // *shrug*++ "texttop": ["vertical-align", "text-top"],+ "absmiddle": ["vertical-align", "middle"],+ "abscenter": ["vertical-align", "middle"],+ "absbottom": ["vertical-align", "bottom"],+};++const kInitialValues = {+ "vertical-align": "baseline",+ "float": "none",+};++const kVerticalAlignKeywords = [+ "baseline",+ "sub",+ "super",+ "text-top",+ "text-bottom",+ "middle",+ "top",+ "bottom",+];++let replaced = document.getElementById("replaced");+let nonReplaced = document.getElementById("non-replaced");+let t = async_test("align attribute mapping");+onload = t.step_func_done(function() {+ for (const attributeValue in kMapping) {+ for (const element of [replaced, nonReplaced]) {+ test(function() {+ element.setAttribute("align", attributeValue);+ let [property, expected] = kMapping[attributeValue];+ let actual = getComputedStyle(element).getPropertyValue(property);+ if (element == nonReplaced) {+ assert_equals(actual, kInitialValues[property], "align shouldn't map to non-replaced elements")+ } else {+ if (expected) {+ assert_equals(actual, expected, `align=${attributeValue} should map to ${property}: ${expected}`);+ } else {+ assert_equals(property, "vertical-align");+ assert_false(+ kVerticalAlignKeywords.includes(actual),+ `align=${attributeValue} should map to a special vertical-align value`+ );+ }+ }+ }, `align=${attributeValue} on ${element == replaced ? "replaced" : "non-replaced"} elements`);+ }+ }+});+</script>