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

[HOLD for payment 2023-03-01] [$1000] Editing header bold text in chat increases vertical gap above the text #14668

Closed
6 tasks
kavimuru opened this issue Jan 30, 2023 · 58 comments
Assignees
Labels
Awaiting Payment Auto-added when associated PR is deployed to production Bug Something is broken. Auto assigns a BugZero manager. Daily KSv2 External Added to denote the issue can be worked on by a contributor

Comments

@kavimuru
Copy link

kavimuru commented Jan 30, 2023

If you haven’t already, check out our contributing guidelines for onboarding and email contributors@expensify.com to request to join our Slack channel!


Action Performed:

  1. Open the app
  2. Open any chat
  3. Send bold text using #
  4. Edit the text

Expected Result:

Vertical gap above the text should not change after edit

Actual Result:

Vertical gap above the text is increased after editing

Workaround:

unknown

Platforms:

Which of our officially supported platforms is this issue occurring on?

  • Android / native
  • Android / Chrome
  • iOS / native
  • iOS / Safari
  • MacOS / Chrome / Safari
  • MacOS / Desktop

Version Number: 1.2.61-0
Reproducible in staging?: Y
Reproducible in production?: Y
If this was caught during regression testing, add the test name, ID and link from TestRail:
Email or phone of affected tester (no customers):
Logs: https://stackoverflow.com/c/expensify/questions/4856
Notes/Photos/Videos:

vertical.gap.above.header.text.mp4
Recording.1404.mp4

Expensify/Expensify Issue URL:
Issue reported by: @dhanashree-sawant
Slack conversation: https://expensify.slack.com/archives/C049HHMV9SM/p1675067869948869

View all open jobs on GitHub

Upwork Automation - Do Not Edit
  • Upwork Job URL: https://www.upwork.com/jobs/~011432c27e94a6da98
  • Upwork Job ID: 1623338262676652032
  • Last Price Increase: 2023-02-08
@kavimuru kavimuru added Daily KSv2 Bug Something is broken. Auto assigns a BugZero manager. labels Jan 30, 2023
@melvin-bot melvin-bot bot locked and limited conversation to collaborators Jan 30, 2023
@dylanexpensify
Copy link
Contributor

@shawnborton can you confirm this is indeed a bug, and I'll get it moved over?

@melvin-bot melvin-bot bot added the Overdue label Feb 6, 2023
@dylanexpensify
Copy link
Contributor

Shawn is OOO, but back this week!

@melvin-bot melvin-bot bot removed the Overdue label Feb 6, 2023
@shawnborton
Copy link
Contributor

Hm yeah, this seems like a bug! It's a bit strange that there was even a blank line inserted above the #/headline in the first place.

@dylanexpensify
Copy link
Contributor

Nice, thanks Shawn!

@dylanexpensify
Copy link
Contributor

Replicated

@dylanexpensify dylanexpensify added the External Added to denote the issue can be worked on by a contributor label Feb 8, 2023
@melvin-bot melvin-bot bot unlocked this conversation Feb 8, 2023
@melvin-bot melvin-bot bot changed the title Editing header bold text in chat increases vertical gap above the text [$1000] Editing header bold text in chat increases vertical gap above the text Feb 8, 2023
@MelvinBot
Copy link

Job added to Upwork: https://www.upwork.com/jobs/~011432c27e94a6da98

@MelvinBot
Copy link

Current assignee @dylanexpensify is eligible for the External assigner, not assigning anyone new.

@MelvinBot
Copy link

Triggered auto assignment to Contributor-plus team member for initial proposal review - @eVoloshchak (External)

@melvin-bot melvin-bot bot added the Help Wanted Apply this label when an issue is open to proposals by contributors label Feb 8, 2023
@MelvinBot
Copy link

Triggered auto assignment to @danieldoglas (External), see https://stackoverflow.com/c/expensify/questions/7972 for more details.

@tienifr

This comment was marked as outdated.

@eh2077
Copy link
Contributor

eh2077 commented Feb 8, 2023

Proposal

Please re-state the problem that we are trying to solve in this issue.

If we edit a sent header bold text, # heading , and save it in chat, there'll be an extra leading line break above the header.

What is the root cause of that problem?

When translating html h1 tag to markdown, we simply wrap it with a leading and a trailing line break. For example, html

