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

Use regex to extract iframe width and height #9770

Closed
wants to merge 3 commits 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
90 changes: 53 additions & 37 deletions packages/block-library/src/embed/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,48 +144,64 @@ export function getEmbedEdit( title, icon ) {
* @param {string} html The preview HTML that possibly contains an iframe with width and height set.
*/
maybeSetAspectRatioClassName( html ) {
const previewDom = document.createElement( 'div' );
previewDom.innerHTML = html;
const iframe = previewDom.querySelector( 'iframe' );
// Extract the first iframe's height and width without having to construct DOM elements.
const matches = html.toLowerCase().match( /<iframe[^>]+(height|width)=['"]?([0-9]+)["']?[^>]+(height|width)=['"]?([0-9]+)["']?.*?>/ );
let height;
let width;

if ( ! iframe ) {
if ( ! matches ) {
return;
}

if ( iframe.height && iframe.width ) {
const aspectRatio = ( iframe.width / iframe.height ).toFixed( 2 );
let aspectRatioClassName;

switch ( aspectRatio ) {
// Common video resolutions.
case '2.33':
aspectRatioClassName = 'wp-embed-aspect-21-9';
break;
case '2.00':
aspectRatioClassName = 'wp-embed-aspect-18-9';
break;
case '1.78':
aspectRatioClassName = 'wp-embed-aspect-16-9';
break;
case '1.33':
aspectRatioClassName = 'wp-embed-aspect-4-3';
break;
// Vertical video and instagram square video support.
case '1.00':
aspectRatioClassName = 'wp-embed-aspect-1-1';
break;
case '0.56':
aspectRatioClassName = 'wp-embed-aspect-9-16';
break;
case '0.50':
aspectRatioClassName = 'wp-embed-aspect-1-2';
break;
}
if ( 'height' === matches[ 1 ] ) {
height = parseInt( matches[ 2 ] );
}
if ( 'height' === matches[ 3 ] ) {
height = parseInt( matches[ 4 ] );
}
if ( 'width' === matches[ 1 ] ) {
width = parseInt( matches[ 2 ] );
}
if ( 'width' === matches[ 3 ] ) {
width = parseInt( matches[ 4 ] );
}

if ( aspectRatioClassName ) {
const className = classnames( this.props.attributes.className, 'wp-has-aspect-ratio', aspectRatioClassName );
this.props.setAttributes( { className } );
}
if ( ! height && ! width ) {
return;
}

const aspectRatio = ( width / height ).toFixed( 2 );
let aspectRatioClassName;

switch ( aspectRatio ) {
// Common video resolutions.
case '2.33':
aspectRatioClassName = 'wp-embed-aspect-21-9';
break;
case '2.00':
aspectRatioClassName = 'wp-embed-aspect-18-9';
break;
case '1.78':
aspectRatioClassName = 'wp-embed-aspect-16-9';
break;
case '1.33':
aspectRatioClassName = 'wp-embed-aspect-4-3';
break;
// Vertical video and instagram square video support.
case '1.00':
aspectRatioClassName = 'wp-embed-aspect-1-1';
break;
case '0.56':
aspectRatioClassName = 'wp-embed-aspect-9-16';
break;
case '0.50':
aspectRatioClassName = 'wp-embed-aspect-1-2';
break;
}

if ( aspectRatioClassName ) {
const className = classnames( this.props.attributes.className, 'wp-has-aspect-ratio', aspectRatioClassName );
this.props.setAttributes( { className } );
}
}

Expand Down