Make WordPress Core

Opened 10 years ago

Closed 9 years ago

#37066 closed defect (bug) (fixed)

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

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

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 10 years ago.
37066.2.patch​ (810 bytes ) - added by azaozz 10 years ago.
37066.3.patch​ (762 bytes ) - added by rellect 10 years ago.
refreshing 37066.2 - correct a typo

Download all attachments as: .zip

Change History (15)

#1 follow-up: @iseulde
10 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
10 years ago

  • Component Editor → TinyMCE

#3 in reply to: ↑ 1 @rellect
10 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
10 years ago

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

#5 @azaozz
10 years ago

  • Keywords needs-patch added; reporter-feedback removed
  • Milestone Awaiting Review → Future Release
  • Severity normal → 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
10 years ago

May I correct a typo here:

pre_wpautop() instead of pre_wpautip()

#7 follow-up: @rellect
10 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 10 years ago by rellect (previous) (diff)

#8 @rellect
10 years ago

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

#9 in reply to: ↑ 7 ; follow-up: @azaozz
10 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
10 years ago

refreshing 37066.2 - correct a typo

#10 in reply to: ↑ 9 @rellect
10 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
9 years ago

  • Milestone Future Release → 4.8

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

#12 @azaozz
9 years ago

  • Owner set to azaozz
  • Resolution → fixed
  • Status new → 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.