summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-05-10 20:44:11 +0200
committerAndreas Kling <kling@serenityos.org>2020-05-10 22:32:12 +0200
commit97adcde36ea642e4facf561cb556cfbb23e7ad0c (patch)
tree619d01bcefcd5fa28deaeb33c6bc687d7bac7bc5
parent0e60e7aef0303263fe9b757e7454f93425884c75 (diff)
downloadserenity-97adcde36ea642e4facf561cb556cfbb23e7ad0c.zip
LibWeb: Teach HtmlView how to open a .txt file
When encountering a text file, we now put it in a wrapper document with the file contents in a <pre> tag. This works really nicely :^)
-rw-r--r--Libraries/LibWeb/HtmlView.cpp27
1 files changed, 27 insertions, 0 deletions
diff --git a/Libraries/LibWeb/HtmlView.cpp b/Libraries/LibWeb/HtmlView.cpp
index 3354125b8a..5ed2bb7504 100644
--- a/Libraries/LibWeb/HtmlView.cpp
+++ b/Libraries/LibWeb/HtmlView.cpp
@@ -321,6 +321,31 @@ void HtmlView::reload()
load(main_frame().document()->url());
}
+static RefPtr<Document> create_text_document(const ByteBuffer& data, const URL& url)
+{
+ auto document = adopt(*new Document(url));
+
+ auto html_element = document->create_element("html");
+ document->append_child(html_element);
+
+ auto head_element = document->create_element("head");
+ html_element->append_child(head_element);
+ auto title_element = document->create_element("title");
+ head_element->append_child(title_element);
+
+ auto title_text = document->create_text_node(url.basename());
+ title_element->append_child(title_text);
+
+ auto body_element = document->create_element("body");
+ html_element->append_child(body_element);
+
+ auto pre_element = create_element(document, "pre");
+ body_element->append_child(pre_element);
+
+ pre_element->append_child(document->create_text_node(String::copy(data)));
+ return document;
+}
+
static RefPtr<Document> create_image_document(const ByteBuffer& data, const URL& url)
{
auto document = adopt(*new Document(url));
@@ -393,6 +418,8 @@ void HtmlView::load(const URL& url)
RefPtr<Document> document;
if (url.path().ends_with(".png") || url.path().ends_with(".gif")) {
document = create_image_document(data, url);
+ } else if (url.path().ends_with(".txt")) {
+ document = create_text_document(data, url);
} else {
String encoding = "utf-8";