Skip to content

Commit c4892ed

Browse files
committed
content: Handle blank text nodes after code blocks
Fixes: #355
1 parent 81feda5 commit c4892ed

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

lib/model/content.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,8 @@ class _ZulipContentParser {
694694
}();
695695
static final _emojiCodeFromClassNameRegexp = RegExp(r"emoji-([^ ]+)");
696696

697+
static final _redundantLineBreaksRegexp = RegExp(r'^\n+$');
698+
697699
InlineContentNode parseInlineContent(dom.Node node) {
698700
assert(_debugParserContext == _ParserContext.inline);
699701
final debugHtmlNode = kDebugMode ? node : null;
@@ -1107,7 +1109,7 @@ class _ZulipContentParser {
11071109
for (final node in nodes) {
11081110
// We get a bunch of newline Text nodes between paragraphs.
11091111
// A browser seems to ignore these; let's do the same.
1110-
if (node is dom.Text && (node.text == '\n')) continue;
1112+
if (node is dom.Text && _redundantLineBreaksRegexp.hasMatch(node.text)) continue;
11111113

11121114
final block = parseBlockContent(node);
11131115
if (block is ImageNode) {

test/model/content_test.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,16 @@ class ContentExample {
301301
'\n</code></pre></div>'),
302302
]);
303303

304+
static const codeBlockFollowedByMultipleLineBreaks = ContentExample(
305+
'blank text nodes after code blocks',
306+
' code block.\n\nsome content',
307+
'<div class="codehilite">'
308+
'<pre><span></span><code>code block.\n</code></pre></div>\n\n'
309+
'<p>some content</p>', [
310+
CodeBlockNode([CodeBlockSpanNode(text: "code block.", type: CodeBlockSpanType.text)]),
311+
ParagraphNode(links: null, nodes: [TextNode("some content")]),
312+
]);
313+
304314
static final mathInline = ContentExample.inline(
305315
'inline math',
306316
r"$$ \lambda $$",
@@ -843,6 +853,7 @@ void main() {
843853
testParseExample(ContentExample.codeBlockHighlightedMultiline);
844854
testParseExample(ContentExample.codeBlockWithHighlightedLines);
845855
testParseExample(ContentExample.codeBlockWithUnknownSpanType);
856+
testParseExample(ContentExample.codeBlockFollowedByMultipleLineBreaks);
846857

847858
testParseExample(ContentExample.mathBlock);
848859
testParseExample(ContentExample.mathBlockInQuote);

0 commit comments

Comments
 (0)