summaryrefslogtreecommitdiff
path: root/Applications/IRCClient/IRCLogBuffer.cpp
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-11-06 20:52:26 +0100
committerAndreas Kling <awesomekling@gmail.com>2019-11-06 20:53:30 +0100
commitf60c46ceb779aa4c1606abb08d1da2862faa2fbe (patch)
treef5ffb9d08ff060cc5beb6a58d0dd57e4ce9fa5cd /Applications/IRCClient/IRCLogBuffer.cpp
parent794f2d5645d9174a54e37380f4f6a784deaa71b0 (diff)
downloadserenity-f60c46ceb779aa4c1606abb08d1da2862faa2fbe.zip
IRCClient: Use parse_html_fragment() to add messages to the HtmlView
HTML fragment strings are so much nicer to work with than raw DOM APIs. This makes it feel like we're using innerHTML :^)
Diffstat (limited to 'Applications/IRCClient/IRCLogBuffer.cpp')
-rw-r--r--Applications/IRCClient/IRCLogBuffer.cpp57
1 files changed, 30 insertions, 27 deletions
diff --git a/Applications/IRCClient/IRCLogBuffer.cpp b/Applications/IRCClient/IRCLogBuffer.cpp
index a56443b145..782a69c6cc 100644
--- a/Applications/IRCClient/IRCLogBuffer.cpp
+++ b/Applications/IRCClient/IRCLogBuffer.cpp
@@ -1,4 +1,6 @@
#include "IRCLogBuffer.h"
+#include <AK/StringBuilder.h>
+#include <LibHTML/DOM/DocumentFragment.h>
#include <LibHTML/DOM/DocumentType.h>
#include <LibHTML/DOM/ElementFactory.h>
#include <LibHTML/DOM/HTMLBodyElement.h>
@@ -33,42 +35,43 @@ IRCLogBuffer::~IRCLogBuffer()
{
}
-void IRCLogBuffer::add_message(char prefix, const String& name, const String& text, Color color)
+static String timestamp_string()
{
- auto message_element = create_element(document(), "div");
- message_element->set_attribute("style", String::format("color: %s;", color.to_string().characters()));
- auto timestamp_element = create_element(document(), "span");
auto now = time(nullptr);
auto* tm = localtime(&now);
- auto timestamp_string = String::format("%02u:%02u:%02u ", tm->tm_hour, tm->tm_min, tm->tm_sec);
- timestamp_element->append_child(adopt(*new Text(document(), timestamp_string)));
- auto nick_element = create_element(document(), "b");
- nick_element->append_child(*new Text(document(), String::format("<%c%s> ", prefix ? prefix : ' ', name.characters())));
- auto text_element = create_element(document(), "span");
- text_element->append_child(*new Text(document(), text));
- message_element->append_child(timestamp_element);
- message_element->append_child(nick_element);
- message_element->append_child(text_element);
- m_container_element->append_child(message_element);
+ return String::format("%02u:%02u:%02u ", tm->tm_hour, tm->tm_min, tm->tm_sec);
+}
+void IRCLogBuffer::add_message(char prefix, const String& name, const String& text, Color color)
+{
+ auto nick_string = String::format("&lt;%c%s&gt; ", prefix ? prefix : ' ', name.characters());
+ auto html = String::format(
+ "<div style=\"color: %s\">"
+ "<span>%s</span>"
+ "<b>%s</b>"
+ "<span>%s</span>"
+ "</div>",
+ color.to_string().characters(),
+ timestamp_string().characters(),
+ nick_string.characters(),
+ text.characters());
+ auto fragment = parse_html_fragment(*m_document, html);
+ m_container_element->append_child(fragment->remove_child(*fragment->first_child()));
m_document->force_layout();
}
void IRCLogBuffer::add_message(const String& text, Color color)
{
- auto message_element = create_element(document(), "div");
- message_element->set_attribute("style", String::format("color: %s;", color.to_string().characters()));
- auto timestamp_element = create_element(document(), "span");
- auto now = time(nullptr);
- auto* tm = localtime(&now);
- auto timestamp_string = String::format("%02u:%02u:%02u ", tm->tm_hour, tm->tm_min, tm->tm_sec);
- timestamp_element->append_child(adopt(*new Text(document(), timestamp_string)));
- auto text_element = create_element(document(), "span");
- text_element->append_child(*new Text(document(), text));
- message_element->append_child(timestamp_element);
- message_element->append_child(text_element);
- m_container_element->append_child(message_element);
-
+ auto html = String::format(
+ "<div style=\"color: %s\">"
+ "<span>%s</span>"
+ "<span>%s</span>"
+ "</div>",
+ color.to_string().characters(),
+ timestamp_string().characters(),
+ text.characters());
+ auto fragment = parse_html_fragment(*m_document, html);
+ m_container_element->append_child(fragment->remove_child(*fragment->first_child()));
m_document->force_layout();
}