Skip to content

Commit

Permalink
fix(simpleLineBreaks): fix simpleLineBreak option breaking lists html
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tivie committed Dec 20, 2016
1 parent 113f5f6 commit ed4c33f
Show file tree
Hide file tree
Showing 14 changed files with 139 additions and 44 deletions.
16 changes: 10 additions & 6 deletions dist/showdown.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/showdown.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/showdown.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/showdown.min.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/subParsers/hashHTMLSpans.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ showdown.subParser('hashHTMLSpans', function (text, config, globals) {
var matches = showdown.helper.matchRecursiveRegExp(text, '<code\\b[^>]*>', '</code>', 'gi');

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;
});
Expand All @@ -19,7 +19,7 @@ showdown.subParser('unhashHTMLSpans', function (text, config, globals) {
'use strict';

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;
Expand Down
4 changes: 4 additions & 0 deletions src/subParsers/lists.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
showdown.subParser('lists', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('lists.before', text, options, globals);

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

return item;
});

Expand Down
8 changes: 4 additions & 4 deletions src/subParsers/spanGamut.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ showdown.subParser('spanGamut', function (text, options, globals) {
text = showdown.subParser('strikethrough')(text, options, globals);

// Do hard breaks

// GFM style hard breaks
if (options.simpleLineBreaks) {
text = text.replace(/\n/g, '<br />\n');
// GFM style hard breaks
text = text.replace(/\b\n\b/g, '<br />\n');
} 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);
Expand Down
48 changes: 48 additions & 0 deletions test/features/#316.new-simpleLineBreaks-option-breaks-lists.html
Original file line number Diff line number Diff line change
@@ -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>
51 changes: 51 additions & 0 deletions test/features/#316.new-simpleLineBreaks-option-breaks-lists.md
Original file line number Diff line number Diff line change
@@ -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

12 changes: 4 additions & 8 deletions test/features/disableForced4SpacesIndentedSublists.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
<ul>
<li>foo

<ul>
<li>bar</li></ul></li>
<li>foo<ul>
<li>bar</li></ul></li>
</ul>
<p>...</p>
<ul>
<li>baz

<ol>
<li>bazinga</li></ol></li>
<li>baz<ol>
<li>bazinga</li></ol></li>
</ul>
18 changes: 6 additions & 12 deletions test/issues/#196.entity-in-code-block-in-nested-list.html
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
<p>Test pre in a list</p>
<ul>
<li>&amp; &lt;</li>
<li><code>&amp; &lt;</code>

<ul>
<li><code>&amp; &lt;</code><ul>
<li>&amp; &lt;</li>
<li><code>&amp; &lt;</code><ul>
<li>&amp; &lt;</li>
<li><code>&amp; &lt;</code>

<ul>
<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>
<li><code>&amp; &lt;</code><ul>
<li>&amp; &lt;</li>
<li><code>&amp; &lt;</code></li></ul></li></ul></li></ul></li>
</ul>
2 changes: 1 addition & 1 deletion test/issues/#196.entity-in-code-block-in-nested-list.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Test pre in a list

- & <
- `& <`
- `& <`
- & <
- `& <`
- & <
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@
</ol>
<p>foo</p>
<ul>
<li>one

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

<ul>
<li>two</li></ul></li>
<li>one<ul>
<li>two</li></ul></li>
</ul>
2 changes: 2 additions & 0 deletions test/node/testsuite.features.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ describe('makeHtml() features testsuite', function () {
converter = new showdown.Converter({disableForced4SpacesIndentedSublists: true});
} else if (testsuite[i].name === '#206.treat-single-line-breaks-as-br') {
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') {
converter = new showdown.Converter({simplifiedAutoLink: true, excludeTrailingPunctuationFromURLs: true});
} else if (testsuite[i].name === 'requireSpaceBeforeHeadingText') {
Expand Down

0 comments on commit ed4c33f

Please sign in to comment.