#include #include #include #include #include #include #include #include #include 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{}; } std::string render_with(std::string_view input, MarkdownOptions options) { const auto r = markdown_to_html(input, options); 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("&'"), std::string{"<a href="x">&'</a>"}); } 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{"

one

\n"}); Void::Testing::expect_eq(render("### three"), std::string{"

three

\n"}); Void::Testing::expect_eq(render("###### six"), std::string{"
six
\n"}); // Seven hashes is not a heading. Void::Testing::expect_eq(render("####### seven"), std::string{"

####### seven

\n"}); } Void::Testing::Test markdown_atx_closing_sequence() { Void::Testing::expect_eq(render("## title ##"), std::string{"

title

\n"}); Void::Testing::expect_eq(render("## title#"), std::string{"

title#

\n"}); } Void::Testing::Test markdown_setext_headings() { Void::Testing::expect_eq(render("Title\n====="), std::string{"

Title

\n"}); Void::Testing::expect_eq(render("Title\n-----"), std::string{"

Title

\n"}); } Void::Testing::Test markdown_paragraphs_and_soft_break() { Void::Testing::expect_eq(render("one\ntwo\n\nthree"), std::string{"

one\ntwo

\n

three

\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{"

em

\n"}); Void::Testing::expect_eq(render("_em_"), std::string{"

em

\n"}); Void::Testing::expect_eq(render("**strong**"), std::string{"

strong

\n"}); Void::Testing::expect_eq(render("__strong__"), std::string{"

strong

\n"}); Void::Testing::expect_eq(render("**bold *and em***"), std::string{"

bold and em

\n"}); } Void::Testing::Test markdown_underscore_intraword_is_literal() { Void::Testing::expect_eq(render("snake_case_name"), std::string{"

snake_case_name

\n"}); } Void::Testing::Test markdown_unmatched_delimiters_are_literal() { Void::Testing::expect_eq(render("*unclosed"), std::string{"

*unclosed

\n"}); Void::Testing::expect_eq(render("a * b"), std::string{"

a * b

\n"}); } Void::Testing::Test markdown_code_spans() { Void::Testing::expect_eq(render("`a < b`"), std::string{"

a < b

\n"}); // A run of two backticks can contain a single backtick. Void::Testing::expect_eq(render("``a ` b``"), std::string{"

a ` b

\n"}); // One space is trimmed from each side when both sides have one. Void::Testing::expect_eq(render("` code `"), std::string{"

code

\n"}); } Void::Testing::Test markdown_backslash_escapes() { Void::Testing::expect_eq(render("\\*not em\\*"), std::string{"

*not em*

\n"}); Void::Testing::expect_eq(render("\\[not a link\\]"), std::string{"

[not a link]

\n"}); } Void::Testing::Test markdown_hard_breaks() { Void::Testing::expect_eq(render("one \ntwo"), std::string{"

one
\ntwo

\n"}); Void::Testing::expect_eq(render("one\\\ntwo"), std::string{"

one
\ntwo

\n"}); } // ── Links, images, autolinks ──────────────────────────────────────────────── Void::Testing::Test markdown_inline_link() { Void::Testing::expect_eq( render("[text](https://example.com)"), std::string{"

text

\n"}); } Void::Testing::Test markdown_inline_link_with_title() { Void::Testing::expect_eq( render("[text](https://example.com \"a title\")"), std::string{"

text

\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{"

\"alt

\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{"

alt text

\n"}); } Void::Testing::Test markdown_autolink() { Void::Testing::expect_eq( render(""), std::string{"

https://example.com/a?b=1

\n"}); } // ── Blocks ────────────────────────────────────────────────────────────────── Void::Testing::Test markdown_thematic_break() { Void::Testing::expect_eq(render("---"), std::string{"
\n"}); Void::Testing::expect_eq(render("***"), std::string{"
\n"}); Void::Testing::expect_eq(render("___"), std::string{"
\n"}); } Void::Testing::Test markdown_fenced_code_block() { Void::Testing::expect_eq(render("```\nint x = 1;\n```"), std::string{"
int x = 1;\n
\n"}); Void::Testing::expect_eq( render("```cpp\nif (a < b) {}\n```"), std::string{"
if (a < b) {}\n
\n"}); // Tilde fences work too. Void::Testing::expect_eq(render("~~~\nx\n~~~"), std::string{"
x\n
\n"}); } Void::Testing::Test markdown_unclosed_fence_renders_to_end() { Void::Testing::expect_eq(render("```\nabc"), std::string{"
abc\n
\n"}); } Void::Testing::Test markdown_unordered_list() { Void::Testing::expect_eq(render("- one\n- two"), std::string{"
    \n
  • one
  • \n
  • two
  • \n
\n"}); Void::Testing::expect_eq(render("* a\n* b"), std::string{"
    \n
  • a
  • \n
  • b
  • \n
\n"}); } Void::Testing::Test markdown_ordered_list_and_start() { Void::Testing::expect_eq(render("1. one\n2. two"), std::string{"
    \n
  1. one
  2. \n
  3. two
  4. \n
\n"}); Void::Testing::expect_eq(render("3. three\n4. four"), std::string{"
    \n
  1. three
  2. \n
  3. four
  4. \n
\n"}); } Void::Testing::Test markdown_blockquote() { Void::Testing::expect_eq(render("> quoted"), std::string{"
\n

quoted

\n
\n"}); Void::Testing::expect_eq(render("> a\n> b"), std::string{"
\n

a\nb

\n
\n"}); } // ── Hostile input ────────────────────────────────────────────────────────── Void::Testing::Test markdown_raw_html_is_escaped() { Void::Testing::expect_eq(render(""), std::string{"

<script>alert(1)</script>

\n"}); Void::Testing::expect_eq( render(""), std::string{"

<img src=x onerror=alert(1)>

\n"}); } Void::Testing::Test markdown_javascript_links_dropped() { Void::Testing::expect_eq(render("[x](javascript:alert(1))"), std::string{"

x

\n"}); Void::Testing::expect_eq(render("[x](JaVaScRiPt:alert(1))"), std::string{"

x

\n"}); Void::Testing::expect_eq(render("[x](data:text/html;base64,AAAA)"), std::string{"

x

\n"}); Void::Testing::expect_eq(render("[x](vbscript:msgbox(1))"), std::string{"

x

\n"}); } Void::Testing::Test markdown_entity_smuggled_scheme_dropped() { Void::Testing::expect_eq(render("[x](javascript:alert(1))"), std::string{"

x

\n"}); Void::Testing::expect_eq(render("[x](javascript:alert(1))"), std::string{"

x

\n"}); Void::Testing::expect_eq(render("[x](javascript:alert(1))"), std::string{"

x

\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(""") != std::string::npos); } Void::Testing::Test markdown_code_block_content_is_escaped() { Void::Testing::expect_eq(render("```\n\n```"), std::string{"
</code><script>x</script>\n
\n"}); } // ── Destination resolver ─────────────────────────────────────────────────── Void::Testing::Test markdown_resolver_called_once_per_destination_in_order() { std::vector seen; MarkdownOptions options; options.resolver = [&seen](std::string_view dest, bool image) { seen.push_back((image ? std::string{"img:"} : std::string{"link:"}) + std::string{dest}); return "https://wiki.local" + std::string{dest}; }; const auto r = markdown_to_html("[a](/one) ![i](/two.png) [b](/three)", options); Void::Testing::expect(r.has_value()); Void::Testing::expect_eq( *r, std::string{"

a " "\"i\" " "b

\n"}); Void::Testing::expect_eq(seen.size(), std::size_t{3}); Void::Testing::expect_eq(seen[0], std::string{"link:/one"}); Void::Testing::expect_eq(seen[1], std::string{"img:/two.png"}); Void::Testing::expect_eq(seen[2], std::string{"link:/three"}); } Void::Testing::Test markdown_resolver_output_is_escaped() { MarkdownOptions options; options.resolver = [](std::string_view, bool) { return std::string{"\">"}; }; const std::string out = render_with("[x](/safe)", options); Void::Testing::expect(out.find("