Skip to content

Commit

Permalink
Fix bug with list element when AutoNewLine is enabled
Browse files Browse the repository at this point in the history
List elements shouldn't be appended a <br/> element is AutoNewLines is
enabed, since the <br/> will add an ugly empty line in the HTML output.
  • Loading branch information
Dynalon committed Sep 24, 2012
1 parent fd10209 commit 12e2ca1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
10 changes: 7 additions & 3 deletions MarkdownSharp/Markdown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ private string RunBlockGamut(string text)
/// <summary>
/// Perform transformations that occur *within* block-level tags like paragraphs, headers, and list items.
/// </summary>
private string RunSpanGamut(string text)
private string RunSpanGamut(string text, bool isListContext = false)
{
text = DoCodeSpans(text);
text = EscapeSpecialCharsWithinTagAttributes(text);
Expand All @@ -412,7 +412,11 @@ private string RunSpanGamut(string text)

text = EncodeAmpsAndAngles(text);
text = DoItalicsAndBold(text);
text = DoHardBreaks(text);
if (!isListContext) {
// do not add hard breaks for list elements,
// as we dont want code like <li>Element1</li><br/>
text = DoHardBreaks(text);
}

return text;
}
Expand Down Expand Up @@ -1244,7 +1248,7 @@ private string ListItemEvaluator(Match match)
// recursion for sub-lists
item = DoLists(Outdent(item));
item = item.TrimEnd('\n');
item = RunSpanGamut(item);
item = RunSpanGamut(item, isListContext: true);
}

return string.Format("<li>{0}</li>\n", item);
Expand Down
41 changes: 41 additions & 0 deletions MarkdownSharpTests/SimpleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,5 +208,46 @@ public void HorizontalRule()

Assert.AreEqual(expected, actual);
}
}

public class NonStandardMDTest : BaseTest
{
protected Markdown m;

[SetUp]
public void InitTest () {
m = new Markdown ();
}

[Test]
public void AutoNewLineListElements ()
{
// if AutoNewLine is enabled, list elements should not get a trailing <br/> element
string input = "* This is a list element";
string expected = "<ul>\n<li>This is a list element</li>\n</ul>\n";

m.AutoNewLines = true;
string actual = m.Transform(input);

Assert.AreEqual(expected, actual);
}
[Test]
public void AutoNewLines ()
{
// if AutoNewLine is enabled, list elements should not get a trailing <br/> element
string input = "This text has\na hard wrap.";
string expected_newlines = "<p>This text has<br />\na hard wrap.</p>\n";
string expected_standard = "<p>This text has\na hard wrap.</p>\n";

// test with enabled AutoNewLines which is not standard markdown compliant
m.AutoNewLines = true;
string actual = m.Transform(input);
Assert.AreEqual(expected_newlines, actual);

// test using standard Markdown
m.AutoNewLines = false;
actual = m.Transform(input);
Assert.AreEqual(expected_standard, actual);
}
}
}

0 comments on commit 12e2ca1

Please sign in to comment.