LibWeb: Start adding support for the <iframe> element! :^)

This patch introduces a bunch of things:

- Subframes (Web::Frame::create_subframe())
- HTMLIFrameElement (loads and owns the hosted Web::Frame)
- LayoutFrame (layout and rendering of the hosted frame)

There's still a huge number of things missing, like scrolling, overflow
handling, event handling, scripting, etc. But we can make a little
iframe in a document and it actually renders another document there.
I think that's pretty cool! :^)
This commit is contained in:
Andreas Kling 2020-06-05 23:36:02 +02:00
parent f2aa21ebc4
commit 422bbe98a5
Notes: sideshowbarker 2024-07-19 05:48:55 +09:00
13 changed files with 321 additions and 0 deletions

View File

@ -0,0 +1,2 @@
This is inside the frame!
<img src=90s-bg.png>

View File

@ -0,0 +1,6 @@
<body>
<p>This page has an iframe in it
<p>Cool huh?
<iframe width=300 height=200 src="iframe-subframe.html" style="border: 1px solid #444"></iframe>
<p><b>this text is after the iframe</b>
</body>

View File

@ -28,6 +28,7 @@ span#ua {
<p>Your user agent is: <b><span id="ua"></span></b></p>
<p>Some small test pages:</p>
<ul>
<li><a href="iframe.html">iframe</a></li>
<li><a href="many-buggies.html">many buggies</a></li>
<li><a href="palette.html">system palette color css extension</a></li>
<li><a href="inline-block-link.html">link inside display: inline-block</a></li>

View File