<h1>heading</h1>

will be translated into markdown string

\n# heading\n

If we change the markdown text to

\n# heading test\n

and save it, then markdown text

\n# heading test\n

will be translated to html

<br /><h1>heading test<h1>

by applying markdown-to-html rules

https://github.com/Expensify/expensify-common/blob/668be7533215fc8d683d658da839361664320965/lib/ExpensiMark.js#L164-L173

The leading line break will be translated to the extra <br /> tag.

To have a closer look at the root cause, let's try another example.

Send a heading message

# heading

In the editing mode, change the markdown string

\n# heading\n

to (remove the leading and trailing line break)

# heading test

Save it and the leading line break is still there.

This is because when we save a changed message, we don't simply call ExpensiMark#replace() once to get the html to save in backend. Instead, we do markdown-to-html, html-to-markdown and markdown-to-html conversion to get the final html to save. So the markdown input of the last markdown-to-html conversion is

\n# heading test\n

instead of

# heading test

See https://github.com/Expensify/App/blob/main/src/libs/actions/Report.js#L829-L832

So the root cause is the invalid regex rule to translate html <h1> header to markdown.

What changes do you think we should make in order to solve the problem?

To fix this issue, we should improve the regex rule to translate h1 tag to markdown. Instead of simply wrapping it with a leading and trailing line break, we should only add line break if there's non-empty text before or after the <h1> heading. We can improve the regex rule from

https://github.com/Expensify/expensify-common/blob/668be7533215fc8d683d658da839361664320965/lib/ExpensiMark.js#L232-L233

to the following

regex: /([\s\S]*?)\s*<(h1)(?:"[^"]*"|'[^']*'|[^'">])*>(.*?)<\/\2>(?![^<]*(<\/pre>|<\/code>))\s*([\s\S]*)/gi,
replacement: (match, g1, g2, g3, g4, g5) => `${g1}${g1 && '\n'}# ${g3}${g5 && '\n'}${g5}`,

So the html

<h1>heading</h1>

will be translated to markdown text

# heading

and the edited markdown text

# heading test

will be translated into html

<h1>heading test</h1>

What alternative solutions did you explore? (Optional)

Call trim method for markdown text

@hackykitty
Copy link

hackykitty commented Feb 9, 2023

I reviewed @eh2077 and @tienifr's proposals. Great.
But string 'edited' is laid horizontally in other line, and adding bold string 'edited' is laid vertically.
To fix this, we need to follow the style:

flex-direction: row;

align-items: center;

Applying style eliminates the need to remove two duplicate \n's before converting the markdown to HTML.
All problems can be solved by adding only 2 styles.
thanks.

@daraksha-dk
Copy link
Contributor

Hey @hackykitty, you might want to read our contributing guidelines to help you better understand the flow.

(After that you might want to join expensify's slack channel!)

@tienifr
Copy link
Contributor

tienifr commented Feb 9, 2023

Proposal

Please re-state the problem that we are trying to solve in this issue.

Vertical gap above the text is increased after editing a h1 tag

What is the root cause of that problem?

In htmlToMarkdown, we're adding new line to both before and after the # , which should not be the case, we should only add new lines if there's non-new-line content before and after the heading.

Why non-new-line? because if before the heading there's already a new line, the # will be on the next line already and we should not add a (redundant) new line in this case. This complies with how HTML renders the h1 tag.

What changes do you think we should make in order to solve the problem?

In here, we should use a regex that satisfies the requirement we should only add new lines if there's non-new-line content before the heading.

diff --git a/lib/ExpensiMark.js b/lib/ExpensiMark.js
index bac4b81..5622980 100644
--- a/lib/ExpensiMark.js
+++ b/lib/ExpensiMark.js
@@ -229,8 +229,8 @@ export default class ExpensiMark {
             },
             {
                 name: 'heading1',
-                regex: /\s*<(h1)(?:"[^"]*"|'[^']*'|[^'">])*>(.*?)<\/\1>(?![^<]*(<\/pre>|<\/code>))\s*/gi,
-                replacement: '\n# $2\n',
+                regex: /([^\r\n]*?)\s*<(h1)(?:"[^"]*"|'[^']*'|[^'">])*>(.*?)<\/\2>(?![^<]*(<\/pre>|<\/code>))\s*([^\r\n]*)/gi,
+                replacement: (match, g1, g2, g3, g4, g5) => `${g1}${g1 && '\n'}# ${g3}${g5 && '\n'}${g5}`,
             },
             {
                 name: 'listItem',

After this fix, another issue will occur. Let's consider this HTML
<h1>heading 1</h1><h1>heading 2</h1>. In here if both h1 blocks matches the regex, it will add 2 new lines in between the blocks, one after heading 1 and one before heading 2. This is because in the match, heading 1 has non-new-line content after it and heading 2 has non-new-line content before it. This is incorrect understanding since after heading 1 is matched, it will add 1 new line after it and heading 2 no longer has non-new-line content before it.

In order to fix this we need to introduce another property into the rule, which can be sequential: true. If the rule is sequential, the regex replacement will be applied once at a time. Example of this in htmlToMarkdown:

if (rule.sequential) {
                while (rule.regex.test(generatedMarkdown)) {
                    generatedMarkdown = generatedMarkdown.replace(rule.regex, rule.replacement);
                }
            } else {
                generatedMarkdown = generatedMarkdown.replace(rule.regex, rule.replacement);
            }

Of course the regex above will need to have the global flag /g removed for this to work.

We'll need to evaluate other block elements (like blockquote, pre) to make sure it's not having this bug. If we're doing to those elements the same thing we do to h1 here then it will have the same issues.

There's another bug relating to the heading tag I found during debugging, reported here.

What alternative solutions did you explore? (Optional)

@eh2077
Copy link
Contributor

eh2077 commented Feb 9, 2023

Proposal

Updated

To fix the following test case mentioned by @tienifr in his latest proposal #14668 (comment)

<h1>heading 1</h1><h1>heading 2</h1>

I'd like to update the solution section of my previous proposal

What changes do you think we should make in order to solve the problem?

I'd like to propose a new solution to improve the regex rule which translates html <h1> header to markdown. The new solution is to replace

https://github.com/Expensify/expensify-common/blob/668be7533215fc8d683d658da839361664320965/lib/ExpensiMark.js#L233

with

replacement: '[block]# $2[block]',

By replacing

\n# $2\n

with

[block]# $2[block]

we can reuse this function ExpensiMark#replaceBlockWithNewLine to handle line break properly. See

https://github.com/Expensify/expensify-common/blob/668be7533215fc8d683d658da839361664320965/lib/ExpensiMark.js#L521

https://github.com/Expensify/expensify-common/blob/668be7533215fc8d683d658da839361664320965/lib/ExpensiMark.js#L455-L467

/**
* replace [block] with '\n' if :
* 1. We have text before the [block].
* 2. The text does not end with a new line.
* 3. The text does not have quote mark '>' .
* 4. [block] not the last [block] in the string.
*
* @param {String} htmlString
* @returns {String}
*/
replaceBlockWithNewLine(htmlString) {

@melvin-bot melvin-bot bot added the Overdue label Feb 10, 2023
@danieldoglas
Copy link
Contributor

@eVoloshchak what's your saying on these proposals?

@melvin-bot melvin-bot bot removed the Overdue label Feb 11, 2023
@MelvinBot
Copy link

MelvinBot commented Feb 22, 2023

BugZero Checklist: The PR fixing this issue has been merged! The following checklist (instructions) will need to be completed before the issue can be closed:

@eh2077
Copy link
Contributor

eh2077 commented Feb 23, 2023

Regression Test Proposal

  1. Open a chat and send this message

    # heading
    
  2. Edit the sent message and verify it's displayed in one line in editing mode

  3. Edit and change the sent message to

    # heading test
    

    and save it

  4. Verify the edited header bold text is still displayed in one line

Do we agree 👍 or 👎

@MelvinBot
Copy link

📣 @eh2077! 📣

Hey, it seems we don’t have your contributor details yet! You'll only have to do this once, and this is how we'll hire you on Upwork.
Please follow these steps:

  1. Get the email address used to login to your Expensify account. If you don't already have an Expensify account, create one here. If you have multiple accounts (e.g. one for testing), please use your main account email.
  2. Get the link to your Upwork profile. It's necessary because we only pay via Upwork. You can access it by logging in, and then clicking on your name. It'll look like this. If you don't already have an account, sign up for one here.
  3. Copy the format below and paste it in a comment on this issue. Replace the placeholder text with your actual details.

Screen Shot 2022-11-16 at 4 42 54 PM

Format:

Contributor details
Your Expensify account email: <REPLACE EMAIL HERE>
Upwork Profile Link: <REPLACE LINK HERE>

@MelvinBot
Copy link

✅ Contributor details stored successfully. Thank you for contributing to Expensify!

@melvin-bot melvin-bot bot added Daily KSv2 and removed Weekly KSv2 labels Mar 1, 2023
@danieldoglas
Copy link
Contributor

@eVoloshchak can you fill this #14668 (comment)? Thanks!

@dylanexpensify
Copy link
Contributor

I like those steps, @eh2077 with some slight tweaking on the verbiage. @danieldoglas I'm curious on your thoughts if we should create a test for this? Or if you think this would get caught without a test? I'm tempted to say the latter.

@danieldoglas
Copy link
Contributor

@dylanexpensify I think we should include this as part of the tests related to the markdown edits.

@eVoloshchak
Copy link
Contributor

@eVoloshchak can you fill this #14668 (comment)? Thanks!

Sure, doing it today

@eVoloshchak
Copy link
Contributor

  • The PR that introduced the bug has been identified. Link to the PR: Add h1 markdown expensify-common#468

  • The offending PR has been commented on, pointing out the bug it caused and why, so the author and reviewers can learn from the mistake. Link to comment: the PR description mentioned the design doc, which I can't access, I can't verify if it's a regression or was by design at the time

  • A discussion in #expensify-bugs has been started about whether any other steps should be taken (e.g. updating the PR review checklist) in order to catch this type of bug sooner. Link to discussion: automated regression tests will catch this

@melvin-bot melvin-bot bot added the Overdue label Mar 6, 2023
@dylanexpensify
Copy link
Contributor

Whipping up RT and payments today

@melvin-bot melvin-bot bot removed the Overdue label Mar 6, 2023
@dylanexpensify
Copy link
Contributor

Hmm @danieldoglas, under the existing markdown test Edit a message with Markdown I feel this scenario is covered for. Thoughts?

@danieldoglas
Copy link
Contributor

@dylanexpensify I think we're being very generalistic in Edit a message with Markdown. the test suggested here will test exactly for the situation where editing a header, so I think it makes sense to update it.

@dylanexpensify
Copy link
Contributor

Bump @danieldoglas! 🙇‍♂️

@eh2077
Copy link
Contributor

eh2077 commented Mar 7, 2023

@dylanexpensify I think we're being very generalistic in Edit a message with Markdown. the test suggested here will test exactly for the situation where editing a header, so I think it makes sense to update it.

Hi @dylanexpensify , is this comment from @danieldoglas you're waiting for?

@dylanexpensify
Copy link
Contributor

Wow, yes @eh2077 haha, I didn't refresh issue before posting 🤦

@dylanexpensify
Copy link
Contributor

RT linked above! Working on payments now!

@dylanexpensify
Copy link
Contributor

@eh2077 sent offer!
@eVoloshchak sent invite to apply to job!
@dhanashree-sawant sent invite to apply to job as reporter!

@eh2077
Copy link
Contributor

eh2077 commented Mar 7, 2023

@dylanexpensify Thanks and accepted the offer!

@dylanexpensify
Copy link
Contributor

@eh2077 payment sent!
@eVoloshchak offer sent!

@dhanashree-sawant
Copy link

@dylanexpensify Thanks and accepted the offer!

@dylanexpensify
Copy link
Contributor

@dhanashree-sawant please apply when you can!

@dhanashree-sawant
Copy link

Hi @dylanexpensify, I have accepted your invite

@dylanexpensify
Copy link
Contributor

oh woops! JEEZ I really have to start reloading the page before replying 🤦

@dylanexpensify
Copy link
Contributor

Offer sent @dhanashree-sawant !

@dylanexpensify
Copy link
Contributor

alrightyyyy we're all settled up!! Thanks everyone!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting Payment Auto-added when associated PR is deployed to production Bug Something is broken. Auto assigns a BugZero manager. Daily KSv2 External Added to denote the issue can be worked on by a contributor
Projects
None yet
Development

No branches or pull requests