Skip to content

Commit ba74abd

Browse files
Khader-1chrisbobbe
authored andcommitted
content: Handle blank text nodes after code blocks
Fixes: #355
1 parent 8c0f9ae commit ba74abd

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lib/model/content.dart

+5-1
Original file line numberDiff line numberDiff line change
@@ -1124,14 +1124,18 @@ class _ZulipContentParser {
11241124
return result;
11251125
}
11261126

1127+
static final _redundantLineBreaksRegexp = RegExp(r'^\n+$');
1128+
11271129
List<BlockContentNode> parseBlockContentList(dom.NodeList nodes) {
11281130
assert(_debugParserContext == _ParserContext.block);
11291131
final List<BlockContentNode> result = [];
11301132
List<ImageNode> imageNodes = [];
11311133
for (final node in nodes) {
11321134
// We get a bunch of newline Text nodes between paragraphs.
11331135
// A browser seems to ignore these; let's do the same.
1134-
if (node is dom.Text && (node.text == '\n')) continue;
1136+
if (node is dom.Text && _redundantLineBreaksRegexp.hasMatch(node.text)) {
1137+
continue;
1138+
}
11351139

11361140
final block = parseBlockContent(node);
11371141
if (block is ImageNode) {

test/model/content_test.dart

+12
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,17 @@ 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+
// https://chat.zulip.org/#narrow/stream/7-test-here/near/1774823
308+
'<div class="codehilite">'
309+
'<pre><span></span><code>code block.\n</code></pre></div>\n\n'
310+
'<p>some content</p>', [
311+
CodeBlockNode([CodeBlockSpanNode(text: "code block.", type: CodeBlockSpanType.text)]),
312+
ParagraphNode(links: null, nodes: [TextNode("some content")]),
313+
]);
314+
304315
static final mathInline = ContentExample.inline(
305316
'inline math',
306317
r"$$ \lambda $$",
@@ -865,6 +876,7 @@ void main() {
865876
testParseExample(ContentExample.codeBlockHighlightedMultiline);
866877
testParseExample(ContentExample.codeBlockWithHighlightedLines);
867878
testParseExample(ContentExample.codeBlockWithUnknownSpanType);
879+
testParseExample(ContentExample.codeBlockFollowedByMultipleLineBreaks);
868880

869881
testParseExample(ContentExample.mathBlock);
870882
testParseExample(ContentExample.mathBlockInQuote);

0 commit comments

Comments
 (0)