From f5bf8f6380f2442ffe8446cd9a8f363afc0f91d6 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 11 Oct 2019 23:16:53 +0200 Subject: [PATCH] LibHTML: Add LayoutNode classes for "display: list-item" and its marker This patch removes the hard-coded hack for "display: list-item" from LayoutBlock and adds LayoutListItem and LayoutListItemMarker. Elements with "display: list-item" now generate a LayoutListItem, which may then also generate a LayoutListItemMarker if appropriate. :^) --- Libraries/LibHTML/DOM/Element.cpp | 5 +++- Libraries/LibHTML/Layout/LayoutBlock.cpp | 12 ---------- Libraries/LibHTML/Layout/LayoutListItem.cpp | 24 +++++++++++++++++++ Libraries/LibHTML/Layout/LayoutListItem.h | 19 +++++++++++++++ .../LibHTML/Layout/LayoutListItemMarker.cpp | 18 ++++++++++++++ .../LibHTML/Layout/LayoutListItemMarker.h | 14 +++++++++++ Libraries/LibHTML/Makefile.shared | 2 ++ 7 files changed, 81 insertions(+), 13 deletions(-) create mode 100644 Libraries/LibHTML/Layout/LayoutListItem.cpp create mode 100644 Libraries/LibHTML/Layout/LayoutListItem.h create mode 100644 Libraries/LibHTML/Layout/LayoutListItemMarker.cpp create mode 100644 Libraries/LibHTML/Layout/LayoutListItemMarker.h diff --git a/Libraries/LibHTML/DOM/Element.cpp b/Libraries/LibHTML/DOM/Element.cpp index 5ef79e9986..2627e628f8 100644 --- a/Libraries/LibHTML/DOM/Element.cpp +++ b/Libraries/LibHTML/DOM/Element.cpp @@ -2,6 +2,7 @@ #include #include #include +#include Element::Element(Document& document, const String& tag_name) : ParentNode(document, NodeType::ELEMENT_NODE) @@ -78,10 +79,12 @@ RefPtr Element::create_layout_node(const StyleResolver& resolver, co if (display == "none") return nullptr; - if (display == "block" || display == "list-item") + if (display == "block") return adopt(*new LayoutBlock(this, move(style))); if (display == "inline") return adopt(*new LayoutInline(*this, move(style))); + if (display == "list-item") + return adopt(*new LayoutListItem(*this, move(style))); ASSERT_NOT_REACHED(); } diff --git a/Libraries/LibHTML/Layout/LayoutBlock.cpp b/Libraries/LibHTML/Layout/LayoutBlock.cpp index dca51eebca..92e3d891bf 100644 --- a/Libraries/LibHTML/Layout/LayoutBlock.cpp +++ b/Libraries/LibHTML/Layout/LayoutBlock.cpp @@ -199,18 +199,6 @@ void LayoutBlock::render(RenderingContext& context) LayoutNode::render(context); - // FIXME: position this properly - if (style().string_or_fallback(CSS::PropertyID::Display, "block") == "list-item") { - Rect bullet_rect { - rect().x() - 8, - rect().y() + 4, - 3, - 3 - }; - - context.painter().fill_rect(bullet_rect, style().color_or_fallback(CSS::PropertyID::Color, document(), Color::Black)); - } - if (children_are_inline()) { for (auto& line_box : m_line_boxes) { for (auto& fragment : line_box.fragments()) { diff --git a/Libraries/LibHTML/Layout/LayoutListItem.cpp b/Libraries/LibHTML/Layout/LayoutListItem.cpp new file mode 100644 index 0000000000..b9174e6569 --- /dev/null +++ b/Libraries/LibHTML/Layout/LayoutListItem.cpp @@ -0,0 +1,24 @@ +#include +#include + +LayoutListItem::LayoutListItem(const Element& element, NonnullRefPtr style) + : LayoutBlock(&element, move(style)) +{ +} + +LayoutListItem::~LayoutListItem() +{ +} + +void LayoutListItem::layout() +{ + LayoutBlock::layout(); + + if (!m_marker) { + m_marker = adopt(*new LayoutListItemMarker); + prepend_child(*m_marker); + } + + Rect marker_rect { rect().x() - 8, rect().y(), 4, rect().height() }; + m_marker->set_rect(marker_rect); +} diff --git a/Libraries/LibHTML/Layout/LayoutListItem.h b/Libraries/LibHTML/Layout/LayoutListItem.h new file mode 100644 index 0000000000..c06aff083f --- /dev/null +++ b/Libraries/LibHTML/Layout/LayoutListItem.h @@ -0,0 +1,19 @@ +#pragma once + +#include +#include + +class LayoutListItemMarker; + +class LayoutListItem final : public LayoutBlock { +public: + LayoutListItem(const Element&, NonnullRefPtr); + virtual ~LayoutListItem() override; + + virtual void layout() override; + +private: + virtual const char* class_name() const override { return "LayoutListItem"; } + + RefPtr m_marker; +}; diff --git a/Libraries/LibHTML/Layout/LayoutListItemMarker.cpp b/Libraries/LibHTML/Layout/LayoutListItemMarker.cpp new file mode 100644 index 0000000000..de2698049b --- /dev/null +++ b/Libraries/LibHTML/Layout/LayoutListItemMarker.cpp @@ -0,0 +1,18 @@ +#include +#include + +LayoutListItemMarker::LayoutListItemMarker() + : LayoutNode(nullptr) +{ +} + +LayoutListItemMarker::~LayoutListItemMarker() +{ +} + +void LayoutListItemMarker::render(RenderingContext& context) +{ + Rect bullet_rect { 0, 0, 4, 4 }; + bullet_rect.center_within(rect()); + context.painter().fill_rect(bullet_rect, style().color_or_fallback(CSS::PropertyID::Color, document(), Color::Black)); +} diff --git a/Libraries/LibHTML/Layout/LayoutListItemMarker.h b/Libraries/LibHTML/Layout/LayoutListItemMarker.h new file mode 100644 index 0000000000..245c182c6f --- /dev/null +++ b/Libraries/LibHTML/Layout/LayoutListItemMarker.h @@ -0,0 +1,14 @@ +#pragma once + +#include + +class LayoutListItemMarker final : public LayoutNode { +public: + LayoutListItemMarker(); + virtual ~LayoutListItemMarker() override; + + virtual void render(RenderingContext&) override; + +private: + virtual const char* class_name() const override { return "LayoutListItemMarker"; } +}; diff --git a/Libraries/LibHTML/Makefile.shared b/Libraries/LibHTML/Makefile.shared index ee4ee44011..e57c9ef9f9 100644 --- a/Libraries/LibHTML/Makefile.shared +++ b/Libraries/LibHTML/Makefile.shared @@ -36,6 +36,8 @@ LIBHTML_OBJS = \ Layout/LayoutDocument.o \ Layout/LayoutReplaced.o \ Layout/LayoutImage.o \ + Layout/LayoutListItem.o \ + Layout/LayoutListItemMarker.o \ Layout/BoxModelMetrics.o \ Layout/LineBox.o \ Layout/LineBoxFragment.o \