From cdc4687db56950fa8e7a572fd93717aa66fadeb3 Mon Sep 17 00:00:00 2001
From: Samuel Sperling Tags
---
..._RegressionWithVaryingNewLines.verified.md | 2 +-
...aragraphs_ThenConvertCorrectly.verified.md | 9 ++
...HTML_ThenConvertWithoutHanging.verified.md | 1 +
...aragraphs_ThenConvertCorrectly.verified.md | 1 +
...sAndSpans_ThenConvertCorrectly.verified.md | 4 +
...aragraphs_ThenConvertCorrectly.verified.md | 15 +++
...evelsDeep_ThenConvertCorrectly.verified.md | 9 ++
...evelsDeep_ThenConvertCorrectly.verified.md | 19 +++
...evelsDeep_ThenConvertCorrectly.verified.md | 1 +
...TextNodes_ThenConvertCorrectly.verified.md | 5 +
src/ReverseMarkdown.Test/ConverterTests.cs | 117 ++++++++++++++++++
src/ReverseMarkdown/Converters/P.cs | 8 +-
12 files changed, 188 insertions(+), 3 deletions(-)
create mode 100644 src/ReverseMarkdown.Test/ConverterTests.When_AlternatingEmptyAndFilledNestedParagraphs_ThenConvertCorrectly.verified.md
create mode 100644 src/ReverseMarkdown.Test/ConverterTests.When_DeeplyNestedParagraphs_WithMalformedHTML_ThenConvertWithoutHanging.verified.md
create mode 100644 src/ReverseMarkdown.Test/ConverterTests.When_EmptyNestedParagraphs_ThenConvertCorrectly.verified.md
create mode 100644 src/ReverseMarkdown.Test/ConverterTests.When_InterleavedParagraphsAndSpans_ThenConvertCorrectly.verified.md
create mode 100644 src/ReverseMarkdown.Test/ConverterTests.When_ManySequentialUnclosedParagraphs_ThenConvertCorrectly.verified.md
create mode 100644 src/ReverseMarkdown.Test/ConverterTests.When_NestedParagraphs_FiveLevelsDeep_ThenConvertCorrectly.verified.md
create mode 100644 src/ReverseMarkdown.Test/ConverterTests.When_NestedParagraphs_TenLevelsDeep_ThenConvertCorrectly.verified.md
create mode 100644 src/ReverseMarkdown.Test/ConverterTests.When_NestedSpans_FiveLevelsDeep_ThenConvertCorrectly.verified.md
create mode 100644 src/ReverseMarkdown.Test/ConverterTests.When_UnclosedParagraphsWithSpansAndTextNodes_ThenConvertCorrectly.verified.md
diff --git a/src/ReverseMarkdown.Test/ConverterTests.Bug393_RegressionWithVaryingNewLines.verified.md b/src/ReverseMarkdown.Test/ConverterTests.Bug393_RegressionWithVaryingNewLines.verified.md
index fa57f5f..90b692d 100644
--- a/src/ReverseMarkdown.Test/ConverterTests.Bug393_RegressionWithVaryingNewLines.verified.md
+++ b/src/ReverseMarkdown.Test/ConverterTests.Bug393_RegressionWithVaryingNewLines.verified.md
@@ -1,6 +1,6 @@
This is regular text
-This is HTML:
+This is HTML:
* Line 1
* Line 2
diff --git a/src/ReverseMarkdown.Test/ConverterTests.When_AlternatingEmptyAndFilledNestedParagraphs_ThenConvertCorrectly.verified.md b/src/ReverseMarkdown.Test/ConverterTests.When_AlternatingEmptyAndFilledNestedParagraphs_ThenConvertCorrectly.verified.md
new file mode 100644
index 0000000..58a9f34
--- /dev/null
+++ b/src/ReverseMarkdown.Test/ConverterTests.When_AlternatingEmptyAndFilledNestedParagraphs_ThenConvertCorrectly.verified.md
@@ -0,0 +1,9 @@
+A
+
+B
+
+C
+
+D
+
+E
\ No newline at end of file
diff --git a/src/ReverseMarkdown.Test/ConverterTests.When_DeeplyNestedParagraphs_WithMalformedHTML_ThenConvertWithoutHanging.verified.md b/src/ReverseMarkdown.Test/ConverterTests.When_DeeplyNestedParagraphs_WithMalformedHTML_ThenConvertWithoutHanging.verified.md
new file mode 100644
index 0000000..5f28270
--- /dev/null
+++ b/src/ReverseMarkdown.Test/ConverterTests.When_DeeplyNestedParagraphs_WithMalformedHTML_ThenConvertWithoutHanging.verified.md
@@ -0,0 +1 @@
+
\ No newline at end of file
diff --git a/src/ReverseMarkdown.Test/ConverterTests.When_EmptyNestedParagraphs_ThenConvertCorrectly.verified.md b/src/ReverseMarkdown.Test/ConverterTests.When_EmptyNestedParagraphs_ThenConvertCorrectly.verified.md
new file mode 100644
index 0000000..5f28270
--- /dev/null
+++ b/src/ReverseMarkdown.Test/ConverterTests.When_EmptyNestedParagraphs_ThenConvertCorrectly.verified.md
@@ -0,0 +1 @@
+
\ No newline at end of file
diff --git a/src/ReverseMarkdown.Test/ConverterTests.When_InterleavedParagraphsAndSpans_ThenConvertCorrectly.verified.md b/src/ReverseMarkdown.Test/ConverterTests.When_InterleavedParagraphsAndSpans_ThenConvertCorrectly.verified.md
new file mode 100644
index 0000000..b9b0568
--- /dev/null
+++ b/src/ReverseMarkdown.Test/ConverterTests.When_InterleavedParagraphsAndSpans_ThenConvertCorrectly.verified.md
@@ -0,0 +1,4 @@
+Text1
+Text2
+Text3
+Text4
\ No newline at end of file
diff --git a/src/ReverseMarkdown.Test/ConverterTests.When_ManySequentialUnclosedParagraphs_ThenConvertCorrectly.verified.md b/src/ReverseMarkdown.Test/ConverterTests.When_ManySequentialUnclosedParagraphs_ThenConvertCorrectly.verified.md
new file mode 100644
index 0000000..7caf2ad
--- /dev/null
+++ b/src/ReverseMarkdown.Test/ConverterTests.When_ManySequentialUnclosedParagraphs_ThenConvertCorrectly.verified.md
@@ -0,0 +1,15 @@
+Part1
+
+Part2
+
+Part3
+
+Part4
+
+Part5
+
+Part6
+
+Part7
+
+Part8
\ No newline at end of file
diff --git a/src/ReverseMarkdown.Test/ConverterTests.When_NestedParagraphs_FiveLevelsDeep_ThenConvertCorrectly.verified.md b/src/ReverseMarkdown.Test/ConverterTests.When_NestedParagraphs_FiveLevelsDeep_ThenConvertCorrectly.verified.md
new file mode 100644
index 0000000..286680b
--- /dev/null
+++ b/src/ReverseMarkdown.Test/ConverterTests.When_NestedParagraphs_FiveLevelsDeep_ThenConvertCorrectly.verified.md
@@ -0,0 +1,9 @@
+Level1
+
+Level2
+
+Level3
+
+Level4
+
+Level5
\ No newline at end of file
diff --git a/src/ReverseMarkdown.Test/ConverterTests.When_NestedParagraphs_TenLevelsDeep_ThenConvertCorrectly.verified.md b/src/ReverseMarkdown.Test/ConverterTests.When_NestedParagraphs_TenLevelsDeep_ThenConvertCorrectly.verified.md
new file mode 100644
index 0000000..a78352c
--- /dev/null
+++ b/src/ReverseMarkdown.Test/ConverterTests.When_NestedParagraphs_TenLevelsDeep_ThenConvertCorrectly.verified.md
@@ -0,0 +1,19 @@
+L1
+
+L2
+
+L3
+
+L4
+
+L5
+
+L6
+
+L7
+
+L8
+
+L9
+
+L10
\ No newline at end of file
diff --git a/src/ReverseMarkdown.Test/ConverterTests.When_NestedSpans_FiveLevelsDeep_ThenConvertCorrectly.verified.md b/src/ReverseMarkdown.Test/ConverterTests.When_NestedSpans_FiveLevelsDeep_ThenConvertCorrectly.verified.md
new file mode 100644
index 0000000..c6e7929
--- /dev/null
+++ b/src/ReverseMarkdown.Test/ConverterTests.When_NestedSpans_FiveLevelsDeep_ThenConvertCorrectly.verified.md
@@ -0,0 +1 @@
+Level1Level2Level3Level4Level5
\ No newline at end of file
diff --git a/src/ReverseMarkdown.Test/ConverterTests.When_UnclosedParagraphsWithSpansAndTextNodes_ThenConvertCorrectly.verified.md b/src/ReverseMarkdown.Test/ConverterTests.When_UnclosedParagraphsWithSpansAndTextNodes_ThenConvertCorrectly.verified.md
new file mode 100644
index 0000000..747b6ad
--- /dev/null
+++ b/src/ReverseMarkdown.Test/ConverterTests.When_UnclosedParagraphsWithSpansAndTextNodes_ThenConvertCorrectly.verified.md
@@ -0,0 +1,5 @@
+Intro
+ Filler text here.
+Section1
+Section2
+Section3
\ No newline at end of file
diff --git a/src/ReverseMarkdown.Test/ConverterTests.cs b/src/ReverseMarkdown.Test/ConverterTests.cs
index fc4c971..803cf41 100644
--- a/src/ReverseMarkdown.Test/ConverterTests.cs
+++ b/src/ReverseMarkdown.Test/ConverterTests.cs
@@ -1518,5 +1518,122 @@ public Task Bug400_MissingSpanSpaceWithItalics()
var html = " tags where HtmlAgilityPack creates nested structure
+ var html = " Level1 Level2 Level3 Level4 Level5What we thought: When we built Pages, we assumed that customers would use them like newsletters to share relevant, continually-updated information with field teams.
";
return CheckConversion(html);
}
+
+ [Fact]
+ public Task When_NestedParagraphs_FiveLevelsDeep_ThenConvertCorrectly()
+ {
+ // Tests moderately nested
pattern common in malformed HTML
+ var html = " Text1 Text2 Text3 Text4 tags as found in user-generated content
+ var html = " Part1 Part2 Part3 Part4 Part5 Part6 Part7 Part8";
+
+ var config = new Config
+ {
+ GithubFlavored = true,
+ UnknownTags = Config.UnknownTagsOption.Bypass
+ };
+
+ return CheckConversion(html, config);
+ }
+
+ [Fact]
+ public Task When_UnclosedParagraphsWithSpansAndTextNodes_ThenConvertCorrectly()
+ {
+ // Tests mixed content: properly closed tags, text nodes, and unclosed nested tags
+ var html = @" Intro Section1 Section2 Section3";
+
+ var config = new Config
+ {
+ GithubFlavored = true,
+ UnknownTags = Config.UnknownTagsOption.Bypass
+ };
+
+ return CheckConversion(html, config);
+ }
+
+ [Fact]
+ public Task When_EmptyNestedParagraphs_ThenConvertCorrectly()
+ {
+ // Tests deeply nested empty tags
+ var html = "
tags + var html = "
A
B
C
D
E
"; + + var config = new Config + { + UnknownTags = Config.UnknownTagsOption.Bypass + }; + + return CheckConversion(html, config); + } + + [Fact] + public Task When_NestedParagraphs_TenLevelsDeep_ThenConvertCorrectly() + { + // Tests deeper nesting (10 levels) to ensure performance remains linear after fix + var html = "L1
L2
L3
L4
L5
L6
L7
L8
L9
L10
"; + + var config = new Config + { + GithubFlavored = true, + UnknownTags = Config.UnknownTagsOption.Bypass + }; + + return CheckConversion(html, config); + } } } diff --git a/src/ReverseMarkdown/Converters/P.cs b/src/ReverseMarkdown/Converters/P.cs index b90bc99..d6cfe20 100644 --- a/src/ReverseMarkdown/Converters/P.cs +++ b/src/ReverseMarkdown/Converters/P.cs @@ -16,9 +16,13 @@ public override string Convert(HtmlNode node) var indentation = IndentationFor(node); var newlineAfter = NewlineAfter(node); - var content = Converter.Config.CleanupUnnecessarySpaces ? TreatChildren(node).Trim() : TreatChildren(node); + var content = TreatChildren(node); + if (Converter.Config.CleanupUnnecessarySpaces) + { + content = content.Trim(); + } - return $"{indentation}{TreatChildren(node)}{newlineAfter}"; + return $"{indentation}{content}{newlineAfter}"; } private static string IndentationFor(HtmlNode node)