Skip to content

Commit 112ad21

Browse files
authored
Merge pull request #13 from nulab/fix/medium-low-severity-bugs
fix: medium- and low-severity parser and formatter bugs found by property tests
2 parents 6467df4 + 286596f commit 112ad21

34 files changed

Lines changed: 1620 additions & 53 deletions

File tree

‎flexmark-core-test/src/test/java/com/vladsch/flexmark/core/test/util/CoreTestSuite.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.vladsch.flexmark.core.test.util.html.HtmlEmbeddedAttributeTest;
44
import com.vladsch.flexmark.core.test.util.html.HtmlRendererTest;
5+
import com.vladsch.flexmark.core.test.util.html.ImageAltUndefinedReferenceTest;
56
import com.vladsch.flexmark.core.test.util.html.PathologicalTestSuite;
67
import com.vladsch.flexmark.core.test.util.parser.*;
78
import com.vladsch.flexmark.core.test.util.parser.ast.AbstractVisitorTest;
@@ -13,16 +14,22 @@
1314
@RunWith(Suite.class)
1415
@Suite.SuiteClasses({
1516
AbstractVisitorTest.class,
17+
BlockNestingLimitTest.class,
1618
DelimitedNodeTest.class,
1719
DelimiterProcessorTest.class,
20+
EmphasisInUndefinedReferenceTest.class,
1821
HtmlRendererTest.class,
22+
ImageAltUndefinedReferenceTest.class,
1923
ParserTest.class,
2024
LinkDestinationParserTest.class,
2125
PathologicalTestSuite.class,
2226
SpecialInputTest.class,
2327
UsageExampleTest.class,
2428
TextCollectingVisitorTest.class,
2529
UndefinedFullReferenceTextTest.class,
30+
LineEndingEquivalenceTest.class,
31+
UnbalancedLinkDestinationTest.class,
32+
ReferenceInBlockQuoteTest.class,
2633
HtmlDeepParserTest.class,
2734
HtmlEmbeddedAttributeTest.class,
2835
})

