Skip to content

Commit

Permalink
Blocks: Short-circuit validation for identical markup (#22506)
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth authored May 21, 2020
1 parent db55195 commit ec9ced4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/blocks/src/api/test/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,15 @@ describe( 'validation', () => {
} );

describe( 'isEquivalentHTML()', () => {
it( 'should return true for identical markup', () => {
const isEquivalent = isEquivalentHTML(
'<div>Hello <span class="b">World!</span></div>',
'<div>Hello <span class="b">World!</span></div>'
);

expect( isEquivalent ).toBe( true );
} );

it( 'should return false for effectively inequivalent html', () => {
const isEquivalent = isEquivalentHTML(
'<div>Hello <span class="b">World!</span></div>',
Expand Down
5 changes: 5 additions & 0 deletions packages/blocks/src/api/validation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,11 @@ export function isClosedByToken( currentToken, nextToken ) {
* @return {boolean} Whether HTML strings are equivalent.
*/
export function isEquivalentHTML( actual, expected, logger = createLogger() ) {
// Short-circuit if markup is identical.
if ( actual === expected ) {
return true;
}

// Tokenize input content and reserialized save content
const [ actualTokens, expectedTokens ] = [
actual,
Expand Down

0 comments on commit ec9ced4

Please sign in to comment.