From 3e486f75ff76463aad5ddb4dc6825fe017b49c27 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 13 Feb 2020 08:46:00 +0100 Subject: [PATCH] AK: Move escape_html_entities() from LibHTML to AK This sort of thing can be useful to things that don't want to link with all of LibHTML. --- AK/String.cpp | 15 +++++++++++++++ AK/String.h | 3 +++ Libraries/LibHTML/Parser/HTMLParser.cpp | 16 ---------------- Libraries/LibHTML/Parser/HTMLParser.h | 1 - 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/AK/String.cpp b/AK/String.cpp index 3ad0ba0a33b..4e064b8070d 100644 --- a/AK/String.cpp +++ b/AK/String.cpp @@ -391,5 +391,20 @@ bool String::equals_ignoring_case(const StringView& other) const return true; } +String escape_html_entities(const StringView& html) +{ + StringBuilder builder; + for (size_t i = 0; i < html.length(); ++i) { + if (html[i] == '<') + builder.append("<"); + else if (html[i] == '>') + builder.append(">"); + else if (html[i] == '&') + builder.append("&"); + else + builder.append(html[i]); + } + return builder.to_string(); } +} diff --git a/AK/String.h b/AK/String.h index e2c4c720367..71c4a48293c 100644 --- a/AK/String.h +++ b/AK/String.h @@ -302,7 +302,10 @@ inline bool operator<=(const char* characters, const String& string) return !(characters > string); } +String escape_html_entities(const StringView& html); + } using AK::CaseInsensitiveStringTraits; using AK::String; +using AK::escape_html_entities; diff --git a/Libraries/LibHTML/Parser/HTMLParser.cpp b/Libraries/LibHTML/Parser/HTMLParser.cpp index 7319d9f157e..d105d877504 100644 --- a/Libraries/LibHTML/Parser/HTMLParser.cpp +++ b/Libraries/LibHTML/Parser/HTMLParser.cpp @@ -378,19 +378,3 @@ RefPtr parse_html_document(const StringView& html, const URL& url) return document; } - -String escape_html_entities(const StringView& html) -{ - StringBuilder builder; - for (size_t i = 0; i < html.length(); ++i) { - if (html[i] == '<') - builder.append("<"); - else if (html[i] == '>') - builder.append(">"); - else if (html[i] == '&') - builder.append("&"); - else - builder.append(html[i]); - } - return builder.to_string(); -} diff --git a/Libraries/LibHTML/Parser/HTMLParser.h b/Libraries/LibHTML/Parser/HTMLParser.h index 9e089d096fa..726cae246e2 100644 --- a/Libraries/LibHTML/Parser/HTMLParser.h +++ b/Libraries/LibHTML/Parser/HTMLParser.h @@ -33,4 +33,3 @@ class DocumentFragment; RefPtr parse_html_document(const StringView&, const URL& = URL()); RefPtr parse_html_fragment(Document&, const StringView&); -String escape_html_entities(const StringView&);