Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
922e991
fix: treat a lone CR as a line ending in normalizeEOL
raydive Dec 3, 2026
2f0979e
fix: normalize line endings in link titles, alt text and undefined re…
raydive Dec 3, 2026
2913ee3
fix: limit block container nesting depth to avoid stack overflow
raydive Dec 3, 2026
24f9af6
fix: reject link destinations with unbalanced parentheses
raydive Dec 3, 2026
1bdf195
fix: write the opening angle bracket of an image destination once
raydive Dec 3, 2026
53b3e6d
fix: pair emphasis in undefined references the same at any paragraph …
raydive Dec 3, 2026
6cb4f41
fix: limit parenthesis nesting in link destinations
raydive Dec 3, 2026
fbd0f47
fix: keep block quotes apart when a reference definition ends one
raydive Dec 3, 2026
36ee7e0
fix: format empty and block-quoted definition items without crashing
raydive Dec 3, 2026
f70a412
fix: stop adding blank lines to an unterminated HTML block
raydive Dec 3, 2026
5ccd2f3
fix: keep the brackets of undefined references in image alt text
raydive Dec 3, 2026
9ab3b57
fix: keep an empty block quote
raydive Dec 3, 2026
62c0cbf
fix: turn a definition item without a term into a paragraph
raydive Dec 3, 2026
1a08541
fix: keep the first block of a list item in the item
raydive Dec 3, 2026
7939a1b
fix: parse reference definitions from the paragraph's content lines
raydive Dec 3, 2026
7705eac
fix: keep paragraph continuation lines from starting a block
raydive Dec 3, 2026
04a0dcb
fix: keep the whitespace inside the brackets of link text
raydive Dec 3, 2026
a92ce4a
fix: keep the whitespace inside the brackets of a reference label
raydive Dec 3, 2026
810b09f
fix: take match offsets from the matcher instead of a match result
raydive Dec 4, 2026
3d71835
fix: harden the continuation line check of the formatter
raydive Dec 4, 2026
b7750cb
fix: treat runs of different lengths in brackets as straddling
raydive Dec 4, 2026
6f27842
fix: ask canContain about the nesting limit only at the limit
raydive Dec 4, 2026
89d2918
fix: address the second review of the bracket, alt text and label fixes
raydive Dec 4, 2026
286596f
fix: address the third review of the bracket, alt text and label fixes
raydive Dec 4, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.vladsch.flexmark.core.test.util.html.HtmlEmbeddedAttributeTest;
import com.vladsch.flexmark.core.test.util.html.HtmlRendererTest;
import com.vladsch.flexmark.core.test.util.html.ImageAltUndefinedReferenceTest;
import com.vladsch.flexmark.core.test.util.html.PathologicalTestSuite;
import com.vladsch.flexmark.core.test.util.parser.*;
import com.vladsch.flexmark.core.test.util.parser.ast.AbstractVisitorTest;
Expand All @@ -13,16 +14,22 @@
@RunWith(Suite.class)
@Suite.SuiteClasses({
AbstractVisitorTest.class,
BlockNestingLimitTest.class,
DelimitedNodeTest.class,
DelimiterProcessorTest.class,
EmphasisInUndefinedReferenceTest.class,
HtmlRendererTest.class,
ImageAltUndefinedReferenceTest.class,
ParserTest.class,
LinkDestinationParserTest.class,
PathologicalTestSuite.class,
SpecialInputTest.class,
UsageExampleTest.class,
TextCollectingVisitorTest.class,
UndefinedFullReferenceTextTest.class,
LineEndingEquivalenceTest.class,
UnbalancedLinkDestinationTest.class,
ReferenceInBlockQuoteTest.class,
HtmlDeepParserTest.class,
HtmlEmbeddedAttributeTest.class,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
BlockQuoteLastChildPrefixFormatterTest.class,
SetextHeadingInBlockQuoteFormatterTest.class,
HtmlCommentBlockFormatterTest.class,
FormatterKeepsDocumentTest.class,
})
public class CoreFormatterTestSuite {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
package com.vladsch.flexmark.core.test.util.formatter;

import com.vladsch.flexmark.formatter.Formatter;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.test.util.Strings;
import com.vladsch.flexmark.util.data.DataHolder;
import com.vladsch.flexmark.util.data.MutableDataSet;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
* Formatting must not change the document: the formatted markdown renders to the same HTML as the original,
* and formatting it again gives the same markdown.
*/
public class FormatterKeepsDocumentTest {
private static final DataHolder OPTIONS = new MutableDataSet().toImmutable();
private static final Parser PARSER = Parser.builder(OPTIONS).build();
private static final HtmlRenderer RENDERER = HtmlRenderer.builder(OPTIONS).build();
private static final Formatter FORMATTER = Formatter.builder(OPTIONS).build();

private static void assertFormatted(String markdown, String expectedMarkdown) {
String expectedHtml = RENDERER.render(PARSER.parse(markdown));

String formatted = FORMATTER.render(PARSER.parse(markdown));
assertEquals("formatted markdown", expectedMarkdown, formatted);
assertEquals("html of formatted markdown", expectedHtml, RENDERER.render(PARSER.parse(formatted)));
assertEquals("formatted again", formatted, FORMATTER.render(PARSER.parse(formatted)));
}

@Test
public void imageWithAngleBracketDestination() {
assertFormatted("![](<>)", "![](<>)\n");
assertFormatted("![](<> '')", "![](<> '')\n");
assertFormatted("![](<a>)", "![](<a>)\n");
assertFormatted("a ![x](<a> \"t\") b", "a ![x](<a> \"t\") b\n");
}

@Test
public void blockQuotesAfterReferenceStayApart() {
assertFormatted(">- [r]: a\n\n>- b", "> - [r]: a\n\n> - b\n\n");
assertFormatted("> [r]: a\n\n> b", "> [r]: a\n\n> b\n\n");
assertFormatted("> > [r]: a\n\n> b", ">> [r]: a\n\n> b\n\n");
}

@Test
public void unterminatedHtmlBlockAtEndOfDocument() {
assertFormatted("<style", "<style\n");
assertFormatted("<script\na", "<script\na\n");
assertFormatted("<?x", "<?x\n");
assertFormatted("<!X", "<!X\n");
assertFormatted("<![CDATA[ a", "<![CDATA[ a\n");
assertFormatted("> <style\n\nb", "> <style\n\nb\n");
}

@Test
public void terminatedHtmlBlockKeepsBlankLineAfterIt() {
assertFormatted("<style>\na\n</style>\nb", "<style>\na\n</style>\n\nb\n");
assertFormatted("<!X>\nb", "<!X>\n\nb\n");
assertFormatted("<div>", "<div>\n\n");
}

@Test
public void blockQuotesAfterHtmlBlockStayApart() {
assertFormatted("> <div>\n\n> b", "> <div>\n\n> b\n\n");
assertFormatted("> a\n> <div>\n\n> b", "> a\n>\n> <div>\n\n> b\n\n");
}

@Test
public void emptyBlockQuote() {
assertFormatted(">", ">\n\n");
assertFormatted(">\n>", ">\n\n");
assertFormatted("> >", "> >\n\n");
assertFormatted("a\n>", "a\n\n>\n\n");
assertFormatted("> a\n>\n> >", "> a\n>\n> >\n\n");
assertFormatted("- >", "- > \n\n");
}

@Test
public void firstBlockOfListItemStaysInItem() {
assertFormatted("3) <u>", "3) <u>\n\n");
assertFormatted("- # a", "- # a\n\n");
assertFormatted("5) ---", "5) ---\n\n");
assertFormatted("- ```\n a\n ```", "- ```\n a\n ```\n\n");
assertFormatted("- - # a\n - b", "- - # a\n - b\n\n");
assertFormatted("> - # a\n> - b", "> - # a\n> - b\n\n");
}

@Test
public void lastBlockOfListItemKeepsListTight() {
assertFormatted("- # a\n- b", "- # a\n- b\n\n");
assertFormatted("5) ---\n6) b", "5) ---\n6) b\n\n");
assertFormatted("- a\n- # b\n- c", "- a\n- # b\n- c\n\n");
assertFormatted("- <u>\n- b", "- <u>\n- b\n\n");
}

@Test
public void lastBlockOfListItemKeepsListLoose() {
assertFormatted("- # a\n\n- b", "- # a\n\n- b\n\n");
assertFormatted("- a\n\n- # b\n- c", "- a\n\n- # b\n\n- c\n\n");
assertFormatted("- a\n\n # b\n- c", "- a\n\n # b\n\n- c\n\n");
}

@Test
public void lazyContinuationLineIsNotSetextUnderline() {
assertFormatted("- _\n=", "- _\n =\n\n");
assertFormatted("> a\n=", "> a\n> =\n\n");
}

@Test
public void indentedContinuationLineDoesNotStartBlock() {
assertFormatted("a\n - b", "a\n - b\n");
assertFormatted("]\n -", "]\n -\n");
assertFormatted("a\n # b", "a\n # b\n");
assertFormatted("a\n > b", "a\n > b\n");
assertFormatted("a\n 1. b", "a\n 1. b\n");
assertFormatted("a\n ***", "a\n ***\n");
assertFormatted("a\n ```", "a\n ```\n");
assertFormatted("a\n <div>", "a\n <div>\n");
assertFormatted("a\\\n - b", "a\\\n - b\n");
assertFormatted("*a\n - b*", "*a\n - b*\n");
assertFormatted("Foo\n - b\n===", "Foo\n - b\n=======\n\n");
}

@Test
public void indentedContinuationLineWithZeroPaddedOrderedItemDoesNotStartBlock() {
assertFormatted("a\n 01. b", "a\n 01. b\n");
assertFormatted("a\n 000000001) b", "a\n 000000001) b\n");
assertFormatted("a\n 0000000001. b", "a\n0000000001. b\n");
assertFormatted("a\n 02. b", "a\n02. b\n");
}

@Test
public void indentedContinuationLineWithThematicBreak() {
assertFormatted("a\n * * *", "a\n * * *\n");
assertFormatted("a\n _\t_ _", "a\n _\t_ _\n");
assertFormatted("a\n _ _ *", "a\n_ _ *\n");
}

@Test
public void longThematicBreakContinuationLineDoesNotOverflowStack() throws Throwable {
String markdown = "a\n " + Strings.repeat("_ ", 10000);
Throwable[] failure = new Throwable[1];
// the stack of the thread that runs the tests can be larger than the default 1 MiB
Thread thread = new Thread(null, () -> {
try {
assertFormatted(markdown, "a\n " + Strings.repeat("_ ", 9999) + "_\n");
} catch (Throwable e) {
failure[0] = e;
}
}, "format", 1024 * 1024);
thread.start();
thread.join();
if (failure[0] != null) throw failure[0];
}

@Test(timeout = 3000)
public void manyContinuationLinesFormatInLinearTime() {
String markdown = Strings.repeat("a\n", 100000);
assertEquals(markdown, FORMATTER.render(PARSER.parse(markdown)));
}

@Test
public void continuationLineThatCannotStartBlockIsUnchanged() {
assertFormatted("a\n2. b", "a\n2. b\n");
assertFormatted("a\n <b>x</b>", "a\n<b>x</b>\n");
assertFormatted("a\n b", "a\nb\n");
}

@Test
public void whiteSpaceInLinkTextIsKept() {
assertFormatted("[\t][o]", "[\t][o]\n");
assertFormatted("[]([ \\]](> '')", "[]([ \\]](> '')\n");
assertFormatted("[ a ](/u)", "[ a ](/u)\n");
assertFormatted("[ ](/u)", "[ ](/u)\n");
assertFormatted("[ a ][b]", "[ a ][b]\n");
assertFormatted("![ a ][b]", "![ a ][b]\n");
assertFormatted("[\na\n](/u)", "[\na\n](/u)\n");
assertFormatted("> [\nfoo\n](/u)", "> [\n> foo\n> ](/u)\n\n");
}

@Test
public void whiteSpaceInReferenceLabelIsKept() {
assertFormatted("[- ]", "[- ]\n");
assertFormatted("[\\ ]", "[\\ ]\n");
assertFormatted("![- ]", "![- ]\n");
assertFormatted("[a][\\ ]", "[a][\\ ]\n");
assertFormatted("![ x ]\n\n[ x ]: /u", "![ x ]\n\n[ x ]: /u\n\n");
assertFormatted("[a][ x ]\n\n[ x ]: /u", "[a][ x ]\n\n[ x ]: /u\n\n");
}

@Test
public void lineBreakInReferenceLabelIsKept() {
// the label is written from its source text, which has no soft line break nodes to write the line break
assertFormatted("[a][\n b]", "[a][\nb]\n");
assertFormatted("[a][b\n]", "[a][b\n]\n");
assertFormatted("![a][\nb]", "![a][\nb]\n");
}

@Test
public void lineBreakInReferenceLabelInContainerIsIdempotent() {
// the container prefix after the line break is written by the writer, not copied from the source
assertFormatted("- [a][b\n ]", "- [a][b\n ]\n\n");
assertFormatted("- [a][\n b]", "- [a][\n b]\n\n");
assertFormatted("> [a][b\n> ]", "> [a][b\n> ]\n\n");
assertFormatted("- ![a][b\n ]", "- ![a][b\n ]\n\n");
assertFormatted("- [a][b\n ]\n\n", "- [a][b\n ]\n\n");
}

@Test
public void referenceInsideBlockQuoteKeepsPrefixedBlankLine() {
assertFormatted("> [r]: a\n> b", "> [r]: a\n>\n> b\n\n");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.vladsch.flexmark.core.test.util.html;

import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.html.IndependentLinkResolverFactory;
import com.vladsch.flexmark.html.LinkResolver;
import com.vladsch.flexmark.html.LinkResolverFactory;
import com.vladsch.flexmark.html.renderer.LinkResolverBasicContext;
import com.vladsch.flexmark.html.renderer.LinkStatus;
import com.vladsch.flexmark.html.renderer.LinkType;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.ast.Document;
import com.vladsch.flexmark.util.data.DataHolder;
import com.vladsch.flexmark.util.data.MutableDataSet;
import org.jetbrains.annotations.NotNull;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
* An undefined reference inside an image description is literal text, so the image alt text keeps its
* brackets and reference label (CommonMark 0.29).
*/
public class ImageAltUndefinedReferenceTest {
private static final Parser PARSER = Parser.builder().build();
private static final HtmlRenderer RENDERER = HtmlRenderer.builder().build();

private static String render(String markdown) {
return RENDERER.render(PARSER.parse(markdown));
}

@Test
public void shortcutReference() {
assertEquals("<p><img src=\"\" alt=\"[x]\" /></p>\n", render("![[x]]()"));
}

@Test
public void shortcutReferenceWithEmphasis() {
assertEquals("<p><img src=\"\" alt=\"[a]\" /></p>\n", render("![[*a*]]()"));
}

@Test
public void fullReferenceWithEscape() {
assertEquals("<p><img src=\"\" alt=\"[a][b*]\" /></p>\n", render("![[a][b\\*]]()"));
}

@Test
public void collapsedReference() {
assertEquals("<p><img src=\"\" alt=\"[x][]\" /></p>\n", render("![[x][]]()"));
}

@Test
public void imageReference() {
assertEquals("<p><img src=\"\" alt=\"![x]\" /></p>\n", render("![![x]]()"));
}

@Test
public void insideImageReference() {
assertEquals("<p><img src=\"/u\" alt=\"[x]\" /></p>\n", render("![[x]][y]\n\n[y]: /u\n"));
}

@Test
public void definedReference_usesLinkText() {
assertEquals("<p><img src=\"\" alt=\"x\" /></p>\n", render("![[x]]()\n\n[x]: /u\n"));
}

@Test
public void headingId_unchanged() {
DataHolder options = new MutableDataSet()
.set(HtmlRenderer.GENERATE_HEADER_ID, true)
.set(HtmlRenderer.RENDER_HEADER_ID, true)
.toImmutable();
String html = HtmlRenderer.builder(options).build().render(Parser.builder(options).build().parse("# a [b][c] d"));
assertEquals("<h1 id=\"a-b-d\">a [b][c] d</h1>\n", html);
}

@Test
public void imageReferenceResolvedByLinkResolver_usesText() {
// the reference is not defined in the document, but the image is rendered, so its own brackets are not alt text
LinkResolverFactory factory = new IndependentLinkResolverFactory() {
@NotNull
@Override
public LinkResolver apply(@NotNull LinkResolverBasicContext context) {
return (node, context1, link) -> link.getLinkType() == LinkType.IMAGE_REF && link.getUrl().equals("photo")
? link.withStatus(LinkStatus.VALID).withUrl("/photo.png") : link;
}
};
HtmlRenderer renderer = HtmlRenderer.builder().linkResolverFactory(factory).build();
assertEquals("<p><img src=\"/photo.png\" alt=\"photo\" /></p>\n", renderer.render(PARSER.parse("![photo]")));
assertEquals("<p><img src=\"/photo.png\" alt=\"[x] y\" /></p>\n", renderer.render(PARSER.parse("![[x] y][photo]")));
}

@Test
public void referenceTransferredAfterParsing() {
// a reference transferred into the document defines a reference that was undefined when it was parsed
Document document = PARSER.parse("![[x]]()");
PARSER.transferReferences(document, PARSER.parse("[x]: /u\n"), null);
assertEquals("<p><img src=\"\" alt=\"x\" /></p>\n", RENDERER.render(document));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,30 @@ public void mismatchedOpenersAndClosers() {
Strings.repeat("*a_ ", x), "<p>" + Strings.repeat("*a_ ", x - 1) + "*a_</p>\n");
}

@Test
public void unclosedLinkDestinations() {
assertRendering(
Strings.repeat("[a](", x), "<p>" + Strings.repeat("[a](", x) + "</p>\n");
}

@Test
public void unclosedAngledLinkDestinations() {
assertRendering(
Strings.repeat("[a](<", x), "<p>" + Strings.repeat("[a](&lt;", x) + "</p>\n");
}

@Test
public void unclosedImageDestinations() {
assertRendering(
Strings.repeat("![a](", x), "<p>" + Strings.repeat("![a](", x) + "</p>\n");
}

@Test
public void unclosedEmptyTextLinkDestinations() {
assertRendering(
Strings.repeat("[](", x), "<p>" + Strings.repeat("[](", x) + "</p>\n");
}

@Test
public void nestedBrackets() {
assertRendering(
Expand Down
Loading