@iframe-resizer/parent
Version:
Keep iframes sized to their content.
78 lines (59 loc) • 1.88 kB
JavaScript
/*!
* @preserve
*
* @module iframe-resizer/parent 5.5.1 (cjs) - 2025-08-01
*
* @license GPL-3.0 for non-commercial use only.
* For commercial use, you must purchase a license from
* https://iframe-resizer.com/pricing
*
* @description Keep same and cross domain iFrames sized to their content
*
* @author David J. Bradshaw <info@iframe-resizer.com>
*
* @see {@link https://iframe-resizer.com}
*
* @copyright (c) 2013 - 2025, David J. Bradshaw. All rights reserved.
*/
;
const connectResizer = require('@iframe-resizer/core');
const LABEL = 'iframeResizer';
const id = `[${LABEL}] `;
function createIframeResize() {
function setup(element) {
switch (true) {
case !element:
throw new TypeError(`${id}iframe is not defined`)
case !element.tagName:
throw new TypeError(`${id}Not a valid DOM element`)
case element.tagName.toUpperCase() !== 'IFRAME':
throw new TypeError(
`${id}Expected <IFRAME> tag, found <${element.tagName}>`,
)
default:
connectWithOptions(element);
iFrames.push(element);
}
}
let connectWithOptions;
let iFrames;
return function (options, target) {
if (typeof window === 'undefined') return [] // don't run for server side render
connectWithOptions = connectResizer(options);
iFrames = []; // Only return iFrames passed in on this call
switch (typeof target) {
case 'undefined':
case 'string':
document.querySelectorAll(target || 'iframe').forEach(setup);
break
case 'object':
setup(target);
break
default:
throw new TypeError(`${id}Unexpected data type (${typeof target})`)
}
return Object.freeze(iFrames)
}
}
const esm = createIframeResize();
module.exports = esm;