瀏覽代碼

LibWeb: Support more presentational hints from the HTML spec

Specifically, this adds support for the align attribute when applied to
heading, paragraph and caption elements.
implicitfield 2 年之前
父節點
當前提交
51f41ea997

+ 19 - 0
Userland/Libraries/LibWeb/HTML/HTMLHeadingElement.cpp

@@ -16,4 +16,23 @@ HTMLHeadingElement::HTMLHeadingElement(DOM::Document& document, DOM::QualifiedNa
 }
 
 HTMLHeadingElement::~HTMLHeadingElement() = default;
+
+// https://html.spec.whatwg.org/multipage/rendering.html#tables-2
+void HTMLHeadingElement::apply_presentational_hints(CSS::StyleProperties& style) const
+{
+    HTMLElement::apply_presentational_hints(style);
+    for_each_attribute([&](auto& name, auto& value) {
+        if (name.equals_ignoring_case("align"sv)) {
+            if (value == "left"sv)
+                style.set_property(CSS::PropertyID::TextAlign, CSS::IdentifierStyleValue::create(CSS::ValueID::Left));
+            else if (value == "right"sv)
+                style.set_property(CSS::PropertyID::TextAlign, CSS::IdentifierStyleValue::create(CSS::ValueID::Right));
+            else if (value == "center"sv)
+                style.set_property(CSS::PropertyID::TextAlign, CSS::IdentifierStyleValue::create(CSS::ValueID::Center));
+            else if (value == "justify"sv)
+                style.set_property(CSS::PropertyID::TextAlign, CSS::IdentifierStyleValue::create(CSS::ValueID::Justify));
+        }
+    });
+}
+
 }

+ 2 - 0
Userland/Libraries/LibWeb/HTML/HTMLHeadingElement.h

@@ -16,6 +16,8 @@ class HTMLHeadingElement final : public HTMLElement {
 public:
     virtual ~HTMLHeadingElement() override;
 
+    virtual void apply_presentational_hints(CSS::StyleProperties&) const override;
+
 private:
     HTMLHeadingElement(DOM::Document&, DOM::QualifiedName);
 };

+ 18 - 0
Userland/Libraries/LibWeb/HTML/HTMLParagraphElement.cpp

@@ -17,4 +17,22 @@ HTMLParagraphElement::HTMLParagraphElement(DOM::Document& document, DOM::Qualifi
 
 HTMLParagraphElement::~HTMLParagraphElement() = default;
 
+// https://html.spec.whatwg.org/multipage/rendering.html#tables-2
+void HTMLParagraphElement::apply_presentational_hints(CSS::StyleProperties& style) const
+{
+    HTMLElement::apply_presentational_hints(style);
+    for_each_attribute([&](auto& name, auto& value) {
+        if (name.equals_ignoring_case("align"sv)) {
+            if (value == "left"sv)
+                style.set_property(CSS::PropertyID::TextAlign, CSS::IdentifierStyleValue::create(CSS::ValueID::Left));
+            else if (value == "right"sv)
+                style.set_property(CSS::PropertyID::TextAlign, CSS::IdentifierStyleValue::create(CSS::ValueID::Right));
+            else if (value == "center"sv)
+                style.set_property(CSS::PropertyID::TextAlign, CSS::IdentifierStyleValue::create(CSS::ValueID::Center));
+            else if (value == "justify"sv)
+                style.set_property(CSS::PropertyID::TextAlign, CSS::IdentifierStyleValue::create(CSS::ValueID::Justify));
+        }
+    });
+}
+
 }

+ 2 - 0
Userland/Libraries/LibWeb/HTML/HTMLParagraphElement.h

@@ -16,6 +16,8 @@ class HTMLParagraphElement final : public HTMLElement {
 public:
     virtual ~HTMLParagraphElement() override;
 
+    virtual void apply_presentational_hints(CSS::StyleProperties&) const override;
+
 private:
     HTMLParagraphElement(DOM::Document&, DOM::QualifiedName);
 };

+ 12 - 0
Userland/Libraries/LibWeb/HTML/HTMLTableCaptionElement.cpp

@@ -17,4 +17,16 @@ HTMLTableCaptionElement::HTMLTableCaptionElement(DOM::Document& document, DOM::Q
 
 HTMLTableCaptionElement::~HTMLTableCaptionElement() = default;
 
+// https://html.spec.whatwg.org/multipage/rendering.html#tables-2
+void HTMLTableCaptionElement::apply_presentational_hints(CSS::StyleProperties& style) const
+{
+    HTMLElement::apply_presentational_hints(style);
+    for_each_attribute([&](auto& name, auto& value) {
+        if (name.equals_ignoring_case("align"sv)) {
+            if (value == "bottom"sv)
+                style.set_property(CSS::PropertyID::CaptionSide, CSS::IdentifierStyleValue::create(CSS::ValueID::Bottom));
+        }
+    });
+}
+
 }

+ 2 - 0
Userland/Libraries/LibWeb/HTML/HTMLTableCaptionElement.h

@@ -16,6 +16,8 @@ class HTMLTableCaptionElement final : public HTMLElement {
 public:
     virtual ~HTMLTableCaptionElement() override;
 
+    virtual void apply_presentational_hints(CSS::StyleProperties&) const override;
+
 private:
     HTMLTableCaptionElement(DOM::Document&, DOM::QualifiedName);
 };