|
| 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("", "\n"); |
| 37 | + assertFormatted("a  b", "a  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 | +} |
0 commit comments