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

Add function which checks if content of post is empty #2536

Closed
wants to merge 1 commit into from
Closed
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
16 changes: 15 additions & 1 deletion editor/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import { serialize, getBlockType } from '@wordpress/blocks';
import { __ } from '@wordpress/i18n';
import { addQueryArgs } from '@wordpress/url';

const REGEXP_EMPTY_CONTENT = /^<!--(.)*>(\s)*<p[^>]*>(&nbsp|\s|<br[^>]*>)*<\/p>(\s)(.)*-->$/mg;
Copy link
Member

@aduth aduth Aug 28, 2017

Choose a reason for hiding this comment

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

.* are prone to greediness, which results in issues like this:

https://regex101.com/r/64lOJ1/1

See: http://www.regular-expressions.info/repeat.html

const CONTENT_LENGTH_ASSUME_SET = 63;

/**
* Returns the current editing mode.
*
Expand Down Expand Up @@ -243,12 +246,23 @@ export function isEditedPostPublishable( state ) {
*/
export function isEditedPostSaveable( state ) {
return (
!! getEditedPostContent( state ) ||
( ( !! getEditedPostContent( state ) ) && ( ! isEmptyContent( state ) ) ) ||
!! getEditedPostTitle( state ) ||
!! getEditedPostExcerpt( state )
);
}

/**
* Check if the content is empty (ignoring empty tags)
*
* @param {Object} state Global application state
* @return {Boolean} Whether it's considered empty
*/
export function isEmptyContent( state ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add some unit tests?

const content = getEditedPostContent( state );
return ( ! content || ( content.length < CONTENT_LENGTH_ASSUME_SET && REGEXP_EMPTY_CONTENT.test( content ) ) );
}

/**
* Return true if the post being edited is being scheduled. Preferring the
* unsaved status values.
Expand Down