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

Fix new lines when replacing <blockquote> and <br /> #474

Merged
merged 11 commits into from
Nov 30, 2022

Conversation

aldo-expensify
Copy link
Contributor

@aldo-expensify aldo-expensify commented Nov 22, 2022

This is part of a group of changes to preserving new lines when converting from message[0].html using ExpensiMark.htmlToText. This should improve:

Fixed Issues

$ Expensify/App#12894 (partially)
Expensify/App#11494 (partially)

Tests

Make sure unit test run (npm run test)

  1. Go to a chat
  2. Go offline
  3. Compose a message by:
    1. Add an emoji
    2. Add two or more empty lines
    3. Add an emoji
      image
  4. Send the message
  5. Find report action in Onyx and make sure that the line breaks appear as \n in message[0].text
    image
  6. Compose another message with the content:
before quotes
> quoted text
after quotes
  1. Find report action in Onyx and make sure that the message[0].text contains before quotes\nquoted text\nafter quotes. This is because <blockquote> are block elements and they should cause line breaks in the content.
    image

QA

  1. Go to a chat
  2. Go offline
  3. Compose a message by:
    1. Add an emoji
    2. Add two or more empty lines
    3. Add an emoji
      image
  4. Send the message
  5. Find report action in Onyx and make sure that the line breaks appear as \n in message[0].text
    image
  6. Compose another message with the content:
before quotes
> quoted text
after quotes
  1. Find report action in Onyx and make sure that the message[0].text contains before quotes\nquoted text\nafter quotes. This is because <blockquote> are block elements and they should cause line breaks in the content.
    image

@aldo-expensify aldo-expensify self-assigned this Nov 22, 2022
@aldo-expensify aldo-expensify requested a review from a team as a code owner November 22, 2022 03:05
@melvin-bot melvin-bot bot requested review from luacmartins and removed request for a team November 22, 2022 03:06
@aldo-expensify
Copy link
Contributor Author

cc @mollfpr since you added this code that transform html into text using regexes. Just tagging you in case you notice that what I'm doing here will cause a bug.

