MCPcopy Create free account
hub / github.com/ampproject/amphtml / mergeHtml

Function mergeHtml

src/friendly-iframe-embed.js:277–331  ·  view source on GitHub ↗

* Merges base and fonts into html document. * @param {!FriendlyIframeSpec} spec * @return {string}

(spec)

Source from the content-addressed store, hash-verified

275 * @return {string}
276 */
277function mergeHtml(spec) {
278 const originalHtml = spec.html;
279 const originalHtmlUp = originalHtml.toUpperCase();
280
281 // Find the insertion point.
282 let ip = originalHtmlUp.indexOf('<HEAD');
283 if (ip != -1) {
284 ip = originalHtmlUp.indexOf('>', ip + 1) + 1;
285 }
286 if (ip == -1) {
287 ip = originalHtmlUp.indexOf('<BODY');
288 }
289 if (ip == -1) {
290 ip = originalHtmlUp.indexOf('<HTML');
291 if (ip != -1) {
292 ip = originalHtmlUp.indexOf('>', ip + 1) + 1;
293 }
294 }
295
296 const result = [];
297
298 // Preamble.
299 if (ip > 0) {
300 result.push(originalHtml.substring(0, ip));
301 }
302
303 // Add <BASE> tag.
304 result.push(`<base href="${escapeHtml(spec.url)}">`);
305
306 // Load fonts.
307 if (spec.fonts) {
308 spec.fonts.forEach((font) => {
309 result.push(
310 `<link href="${escapeHtml(font)}" rel="stylesheet" type="text/css">`
311 );
312 });
313 }
314
315 const cspScriptSrc = getFieSafeScriptSrcs();
316
317 // Load CSP
318 result.push(
319 '<meta http-equiv=Content-Security-Policy ' +
320 `content="script-src ${cspScriptSrc};object-src 'none';child-src 'none'">`
321 );
322
323 // Postambule.
324 if (ip > 0) {
325 result.push(originalHtml.substring(ip));
326 } else {
327 result.push(originalHtml);
328 }
329
330 return result.join('');
331}
332
333/**
334 * Exposes `mergeHtml` for testing purposes.

Callers 2

mergeHtmlForTestingFunction · 0.85

Calls 4

escapeHtmlFunction · 0.90
getFieSafeScriptSrcsFunction · 0.85
pushMethod · 0.45
forEachMethod · 0.45

Tested by

no test coverage detected