Skip to content

Commit 72ad468

Browse files
committed
content: Handle code block elements without grandchildren.
A code block containing no grandchildren (i.e. an empty <pre> block within the <div class="codehilite"> element) for some very dated messages can crash the parser if not handled. The affect messages in public https://chat.zulip.org history ranged from message ID 5020 to 29855, all in 2016. Signed-off-by: Zixuan James Li <[email protected]>
1 parent b9dd217 commit 72ad468

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

lib/model/content.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ 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

test/model/content_test.dart

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

347+
// Current servers no longer produce this, but it can be found in ancient
348+
// messages. For example:
349+
// https://chat.zulip.org/#narrow/stream/2-general/topic/Error.20in.20dev.20server/near/18765
350+
static final codeBlockWithEmptyBody = ContentExample(
351+
'code block, with an empty body',
352+
'```',
353+
'<div class="codehilite"><pre></pre></div>', [
354+
blockUnimplemented(
355+
'<div class="codehilite"><pre></pre></div>'),
356+
]);
357+
347358
static final codeBlockWithHighlightedLines = ContentExample(
348359
'code block, with syntax highlighting and highlighted lines',
349360
'```\n::markdown hl_lines="2 4"\n# he\n## llo\n### world\n```',
@@ -1149,6 +1160,7 @@ void main() {
11491160
testParseExample(ContentExample.codeBlockPlain);
11501161
testParseExample(ContentExample.codeBlockHighlightedShort);
11511162
testParseExample(ContentExample.codeBlockHighlightedMultiline);
1163+
testParseExample(ContentExample.codeBlockWithEmptyBody);
11521164
testParseExample(ContentExample.codeBlockWithHighlightedLines);
11531165
testParseExample(ContentExample.codeBlockWithUnknownSpanType);
11541166
testParseExample(ContentExample.codeBlockFollowedByMultipleLineBreaks);

0 commit comments

Comments
 (0)