LibHTML: Flesh out <img> element with LayoutImage and LayoutReplaced

This patch adds parsing of <img> into HTMLImageElement objects.
It also adds LayoutImage and its parent class LayoutReplaced, which is
going to represent CSS "replaced elements."
This commit is contained in:
Andreas Kling 2019-10-05 22:07:45 +02:00
parent 7162347563
commit 09dccb3224
Notes: sideshowbarker 2024-07-19 11:48:34 +09:00
11 changed files with 96 additions and 2 deletions

View File

@ -0,0 +1,10 @@
#include <LibHTML/DOM/HTMLImageElement.h>
HTMLImageElement::HTMLImageElement(Document& document, const String& tag_name)
: HTMLElement(document, tag_name)
{
}
HTMLImageElement::~HTMLImageElement()
{
}

View File

@ -0,0 +1,12 @@
#pragma once
#include <LibHTML/DOM/HTMLElement.h>
class HTMLImageElement : public HTMLElement {
public:
HTMLImageElement(Document&, const String& tag_name);
virtual ~HTMLImageElement() override;
String alt() const { return attribute("alt"); }
String src() const { return attribute("src"); }
};

View File

@ -0,0 +1,20 @@
#include <LibHTML/Layout/LayoutImage.h>
LayoutImage::LayoutImage(const HTMLImageElement& element, NonnullRefPtr<StyleProperties> style)
: LayoutReplaced(element, move(style))
{
}
LayoutImage::~LayoutImage()
{
}
void LayoutImage::layout()
{
LayoutReplaced::layout();
}
void LayoutImage::render(RenderingContext& context)
{
LayoutReplaced::render(context);
}

View File

@ -0,0 +1,18 @@
#pragma once
#include <LibHTML/DOM/HTMLImageElement.h>
#include <LibHTML/Layout/LayoutReplaced.h>
class HTMLImageElement;
class LayoutImage : public LayoutReplaced {
public:
LayoutImage(const HTMLImageElement&, NonnullRefPtr<StyleProperties>);
virtual ~LayoutImage() override;
virtual void layout() override;
virtual void render(RenderingContext&) override;
const HTMLImageElement& node() const { return static_cast<const HTMLImageElement&>(LayoutReplaced::node()); }
private:
};

View File

@ -4,6 +4,7 @@
LayoutInline::LayoutInline(const Node& node, RefPtr<StyleProperties> style_properties)
: LayoutNode(&node, move(style_properties))
{
set_inline(true);
}
LayoutInline::~LayoutInline()

View File

@ -10,7 +10,6 @@ public:
virtual ~LayoutInline() override;
virtual const char* class_name() const override { return "LayoutInline"; }
virtual bool is_inline() const override { return true; }
virtual void split_into_lines(LayoutBlock& container);

View File

@ -53,7 +53,9 @@ public:
virtual const char* class_name() const { return "LayoutNode"; }
virtual bool is_text() const { return false; }
virtual bool is_block() const { return false; }
virtual bool is_inline() const { return false; }
bool is_inline() const { return m_inline; }
void set_inline(bool b) { m_inline = b; }
virtual void layout();
virtual void render(RenderingContext&);
@ -81,4 +83,5 @@ private:
RefPtr<StyleProperties> m_style_properties;
BoxModelMetrics m_style;
Rect m_rect;
bool m_inline { false };
};

View File

@ -0,0 +1,13 @@
#include <LibHTML/DOM/Element.h>
#include <LibHTML/Layout/LayoutReplaced.h>
LayoutReplaced::LayoutReplaced(const Element& element, NonnullRefPtr<StyleProperties> style)
: LayoutNode(&element, move(style))
{
// FIXME: Allow non-inline replaced elements.
set_inline(true);
}
LayoutReplaced::~LayoutReplaced()
{
}

View File

@ -0,0 +1,12 @@
#include <LibHTML/DOM/Element.h>
#include <LibHTML/Layout/LayoutNode.h>
class LayoutReplaced : public LayoutNode {
public:
LayoutReplaced(const Element&, NonnullRefPtr<StyleProperties>);
virtual ~LayoutReplaced();
const Element& node() const { return static_cast<const Element&>(*LayoutNode::node()); }
private:
};

View File

@ -12,6 +12,7 @@ LIBHTML_OBJS = \
DOM/HTMLTitleElement.o \
DOM/HTMLBodyElement.o \
DOM/HTMLFontElement.o \
DOM/HTMLImageElement.o \
DOM/Document.o \
DOM/Text.o \
CSS/Selector.o \
@ -29,6 +30,8 @@ LIBHTML_OBJS = \
Layout/LayoutBlock.o \
Layout/LayoutInline.o \
Layout/LayoutDocument.o \
Layout/LayoutReplaced.o \
Layout/LayoutImage.o \
Layout/BoxModelMetrics.o \
Layout/LineBox.o \
Layout/LineBoxFragment.o \

View File

@ -9,6 +9,7 @@
#include <LibHTML/DOM/HTMLHeadElement.h>
#include <LibHTML/DOM/HTMLHeadingElement.h>
#include <LibHTML/DOM/HTMLHtmlElement.h>
#include <LibHTML/DOM/HTMLImageElement.h>
#include <LibHTML/DOM/HTMLStyleElement.h>
#include <LibHTML/DOM/HTMLTitleElement.h>
#include <LibHTML/DOM/Text.h>
@ -35,6 +36,8 @@ static NonnullRefPtr<Element> create_element(Document& document, const String& t
return adopt(*new HTMLStyleElement(document, tag_name));
if (lowercase_tag_name == "title")
return adopt(*new HTMLTitleElement(document, tag_name));
if (lowercase_tag_name == "img")
return adopt(*new HTMLImageElement(document, tag_name));
if (lowercase_tag_name == "h1"
|| lowercase_tag_name == "h2"
|| lowercase_tag_name == "h3"