|
| 1 | +/** |
| 2 | + * Utility functions for iframe attribute handling. |
| 3 | + */ |
| 4 | + |
| 5 | +/** |
| 6 | + * Map HTML attribute names to React prop names. |
| 7 | + * |
| 8 | + * @param {string} attributeName - The HTML attribute name. |
| 9 | + * @return {string} The React prop name. |
| 10 | + */ |
| 11 | +export function mapHtmlAttributeToReact( attributeName ) { |
| 12 | + const attributeMap = { |
| 13 | + allowfullscreen: 'allowFullScreen', |
| 14 | + allowpaymentrequest: 'allowPaymentRequest', |
| 15 | + referrerpolicy: 'referrerPolicy', |
| 16 | + }; |
| 17 | + |
| 18 | + return attributeMap[ attributeName.toLowerCase() ] || attributeName; |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * Check if an attribute is a boolean HTML attribute. |
| 23 | + * |
| 24 | + * @param {string} attributeName - The name of the attribute to check. |
| 25 | + * @return {boolean} True if the attribute is boolean, false otherwise. |
| 26 | + */ |
| 27 | +export function isBooleanAttribute( attributeName ) { |
| 28 | + const booleanAttrs = [ 'allowfullscreen', 'allowpaymentrequest' ]; |
| 29 | + |
| 30 | + return booleanAttrs.includes( attributeName.toLowerCase() ); |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Convert iframe attributes array to props object for React. |
| 35 | + * Handles boolean attributes and React prop name mapping correctly. |
| 36 | + * |
| 37 | + * @param {Array} attributes - Array of {key, value} objects. |
| 38 | + * @return {Object} Props object for React component. |
| 39 | + */ |
| 40 | +export function convertAttributesToProps( attributes ) { |
| 41 | + return Object.fromEntries( |
| 42 | + ( attributes || [] ).map( ( attr ) => { |
| 43 | + // Map HTML attribute name to React prop name |
| 44 | + const propName = mapHtmlAttributeToReact( attr.key ); |
| 45 | + |
| 46 | + // Convert 'true' string to boolean for boolean attributes |
| 47 | + const value = |
| 48 | + isBooleanAttribute( attr.key ) && attr.value === 'true' |
| 49 | + ? true |
| 50 | + : attr.value; |
| 51 | + |
| 52 | + return [ propName, value ]; |
| 53 | + } ) |
| 54 | + ); |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Check if an iframe attribute should be excluded. |
| 59 | + * |
| 60 | + * @param {string} attributeName - The name of the attribute to check. |
| 61 | + * @return {boolean} True if the attribute should be excluded, false otherwise. |
| 62 | + */ |
| 63 | +export function isExcludedIframeAttribute( attributeName ) { |
| 64 | + const excludedAttrs = [ |
| 65 | + 'src', // Managed separately |
| 66 | + 'loading', // Managed by lazyload option |
| 67 | + 'title', // Managed separately |
| 68 | + 'width', // Managed by block dimension supports |
| 69 | + 'height', // Managed by block dimension supports |
| 70 | + 'style', // Requires object format in React, not string |
| 71 | + 'frameborder', // Deprecated HTML attribute |
| 72 | + 'marginwidth', // Deprecated HTML attribute |
| 73 | + 'marginheight', // Deprecated HTML attribute |
| 74 | + 'scrolling', // Deprecated HTML attribute |
| 75 | + 'align', // Deprecated HTML attribute |
| 76 | + 'longdesc', // Deprecated HTML attribute |
| 77 | + 'name', // Can cause conflicts |
| 78 | + ]; |
| 79 | + |
| 80 | + return excludedAttrs.includes( attributeName.toLowerCase() ); |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * Parse iframe HTML code and extract src URL, title, and attributes. |
| 85 | + * |
| 86 | + * @param {string} value - The value that could be a URL or iframe HTML code. |
| 87 | + * @return {Object|null} Object with url, title, and attributes array, or null if not an iframe. |
| 88 | + */ |
| 89 | +export function parseIframeCode( value ) { |
| 90 | + // Check if the value contains iframe tag |
| 91 | + const iframeRegex = /<iframe[^>]*>/i; |
| 92 | + const match = value.match( iframeRegex ); |
| 93 | + |
| 94 | + if ( ! match ) { |
| 95 | + return null; |
| 96 | + } |
| 97 | + |
| 98 | + // Extract only the opening tag (ignore any content inside iframe) |
| 99 | + const iframeTag = match[ 0 ]; |
| 100 | + |
| 101 | + // Create a temporary DOM element to parse the HTML |
| 102 | + // Use a self-closing iframe to avoid parsing issues with content |
| 103 | + const tempDiv = document.createElement( 'div' ); |
| 104 | + tempDiv.insertAdjacentHTML( 'beforeend', iframeTag + '</iframe>' ); |
| 105 | + const iframeElement = tempDiv.querySelector( 'iframe' ); |
| 106 | + |
| 107 | + if ( ! iframeElement ) { |
| 108 | + return null; |
| 109 | + } |
| 110 | + |
| 111 | + // Extract src attribute |
| 112 | + const src = iframeElement.getAttribute( 'src' ) || ''; |
| 113 | + |
| 114 | + // Extract title attribute |
| 115 | + const title = iframeElement.getAttribute( 'title' ) || ''; |
| 116 | + |
| 117 | + // Extract all other attributes (excluding managed and deprecated ones) |
| 118 | + const attributes = []; |
| 119 | + |
| 120 | + for ( const attr of iframeElement.attributes ) { |
| 121 | + if ( ! isExcludedIframeAttribute( attr.name ) ) { |
| 122 | + // For boolean attributes, store 'true' as value if present |
| 123 | + const value = isBooleanAttribute( attr.name ) ? 'true' : attr.value; |
| 124 | + |
| 125 | + attributes.push( { |
| 126 | + key: attr.name, |
| 127 | + value, |
| 128 | + } ); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return { |
| 133 | + url: src, |
| 134 | + title, |
| 135 | + attributes, |
| 136 | + }; |
| 137 | +} |
0 commit comments