mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 15:48:12 +00:00
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 :^)
This commit is contained in:
parent
794f2d5645
commit
f60c46ceb7
1 changed files with 30 additions and 27 deletions
|
@ -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("<%c%s> ", 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();
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue