fix(simpleLineBreaks): fix simpleLineBreak option breaking lists html

When option was enabled, `<br />` tags where being added wrongfully
between `<li>` tags, which resulted in malformed html. This commit
prevents this behavior.

Closes #316
This commit is contained in:
Estevao Soares dos Santos 2016-12-20 23:57:10 +00:00
parent 113f5f64b1
commit ed4c33fe4e
14 changed files with 126 additions and 35 deletions

BIN
dist/showdown.js vendored

Binary file not shown.

BIN
dist/showdown.js.map vendored

Binary file not shown.

BIN
dist/showdown.min.js vendored

Binary file not shown.

Binary file not shown.

View File

@ -7,7 +7,7 @@ showdown.subParser('hashHTMLSpans', function (text, config, globals) {
var matches = showdown.helper.matchRecursiveRegExp(text, '<code\\b[^>]*>', '</code>', 'gi'); var matches = showdown.helper.matchRecursiveRegExp(text, '<code\\b[^>]*>', '</code>', 'gi');
for (var i = 0; i < matches.length; ++i) { for (var i = 0; i < matches.length; ++i) {
text = text.replace(matches[i][0], '~L' + (globals.gHtmlSpans.push(matches[i][0]) - 1) + 'L'); text = text.replace(matches[i][0], '~C' + (globals.gHtmlSpans.push(matches[i][0]) - 1) + 'C');
} }
return text; return text;
}); });
@ -19,7 +19,7 @@ showdown.subParser('unhashHTMLSpans', function (text, config, globals) {
'use strict'; 'use strict';
for (var i = 0; i < globals.gHtmlSpans.length; ++i) { for (var i = 0; i < globals.gHtmlSpans.length; ++i) {
text = text.replace('~L' + i + 'L', globals.gHtmlSpans[i]); text = text.replace('~C' + i + 'C', globals.gHtmlSpans[i]);
} }
return text; return text;

View File

@ -4,6 +4,7 @@
showdown.subParser('lists', function (text, options, globals) { showdown.subParser('lists', function (text, options, globals) {
'use strict'; 'use strict';
text = globals.converter._dispatch('lists.before', text, options, globals); text = globals.converter._dispatch('lists.before', text, options, globals);
/** /**
* Process the contents of a single ordered or unordered list, splitting it * Process the contents of a single ordered or unordered list, splitting it
* into individual list items. * into individual list items.
@ -91,6 +92,8 @@ showdown.subParser('lists', function (text, options, globals) {
// Recursion for sub-lists: // Recursion for sub-lists:
item = showdown.subParser('lists')(item, options, globals); item = showdown.subParser('lists')(item, options, globals);
item = item.replace(/\n$/, ''); // chomp(item) item = item.replace(/\n$/, ''); // chomp(item)
item = showdown.subParser('hashHTMLBlocks')(item, options, globals);
item = item.replace(/\n\n+/g, '\n\n');
if (isParagraphed) { if (isParagraphed) {
item = showdown.subParser('paragraphs')(item, options, globals); item = showdown.subParser('paragraphs')(item, options, globals);
} else { } else {
@ -102,6 +105,7 @@ showdown.subParser('lists', function (text, options, globals) {
item = item.replace('~A', ''); item = item.replace('~A', '');
// we can finally wrap the line in list item tags // we can finally wrap the line in list item tags
item = '<li' + bulletStyle + '>' + item + '</li>\n'; item = '<li' + bulletStyle + '>' + item + '</li>\n';
return item; return item;
}); });

View File

@ -24,12 +24,12 @@ showdown.subParser('spanGamut', function (text, options, globals) {
text = showdown.subParser('strikethrough')(text, options, globals); text = showdown.subParser('strikethrough')(text, options, globals);
// Do hard breaks // Do hard breaks
// GFM style hard breaks
if (options.simpleLineBreaks) { if (options.simpleLineBreaks) {
text = text.replace(/\n/g, '<br />\n'); // GFM style hard breaks
text = text.replace(/\b\n\b/g, '<br />\n');
} else { } else {
text = text.replace(/ +\n/g, '<br />\n'); // Vanilla hard breaks
text = text.replace(/\b +\n\b/g, '<br />\n');
} }
text = globals.converter._dispatch('spanGamut.after', text, options, globals); text = globals.converter._dispatch('spanGamut.after', text, options, globals);

View File

@ -0,0 +1,48 @@
<ol>
<li>One</li>
<li>Two<ul>
<li>A</li>
<li>B</li></ul></li>
<li>Three</li>
</ol>
<blockquote>
<p>this has<br />
simple linebreaks</p>
</blockquote>
<pre><code>testing
some
code
</code></pre>
<ol>
<li><p>paragraphed list</p>
<p>this belongs<br />
to the first list item</p></li>
<li><p>This text<br />
also</p></li>
</ol>
<p>simple<br />
text</p>
<ul>
<li>a list<br />
item</li>
<li>another<br />
list item</li>
</ul>
<p>simple<br />
text</p>
<ul>
<li><p>some item</p>
<p>another<br />
paragraph</p>
<ul>
<li><p>And<br />
now</p>
<p>paragraph<br />
sublist</p>
<ul>
<li><p>and<br />
even</p>
<p>another<br />
one</p></li></ul></li></ul></li>
<li><p>foo</p></li>
</ul>

View File

@ -0,0 +1,51 @@
1. One
2. Two
- A
- B
3. Three
> this has
> simple linebreaks
testing
some
code
1. paragraphed list
this belongs
to the first list item
2. This text
also
simple
text
- a list
item
- another
list item
simple
text
- some item
another
paragraph
- And
now
paragraph
sublist
- and
even
another
one
- foo

View File

@ -1,13 +1,9 @@
<ul> <ul>
<li>foo <li>foo<ul>
<li>bar</li></ul></li>
<ul>
<li>bar</li></ul></li>
</ul> </ul>
<p>...</p> <p>...</p>
<ul> <ul>
<li>baz <li>baz<ol>
<li>bazinga</li></ol></li>
<ol>
<li>bazinga</li></ol></li>
</ul> </ul>

View File

@ -1,17 +1,11 @@
<p>Test pre in a list</p> <p>Test pre in a list</p>
<ul> <ul>
<li>&amp; &lt;</li> <li>&amp; &lt;</li>
<li><code>&amp; &lt;</code> <li><code>&amp; &lt;</code><ul>
<li>&amp; &lt;</li>
<ul> <li><code>&amp; &lt;</code><ul>
<li>&amp; &lt;</li> <li>&amp; &lt;</li>
<li><code>&amp; &lt;</code> <li><code>&amp; &lt;</code><ul>
<li>&amp; &lt;</li>
<ul> <li><code>&amp; &lt;</code></li></ul></li></ul></li></ul></li>
<li>&amp; &lt;</li>
<li><code>&amp; &lt;</code>
<ul>
<li>&amp; &lt;</li>
<li><code>&amp; &lt;</code></li></ul></li></ul></li></ul></li>
</ul> </ul>

View File

@ -20,10 +20,8 @@
</ol> </ol>
<p>foo</p> <p>foo</p>
<ul> <ul>
<li>one <li>one<ol>
<li>two</li></ol></li>
<ol>
<li>two</li></ol></li>
</ul> </ul>
<p>foo</p> <p>foo</p>
<ul> <ul>
@ -47,8 +45,6 @@
</ul> </ul>
<p>foo</p> <p>foo</p>
<ul> <ul>
<li>one <li>one<ul>
<li>two</li></ul></li>
<ul>
<li>two</li></ul></li>
</ul> </ul>

View File

@ -37,6 +37,8 @@ describe('makeHtml() features testsuite', function () {
converter = new showdown.Converter({disableForced4SpacesIndentedSublists: true}); converter = new showdown.Converter({disableForced4SpacesIndentedSublists: true});
} else if (testsuite[i].name === '#206.treat-single-line-breaks-as-br') { } else if (testsuite[i].name === '#206.treat-single-line-breaks-as-br') {
converter = new showdown.Converter({simpleLineBreaks: true}); converter = new showdown.Converter({simpleLineBreaks: true});
} else if (testsuite[i].name === '#316.new-simpleLineBreaks-option-breaks-lists') {
converter = new showdown.Converter({simpleLineBreaks: true});
} else if (testsuite[i].name === 'excludeTrailingPunctuationFromURLs-option') { } else if (testsuite[i].name === 'excludeTrailingPunctuationFromURLs-option') {
converter = new showdown.Converter({simplifiedAutoLink: true, excludeTrailingPunctuationFromURLs: true}); converter = new showdown.Converter({simplifiedAutoLink: true, excludeTrailingPunctuationFromURLs: true});
} else if (testsuite[i].name === 'requireSpaceBeforeHeadingText') { } else if (testsuite[i].name === 'requireSpaceBeforeHeadingText') {