summaryrefslogtreecommitdiff
path: root/Libraries/LibHTML
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-10-05 22:07:45 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-10-05 23:29:01 +0200
commit09dccb32245485e626048edfec484c52de145007 (patch)
tree780f134b4b620977e7e48d84dd472e88de1fdb96 /Libraries/LibHTML
parent7162347563ded87d57066080d481222505eca6df (diff)
downloadserenity-09dccb32245485e626048edfec484c52de145007.zip
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."
Diffstat (limited to 'Libraries/LibHTML')
-rw-r--r--Libraries/LibHTML/DOM/HTMLImageElement.cpp10
-rw-r--r--Libraries/LibHTML/DOM/HTMLImageElement.h12
-rw-r--r--Libraries/LibHTML/Layout/LayoutImage.cpp20
-rw-r--r--Libraries/LibHTML/Layout/LayoutImage.h18
-rw-r--r--Libraries/LibHTML/Layout/LayoutInline.cpp1
-rw-r--r--Libraries/LibHTML/Layout/LayoutInline.h1
-rw-r--r--Libraries/LibHTML/Layout/LayoutNode.h5
-rw-r--r--Libraries/LibHTML/Layout/LayoutReplaced.cpp13
-rw-r--r--Libraries/LibHTML/Layout/LayoutReplaced.h12
-rw-r--r--Libraries/LibHTML/Makefile.shared3
-rw-r--r--Libraries/LibHTML/Parser/HTMLParser.cpp3
11 files changed, 96 insertions, 2 deletions
diff --git a/Libraries/LibHTML/DOM/HTMLImageElement.cpp b/Libraries/LibHTML/DOM/HTMLImageElement.cpp
new file mode 100644
index 0000000000..864cc2b91e
--- /dev/null
+++ b/Libraries/LibHTML/DOM/HTMLImageElement.cpp
@@ -0,0 +1,10 @@
+#include <LibHTML/DOM/HTMLImageElement.h>
+
+HTMLImageElement::HTMLImageElement(Document& document, const String& tag_name)
+ : HTMLElement(document, tag_name)
+{
+}
+
+HTMLImageElement::~HTMLImageElement()
+{
+}
diff --git a/Libraries/LibHTML/DOM/HTMLImageElement.h b/Libraries/LibHTML/DOM/HTMLImageElement.h
new file mode 100644
index 0000000000..0bb37bd2ed
--- /dev/null
+++ b/Libraries/LibHTML/DOM/HTMLImageElement.h
@@ -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"); }
+};
diff --git a/Libraries/LibHTML/Layout/LayoutImage.cpp b/Libraries/LibHTML/Layout/LayoutImage.cpp
new file mode 100644
index 0000000000..36dcafafaf
--- /dev/null
+++ b/Libraries/LibHTML/Layout/LayoutImage.cpp
@@ -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);
+}
diff --git a/Libraries/LibHTML/Layout/LayoutImage.h b/Libraries/LibHTML/Layout/LayoutImage.h
new file mode 100644
index 0000000000..0a6105a891
--- /dev/null
+++ b/Libraries/LibHTML/Layout/LayoutImage.h
@@ -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:
+};
diff --git a/Libraries/LibHTML/Layout/LayoutInline.cpp b/Libraries/LibHTML/Layout/LayoutInline.cpp
index 345bfbdef6..60f195faf5 100644
--- a/Libraries/LibHTML/Layout/LayoutInline.cpp
+++ b/Libraries/LibHTML/Layout/LayoutInline.cpp
@@ -4,6 +4,7 @@
LayoutInline::LayoutInline(const Node& node, RefPtr<StyleProperties> style_properties)
: LayoutNode(&node, move(style_properties))
{
+ set_inline(true);
}
LayoutInline::~LayoutInline()
diff --git a/Libraries/LibHTML/Layout/LayoutInline.h b/Libraries/LibHTML/Layout/LayoutInline.h
index 34dfba49d6..8eecf9fc49 100644
--- a/Libraries/LibHTML/Layout/LayoutInline.h
+++ b/Libraries/LibHTML/Layout/LayoutInline.h
@@ -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);
diff --git a/Libraries/LibHTML/Layout/LayoutNode.h b/Libraries/LibHTML/Layout/LayoutNode.h
index 2055a12f4e..f75da03d6c 100644
--- a/Libraries/LibHTML/Layout/LayoutNode.h
+++ b/Libraries/LibHTML/Layout/LayoutNode.h
@@ -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 };
};
diff --git a/Libraries/LibHTML/Layout/LayoutReplaced.cpp b/Libraries/LibHTML/Layout/LayoutReplaced.cpp
new file mode 100644
index 0000000000..2634be9b09
--- /dev/null
+++ b/Libraries/LibHTML/Layout/LayoutReplaced.cpp
@@ -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()
+{
+}
diff --git a/Libraries/LibHTML/Layout/LayoutReplaced.h b/Libraries/LibHTML/Layout/LayoutReplaced.h
new file mode 100644
index 0000000000..40f11a283c
--- /dev/null
+++ b/Libraries/LibHTML/Layout/LayoutReplaced.h
@@ -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:
+};
diff --git a/Libraries/LibHTML/Makefile.shared b/Libraries/LibHTML/Makefile.shared
index 3427f2bd06..831eefa4c2 100644
--- a/Libraries/LibHTML/Makefile.shared
+++ b/Libraries/LibHTML/Makefile.shared
@@ -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 \
diff --git a/Libraries/LibHTML/Parser/HTMLParser.cpp b/Libraries/LibHTML/Parser/HTMLParser.cpp
index 4d48f0b82e..eddf861b39 100644
--- a/Libraries/LibHTML/Parser/HTMLParser.cpp
+++ b/Libraries/LibHTML/Parser/HTMLParser.cpp
@@ -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"