diff --git a/src/banner/banner-manager.mjs b/src/banner/banner-manager.mjs index 712ba2c..8632167 100644 --- a/src/banner/banner-manager.mjs +++ b/src/banner/banner-manager.mjs @@ -32,8 +32,8 @@ export class BannerManager { this.STATE = State; } - addExternalBanner(element) { - element = getHtmlElement(element); + addExternalBanner(element, refWindow = window) { + element = getHtmlElement(element, refWindow); element.setAttribute('data-amp-attached', ''); @@ -49,12 +49,12 @@ export class BannerManager { return banner; } - addManagedBanner(element, position, resources = {}, options = {}) { + addManagedBanner(element, position, resources = {}, options = {}, refWindow = window) { if (null === this.#bannerRenderer) { throw new Error(`Unable to add managed banner, renderer is not provided.`); } - element = getHtmlElement(element); + element = getHtmlElement(element, refWindow); element.setAttribute('data-amp-attached', ''); @@ -75,8 +75,8 @@ export class BannerManager { } addEmbedBanner(element, iframe, position, options) { - element = getHtmlElement(element); - iframe = getHtmlElement(iframe); + element = getHtmlElement(element, window); + iframe = getHtmlElement(iframe, window); element.setAttribute('data-amp-attached', ''); diff --git a/src/client/standard/client.mjs b/src/client/standard/client.mjs index 9ba5e71..72dd407 100644 --- a/src/client/standard/client.mjs +++ b/src/client/standard/client.mjs @@ -159,8 +159,8 @@ export class Client { return this.#gateway; } - createBanner(element, position, resources = {}, options = {}, mode = 'managed') { - element = getHtmlElement(element); + createBanner(element, position, resources = {}, options = {}, mode = 'managed', refWindow = window) { + element = getHtmlElement(element, refWindow); if ('embed' === mode) { const iframe = this.#createIframe(element, position, resources, options); @@ -172,14 +172,14 @@ export class Client { return banner; } - return this.#bannerManager.addManagedBanner(element, position, resources, options); + return this.#bannerManager.addManagedBanner(element, position, resources, options, refWindow); } closeBanner(bannerId) { this.#closingManager.closeBanner(bannerId); } - attachBanners(snippet = document) { + attachBanners(snippet = document, refWindow = window) { const elements = snippet.querySelectorAll('[data-amp-banner]:not([data-amp-attached])'); for (let element of elements) { @@ -194,13 +194,13 @@ export class Client { let banner; if ('ampBannerExternal' in element.dataset) { - banner = this.#bannerManager.addExternalBanner(element); + banner = this.#bannerManager.addExternalBanner(element, refWindow); } else { const resources = AttributesParser.parseResources(element); const options = AttributesParser.parseOptions(element); const mode = element.dataset.ampMode || 'managed'; - banner = this.createBanner(element, position, resources, options, mode); + banner = this.createBanner(element, position, resources, options, mode, refWindow); } this.#eventBus.dispatch(this.EVENTS.ON_BANNER_ATTACHED, { banner }); diff --git a/src/utils/dom-helpers.mjs b/src/utils/dom-helpers.mjs index b7eeff4..aee23cc 100644 --- a/src/utils/dom-helpers.mjs +++ b/src/utils/dom-helpers.mjs @@ -1,5 +1,11 @@ -export function getHtmlElement(el) { - if (el instanceof HTMLElement) { +/** + * + * @param {string|HTMLElement} el + * @param {Window} refWindow + * @returns {HTMLElement} + */ +export function getHtmlElement(el, refWindow) { + if (el instanceof refWindow.HTMLElement) { return el; } @@ -15,7 +21,7 @@ export function getHtmlElement(el) { htmlEl = document.querySelector(el); } - if (!(htmlEl instanceof HTMLElement)) { + if (!(htmlEl instanceof refWindow.HTMLElement)) { throw new TypeError('Selector ' + el + ' is invalid.'); }