Files
Library/tests/standalone/Text/Markdown.cpp
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

311 lines
11 KiB
C++

#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <Void/Text/Html.hpp>
#include <Void/Text/Markdown.hpp>
#include <cstddef>
#include <string>
#include <string_view>
namespace
{
using Void::Text::MarkdownError;
using Void::Text::MarkdownLimits;
using Void::Text::MarkdownOptions;
using Void::Text::markdown_to_html;
std::string render(std::string_view input)
{
const auto r = markdown_to_html(input);
Void::Testing::expect(r.has_value());
return r ? *r : std::string{};
}
MarkdownError render_error(std::string_view input, MarkdownOptions options)
{
const auto r = markdown_to_html(input, options);
Void::Testing::expect(!r.has_value());
return r ? MarkdownError{} : r.error();
}
} // namespace
// ── HTML escaping ────────────────────────────────────────────────────────────
Void::Testing::Test html_escape_special_characters()
{
Void::Testing::expect_eq(Void::Text::html_escape("<a href=\"x\">&'</a>"),
std::string{"&lt;a href=&quot;x&quot;&gt;&amp;&#39;&lt;/a&gt;"});
}
Void::Testing::Test html_escape_passthrough()
{
Void::Testing::expect_eq(Void::Text::html_escape("plain text 123"),
std::string{"plain text 123"});
Void::Testing::expect(Void::Text::html_escape("").empty());
}
// ── Headings and paragraphs ──────────────────────────────────────────────────
Void::Testing::Test markdown_atx_headings()
{
Void::Testing::expect_eq(render("# one"), std::string{"<h1>one</h1>\n"});
Void::Testing::expect_eq(render("### three"), std::string{"<h3>three</h3>\n"});
Void::Testing::expect_eq(render("###### six"), std::string{"<h6>six</h6>\n"});
// Seven hashes is not a heading.
Void::Testing::expect_eq(render("####### seven"),
std::string{"<p>####### seven</p>\n"});
}
Void::Testing::Test markdown_atx_closing_sequence()
{
Void::Testing::expect_eq(render("## title ##"), std::string{"<h2>title</h2>\n"});
Void::Testing::expect_eq(render("## title#"), std::string{"<h2>title#</h2>\n"});
}
Void::Testing::Test markdown_setext_headings()
{
Void::Testing::expect_eq(render("Title\n====="), std::string{"<h1>Title</h1>\n"});
Void::Testing::expect_eq(render("Title\n-----"), std::string{"<h2>Title</h2>\n"});
}
Void::Testing::Test markdown_paragraphs_and_soft_break()
{
Void::Testing::expect_eq(render("one\ntwo\n\nthree"),
std::string{"<p>one\ntwo</p>\n<p>three</p>\n"});
}
Void::Testing::Test markdown_empty_input()
{
Void::Testing::expect_eq(render(""), std::string{});
Void::Testing::expect_eq(render("\n\n \n"), std::string{});
}
// ── Inline constructs ───────────────────────────────────────────────────────
Void::Testing::Test markdown_emphasis_and_strong()
{
Void::Testing::expect_eq(render("*em*"), std::string{"<p><em>em</em></p>\n"});
Void::Testing::expect_eq(render("_em_"), std::string{"<p><em>em</em></p>\n"});
Void::Testing::expect_eq(render("**strong**"),
std::string{"<p><strong>strong</strong></p>\n"});
Void::Testing::expect_eq(render("__strong__"),
std::string{"<p><strong>strong</strong></p>\n"});
Void::Testing::expect_eq(render("**bold *and em***"),
std::string{"<p><strong>bold <em>and em</em></strong></p>\n"});
}
Void::Testing::Test markdown_underscore_intraword_is_literal()
{
Void::Testing::expect_eq(render("snake_case_name"),
std::string{"<p>snake_case_name</p>\n"});
}
Void::Testing::Test markdown_unmatched_delimiters_are_literal()
{
Void::Testing::expect_eq(render("*unclosed"), std::string{"<p>*unclosed</p>\n"});
Void::Testing::expect_eq(render("a * b"), std::string{"<p>a * b</p>\n"});
}
Void::Testing::Test markdown_code_spans()
{
Void::Testing::expect_eq(render("`a < b`"), std::string{"<p><code>a &lt; b</code></p>\n"});
// A run of two backticks can contain a single backtick.
Void::Testing::expect_eq(render("``a ` b``"),
std::string{"<p><code>a ` b</code></p>\n"});
// One space is trimmed from each side when both sides have one.
Void::Testing::expect_eq(render("` code `"), std::string{"<p><code>code</code></p>\n"});
}
Void::Testing::Test markdown_backslash_escapes()
{
Void::Testing::expect_eq(render("\\*not em\\*"), std::string{"<p>*not em*</p>\n"});
Void::Testing::expect_eq(render("\\[not a link\\]"),
std::string{"<p>[not a link]</p>\n"});
}
Void::Testing::Test markdown_hard_breaks()
{
Void::Testing::expect_eq(render("one \ntwo"),
std::string{"<p>one<br />\ntwo</p>\n"});
Void::Testing::expect_eq(render("one\\\ntwo"),
std::string{"<p>one<br />\ntwo</p>\n"});
}
// ── Links, images, autolinks ────────────────────────────────────────────────
Void::Testing::Test markdown_inline_link()
{
Void::Testing::expect_eq(
render("[text](https://example.com)"),
std::string{"<p><a href=\"https://example.com\">text</a></p>\n"});
}
Void::Testing::Test markdown_inline_link_with_title()
{
Void::Testing::expect_eq(
render("[text](https://example.com \"a title\")"),
std::string{"<p><a href=\"https://example.com\" title=\"a title\">text</a></p>\n"});
}
Void::Testing::Test markdown_relative_and_mailto_links_allowed()
{
Void::Testing::expect(render("[/x](/x)").find("href=\"/x\"") != std::string::npos);
Void::Testing::expect(render("[#a](#a)").find("href=\"#a\"") != std::string::npos);
Void::Testing::expect(render("[m](mailto:a@b.c)").find("href=\"mailto:a@b.c\"") !=
std::string::npos);
}
Void::Testing::Test markdown_image()
{
Void::Testing::expect_eq(
render("![alt text](https://example.com/x.png)"),
std::string{"<p><img src=\"https://example.com/x.png\" alt=\"alt text\" /></p>\n"});
}
Void::Testing::Test markdown_image_disallowed_renders_alt()
{
MarkdownOptions options;
options.allow_images = false;
const auto r = markdown_to_html("![alt text](https://example.com/x.png)", options);
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(*r, std::string{"<p>alt text</p>\n"});
}
Void::Testing::Test markdown_autolink()
{
Void::Testing::expect_eq(
render("<https://example.com/a?b=1>"),
std::string{"<p><a href=\"https://example.com/a?b=1\">https://example.com/a?b=1</a></p>\n"});
}
// ── Blocks ──────────────────────────────────────────────────────────────────
Void::Testing::Test markdown_thematic_break()
{
Void::Testing::expect_eq(render("---"), std::string{"<hr />\n"});
Void::Testing::expect_eq(render("***"), std::string{"<hr />\n"});
Void::Testing::expect_eq(render("___"), std::string{"<hr />\n"});
}
Void::Testing::Test markdown_fenced_code_block()
{
Void::Testing::expect_eq(render("```\nint x = 1;\n```"),
std::string{"<pre><code>int x = 1;\n</code></pre>\n"});
Void::Testing::expect_eq(
render("```cpp\nif (a < b) {}\n```"),
std::string{"<pre><code class=\"language-cpp\">if (a &lt; b) {}\n</code></pre>\n"});
// Tilde fences work too.
Void::Testing::expect_eq(render("~~~\nx\n~~~"),
std::string{"<pre><code>x\n</code></pre>\n"});
}
Void::Testing::Test markdown_unclosed_fence_renders_to_end()
{
Void::Testing::expect_eq(render("```\nabc"),
std::string{"<pre><code>abc\n</code></pre>\n"});
}
Void::Testing::Test markdown_unordered_list()
{
Void::Testing::expect_eq(render("- one\n- two"),
std::string{"<ul>\n<li>one</li>\n<li>two</li>\n</ul>\n"});
Void::Testing::expect_eq(render("* a\n* b"),
std::string{"<ul>\n<li>a</li>\n<li>b</li>\n</ul>\n"});
}
Void::Testing::Test markdown_ordered_list_and_start()
{
Void::Testing::expect_eq(render("1. one\n2. two"),
std::string{"<ol>\n<li>one</li>\n<li>two</li>\n</ol>\n"});
Void::Testing::expect_eq(render("3. three\n4. four"),
std::string{"<ol start=\"3\">\n<li>three</li>\n<li>four</li>\n</ol>\n"});
}
Void::Testing::Test markdown_blockquote()
{
Void::Testing::expect_eq(render("> quoted"),
std::string{"<blockquote>\n<p>quoted</p>\n</blockquote>\n"});
Void::Testing::expect_eq(render("> a\n> b"),
std::string{"<blockquote>\n<p>a\nb</p>\n</blockquote>\n"});
}
// ── Hostile input ──────────────────────────────────────────────────────────
Void::Testing::Test markdown_raw_html_is_escaped()
{
Void::Testing::expect_eq(render("<script>alert(1)</script>"),
std::string{"<p>&lt;script&gt;alert(1)&lt;/script&gt;</p>\n"});
Void::Testing::expect_eq(
render("<img src=x onerror=alert(1)>"),
std::string{"<p>&lt;img src=x onerror=alert(1)&gt;</p>\n"});
}
Void::Testing::Test markdown_javascript_links_dropped()
{
Void::Testing::expect_eq(render("[x](javascript:alert(1))"),
std::string{"<p>x</p>\n"});
Void::Testing::expect_eq(render("[x](JaVaScRiPt:alert(1))"),
std::string{"<p>x</p>\n"});
Void::Testing::expect_eq(render("[x](data:text/html;base64,AAAA)"),
std::string{"<p>x</p>\n"});
Void::Testing::expect_eq(render("[x](vbscript:msgbox(1))"),
std::string{"<p>x</p>\n"});
}
Void::Testing::Test markdown_entity_smuggled_scheme_dropped()
{
Void::Testing::expect_eq(render("[x](javascript&colon;alert(1))"),
std::string{"<p>x</p>\n"});
Void::Testing::expect_eq(render("[x](javascript&#58;alert(1))"),
std::string{"<p>x</p>\n"});
Void::Testing::expect_eq(render("[x](javascript&#x3a;alert(1))"),
std::string{"<p>x</p>\n"});
}
Void::Testing::Test markdown_attribute_injection_is_escaped()
{
const std::string out = render("[x](https://e.com/\" onmouseover=\"alert(1))");
Void::Testing::expect(out.find("onmouseover=\"") == std::string::npos);
Void::Testing::expect(out.find("&quot;") != std::string::npos);
}
Void::Testing::Test markdown_code_block_content_is_escaped()
{
Void::Testing::expect_eq(render("```\n</code><script>x</script>\n```"),
std::string{"<pre><code>&lt;/code&gt;&lt;script&gt;x&lt;/script&gt;\n</code></pre>\n"});
}
// ── Bounds ─────────────────────────────────────────────────────────────────
Void::Testing::Test markdown_input_limit()
{
MarkdownOptions options;
options.limits.max_input = 4;
const auto err = render_error("12345", options);
Void::Testing::expect(err.code == MarkdownError::Code::input_limit);
}
Void::Testing::Test markdown_output_limit()
{
MarkdownOptions options;
options.limits.max_output = 8;
const auto err = render_error("# a long heading", options);
Void::Testing::expect(err.code == MarkdownError::Code::output_limit);
}
Void::Testing::Test markdown_block_limit()
{
MarkdownOptions options;
options.limits.max_blocks = 1;
const auto err = render_error("one\n\ntwo", options);
Void::Testing::expect(err.code == MarkdownError::Code::block_limit);
}
Void::Testing::Test markdown_depth_limit()
{
MarkdownOptions options;
options.limits.max_depth = 1;
const auto err = render_error("**a *b* c**", options);
Void::Testing::expect(err.code == MarkdownError::Code::depth_limit);
}