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
This commit is contained in:
1 parent
1f98938061
commit
549c7cfccb
3 files changed
+1459
No files matched your search
@@ -42,6 +42,26 @@ inline constexpr std::array<NamedEntity, 12> named_entities{{
|
||||
}};
|
||||
} // namespace Detail
|
||||
|
||||
/// Escape `s` for safe interpolation into HTML text or a quoted attribute:
|
||||
/// `&` -> `&`, `<` -> `<`, `>` -> `>`, `"` -> `"`, `'` -> `'`.
|
||||
/// 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 += "&"; break;
|
||||
case '<': out += "<"; break;
|
||||
case '>': out += ">"; break;
|
||||
case '"': out += """; break;
|
||||
case '\'': out += "'"; break;
|
||||
default: out += c; break;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/// Decode HTML character references in `s` to UTF-8: numeric (`Ӓ`,
|
||||
/// `😀`) and the common named entities (& < > " '
|
||||
/// © …). Unknown or malformed references are left verbatim.
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user