LibHTML: Add load(URL) and reload() functions to HtmlView

You can now pass a file:/// URL to HtmlView and it will take care of
the loading logic for you. Each Document remembers the URL it was
loaded from, which allows us to also have reload().

This patch also adds a very simple function for resolving relative
URL's into absolute ones.
This commit is contained in:
Andreas Kling 2019-10-05 10:16:27 +02:00
parent d64c054d25
commit 78d65c1c64
4 changed files with 69 additions and 0 deletions

View file

@ -1,3 +1,5 @@
#include <AK/FileSystemPath.h>
#include <AK/StringBuilder.h>
#include <LibHTML/CSS/StyleResolver.h>
#include <LibHTML/DOM/Document.h>
#include <LibHTML/DOM/Element.h>
@ -99,3 +101,27 @@ Color Document::background_color() const
return background_color.value()->to_color();
}
URL Document::complete_url(const String& string) const
{
URL url(string);
if (url.is_valid())
return url;
FileSystemPath fspath(m_url.path());
StringBuilder builder;
builder.append('/');
for (int i = 0; i < fspath.parts().size(); ++i) {
if (i == fspath.parts().size() - 1)
break;
builder.append(fspath.parts()[i]);
builder.append('/');
}
builder.append(string);
auto built = builder.to_string();
fspath = FileSystemPath(built);
url = m_url;
url.set_path(fspath.string());
return url;
}

View file

@ -3,6 +3,7 @@
#include <AK/NonnullRefPtrVector.h>
#include <AK/OwnPtr.h>
#include <AK/String.h>
#include <AK/URL.h>
#include <AK/WeakPtr.h>
#include <LibHTML/CSS/StyleResolver.h>
#include <LibHTML/CSS/StyleSheet.h>
@ -21,6 +22,11 @@ public:
Document();
virtual ~Document() override;
void set_url(const URL& url) { m_url = url; }
const URL& url() const { return m_url; }
URL complete_url(const String&) const;
void normalize();
StyleResolver& style_resolver();
@ -53,4 +59,5 @@ private:
NonnullRefPtrVector<StyleSheet> m_sheets;
RefPtr<Node> m_hovered_node;
WeakPtr<Frame> m_frame;
URL m_url;
};

View file

@ -1,3 +1,4 @@
#include <LibCore/CFile.h>
#include <LibGUI/GApplication.h>
#include <LibGUI/GPainter.h>
#include <LibGUI/GScrollBar.h>
@ -7,6 +8,7 @@
#include <LibHTML/Frame.h>
#include <LibHTML/HtmlView.h>
#include <LibHTML/Layout/LayoutNode.h>
#include <LibHTML/Parser/HTMLParser.h>
#include <LibHTML/RenderingContext.h>
#include <stdio.h>
@ -147,3 +149,30 @@ void HtmlView::mousedown_event(GMouseEvent& event)
update();
event.accept();
}
void HtmlView::reload()
{
load(main_frame().document()->url());
}
void HtmlView::load(const URL& url)
{
dbg() << "HtmlView::load: " << url;
if (on_load_start)
on_load_start(url);
auto f = CFile::construct();
f->set_filename(url.path());
if (!f->open(CIODevice::OpenMode::ReadOnly)) {
dbg() << "HtmlView::load: Error: " << f->error_string();
return;
}
String html = String::copy(f->read_all());
auto document = parse_html(html);
document->set_url(url);
document->normalize();
set_document(document);
}

View file

@ -1,5 +1,6 @@
#pragma once
#include <AK/URL.h>
#include <LibGUI/GScrollableWidget.h>
#include <LibHTML/DOM/Document.h>
@ -17,8 +18,14 @@ public:
Frame& main_frame() { return *m_main_frame; }
const Frame& main_frame() const { return *m_main_frame; }
void reload();
void load(const URL&);
URL url() const;
Function<void(const String&)> on_link_click;
Function<void(const String&)> on_title_change;
Function<void(const URL&)> on_load_start;
protected:
HtmlView(GWidget* parent = nullptr);