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

Parser: Round float attributes to check validity #3494

Closed
wants to merge 1 commit 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
26 changes: 26 additions & 0 deletions blocks/api/test/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,32 @@ describe( 'validation', () => {

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

it( 'returns true if equal float values', () => {
const isEqual = isEqualTagAttributePairs(
[
[ 'height', '1.012345678910' ],
],
[
[ 'height', '1.01234567891011' ],
]
);

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

it( 'returns false for different float values', () => {
const isEqual = isEqualTagAttributePairs(
[
[ 'height', '1.0123' ],
],
[
[ 'height', '1.0124' ],
]
);

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

describe( 'isEqualTokensOfType', () => {
Expand Down
18 changes: 14 additions & 4 deletions blocks/api/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,15 +267,25 @@ export function isEqualTagAttributePairs( a, b ) {

const aValue = aAttributes[ name ];
const bValue = bAttributes[ name ];
let attributesAreEqual = true;

const isEqualAttributes = isEqualAttributesOfName[ name ];
if ( isEqualAttributes ) {
// Defer custom attribute equality handling
if ( ! isEqualAttributes( aValue, bValue ) ) {
return false;
}
} else if ( aValue !== bValue ) {
attributesAreEqual = isEqualAttributes( aValue, bValue );
} else if (
! isNaN( parseFloat( aValue ) ) &&
! isNaN( parseFloat( bValue ) )
) {
// Float values should be compared using the rounded values,
// PHP and JavaScript serializing have different precisions
attributesAreEqual = Number( aValue ).toFixed( 10 ) === Number( bValue ).toFixed( 10 );
} else {
// Otherwise strict inequality should bail
attributesAreEqual = aValue === bValue;
}

if ( ! attributesAreEqual ) {
return false;
}
}
Expand Down