MCPcopy
hub / github.com/ampproject/amphtml / installFriendlyIframeEmbed

Function installFriendlyIframeEmbed

src/friendly-iframe-embed.js:136–250  ·  view source on GitHub ↗
(
  iframe,
  container,
  spec,
  opt_preinstallCallback // TODO(#22733): remove "window" argument.
)

Source from the content-addressed store, hash-verified

134 * @return {!Promise<!FriendlyIframeEmbed>}
135 */
136export function installFriendlyIframeEmbed(
137 iframe,
138 container,
139 spec,
140 opt_preinstallCallback // TODO(#22733): remove "window" argument.
141) {
142 /** @const {!Window} */
143 const win = getTopWindow(getWin(iframe));
144 /** @const {!./service/extensions-impl.Extensions} */
145 const extensionsService = Services.extensionsFor(win);
146 /** @const {!./service/ampdoc-impl.AmpDocService} */
147 const ampdocService = Services.ampdocServiceFor(win);
148
149 setStyle(iframe, 'visibility', 'hidden');
150 iframe.setAttribute('referrerpolicy', 'unsafe-url');
151 iframe.setAttribute('marginheight', '0');
152 iframe.setAttribute('marginwidth', '0');
153
154 const extensions = spec.extensions || [];
155
156 // Pre-load extensions.
157 preloadFriendlyIframeEmbedExtensions(win, extensions);
158
159 const html = spec.skipHtmlMerge ? spec.html : mergeHtml(spec);
160 // Receive the signal when iframe is ready: it's document is formed.
161 iframe.onload = () => {
162 // Chrome does not reflect the iframe readystate.
163 iframe.readyState = 'complete';
164 };
165 const registerViolationListener = () => {
166 iframe.contentWindow.addEventListener(
167 'securitypolicyviolation',
168 (violationEvent) => {
169 dev().warn('FIE', 'security policy violation', violationEvent);
170 }
171 );
172 };
173 let loadedPromise;
174 if (isSrcdocSupported()) {
175 iframe.srcdoc = html;
176 loadedPromise = loadPromise(iframe);
177 container.appendChild(iframe);
178 registerViolationListener();
179 } else {
180 iframe.src = 'about:blank';
181 container.appendChild(iframe);
182 const childDoc = iframe.contentWindow.document;
183 registerViolationListener();
184 childDoc.open();
185 childDoc.write(devAssert(html));
186 // With document.write, `iframe.onload` arrives almost immediately, thus
187 // we need to wait for child's `window.onload`.
188 loadedPromise = loadPromise(iframe.contentWindow);
189 childDoc.close();
190 }
191
192 // Wait for document ready signal.
193 // This is complicated due to crbug.com/649201 on Chrome and a similar issue

Callers 5

setupMethod · 0.90

Calls 15

getTopWindowFunction · 0.90
getWinFunction · 0.90
setStyleFunction · 0.90
loadPromiseFunction · 0.90
devAssertFunction · 0.90
rethrowAsyncFunction · 0.90
mergeHtmlFunction · 0.85
isSrcdocSupportedFunction · 0.85
isIframeReadyFunction · 0.85
resolveMethod · 0.80

Tested by

no test coverage detected