Files
Library/include/Void/Text/Html.hpp
T
faumaray 549c7cfccb Text: bounded CommonMark-subset Markdown parser and safe HTML renderer
Add Void/Text/Markdown.hpp: a bounded CommonMark-subset parser with a safe
HTML renderer. Raw HTML is always escaped, link/image destinations are
allow-listed (http, https, mailto or relative), and unsafe schemes such as
javascript: and data: are dropped, including entity-smuggled variants.
Add html_escape to Void/Text/Html.hpp.

Closes #58
2026-12-04 13:29:25 +03:00

134 lines
4.4 KiB
C++

#pragma once
#include <array>
#include <cstdint>
#include <string>
#include <string_view>
namespace Void::Text
{
namespace Detail
{
// Append `cp` as UTF-8 to `out`.
inline void append_utf8(std::string& out, std::uint32_t cp)
{
if (cp <= 0x7F) {
out += static_cast<char>(cp);
} else if (cp <= 0x7FF) {
out += static_cast<char>(0xC0 | (cp >> 6));
out += static_cast<char>(0x80 | (cp & 0x3F));
} else if (cp <= 0xFFFF) {
out += static_cast<char>(0xE0 | (cp >> 12));
out += static_cast<char>(0x80 | ((cp >> 6) & 0x3F));
out += static_cast<char>(0x80 | (cp & 0x3F));
} else if (cp <= 0x10FFFF) {
out += static_cast<char>(0xF0 | (cp >> 18));
out += static_cast<char>(0x80 | ((cp >> 12) & 0x3F));
out += static_cast<char>(0x80 | ((cp >> 6) & 0x3F));
out += static_cast<char>(0x80 | (cp & 0x3F));
}
}
// The common named HTML entities. Numeric entities cover everything else.
struct NamedEntity {
std::string_view name;
std::uint32_t cp;
};
inline constexpr std::array<NamedEntity, 12> named_entities{{
{"amp", '&'}, {"lt", '<'}, {"gt", '>'}, {"quot", '"'},
{"apos", '\''}, {"nbsp", 0xA0}, {"copy", 0xA9}, {"reg", 0xAE},
{"hellip", 0x2026}, {"mdash", 0x2014}, {"ndash", 0x2013}, {"trade", 0x2122},
}};
} // namespace Detail
/// Escape `s` for safe interpolation into HTML text or a quoted attribute:
/// `&` -> `&amp;`, `<` -> `&lt;`, `>` -> `&gt;`, `"` -> `&quot;`, `'` -> `&#39;`.
/// Everything else is passed through byte-for-byte.
[[nodiscard]] inline std::string html_escape(std::string_view s)
{
std::string out;
out.reserve(s.size());
for (const char c : s) {
switch (c) {
case '&': out += "&amp;"; break;
case '<': out += "&lt;"; break;
case '>': out += "&gt;"; break;
case '"': out += "&quot;"; break;
case '\'': out += "&#39;"; break;
default: out += c; break;
}
}
return out;
}
/// Decode HTML character references in `s` to UTF-8: numeric (`&#1234;`,
/// `&#x1F600;`) and the common named entities (&amp; &lt; &gt; &quot; &apos;
/// &nbsp; &copy; …). Unknown or malformed references are left verbatim.
/// Generic — no markup parsing, just entity substitution.
[[nodiscard]] inline std::string html_unescape(std::string_view s)
{
std::string out;
out.reserve(s.size());
for (std::size_t i = 0; i < s.size();) {
if (s[i] != '&') {
out += s[i++];
continue;
}
// Find the terminating ';' within a small window.
const std::size_t semi = s.find(';', i + 1);
if (semi == std::string_view::npos || semi - i > 12) {
out += s[i++];
continue;
}
const std::string_view body = s.substr(i + 1, semi - i - 1);
bool handled = false;
if (!body.empty() && body[0] == '#') {
// Numeric reference (decimal or hex).
std::uint32_t cp = 0;
bool ok = false;
if (body.size() > 1 && (body[1] == 'x' || body[1] == 'X')) {
for (std::size_t k = 2; k < body.size(); ++k) {
const char c = body[k];
int v;
if (c >= '0' && c <= '9') v = c - '0';
else if (c >= 'a' && c <= 'f') v = c - 'a' + 10;
else if (c >= 'A' && c <= 'F') v = c - 'A' + 10;
else { ok = false; break; }
cp = cp * 16 + static_cast<std::uint32_t>(v);
ok = true;
}
} else {
for (std::size_t k = 1; k < body.size(); ++k) {
const char c = body[k];
if (c < '0' || c > '9') { ok = false; break; }
cp = cp * 10 + static_cast<std::uint32_t>(c - '0');
ok = true;
}
}
if (ok && cp <= 0x10FFFF) {
Detail::append_utf8(out, cp);
handled = true;
}
} else {
for (const auto& e : Detail::named_entities) {
if (e.name == body) {
Detail::append_utf8(out, e.cp);
handled = true;
break;
}
}
}
if (handled) {
i = semi + 1;
} else {
out += s[i++]; // leave the '&' as-is
}
}
return out;
}
} // namespace Void::Text