@@ -85,7 +85,7 @@ func TestMarkdownToHTML(t *testing.T) {
8585 {
8686 name : "list followed by blank line then paragraph" ,
8787 input : "- Item 1\n - Item 2\n \n Following paragraph." ,
88- expected : "<ul>\n <li>Item 1</li>\n <li>Item 2</li>\n </ul>\n <br >\n <p>Following paragraph.</p>" ,
88+ expected : "<ul>\n <li>Item 1</li>\n <li>Item 2</li>\n </ul>\n <p><br></p >\n <p>Following paragraph.</p>" ,
8989 },
9090 {
9191 // CommonMark §5.4: "After" is a lazy continuation of the second list item.
@@ -128,7 +128,7 @@ func TestMarkdownToHTML(t *testing.T) {
128128 {
129129 name : "mixed formatting" ,
130130 input : "# Title\n \n This is **bold** and *italic* and `code`." ,
131- expected : "<h1>Title</h1>\n <br >\n <p>This is <strong>bold</strong> and <em>italic</em> and <code>code</code>.</p>" ,
131+ expected : "<h1>Title</h1>\n <p><br></p >\n <p>This is <strong>bold</strong> and <em>italic</em> and <code>code</code>.</p>" ,
132132 },
133133 {
134134 name : "escapes HTML" ,
@@ -143,12 +143,12 @@ func TestMarkdownToHTML(t *testing.T) {
143143 {
144144 name : "paragraph spacing with blank line" ,
145145 input : "First paragraph\n \n Second paragraph" ,
146- expected : "<p>First paragraph</p>\n <br >\n <p>Second paragraph</p>" ,
146+ expected : "<p>First paragraph</p>\n <p><br></p >\n <p>Second paragraph</p>" ,
147147 },
148148 {
149149 name : "multiple blank lines collapse to one break" ,
150150 input : "First\n \n \n \n Second" ,
151- expected : "<p>First</p>\n <br >\n <p>Second</p>" ,
151+ expected : "<p>First</p>\n <p><br></p >\n <p>Second</p>" ,
152152 },
153153 {
154154 name : "consecutive lines join into one paragraph" ,
@@ -158,12 +158,12 @@ func TestMarkdownToHTML(t *testing.T) {
158158 {
159159 name : "blank line before list" ,
160160 input : "Intro\n \n - Item 1\n - Item 2" ,
161- expected : "<p>Intro</p>\n <br >\n <ul>\n <li>Item 1</li>\n <li>Item 2</li>\n </ul>" ,
161+ expected : "<p>Intro</p>\n <p><br></p >\n <ul>\n <li>Item 1</li>\n <li>Item 2</li>\n </ul>" ,
162162 },
163163 {
164164 name : "blank line before code block" ,
165165 input : "Intro\n \n ```\n code\n ```" ,
166- expected : "<p>Intro</p>\n <br >\n <pre><code>code\n </code></pre>" ,
166+ expected : "<p>Intro</p>\n <p><br></p >\n <pre><code>code\n </code></pre>" ,
167167 },
168168 {
169169 name : "leading blank lines ignored" ,
@@ -173,12 +173,12 @@ func TestMarkdownToHTML(t *testing.T) {
173173 {
174174 name : "blank line before blockquote" ,
175175 input : "Intro\n \n > A quote" ,
176- expected : "<p>Intro</p>\n <br >\n <blockquote>A quote</blockquote>" ,
176+ expected : "<p>Intro</p>\n <p><br></p >\n <blockquote>A quote</blockquote>" ,
177177 },
178178 {
179179 name : "blank line before horizontal rule" ,
180180 input : "Intro\n \n ---" ,
181- expected : "<p>Intro</p>\n <br >\n <hr>" ,
181+ expected : "<p>Intro</p>\n <p><br></p >\n <hr>" ,
182182 },
183183 {
184184 name : "heading flushes accumulated paragraph" ,
@@ -214,7 +214,7 @@ func TestMarkdownToHTML(t *testing.T) {
214214 {
215215 name : "fenced code block containing HTML tags is converted" ,
216216 input : "intro\n \n ```\n <div>hello</div>\n ```" ,
217- expected : "<p>intro</p>\n <br >\n <pre><code><div>hello</div>\n </code></pre>" ,
217+ expected : "<p>intro</p>\n <p><br></p >\n <pre><code><div>hello</div>\n </code></pre>" ,
218218 },
219219 }
220220
@@ -2223,30 +2223,30 @@ func TestMarkdownToHTMLInsertsParagraphSeparators(t *testing.T) {
22232223 {
22242224 name : "two contiguous paragraphs get a separator" ,
22252225 input : "<p>Line 1</p><p>Line 2</p>" ,
2226- expected : "<p>Line 1</p><br ><p>Line 2</p>" ,
2226+ expected : "<p>Line 1</p><p><br></p ><p>Line 2</p>" ,
22272227 },
22282228 {
22292229 name : "three contiguous paragraphs get separators between each" ,
22302230 input : "<p>A</p><p>B</p><p>C</p>" ,
2231- expected : "<p>A</p><br><p> B</p><br ><p>C</p>" ,
2231+ expected : "<p>A</p><p>< br></p><p> B</p><p><br></p ><p>C</p>" ,
22322232 },
22332233 {
22342234 name : "paragraphs with attributes are separated" ,
22352235 input : `<p dir="auto">A</p><p dir="auto">B</p>` ,
2236- expected : `<p dir="auto">A</p><br ><p dir="auto">B</p>` ,
2236+ expected : `<p dir="auto">A</p><p><br></p ><p dir="auto">B</p>` ,
22372237 },
22382238 {
22392239 name : "whitespace-only gap is preserved and separator added" ,
22402240 input : "<p>A</p>\n <p>B</p>" ,
2241- expected : "<p>A</p>\n <br ><p>B</p>" ,
2241+ expected : "<p>A</p>\n <p><br></p ><p>B</p>" ,
22422242 },
22432243 {
2244- name : "existing bare br separator is left untouched (idempotent) " ,
2244+ name : "bare br separator becomes an editor-durable separator paragraph " ,
22452245 input : "<p>A</p><br><p>B</p>" ,
2246- expected : "<p>A</p><br ><p>B</p>" ,
2246+ expected : "<p>A</p><p><br></p ><p>B</p>" ,
22472247 },
22482248 {
2249- name : "empty separator paragraph is left untouched (Lexxy canonical)" ,
2249+ name : "separator paragraph is left untouched (editor canonical)" ,
22502250 input : "<p>A</p><p><br></p><p>B</p>" ,
22512251 expected : "<p>A</p><p><br></p><p>B</p>" ,
22522252 },
@@ -2293,17 +2293,17 @@ func TestMarkdownToHTMLInsertsParagraphSeparators(t *testing.T) {
22932293 {
22942294 name : "paragraph with inline br is still non-empty and separated" ,
22952295 input : "<p>A<br>C</p><p>B</p>" ,
2296- expected : "<p>A<br>C</p><br ><p>B</p>" ,
2296+ expected : "<p>A<br>C</p><p><br></p ><p>B</p>" ,
22972297 },
22982298 {
22992299 name : "leading empty paragraph is left alone" ,
23002300 input : "<p></p><p>A</p>" ,
23012301 expected : "<p></p><p>A</p>" ,
23022302 },
23032303 {
2304- name : "mix of separated and contiguous only fills the gap that lacks a separator" ,
2304+ name : "mix of contiguous and bare-br boundaries both get separator paragraphs " ,
23052305 input : "<p>A</p><p>B</p><br><p>C</p>" ,
2306- expected : "<p>A</p><br><p> B</p><br ><p>C</p>" ,
2306+ expected : "<p>A</p><p>< br></p><p> B</p><p><br></p ><p>C</p>" ,
23072307 },
23082308 }
23092309
@@ -2325,6 +2325,7 @@ func TestMarkdownToHTMLParagraphSeparatorsIdempotent(t *testing.T) {
23252325 `<p dir="auto">A</p><p dir="auto">B</p>` ,
23262326 "<p>A</p>\n <p>B</p>" ,
23272327 "<p>A</p><p><br></p><p>B</p>" ,
2328+ "<p>A</p><br><p>B</p>" ,
23282329 "<p>A</p><h3>H</h3><p>B</p>" ,
23292330 }
23302331
@@ -2345,10 +2346,27 @@ func TestMarkdownToHTMLParagraphSeparatorsMatchMarkdownPath(t *testing.T) {
23452346 fromMarkdown := MarkdownToHTML ("Line 1\n \n Line 2" )
23462347 fromHTML := MarkdownToHTML ("<p>Line 1</p><p>Line 2</p>" )
23472348
2348- if ! strings .Contains (fromMarkdown , "<br>" ) {
2349- t .Fatalf ("markdown path unexpectedly produced no <br>: %q" , fromMarkdown )
2349+ if fromMarkdown != "<p>Line 1</p>\n <p><br></p>\n <p>Line 2</p>" {
2350+ t .Errorf ("markdown path = %q" , fromMarkdown )
2351+ }
2352+ if fromHTML != "<p>Line 1</p><p><br></p><p>Line 2</p>" {
2353+ t .Errorf ("HTML path = %q, want %q" , fromHTML , "<p>Line 1</p><p><br></p><p>Line 2</p>" )
2354+ }
2355+ }
2356+
2357+ // Basecamp's editor discards a bare top-level <br> the first time the content is
2358+ // edited, collapsing the spacing. No blank line between blocks may rely on one.
2359+ func TestMarkdownToHTMLEmitsNoBareTopLevelBreaks (t * testing.T ) {
2360+ markdown := "Para one.\n \n Para two.\n \n ## Heading\n \n - a\n - b\n \n > A quote\n \n ```\n code\n ```\n \n ---\n \n Closing."
2361+
2362+ html := MarkdownToHTML (markdown )
2363+
2364+ for _ , block := range []string {"<p>" , "<h2>" , "<ul>" , "<blockquote>" , "<pre>" , "<hr>" } {
2365+ if strings .Contains (html , "<br>\n " + block ) {
2366+ t .Errorf ("bare <br> separator before %s in %q" , block , html )
2367+ }
23502368 }
2351- if fromHTML != "<p>Line 1</p><br><p>Line 2</p>" {
2352- t .Errorf ("HTML path = %q , want %q" , fromHTML , "<p>Line 1</p><br><p>Line 2</p>" )
2369+ if want := strings . Count ( markdown , " \n \n " ); strings . Count ( html , paragraphSeparator ) != want {
2370+ t .Errorf ("got %d separator paragraphs , want %d in % q" , strings . Count ( html , paragraphSeparator ), want , html )
23532371 }
23542372}
0 commit comments