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

Function installStylesLegacy

testing/iframe.js:301–332  ·  view source on GitHub ↗

* @param {!Document} doc * @param {string} cssText * @param {function()} cb

(doc, cssText, cb)

Source from the content-addressed store, hash-verified

299 * @param {function()} cb
300 */
301function installStylesLegacy(doc, cssText, cb) {
302 const style = doc.createElement('style');
303 style.textContent = cssText;
304 doc.head.appendChild(style);
305
306 // Styles aren't always available synchronously. E.g. if there is a
307 // pending style download, it will have to finish before the new
308 // style is visible.
309 // For this reason we poll until the style becomes available.
310 // Sync case.
311 const styleLoaded = () => {
312 const sheets = doc.styleSheets;
313 for (let i = 0; i < sheets.length; i++) {
314 const sheet = sheets[i];
315 if (sheet.ownerNode == style) {
316 return true;
317 }
318 }
319 return false;
320 };
321
322 if (styleLoaded()) {
323 cb();
324 } else {
325 const interval = setInterval(() => {
326 if (styleLoaded()) {
327 clearInterval(interval);
328 cb();
329 }
330 }, 4);
331 }
332}
333
334export function createServedIframe(src) {
335 return new Promise(function (resolve, reject) {

Callers 1

createIframePromiseFunction · 0.85

Calls 4

styleLoadedFunction · 0.70
cbFunction · 0.50
setIntervalFunction · 0.50
clearIntervalFunction · 0.50

Tested by

no test coverage detected