Skip to content

Commit 99f5fa7

Browse files
committed
content: Handle code block elements without grandchildren.
Signed-off-by: Zixuan James Li <[email protected]>
1 parent eb1c818 commit 99f5fa7

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

lib/model/content.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -990,14 +990,14 @@ class _ZulipContentParser {
990990
if (child is! dom.Element) return null;
991991
if (child.localName != 'pre') return null;
992992

993-
if (child.nodes.length > 2) return null;
993+
if (child.nodes.length > 2 || child.nodes.isEmpty) return null;
994994
if (child.nodes.length == 2) {
995995
final first = child.nodes[0];
996996
if (first is! dom.Element
997997
|| first.localName != 'span'
998998
|| first.nodes.isNotEmpty) return null;
999999
}
1000-
final grandchild = child.nodes[child.nodes.length - 1];
1000+
final grandchild = child.nodes.last;
10011001
if (grandchild is! dom.Element) return null;
10021002
if (grandchild.localName != 'code') return null;
10031003

test/model/content_test.dart

+9
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,14 @@ class ContentExample {
344344
]),
345345
]);
346346

347+
static final codeBlockWithEmptyBody = ContentExample(
348+
'code block, with an empty body',
349+
'```',
350+
'<div class="codehilite"><pre></pre></div>', [
351+
blockUnimplemented(
352+
'<div class="codehilite"><pre></pre></div>'),
353+
]);
354+
347355
static final codeBlockWithHighlightedLines = ContentExample(
348356
'code block, with syntax highlighting and highlighted lines',
349357
'```\n::markdown hl_lines="2 4"\n# he\n## llo\n### world\n```',
@@ -1149,6 +1157,7 @@ void main() {
11491157
testParseExample(ContentExample.codeBlockPlain);
11501158
testParseExample(ContentExample.codeBlockHighlightedShort);
11511159
testParseExample(ContentExample.codeBlockHighlightedMultiline);
1160+
testParseExample(ContentExample.codeBlockWithEmptyBody);
11521161
testParseExample(ContentExample.codeBlockWithHighlightedLines);
11531162
testParseExample(ContentExample.codeBlockWithUnknownSpanType);
11541163
testParseExample(ContentExample.codeBlockFollowedByMultipleLineBreaks);

0 commit comments

Comments
 (0)