@ -48,6 +48,7 @@ set(SOURCES
DOM/HTMLHeadingElement.cpp
DOM/HTMLHRElement.cpp
DOM/HTMLHtmlElement.cpp
DOM/HTMLIFrameElement.cpp
DOM/HTMLImageElement.cpp
DOM/HTMLInputElement.cpp
DOM/HTMLLinkElement.cpp
@ -70,6 +71,7 @@ set(SOURCES
Layout/LayoutBreak.cpp
Layout/LayoutCanvas.cpp
Layout/LayoutDocument.cpp
Layout/LayoutFrame.cpp
Layout/LayoutImage.cpp
Layout/LayoutInline.cpp
Layout/LayoutListItem.cpp

View File

@ -36,6 +36,7 @@
#include <LibWeb/DOM/HTMLHeadElement.h>
#include <LibWeb/DOM/HTMLHeadingElement.h>
#include <LibWeb/DOM/HTMLHtmlElement.h>
#include <LibWeb/DOM/HTMLIFrameElement.h>
#include <LibWeb/DOM/HTMLImageElement.h>
#include <LibWeb/DOM/HTMLInputElement.h>
#include <LibWeb/DOM/HTMLLinkElement.h>
@ -76,6 +77,8 @@ NonnullRefPtr<Element> create_element(Document& document, const FlyString& tag_n
return adopt(*new HTMLInputElement(document, lowercase_tag_name));
if (lowercase_tag_name == "br")
return adopt(*new HTMLBRElement(document, lowercase_tag_name));
if (lowercase_tag_name == "iframe")
return adopt(*new HTMLIFrameElement(document, lowercase_tag_name));
if (lowercase_tag_name == "h1"
|| lowercase_tag_name == "h2"
|| lowercase_tag_name == "h3"

View File

@ -0,0 +1,96 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <LibCore/ElapsedTimer.h>
#include <LibGUI/Button.h>
#include <LibGUI/TextBox.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Event.h>
#include <LibWeb/DOM/HTMLFormElement.h>
#include <LibWeb/DOM/HTMLIFrameElement.h>
#include <LibWeb/Dump.h>
#include <LibWeb/Frame.h>
#include <LibWeb/Layout/LayoutFrame.h>
#include <LibWeb/Layout/LayoutWidget.h>
#include <LibWeb/Loader/ResourceLoader.h>
#include <LibWeb/PageView.h>
#include <LibWeb/Parser/HTMLDocumentParser.h>
namespace Web {
HTMLIFrameElement::HTMLIFrameElement(Document& document, const FlyString& tag_name)
: HTMLElement(document, tag_name)
{
m_hosted_frame = Frame::create_subframe();
}
HTMLIFrameElement::~HTMLIFrameElement()
{
}
RefPtr<LayoutNode> HTMLIFrameElement::create_layout_node(const StyleProperties* parent_style) const
{
auto style = document().style_resolver().resolve_style(*this, parent_style);
return adopt(*new LayoutFrame(*this, move(style)));
}
void HTMLIFrameElement::parse_attribute(const FlyString& name, const String& value)
{
HTMLElement::parse_attribute(name, value);
if (name == HTML::AttributeNames::src) {
load_src(value);
return;
}
}
void HTMLIFrameElement::load_src(const String& value)
{
dbg() << "Loading iframe document from " << value;
auto url = document().complete_url(value);
if (!url.is_valid()) {
dbg() << "Actually no I'm not, because the URL is not valid :(";
return;
}
ResourceLoader::the().load_sync(
url,
[&](auto& data, auto& headers) {
(void)headers;
auto html_source = String::copy(data);
HTMLDocumentParser parser(html_source, "utf-8");
parser.run(url);
m_hosted_frame->set_document(&parser.document());
dbg() << "The hosted frame now has this DOM:";
dump_tree(*m_hosted_frame->document());
},
[&](auto& error) {
dbg() << "<iframe> failed to load: " << error;
});
}
}

View File

@ -0,0 +1,57 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <LibWeb/DOM/HTMLElement.h>
namespace Web {
class HTMLIFrameElement : public HTMLElement {
public:
HTMLIFrameElement(Document&, const FlyString& tag_name);
virtual ~HTMLIFrameElement() override;
virtual RefPtr<LayoutNode> create_layout_node(const StyleProperties* parent_style) const override;
Frame* hosted_frame() { return m_hosted_frame; }
const Frame* hosted_frame() const { return m_hosted_frame; }
virtual void parse_attribute(const FlyString& name, const String& value) override;
private:
void load_src(const String&);
RefPtr<Frame> m_hosted_frame;
};
template<>
inline bool is<HTMLIFrameElement>(const Node& node)
{
return is<Element>(node) && to<Element>(node).tag_name().equals_ignoring_case("iframe");
}
}

View File

@ -32,6 +32,10 @@
namespace Web {
Frame::Frame()
{
}
Frame::Frame(PageView& page_view)
: m_page_view(page_view.make_weak_ptr())
{

View File

@ -41,6 +41,7 @@ class PageView;
class Frame : public TreeNode<Frame> {
public:
static NonnullRefPtr<Frame> create_subframe() { return adopt(*new Frame); }
static NonnullRefPtr<Frame> create(PageView& page_view) { return adopt(*new Frame(page_view)); }
~Frame();
@ -64,6 +65,7 @@ public:
void did_scroll(Badge<PageView>);
private:
Frame();
explicit Frame(PageView&);
WeakPtr<PageView> m_page_view;

View File

@ -0,0 +1,88 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <LibGUI/Painter.h>
#include <LibGUI/ScrollBar.h>
#include <LibGUI/Widget.h>
#include <LibGfx/Font.h>
#include <LibGfx/StylePainter.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/Frame.h>
#include <LibWeb/Layout/LayoutDocument.h>
#include <LibWeb/Layout/LayoutFrame.h>
#include <LibWeb/PageView.h>
namespace Web {
LayoutFrame::LayoutFrame(const Element& element, NonnullRefPtr<StyleProperties> style)
: LayoutReplaced(element, move(style))
{
}
LayoutFrame::~LayoutFrame()
{
}
void LayoutFrame::layout(LayoutMode layout_mode)
{
set_has_intrinsic_width(true);
set_has_intrinsic_height(true);
// FIXME: Do proper error checking, etc.
bool ok;
set_intrinsic_width(node().attribute(HTML::AttributeNames::width).to_int(ok));
set_intrinsic_height(node().attribute(HTML::AttributeNames::height).to_int(ok));
LayoutReplaced::layout(layout_mode);
}
void LayoutFrame::render(RenderingContext& context)
{
LayoutReplaced::render(context);
context.painter().save();
auto old_viewport_rect = context.viewport_rect();
context.painter().add_clip_rect(enclosing_int_rect(rect()));
context.painter().translate(x(), y());
context.set_viewport_rect({ {}, node().hosted_frame()->size() });
node().hosted_frame()->document()->layout_node()->render(context);
context.set_viewport_rect(old_viewport_rect);
context.painter().restore();
}
void LayoutFrame::did_set_rect()
{
LayoutReplaced::did_set_rect();
ASSERT(node().hosted_frame());
node().hosted_frame()->set_size(Gfx::Size(rect().width(), rect().height()));
node().hosted_frame()->document()->layout();
}
}

View File

@ -0,0 +1,57 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <LibWeb/Layout/LayoutReplaced.h>
#include <LibWeb/DOM/HTMLIFrameElement.h>
namespace Web {
class LayoutFrame final : public LayoutReplaced {
public:
LayoutFrame(const Element&, NonnullRefPtr<StyleProperties>);
virtual ~LayoutFrame() override;
virtual void render(RenderingContext&) override;
virtual void layout(LayoutMode) override;
const HTMLIFrameElement& node() const { return static_cast<const HTMLIFrameElement&>(LayoutReplaced::node()); }
HTMLIFrameElement& node() { return static_cast<HTMLIFrameElement&>(LayoutReplaced::node()); }
private:
virtual bool is_frame() const final { return true; }
virtual const char* class_name() const override { return "LayoutFrame"; }
virtual void did_set_rect() override;
};
template<>
inline bool is<LayoutFrame>(const LayoutNode& node)
{
return node.is_frame();
}
}

View File

@ -60,6 +60,7 @@ public:
bool is_anonymous() const { return !m_node; }
const Node* node() const { return m_node; }
Node* node() { return const_cast<Node*>(m_node); }
Document& document();
const Document& document() const;
@ -87,6 +88,7 @@ public:
virtual bool is_block() const { return false; }
virtual bool is_replaced() const { return false; }
virtual bool is_widget() const { return false; }
virtual bool is_frame() const { return false; }
virtual bool is_image() const { return false; }
virtual bool is_canvas() const { return false; }
virtual bool is_box() const { return false; }

View File

@ -37,6 +37,7 @@ public:
virtual ~LayoutReplaced() override;
const Element& node() const { return to<Element>(*LayoutNode::node()); }
Element& node() { return to<Element>(*LayoutNode::node()); }
virtual bool is_replaced() const final { return true; }