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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/autop/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Master

### Bug Fix

- `removep` will correctly preserve multi-line paragraph tags where attributes are present.

## 2.1.0 (2019-03-06)

### Bug Fix
Expand Down
2 changes: 1 addition & 1 deletion packages/autop/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ export function removep( html ) {
html = html.replace( new RegExp( '\\s*<((?:' + blocklist1 + ')(?: [^>]*)?)>', 'g' ), '\n<$1>' );

// Mark </p> if it has any attributes.
html = html.replace( /(<p [^>]+>.*?)<\/p>/g, '$1</p#>' );
html = html.replace( /(<p [^>]+>[\s\S]*?)<\/p>/g, '$1</p#>' );

// Preserve the first <p> inside a <div>.
html = html.replace( /<div( [^>]*)?>\s*<p>/gi, '<div$1>\n\n' );
Expand Down
10 changes: 10 additions & 0 deletions packages/autop/src/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import {
autop,
removep,
} from '../';

test( 'empty string', () => {
Expand Down Expand Up @@ -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

const content = '<p style="text-align: center">\nHello World\n</p>';
const expected = '<p style="text-align: center">\nHello World</p>';

expect( removep( content ) ).toBe( expected );
} );
} );