Make WordPress Core

Opened 8 years ago

Closed 7 years ago

#37066 closed defect (bug) (fixed)

Two paragraphs will merge if the later starts with <br>

Reported by: rellect's profile rellect Owned by: azaozz's profile azaozz
Milestone: 4.8 Priority: normal
Severity: minor Version: 4.5.2
Component: TinyMCE Keywords: has-patch
Focuses: administration Cc:

Description

Reproduce:

Write a new post and do the following:

  • Write few words
  • Shift + Enter (to break line)
  • Write few words

So now you'll have something like that:

First line..
Second line..

Now, between the first and the second line, click enter. You will get this

First line..
 
Second line..

You will also see the space between the two lines in the editor.

Now save or switch to html mode

Expected:
Two paragraphs, and space between the two lines.

Current Result:
One paragraph, no space between the two lines.

Attachments (3)

37066.patch (514 bytes) - added by rellect 7 years ago.
37066.2.patch (810 bytes) - added by azaozz 7 years ago.
37066.3.patch (762 bytes) - added by rellect 7 years ago.
refreshing 37066.2 - correct a typo

Download all attachments as: .zip

Change History (15)

#1 follow-up: @iseulde
8 years ago

  • Keywords reporter-feedback added

Thanks for the ticket. Could you give more detailed steps to reproduce?

Now, between the first and the second line, click enter.

Where is the cursor? On the first or second line?

Could you give the expected and actual HTML result?

#2 @iseulde
8 years ago

  • Component changed from Editor to TinyMCE

#3 in reply to: ↑ 1 @rellect
7 years ago

Replying to iseulde:

Thanks for the ticket. Could you give more detailed steps to reproduce?

Now, between the first and the second line, click enter.

Where is the cursor? On the first or second line?

Could you give the expected and actual HTML result?

Cursor is at the end of the first line, then pressing enter.
The result looks like this
http://i.imgur.com/jdMHryg.png

Then switching to Text mode, it removes the spaces between the lines:
http://i.imgur.com/pBGCFP7.png

#4 @rellect
7 years ago

  • Summary changed from Paragraph after break line isn't maintained when saving to Enter after a break line being removed when switching to editor text mode

#5 @azaozz
7 years ago

  • Keywords needs-patch added; reporter-feedback removed
  • Milestone changed from Awaiting Review to Future Release
  • Severity changed from normal to minor

Confirmed. It's a (minor) bug in pre_wpautip(). Exact HTML:

<p>111</p>
<p><br>222</p>

Note that

<p>111<br></p>
<p>222</p>

works as expected.

#6 @Presskopp
7 years ago

May I correct a typo here:

pre_wpautop() instead of pre_wpautip()

@rellect
7 years ago

#7 follow-up: @rellect
7 years ago

  • Keywords has-patch added; needs-patch removed

The problem was with the following regex (in editor.js):

html = html.replace( /\s*<br ?\/?>\s*/gi, '\n' );

At this point of the code, all <p> tags are already replaced with \n
And the above regex strips all whitespaces before and after the <br>s, which also removes the break lines separating the paragraphs.

The fix I've included in the patch is:

html = html.replace( /\s*?(\n)?\s*<br ?\/?>\s*/gi, '$1\n' );

Specificaly \s*?(\n)?\s* - this will look for one new line within the whitespace, and append it back to the string.

Last edited 7 years ago by rellect (previous) (diff)

#8 @rellect
7 years ago

  • Summary changed from Enter after a break line being removed when switching to editor text mode to Two paragraphs will merge if the later starts with <br>

@azaozz
7 years ago

#9 in reply to: ↑ 7 ; follow-up: @azaozz
7 years ago

Replying to rellect:

37066.patch looks good. Thinking that 37066.2.patch would probably be slightly better as less backtracking in the regex (the logic is identical, just uses a callback instead of more complex regex).

@rellect
7 years ago

refreshing 37066.2 - correct a typo

#10 in reply to: ↑ 9 @rellect
7 years ago

Replying to azaozz:

Replying to rellect:

37066.patch looks good. Thinking that 37066.2.patch would probably be slightly better as less backtracking in the regex (the logic is identical, just uses a callback instead of more complex regex).

Agreed, looks better. Added 37066.3 to correct a typo in the relevant comment.

#11 @azaozz
7 years ago

  • Milestone changed from Future Release to 4.8

Just re-tested this. The patch works as expected.

#12 @azaozz
7 years ago

  • Owner set to azaozz
  • Resolution set to fixed
  • Status changed from new to closed

In 40787:

Editor: When stripping paragraph tags, and there is a <br> at the beginning or the end, merge them and keep the paragraph, not the <br>.

Props rellect.
Fixes #37066.

Note: See TracTickets for help on using tickets.