From f60c46ceb779aa4c1606abb08d1da2862faa2fbe Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 6 Nov 2019 20:52:26 +0100 Subject: [PATCH] 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 :^) --- Applications/IRCClient/IRCLogBuffer.cpp | 57 +++++++++++++------------ 1 file 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 +#include #include #include #include @@ -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("<%c%s> ", prefix ? prefix : ' ', name.characters()); + auto html = String::format( + "
" + "%s" + "%s" + "%s" + "
", + 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( + "
" + "%s" + "%s" + "
", + 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(); }