@@ -287,7 +287,7 @@ export default class ExpensiMark {
this.htmlToTextRules = [
{
name: 'breakline',
regex: /((<br[^>]*>)+)/gi,
regex: /<br[^>]*>/gi,
Copy link
Contributor

Choose a reason for hiding this comment

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

I just roll back to the issue and the PR. Back then the issue is about the last message text in the LHN when sending a blockquote. So the solution I propose is to replace the blockquote closing tag with a space. Other regex rules in the htmlToText rule from the E/App that I move to expensi-mark.

I believe this will affect the text in LHN where it should be replacing adjoin break lines with a space.

Copy link
Contributor

Choose a reason for hiding this comment

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

@mollfpr I tested blockquotes but couldn't spot any new bugs. Did you find any issues with these changes?

I did notice that entering two block quotes with empty new lines seems to remove the space in the LHN message, but that's also happening on staging so it's not introduced here.

web.mov

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe this will affect the text in LHN where it should be replacing adjoin break lines with a space.

I had this thought after I stopped working hehe, but I just checked and it doesn't affect in web:

image

I'm going to check Android/IOS

Copy link
Contributor Author

@aldo-expensify aldo-expensify Nov 22, 2022

Choose a reason for hiding this comment

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

Also, I noticed that the server:

  • for the report action, it replaces each <br> for a ' ' (whitespace), but
    image
  • for the report, the lastMessageText has collapsed consecutive <br> into a single ' '
    image

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess we should separate the function to convert the message HTML to text between LHN and chat.

Copy link
Contributor Author

@aldo-expensify aldo-expensify Nov 22, 2022

Choose a reason for hiding this comment

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

Yeah, something like that... I was looking at the backend, and convert the html to lastMessageText using a function that applies the same regexes that htmlToText apply, including /((<br[^>]*>)+)/.

I was thinking that it doesn't make much sense that 🥶.<br /><br /><br />🥶 gets converted to 🥶..🥶 (two spaces) just collapsing the <br /> into a single space... why the result is not 🥶.🥶 (single space) if our intention is really to collapse multiple white spaces. I'm thinking that htmlToText should maybe be changed to:

  • Step 1: replace each <br /> by single spaces
  • Step 2: collapse all consecutive spaces into a single space

I still have to look at what the back end is doing to produce the text of the message in the report action from the html. Is there a reason why we don't collapse the spaces when deriving text from html?

btw, I replaced the whitespaces in 🥶 🥶 by . because they are getting collapsed here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The html of a report action's message gets converted to text here, and it replaces each <br /> with a single whitespace (no collapsing).

The lastMessageText of the report gets derived from html here, collapsing multiple <br /> into a single whitespace.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess we should separate the function to convert the message HTML to text between LHN and chat.

@mollfpr so.. I think the best way to go here is:

  • htmlToText won't collapse multiple <br /> into a single whitespace because that is only needed for the lastMessageText which is used in the LHN (what this PR is doing)
  • The function ReportUtils.formatReportLastMessageText will have the responsibility of collapsing consecutive whitespaces into a single one. We already use this function to process what we put in lastMessageText

Copy link
Contributor

Choose a reason for hiding this comment

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

@aldo-expensify sounds good to me!

I found an issue when testing this PR, but it's not related to the PR changes. The last message on LHN flinched without whitespace when sending a text with a blockquote.

Screen.Recording.2022-11-27.at.22.16.53.mov

Copy link
Contributor

Choose a reason for hiding this comment

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

luacmartins
luacmartins previously approved these changes Nov 22, 2022
Copy link
Contributor

@luacmartins luacmartins left a comment

Choose a reason for hiding this comment

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

LGTM just waiting on @mollfpr confirmation on any new issues with blockquotes

@aldo-expensify aldo-expensify changed the title Replace each <br> for single whitespace [HOLD - this may not be ready] Replace each <br> for single whitespace Nov 22, 2022
@mollfpr
Copy link
Contributor

mollfpr commented Nov 22, 2022

Okay, I couldn't find any issue in the current LHN with this change but some things broke the current LHN.

As you can see the alternateText creates spaces between the text because we replaced every breakline with a space. The alternateText value itself is from the lastMessageText key from the report object where the text is using htmlToText. But apparently, the current LHN doesn't show that lastMessageText is in LHN.
Screen Shot 2022-11-23 at 01 06 22


I found another issue in LHN, but it's not related to these changes but related to the htmlToText function. The optimistic last message in LHN should be Confusion This.

Screen.Recording.2022-11-23.at.01.14.19.mov

With the current LHN behavior to show the last message, this PR will not affect the LHN but I think we should fix the above issue to show the last message correctly.

@aldo-expensify aldo-expensify changed the title [HOLD - this may not be ready] Replace each <br> for single whitespace Fix new lines when replacing <blockquote> and <br /> Nov 29, 2022
@aldo-expensify aldo-expensify requested review from mollfpr and luacmartins and removed request for mollfpr November 29, 2022 20:25
@aldo-expensify
Copy link
Contributor Author

aldo-expensify commented Nov 29, 2022

@mollfpr @luacmartins can you review from 0 again 🙏 ? I changed this PR to:

  • <blockquote> are replaced by \n if there is content before
  • </blockquote> are replaced by \n if there is content after
  • <br /> are replaced by \n

@mollfpr
Copy link
Contributor

mollfpr commented Nov 30, 2022

Will test in a couple of hours.

@mollfpr
Copy link
Contributor

mollfpr commented Nov 30, 2022

Sorry, I have a question I'm a little bit confused. Is this PR will fix Expensify/App#12894 and Expensify/App#11494?

Anyway, the changes look much better now 👍

@aldo-expensify
Copy link
Contributor Author

aldo-expensify commented Nov 30, 2022

Sorry, I have a question I'm a little bit confused. Is this PR will fix Expensify/App#12894 and Expensify/App#11494?

Partially, there is also a PR for the API and also for the App.

This PR:

  • Updates ExpensiMark.htmlToText to:
    • replace each <br /> by \n
    • replace <blockquote> by \n when there is content before it
    • replace </blockquote> by \n when there is content after it
  • Updated tests.

Other PRs:

Copy link
Contributor

@luacmartins luacmartins left a comment

Choose a reason for hiding this comment

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

LGTM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants