Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Adjust wp-editor-meta-box E2E test
  • Loading branch information
adamziel committed Jul 8, 2021
commit ae4b7ac16131944d2a29af47371949258123311c
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ describe( 'WP Editor Meta Boxes', () => {
( textarea ) => textarea.value
);

expect( content ).toMatchInlineSnapshot(
`"<p>Typing in a metabox</p>"`
);
expect( content ).toBe( 'Typing in a metabox' );
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this change expected, I feel this test has been updated back and forth several times already in the last couple releases, do we understand why this change is needed?

Copy link
Contributor Author

@adamziel adamziel Jul 9, 2021

Choose a reason for hiding this comment

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

It turns out that TinyMCE's WordPress plugin does the following:
https://github.com/WordPress/wordpress-develop/blob/2382765afa36e10bf3c74420024ad4e85763a47c/src/js/_enqueues/vendor/tinymce/plugins/wordpress/plugin.js#L15
https://github.com/WordPress/wordpress-develop/blob/2382765afa36e10bf3c74420024ad4e85763a47c/src/js/_enqueues/vendor/tinymce/plugins/wordpress/plugin.js#L132-L133
https://github.com/WordPress/wordpress-develop/blob/2382765afa36e10bf3c74420024ad4e85763a47c/src/js/_enqueues/vendor/tinymce/plugins/wordpress/plugin.js#L593-L609

hasWpautop = ( wp && wp.editor && wp.editor.autop && editor.getParam( 'wpautop', true ) ),
editor.on( 'BeforeSetContent', function( event ) {
	// ...
	if ( hasWpautop ) {
	    event.content = wp.editor.autop( event.content );
	} else {
	    // Prevent creation of paragraphs out of multiple HTML comments
	    event.content = event.content.replace( /-->\s+<!--/g, '--><!--' );
	}
});

// ...

editor.on( 'SaveContent', function( event ) {
	// ...
	// Keep empty paragraphs :(
	event.content = event.content.replace( /<p>(?:<br ?\/?>|\u00a0|\uFEFF| )*<\/p>/g, '<p>&nbsp;</p>' );

	if ( hasWpautop ) {
		event.content = wp.editor.removep( event.content );
	} else {
		// Restore formatting of block boundaries.
		event.content = event.content.replace( /-->\s*<!-- wp:/g, '-->\n\n<!-- wp:' );
	}
});

So:

  • if Object.assign( window.wp.editor, window.wp.oldEditor ); is in place, then hasWpauto is set to true by editor.getParam( 'wpautop', true ); .
    • When I switch from HTML to visual mode of TinyMCE, event.content is wrapped in a <p>
    • When I switch back from the visual mode to the HTML mode, SaveContent is dispatched and it calls wp.editor.removep which removes the <p> tags.
  • If Object.assign thing is not in place, then hasWpauto is set to false.
    • When I switch from HTML to visual mode of TinyMCE, event.content is not wrapped in a <p> but TinyMCE wraps it anyway.
    • When I switch back from the visual mode to the HTML mode, SaveContent is dispatched and it does not call wp.editor.removep so the <p> tags are not automatically removed at that stage.

Now, for the purposes of this test, we explicitly set wpautop to true in the test plugin here:

This means that we do not want to see the the <p> tags in the editor. I added some documentation to that test to explain how to proceed in case it flips back again at some point @youknowriad .

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the detailed explanation :)

Copy link
Member

Choose a reason for hiding this comment

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

Good one @adamziel, this test should be finally reliable 👍🏻

} );
} );