‎flexmark-core-test/src/test/java/com/vladsch/flexmark/core/test/util/formatter/CoreFormatterTestSuite.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
BlockQuoteLastChildPrefixFormatterTest.class,
1414
SetextHeadingInBlockQuoteFormatterTest.class,
1515
HtmlCommentBlockFormatterTest.class,
16+
FormatterKeepsDocumentTest.class,
1617
})
1718
public class CoreFormatterTestSuite {
1819
}
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
package com.vladsch.flexmark.core.test.util.formatter;
2+
3+
import com.vladsch.flexmark.formatter.Formatter;
4+
import com.vladsch.flexmark.html.HtmlRenderer;
5+
import com.vladsch.flexmark.parser.Parser;
6+
import com.vladsch.flexmark.test.util.Strings;
7+
import com.vladsch.flexmark.util.data.DataHolder;
8+
import com.vladsch.flexmark.util.data.MutableDataSet;
9+
import org.junit.Test;
10+
11+
import static org.junit.Assert.assertEquals;
12+
13+
/**
14+
* Formatting must not change the document: the formatted markdown renders to the same HTML as the original,
15+
* and formatting it again gives the same markdown.
16+
*/
17+
public class FormatterKeepsDocumentTest {
18+
private static final DataHolder OPTIONS = new MutableDataSet().toImmutable();
19+
private static final Parser PARSER = Parser.builder(OPTIONS).build();
20+
private static final HtmlRenderer RENDERER = HtmlRenderer.builder(OPTIONS).build();
21+
private static final Formatter FORMATTER = Formatter.builder(OPTIONS).build();
22+
23+
private static void assertFormatted(String markdown, String expectedMarkdown) {
24+
String expectedHtml = RENDERER.render(PARSER.parse(markdown));
25+
26+
String formatted = FORMATTER.render(PARSER.parse(markdown));
27+
assertEquals("formatted markdown", expectedMarkdown, formatted);
28+
assertEquals("html of formatted markdown", expectedHtml, RENDERER.render(PARSER.parse(formatted)));
29+
assertEquals("formatted again", formatted, FORMATTER.render(PARSER.parse(formatted)));
30+
}
31+
32+
@Test
33+
public void imageWithAngleBracketDestination() {
34+
assertFormatted("![](<>)", "![](<>)\n");
35+
assertFormatted("![](<> '')", "![](<> '')\n");
36+
assertFormatted("![](<a>)", "![](<a>)\n");
37+
assertFormatted("a ![x](<a> \"t\") b", "a ![x](<a> \"t\") b\n");
38+
}
39+
40+
@Test
41+
public void blockQuotesAfterReferenceStayApart() {
42+
assertFormatted(">- [r]: a\n\n>- b", "> - [r]: a\n\n> - b\n\n");
43+
assertFormatted("> [r]: a\n\n> b", "> [r]: a\n\n> b\n\n");
44+
assertFormatted("> > [r]: a\n\n> b", ">> [r]: a\n\n> b\n\n");
45+
}
46+
47+
@Test
48+
public void unterminatedHtmlBlockAtEndOfDocument() {
49+
assertFormatted("<style", "<style\n");
50+
assertFormatted("<script\na", "<script\na\n");
51+
assertFormatted("<?x", "<?x\n");
52+
assertFormatted("<!X", "<!X\n");
53+
assertFormatted("<![CDATA[ a", "<![CDATA[ a\n");
54+
assertFormatted("> <style\n\nb", "> <style\n\nb\n");
55+
}
56+
57+
@Test
58+
public void terminatedHtmlBlockKeepsBlankLineAfterIt() {
59+
assertFormatted("<style>\na\n</style>\nb", "<style>\na\n</style>\n\nb\n");
60+
assertFormatted("<!X>\nb", "<!X>\n\nb\n");
61+
assertFormatted("<div>", "<div>\n\n");
62+
}
63+
64+
@Test
65+
public void blockQuotesAfterHtmlBlockStayApart() {
66+
assertFormatted("> <div>\n\n> b", "> <div>\n\n> b\n\n");
67+
assertFormatted("> a\n> <div>\n\n> b", "> a\n>\n> <div>\n\n> b\n\n");
68+
}
69+
70+
@Test
71+
public void emptyBlockQuote() {
72+
assertFormatted(">", ">\n\n");
73+
assertFormatted(">\n>", ">\n\n");
74+
assertFormatted("> >", "> >\n\n");
75+
assertFormatted("a\n>", "a\n\n>\n\n");
76+
assertFormatted("> a\n>\n> >", "> a\n>\n> >\n\n");
77+
assertFormatted("- >", "- > \n\n");
78+
}
79+
80+
@Test
81+
public void firstBlockOfListItemStaysInItem() {
82+
assertFormatted("3) <u>", "3) <u>\n\n");
83+
assertFormatted("- # a", "- # a\n\n");
84+
assertFormatted("5) ---", "5) ---\n\n");
85+
assertFormatted("- ```\n a\n ```", "- ```\n a\n ```\n\n");
86+
assertFormatted("- - # a\n - b", "- - # a\n - b\n\n");
87+
assertFormatted("> - # a\n> - b", "> - # a\n> - b\n\n");
88+
}
89+
90+
@Test
91+
public void lastBlockOfListItemKeepsListTight() {
92+
assertFormatted("- # a\n- b", "- # a\n- b\n\n");
93+
assertFormatted("5) ---\n6) b", "5) ---\n6) b\n\n");
94+
assertFormatted("- a\n- # b\n- c", "- a\n- # b\n- c\n\n");
95+
assertFormatted("- <u>\n- b", "- <u>\n- b\n\n");
96+
}
97+
98+
@Test
99+
public void lastBlockOfListItemKeepsListLoose() {
100+
assertFormatted("- # a\n\n- b", "- # a\n\n- b\n\n");
101+
assertFormatted("- a\n\n- # b\n- c", "- a\n\n- # b\n\n- c\n\n");
102+
assertFormatted("- a\n\n # b\n- c", "- a\n\n # b\n\n- c\n\n");
103+
}
104+
105+
@Test
106+
public void lazyContinuationLineIsNotSetextUnderline() {
107+
assertFormatted("- _\n=", "- _\n =\n\n");
108+
assertFormatted("> a\n=", "> a\n> =\n\n");
109+
}
110+
111+
@Test
112+
public void indentedContinuationLineDoesNotStartBlock() {
113+
assertFormatted("a\n - b", "a\n - b\n");
114+
assertFormatted("]\n -", "]\n -\n");
115+
assertFormatted("a\n # b", "a\n # b\n");
116+
assertFormatted("a\n > b", "a\n > b\n");
117+
assertFormatted("a\n 1. b", "a\n 1. b\n");
118+
assertFormatted("a\n ***", "a\n ***\n");
119+
assertFormatted("a\n ```", "a\n ```\n");
120+
assertFormatted("a\n <div>", "a\n <div>\n");
121+
assertFormatted("a\\\n - b", "a\\\n - b\n");
122+
assertFormatted("*a\n - b*", "*a\n - b*\n");
123+
assertFormatted("Foo\n - b\n===", "Foo\n - b\n=======\n\n");
124+
}
125+
126+
@Test
127+
public void indentedContinuationLineWithZeroPaddedOrderedItemDoesNotStartBlock() {
128+
assertFormatted("a\n 01. b", "a\n 01. b\n");
129+
assertFormatted("a\n 000000001) b", "a\n 000000001) b\n");
130+
assertFormatted("a\n 0000000001. b", "a\n0000000001. b\n");
131+
assertFormatted("a\n 02. b", "a\n02. b\n");
132+
}
133+
134+
@Test
135+
public void indentedContinuationLineWithThematicBreak() {
136+
assertFormatted("a\n * * *", "a\n * * *\n");
137+
assertFormatted("a\n _\t_ _", "a\n _\t_ _\n");
138+
assertFormatted("a\n _ _ *", "a\n_ _ *\n");
139+
}
140+
141+
@Test
142+
public void longThematicBreakContinuationLineDoesNotOverflowStack() throws Throwable {
143+
String markdown = "a\n " + Strings.repeat("_ ", 10000);
144+
Throwable[] failure = new Throwable[1];
145+
// the stack of the thread that runs the tests can be larger than the default 1 MiB
146+
Thread thread = new Thread(null, () -> {
147+
try {
148+
assertFormatted(markdown, "a\n " + Strings.repeat("_ ", 9999) + "_\n");
149+
} catch (Throwable e) {
150+
failure[0] = e;
151+
}
152+
}, "format", 1024 * 1024);
153+
thread.start();
154+
thread.join();
155+
if (failure[0] != null) throw failure[0];
156+
}
157+
158+
@Test(timeout = 3000)
159+
public void manyContinuationLinesFormatInLinearTime() {
160+
String markdown = Strings.repeat("a\n", 100000);
161+
assertEquals(markdown, FORMATTER.render(PARSER.parse(markdown)));
162+
}
163+
164+
@Test
165+
public void continuationLineThatCannotStartBlockIsUnchanged() {
166+
assertFormatted("a\n2. b", "a\n2. b\n");
167+
assertFormatted("a\n <b>x</b>", "a\n<b>x</b>\n");
168+
assertFormatted("a\n b", "a\nb\n");
169+
}
170+
171+
@Test
172+
public void whiteSpaceInLinkTextIsKept() {
173+
assertFormatted("[\t][o]", "[\t][o]\n");
174+
assertFormatted("[]([ \\]](> '')", "[]([ \\]](> '')\n");
175+
assertFormatted("[ a ](/u)", "[ a ](/u)\n");
176+
assertFormatted("[ ](/u)", "[ ](/u)\n");
177+
assertFormatted("[ a ][b]", "[ a ][b]\n");
178+
assertFormatted("![ a ][b]", "![ a ][b]\n");
179+
assertFormatted("[\na\n](/u)", "[\na\n](/u)\n");
180+
assertFormatted("> [\nfoo\n](/u)", "> [\n> foo\n> ](/u)\n\n");
181+
}
182+
183+
@Test
184+
public void whiteSpaceInReferenceLabelIsKept() {
185+
assertFormatted("[- ]", "[- ]\n");
186+
assertFormatted("[\\ ]", "[\\ ]\n");
187+
assertFormatted("![- ]", "![- ]\n");
188+
assertFormatted("[a][\\ ]", "[a][\\ ]\n");
189+
assertFormatted("![ x ]\n\n[ x ]: /u", "![ x ]\n\n[ x ]: /u\n\n");
190+
assertFormatted("[a][ x ]\n\n[ x ]: /u", "[a][ x ]\n\n[ x ]: /u\n\n");
191+
}
192+
193+
@Test
194+
public void lineBreakInReferenceLabelIsKept() {
195+
// the label is written from its source text, which has no soft line break nodes to write the line break
196+
assertFormatted("[a][\n b]", "[a][\nb]\n");
197+
assertFormatted("[a][b\n]", "[a][b\n]\n");
198+
assertFormatted("![a][\nb]", "![a][\nb]\n");
199+
}
200+
201+
@Test
202+
public void lineBreakInReferenceLabelInContainerIsIdempotent() {
203+
// the container prefix after the line break is written by the writer, not copied from the source
204+
assertFormatted("- [a][b\n ]", "- [a][b\n ]\n\n");
205+
assertFormatted("- [a][\n b]", "- [a][\n b]\n\n");
206+
assertFormatted("> [a][b\n> ]", "> [a][b\n> ]\n\n");
207+
assertFormatted("- ![a][b\n ]", "- ![a][b\n ]\n\n");
208+
assertFormatted("- [a][b\n ]\n\n", "- [a][b\n ]\n\n");
209+
}
210+
211+
@Test
212+
public void referenceInsideBlockQuoteKeepsPrefixedBlankLine() {
213+
assertFormatted("> [r]: a\n> b", "> [r]: a\n>\n> b\n\n");
214+
}
215+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.vladsch.flexmark.core.test.util.html;
2+
3+
import com.vladsch.flexmark.html.HtmlRenderer;
4+
import com.vladsch.flexmark.html.IndependentLinkResolverFactory;
5+
import com.vladsch.flexmark.html.LinkResolver;
6+
import com.vladsch.flexmark.html.LinkResolverFactory;
7+
import com.vladsch.flexmark.html.renderer.LinkResolverBasicContext;
8+
import com.vladsch.flexmark.html.renderer.LinkStatus;
9+
import com.vladsch.flexmark.html.renderer.LinkType;
10+
import com.vladsch.flexmark.parser.Parser;
11+
import com.vladsch.flexmark.util.ast.Document;
12+
import com.vladsch.flexmark.util.data.DataHolder;
13+
import com.vladsch.flexmark.util.data.MutableDataSet;
14+
import org.jetbrains.annotations.NotNull;
15+
import org.junit.Test;
16+
17+
import static org.junit.Assert.assertEquals;
18+
19+
/**
20+
* An undefined reference inside an image description is literal text, so the image alt text keeps its
21+
* brackets and reference label (CommonMark 0.29).
22+
*/
23+
public class ImageAltUndefinedReferenceTest {
24+
private static final Parser PARSER = Parser.builder().build();
25+
private static final HtmlRenderer RENDERER = HtmlRenderer.builder().build();
26+
27+
private static String render(String markdown) {
28+
return RENDERER.render(PARSER.parse(markdown));
29+
}
30+
31+
@Test
32+
public void shortcutReference() {
33+
assertEquals("<p><img src=\"\" alt=\"[x]\" /></p>\n", render("![[x]]()"));
34+
}
35+
36+
@Test
37+
public void shortcutReferenceWithEmphasis() {
38+
assertEquals("<p><img src=\"\" alt=\"[a]\" /></p>\n", render("![[*a*]]()"));
39+
}
40+
41+
@Test
42+
public void fullReferenceWithEscape() {
43+
assertEquals("<p><img src=\"\" alt=\"[a][b*]\" /></p>\n", render("![[a][b\\*]]()"));
44+
}
45+
46+
@Test
47+
public void collapsedReference() {
48+
assertEquals("<p><img src=\"\" alt=\"[x][]\" /></p>\n", render("![[x][]]()"));
49+
}
50+
51+
@Test
52+
public void imageReference() {
53+
assertEquals("<p><img src=\"\" alt=\"![x]\" /></p>\n", render("![![x]]()"));
54+
}
55+
56+
@Test
57+
public void insideImageReference() {
58+
assertEquals("<p><img src=\"/u\" alt=\"[x]\" /></p>\n", render("![[x]][y]\n\n[y]: /u\n"));
59+
}
60+
61+
@Test
62+
public void definedReference_usesLinkText() {
63+
assertEquals("<p><img src=\"\" alt=\"x\" /></p>\n", render("![[x]]()\n\n[x]: /u\n"));
64+
}
65+
66+
@Test
67+
public void headingId_unchanged() {
68+
DataHolder options = new MutableDataSet()
69+
.set(HtmlRenderer.GENERATE_HEADER_ID, true)
70+
.set(HtmlRenderer.RENDER_HEADER_ID, true)
71+
.toImmutable();
72+
String html = HtmlRenderer.builder(options).build().render(Parser.builder(options).build().parse("# a [b][c] d"));
73+
assertEquals("<h1 id=\"a-b-d\">a [b][c] d</h1>\n", html);
74+
}
75+
76+
@Test
77+
public void imageReferenceResolvedByLinkResolver_usesText() {
78+
// the reference is not defined in the document, but the image is rendered, so its own brackets are not alt text
79+
LinkResolverFactory factory = new IndependentLinkResolverFactory() {
80+
@NotNull
81+
@Override
82+
public LinkResolver apply(@NotNull LinkResolverBasicContext context) {
83+
return (node, context1, link) -> link.getLinkType() == LinkType.IMAGE_REF && link.getUrl().equals("photo")
84+
? link.withStatus(LinkStatus.VALID).withUrl("/photo.png") : link;
85+
}
86+
};
87+
HtmlRenderer renderer = HtmlRenderer.builder().linkResolverFactory(factory).build();
88+
assertEquals("<p><img src=\"/photo.png\" alt=\"photo\" /></p>\n", renderer.render(PARSER.parse("![photo]")));
89+
assertEquals("<p><img src=\"/photo.png\" alt=\"[x] y\" /></p>\n", renderer.render(PARSER.parse("![[x] y][photo]")));
90+
}
91+
92+
@Test
93+
public void referenceTransferredAfterParsing() {
94+
// a reference transferred into the document defines a reference that was undefined when it was parsed
95+
Document document = PARSER.parse("![[x]]()");
96+
PARSER.transferReferences(document, PARSER.parse("[x]: /u\n"), null);
97+
assertEquals("<p><img src=\"\" alt=\"x\" /></p>\n", RENDERER.render(document));
98+
}
99+
}

‎flexmark-core-test/src/test/java/com/vladsch/flexmark/core/test/util/html/PathologicalRenderingTestCase.java‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,30 @@ public void mismatchedOpenersAndClosers() {
9898
Strings.repeat("*a_ ", x), "<p>" + Strings.repeat("*a_ ", x - 1) + "*a_</p>\n");
9999
}
100100

101+
@Test
102+
public void unclosedLinkDestinations() {
103+
assertRendering(
104+
Strings.repeat("[a](", x), "<p>" + Strings.repeat("[a](", x) + "</p>\n");
105+
}
106+
107+
@Test
108+
public void unclosedAngledLinkDestinations() {
109+
assertRendering(
110+
Strings.repeat("[a](<", x), "<p>" + Strings.repeat("[a](&lt;", x) + "</p>\n");
111+
}
112+
113+
@Test
114+
public void unclosedImageDestinations() {
115+
assertRendering(
116+
Strings.repeat("![a](", x), "<p>" + Strings.repeat("![a](", x) + "</p>\n");
117+
}
118+
119+
@Test
120+
public void unclosedEmptyTextLinkDestinations() {
121+
assertRendering(
122+
Strings.repeat("[](", x), "<p>" + Strings.repeat("[](", x) + "</p>\n");
123+
}
124+
101125
@Test
102126
public void nestedBrackets() {
103127
assertRendering(

0 commit comments

Comments
 (0)