Skip to content

Commit

Permalink
content [nfc]: Factor out consumeImageNodes in block-content parse loops
Browse files Browse the repository at this point in the history
This hopefully makes the logic of these loops a bit more readable.
  • Loading branch information
gnprice committed Jan 10, 2025
1 parent 9cd591a commit c8d683e
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions lib/model/content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1579,8 +1579,14 @@ class _ZulipContentParser {
/// See [ParagraphNode].
List<BlockContentNode> parseImplicitParagraphBlockContentList(dom.NodeList nodes) {
final List<BlockContentNode> result = [];
final List<dom.Node> currentParagraph = [];

List<ImageNode> imageNodes = [];
void consumeImageNodes() {
result.add(ImageNodeList(imageNodes));
imageNodes = [];
}

final List<dom.Node> currentParagraph = [];
void consumeParagraph() {
final parsed = parseBlockInline(currentParagraph);
result.add(ParagraphNode(
Expand All @@ -1595,8 +1601,7 @@ class _ZulipContentParser {

if (_isPossibleInlineNode(node)) {
if (imageNodes.isNotEmpty) {
result.add(ImageNodeList(imageNodes));
imageNodes = [];
consumeImageNodes();
// In a context where paragraphs are implicit it should be impossible
// to have more paragraph content after image previews.
result.add(UnimplementedBlockContentNode(htmlNode: node));
Expand All @@ -1611,23 +1616,25 @@ class _ZulipContentParser {
imageNodes.add(block);
continue;
}
if (imageNodes.isNotEmpty) {
result.add(ImageNodeList(imageNodes));
imageNodes = [];
}
if (imageNodes.isNotEmpty) consumeImageNodes();
result.add(block);
}
if (currentParagraph.isNotEmpty) consumeParagraph();
if (imageNodes.isNotEmpty) result.add(ImageNodeList(imageNodes));

if (imageNodes.isNotEmpty) consumeImageNodes();
return result;
}

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

List<BlockContentNode> parseBlockContentList(dom.NodeList nodes) {
final List<BlockContentNode> result = [];

List<ImageNode> imageNodes = [];
void consumeImageNodes() {
result.add(ImageNodeList(imageNodes));
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.
Expand All @@ -1640,13 +1647,10 @@ class _ZulipContentParser {
imageNodes.add(block);
continue;
}
if (imageNodes.isNotEmpty) {
result.add(ImageNodeList(imageNodes));
imageNodes = [];
}
if (imageNodes.isNotEmpty) consumeImageNodes();
result.add(block);
}
if (imageNodes.isNotEmpty) result.add(ImageNodeList(imageNodes));
if (imageNodes.isNotEmpty) consumeImageNodes();
return result;
}

Expand Down

0 comments on commit c8d683e

Please sign in to comment.