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

autop: Correctly preserve attributed multi-line paragraphs in removep #15128

Merged
merged 1 commit into from
Apr 24, 2019

Conversation

aduth
Copy link
Member

@aduth aduth commented Apr 23, 2019

Related: #9056 (comment)

This pull request seeks to resolve an issue where removep will wrongly remove a paragraph tag when it has attributes, if the paragraph spans multiple lines. As described in #9056 (comment), this occurs because the default behavior of . in JavaScript regular expressions will not match newline characters.

Before:

removep( '<p style="text-align: center">\nHello World\n</p>' );
// '<p style="text-align: center">\nHello World'

After:

removep( '<p style="text-align: center">\nHello World\n</p>' );
// '<p style="text-align: center">\nHello World</p>'

Testing Instructions:

Ensure unit tests pass.

Verify that content is not mangled when multi-line paragraph in a classic block (i.e. in non-block-demarcated content).

  1. Navigate to Posts > Add New
  2. Switch to Code Editor
  3. In the text field, paste:
<p style="text-align: center">
Hello World
</p>
  1. Blur the text field
  2. Observe that text is not mangled
    • The trailing newline preceding the closing tag will be omitted. This had existed in the source implementation, and could likely be preserved, though it seemed incidental to the primary bug.

@aduth aduth added [Type] Bug An existing feature does not function as intended Backwards Compatibility Issues or PRs that impact backwards compatability [Package] Autop /packages/autop labels Apr 23, 2019
@aduth aduth requested a review from youknowriad as a code owner April 23, 2019 17:52
@@ -502,3 +503,12 @@ test( 'that autop correctly adds a start and end tag when followed by a div', ()

expect( autop( content ).trim() ).toBe( expected );
} );

describe( 'removep', () => {
test( 'preserves paragraphs with attributes', () => {
Copy link
Member

Choose a reason for hiding this comment

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

It's a bit scary that this is the only removep test for such a huge pile of regular expressions.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's a bit scary that this is the only removep test for such a huge pile of regular expressions.

It is, yes. For historical context, the module originated as a direct port of the PHP implementation of autop, including its tests. There is no removep in PHP, so there are no tests. There was a previous removep implementation in JavaScript from which this implementation was adapted, but it had no tests to port over.

I can plan to create a task issue to follow-up on adding more tests here.

Copy link
Member Author

Choose a reason for hiding this comment

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

I can plan to create a task issue to follow-up on adding more tests here.

See #15152

@aduth aduth merged commit d5bdd53 into master Apr 24, 2019
@aduth aduth deleted the fix/autop-removep-paragraph branch April 24, 2019 15:08
@youknowriad youknowriad added this to the 5.6 (Gutenberg) milestone May 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backwards Compatibility Issues or PRs that impact backwards compatability [Package] Autop /packages/autop [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants