Skip to content

Commit 35d83f2

Browse files
gnpricePIG208
authored andcommitted
content [nfc]: Factor out consumeImageNodes in block-content parse loops
This hopefully makes the logic of these loops a bit more readable.
1 parent 612c91e commit 35d83f2

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

lib/model/content.dart

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,8 +1579,14 @@ class _ZulipContentParser {
15791579
/// See [ParagraphNode].
15801580
List<BlockContentNode> parseImplicitParagraphBlockContentList(dom.NodeList nodes) {
15811581
final List<BlockContentNode> result = [];
1582-
final List<dom.Node> currentParagraph = [];
1582+
15831583
List<ImageNode> imageNodes = [];
1584+
void consumeImageNodes() {
1585+
result.add(ImageNodeList(imageNodes));
1586+
imageNodes = [];
1587+
}
1588+
1589+
final List<dom.Node> currentParagraph = [];
15841590
void consumeParagraph() {
15851591
final parsed = parseBlockInline(currentParagraph);
15861592
result.add(ParagraphNode(
@@ -1595,8 +1601,7 @@ class _ZulipContentParser {
15951601

15961602
if (_isPossibleInlineNode(node)) {
15971603
if (imageNodes.isNotEmpty) {
1598-
result.add(ImageNodeList(imageNodes));
1599-
imageNodes = [];
1604+
consumeImageNodes();
16001605
// In a context where paragraphs are implicit it should be impossible
16011606
// to have more paragraph content after image previews.
16021607
result.add(UnimplementedBlockContentNode(htmlNode: node));
@@ -1611,23 +1616,25 @@ class _ZulipContentParser {
16111616
imageNodes.add(block);
16121617
continue;
16131618
}
1614-
if (imageNodes.isNotEmpty) {
1615-
result.add(ImageNodeList(imageNodes));
1616-
imageNodes = [];
1617-
}
1619+
if (imageNodes.isNotEmpty) consumeImageNodes();
16181620
result.add(block);
16191621
}
16201622
if (currentParagraph.isNotEmpty) consumeParagraph();
1621-
if (imageNodes.isNotEmpty) result.add(ImageNodeList(imageNodes));
1622-
1623+
if (imageNodes.isNotEmpty) consumeImageNodes();
16231624
return result;
16241625
}
16251626

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

16281629
List<BlockContentNode> parseBlockContentList(dom.NodeList nodes) {
16291630
final List<BlockContentNode> result = [];
1631+
16301632
List<ImageNode> imageNodes = [];
1633+
void consumeImageNodes() {
1634+
result.add(ImageNodeList(imageNodes));
1635+
imageNodes = [];
1636+
}
1637+
16311638
for (final node in nodes) {
16321639
// We get a bunch of newline Text nodes between paragraphs.
16331640
// A browser seems to ignore these; let's do the same.
@@ -1640,13 +1647,10 @@ class _ZulipContentParser {
16401647
imageNodes.add(block);
16411648
continue;
16421649
}
1643-
if (imageNodes.isNotEmpty) {
1644-
result.add(ImageNodeList(imageNodes));
1645-
imageNodes = [];
1646-
}
1650+
if (imageNodes.isNotEmpty) consumeImageNodes();
16471651
result.add(block);
16481652
}
1649-
if (imageNodes.isNotEmpty) result.add(ImageNodeList(imageNodes));
1653+
if (imageNodes.isNotEmpty) consumeImageNodes();
16501654
return result;
16511655
}
16521656

0 commit comments

Comments
 (0)