From 78d65c1c645ca9c51003b3c395d8b5ac07b6a8cb Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 5 Oct 2019 10:16:27 +0200 Subject: [PATCH] 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. --- Libraries/LibHTML/DOM/Document.cpp | 26 ++++++++++++++++++++++++++ Libraries/LibHTML/DOM/Document.h | 7 +++++++ Libraries/LibHTML/HtmlView.cpp | 29 +++++++++++++++++++++++++++++ Libraries/LibHTML/HtmlView.h | 7 +++++++ 4 files changed, 69 insertions(+) diff --git a/Libraries/LibHTML/DOM/Document.cpp b/Libraries/LibHTML/DOM/Document.cpp index 97e5873c4f..4cdceb6069 100644 --- a/Libraries/LibHTML/DOM/Document.cpp +++ b/Libraries/LibHTML/DOM/Document.cpp @@ -1,3 +1,5 @@ +#include +#include #include #include #include @@ -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; +} diff --git a/Libraries/LibHTML/DOM/Document.h b/Libraries/LibHTML/DOM/Document.h index 07ea5454b2..c66ebc6afa 100644 --- a/Libraries/LibHTML/DOM/Document.h +++ b/Libraries/LibHTML/DOM/Document.h @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -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 m_sheets; RefPtr m_hovered_node; WeakPtr m_frame; + URL m_url; }; diff --git a/Libraries/LibHTML/HtmlView.cpp b/Libraries/LibHTML/HtmlView.cpp index 89d4cb8df2..3669ef2e95 100644 --- a/Libraries/LibHTML/HtmlView.cpp +++ b/Libraries/LibHTML/HtmlView.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -7,6 +8,7 @@ #include #include #include +#include #include #include @@ -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); +} diff --git a/Libraries/LibHTML/HtmlView.h b/Libraries/LibHTML/HtmlView.h index 61d374d01c..bccd78286a 100644 --- a/Libraries/LibHTML/HtmlView.h +++ b/Libraries/LibHTML/HtmlView.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -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 on_link_click; Function on_title_change; + Function on_load_start; protected: HtmlView(GWidget* parent = nullptr);