Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle the case when a code block has no grandchildren #919

Merged
merged 2 commits into from
Sep 13, 2024

Conversation

PIG208
Copy link
Member

@PIG208 PIG208 commented Aug 28, 2024

For some very old messages (e.g.: this), a code block might contain no grandchildren, or equivalently, the <pre></pre> block within the <div class="codehilite"></div> element is empty.

While these messages might be present on CZO only due to some ancient server bugs, the client should not crash when it does encounter them.

Crashes:
- RangeError (length): Invalid value: Valid value range is empty: -1
  Oldest message: 5020; newest message: 29855

@PIG208 PIG208 marked this pull request as ready for review August 29, 2024 06:25
@PIG208
Copy link
Member Author

PIG208 commented Aug 29, 2024

#917 is stacked on top of this.

@PIG208 PIG208 added the maintainer review PR ready for review by Zulip maintainers label Aug 29, 2024
@PIG208 PIG208 requested a review from chrisbobbe August 29, 2024 17:46
Copy link
Collaborator

@chrisbobbe chrisbobbe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Some small comments below.

@@ -344,6 +344,14 @@ class ContentExample {
]),
]);

static final codeBlockWithEmptyBody = ContentExample(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's have a comment that makes it clear we don't expect this in messages from current servers. The link to the ancient message you put in the PR description is helpful; we could include that here too.

final grandchild = child.nodes[child.nodes.length - 1];
final grandchild = child.nodes.last;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a pure refactor, or does this change behavior somehow? I noticed there's a line final first = child.nodes[0]; above this; should that also be changed to say child.nodes.first?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This refactoring is purely for readability. Let's get an nfc commit for both.

@chrisbobbe
Copy link
Collaborator

Thanks! I happened to check in on this PR and noticed that the current revision (ending in fb5ea67) responds to my review, so I'm comfortable marking this as ready for Greg's review.

@chrisbobbe chrisbobbe added integration review Added by maintainers when PR may be ready for integration and removed maintainer review PR ready for review by Zulip maintainers labels Sep 3, 2024
@PIG208

This comment was marked as resolved.

@chrisbobbe

This comment was marked as resolved.

Copy link
Member

@gnprice gnprice left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @PIG208 for the fix, and @chrisbobbe for the reviews!

Generally this looks good. Two small comments below.

Comment on lines 988 to 989
if (divElement.nodes.length != 1) return null;
final child = divElement.nodes[0];
final child = divElement.nodes.first;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we use a fancy getter here, it should be single — that way we correctly express the expectation that this is the only node (not the first of potentially several nodes).

if (child is! dom.Element) return null;
if (child.localName != 'pre') return null;

if (child.nodes.length > 2) return null;
if (child.nodes.length > 2 || child.nodes.isEmpty) return null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: include the key "why" information in the commit message, rather than rely on the PR description. We aim for the Git history to be able to serve as our primary record of what we did.

In particular key points include

  • this was a crash bug;
  • the affected messages in public chat.zulip.org history ranged from message ID 5020 to 29855, all in 2016.

Details that can be read off from the commit's added test case don't need to be repeated in the commit message, though.

@PIG208 PIG208 force-pushed the content branch 2 times, most recently from 72ad468 to 6cf84df Compare September 7, 2024 05:10
@PIG208
Copy link
Member Author

PIG208 commented Sep 7, 2024

Thanks for the review. Updated the PR!

@PIG208 PIG208 requested a review from gnprice September 7, 2024 05:11
This improves readability, especially in the .last case.

Signed-off-by: Zixuan James Li <[email protected]>
A code block containing no grandchildren (i.e. a
<div class="codehilite"> element containing an empty <pre> block)
appears in some very old messages.  The parser was crashing on them.

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]>
@gnprice
Copy link
Member

gnprice commented Sep 13, 2024

Thanks! Looks good; merging, with a tweak to clarify the commit message a bit:

         A code block containing no grandchildren (i.e. a
         <div class="codehilite"> element containing an empty <pre> block)
    -    for some very dated messages can crash the parser if not handled.
    +    appears in some very old messages.  The parser was crashing on them.

@gnprice gnprice merged commit 5d36977 into zulip:main Sep 13, 2024
1 check passed
@PIG208 PIG208 deleted the content branch September 13, 2024 14:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
integration review Added by maintainers when PR may be ready for integration
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants