Skip to content

Commit

Permalink
LibHTML: Rename parse_html() => parse_html_document()
Browse files Browse the repository at this point in the history
  • Loading branch information
awesomekling committed Nov 6, 2019
1 parent 635717e commit f643978
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Applications/Help/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ int main(int argc, char* argv[])
ASSERT(success);

String html = md_document.render_to_html();
auto html_document = parse_html(html);
auto html_document = parse_html_document(html);
html_view->set_document(html_document);

String page_and_section = model->page_and_section(tree_view->selection().first());
Expand Down
57 changes: 30 additions & 27 deletions Applications/IRCClient/IRCLogBuffer.cpp
Original file line number Diff line number Diff line change
@@ -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>
Expand Down Expand Up @@ -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();
}

Expand Down
2 changes: 1 addition & 1 deletion DevTools/HackStudio/Editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ void Editor::show_documentation_tooltip_if_available(const String& hovered_token

auto html_text = man_document.render_to_html();

auto html_document = parse_html(html_text);
auto html_document = parse_html_document(html_text);
if (!html_document) {
dbg() << "failed to parse HTML";
return;
Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibHTML/HtmlView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ void HtmlView::load(const URL& url)
if (url.path().ends_with(".png")) {
document = create_image_document(data, url);
} else {
document = parse_html(data, url);
document = parse_html_document(data, url);
}
ASSERT(document);
set_document(document);
Expand Down
8 changes: 4 additions & 4 deletions Libraries/LibHTML/Parser/HTMLParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static bool is_self_closing_tag(const StringView& tag_name)
|| tag_name == "wbr";
}

static bool parse_html(const StringView& html, Document& document, ParentNode& root)
static bool parse_html_document(const StringView& html, Document& document, ParentNode& root)
{
NonnullRefPtrVector<ParentNode> node_stack;
node_stack.append(root);
Expand Down Expand Up @@ -313,17 +313,17 @@ static bool parse_html(const StringView& html, Document& document, ParentNode& r
RefPtr<DocumentFragment> parse_html_fragment(Document& document, const StringView& html)
{
auto fragment = adopt(*new DocumentFragment(document));
if (!parse_html(html, document, *fragment))
if (!parse_html_document(html, document, *fragment))
return nullptr;
return fragment;
}

NonnullRefPtr<Document> parse_html(const StringView& html, const URL& url)
NonnullRefPtr<Document> parse_html_document(const StringView& html, const URL& url)
{
auto document = adopt(*new Document);
document->set_url(url);

bool success = parse_html(html, *document, *document);
bool success = parse_html_document(html, *document, *document);
ASSERT(success);

document->fixup();
Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibHTML/Parser/HTMLParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

class DocumentFragment;

NonnullRefPtr<Document> parse_html(const StringView&, const URL& = URL());
NonnullRefPtr<Document> parse_html_document(const StringView&, const URL& = URL());
RefPtr<DocumentFragment> parse_html_fragment(Document&, const StringView&);
2 changes: 1 addition & 1 deletion Userland/html.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ int main(int argc, char** argv)
}

String html = String::copy(f->read_all());
auto document = parse_html(html);
auto document = parse_html_document(html);

auto window = GWindow::construct();
auto widget = HtmlView::construct();
Expand Down

0 comments on commit f643978

Please sign in to comment.