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

Test/optional ignore style validation #39417

Closed
wants to merge 3 commits into from
Closed
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
1 change: 1 addition & 0 deletions packages/block-library/src/spacer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ export const settings = {
edit,
save,
deprecated,
validationRules: { ignoreStyles: true },
};
3 changes: 3 additions & 0 deletions packages/blocks/src/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,9 @@ export function registerBlockType( blockNameOrMetadata, settings ) {
supports: {},
styles: [],
variations: [],
validationRules: settings.validationRules
? settings.validationRules
: {},
save: () => null,
...serverSideBlockDefinitions?.[ name ],
...settings,
Expand Down
4 changes: 2 additions & 2 deletions packages/blocks/src/api/test/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,13 @@ describe( 'validation', () => {
expect( isEqual ).toBe( true );
} );

it( 'returns false if not same style', () => {
it( 'returns true even if the style is different', () => {
const isEqual = isEqualAttributesOfName.style(
'background-image: url( "https://wordpress.org/img.png" ); color: red;',
"color: red; font-size: 13px; background-image: url('https://wordpress.org/img.png');"
);

expect( isEqual ).toBe( false );
expect( isEqual ).toBe( true );
} );
} );

Expand Down
43 changes: 35 additions & 8 deletions packages/blocks/src/api/validation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import { Tokenizer } from 'simple-html-tokenizer';
import { identity, xor, fromPairs, isEqual, includes, stubTrue } from 'lodash';
import { identity, xor, fromPairs, includes, isEqual, stubTrue } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -17,6 +17,7 @@ import { getSaveContent } from '../serializer';
import {
getFreeformContentHandlerName,
getUnregisteredTypeHandlerName,
getBlockType,
} from '../registration';
import { normalizeBlockType } from '../utils';

Expand Down Expand Up @@ -407,8 +408,10 @@ export const isEqualAttributesOfName = {
...[ actual, expected ].map( getTextPiecesSplitOnWhitespace )
).length;
},
style: ( actual, expected ) => {
return isEqual( ...[ actual, expected ].map( getStyleProperties ) );
style: ( actual, expected, validationRules ) => {
return validationRules.ignoreStyles === true
? true
: isEqual( ...[ actual, expected ].map( getStyleProperties ) );
},
// For each boolean attribute, mere presence of attribute in both is enough
// to assume equivalence.
Expand All @@ -430,6 +433,7 @@ export const isEqualAttributesOfName = {
export function isEqualTagAttributePairs(
actual,
expected,
validationRules = {},
logger = createLogger()
) {
// Attributes is tokenized as tuples. Their lengths should match. This also
Expand Down Expand Up @@ -468,7 +472,13 @@ export function isEqualTagAttributePairs(

if ( isEqualAttributes ) {
// Defer custom attribute equality handling.
if ( ! isEqualAttributes( actualValue, expectedValue ) ) {
if (
! isEqualAttributes(
actualValue,
expectedValue,
validationRules
)
) {
logger.warning(
'Expected attribute `%s` of value `%s`, saw `%s`.',
name,
Expand Down Expand Up @@ -498,7 +508,12 @@ export function isEqualTagAttributePairs(
* @type {Object}
*/
export const isEqualTokensOfType = {
StartTag: ( actual, expected, logger = createLogger() ) => {
StartTag: (
actual,
expected,
validationRules = {},
logger = createLogger()
) => {
if (
actual.tagName !== expected.tagName &&
// Optimization: Use short-circuit evaluation to defer case-
Expand All @@ -516,6 +531,7 @@ export const isEqualTokensOfType = {

return isEqualTagAttributePairs(
...[ actual, expected ].map( getMeaningfulAttributePairs ),
validationRules,
logger
);
},
Expand Down Expand Up @@ -602,7 +618,12 @@ export function isClosedByToken( currentToken, nextToken ) {
*
* @return {boolean} Whether HTML strings are equivalent.
*/
export function isEquivalentHTML( actual, expected, logger = createLogger() ) {
export function isEquivalentHTML(
actual,
expected,
validationRules = {},
logger = createLogger()
) {
// Short-circuit if markup is identical.
if ( actual === expected ) {
return true;
Expand Down Expand Up @@ -649,7 +670,12 @@ export function isEquivalentHTML( actual, expected, logger = createLogger() ) {
const isEqualTokens = isEqualTokensOfType[ actualToken.type ];
if (
isEqualTokens &&
! isEqualTokens( actualToken, expectedToken, logger )
! isEqualTokens(
actualToken,
expectedToken,
validationRules,
logger
)
) {
return false;
}
Expand Down Expand Up @@ -728,10 +754,11 @@ export function validateBlock( block, blockTypeOrName ) {

return [ false, logger.getItems() ];
}

const { validationRules } = getBlockType( block.name );
const isValid = isEquivalentHTML(
block.originalContent,
generatedBlockContent,
validationRules,
logger
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"attributes": {
"url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==",
"id": 35,
"isRepeated": false,
"hasParallax": false,
"dimRatio": 50,
"backgroundType": "image",
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.