Skip to content

Commit

Permalink
content: Handle blank text nodes after code blocks
Browse files Browse the repository at this point in the history
Fixes: #355
  • Loading branch information
Khader-1 authored and chrisbobbe committed Apr 10, 2024
1 parent 8c0f9ae commit ba74abd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/model/content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1124,14 +1124,18 @@ class _ZulipContentParser {
return result;
}

static final _redundantLineBreaksRegexp = RegExp(r'^\n+$');

List<BlockContentNode> parseBlockContentList(dom.NodeList nodes) {
assert(_debugParserContext == _ParserContext.block);
final List<BlockContentNode> result = [];
List<ImageNode> imageNodes = [];
for (final node in nodes) {
// We get a bunch of newline Text nodes between paragraphs.
// A browser seems to ignore these; let's do the same.
if (node is dom.Text && (node.text == '\n')) continue;
if (node is dom.Text && _redundantLineBreaksRegexp.hasMatch(node.text)) {
continue;
}

final block = parseBlockContent(node);
if (block is ImageNode) {
Expand Down
12 changes: 12 additions & 0 deletions test/model/content_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,17 @@ class ContentExample {
'\n</code></pre></div>'),
]);

static const codeBlockFollowedByMultipleLineBreaks = ContentExample(
'blank text nodes after code blocks',
' code block.\n\nsome content',
// https://chat.zulip.org/#narrow/stream/7-test-here/near/1774823
'<div class="codehilite">'
'<pre><span></span><code>code block.\n</code></pre></div>\n\n'
'<p>some content</p>', [
CodeBlockNode([CodeBlockSpanNode(text: "code block.", type: CodeBlockSpanType.text)]),
ParagraphNode(links: null, nodes: [TextNode("some content")]),
]);

static final mathInline = ContentExample.inline(
'inline math',
r"$$ \lambda $$",
Expand Down Expand Up @@ -865,6 +876,7 @@ void main() {
testParseExample(ContentExample.codeBlockHighlightedMultiline);
testParseExample(ContentExample.codeBlockWithHighlightedLines);
testParseExample(ContentExample.codeBlockWithUnknownSpanType);
testParseExample(ContentExample.codeBlockFollowedByMultipleLineBreaks);

testParseExample(ContentExample.mathBlock);
testParseExample(ContentExample.mathBlockInQuote);
Expand Down

0 comments on commit ba74abd

Please sign in to comment.