* Merges base and fonts into html document. * @param {!FriendlyIframeSpec} spec * @return {string}
(spec)
| 275 | * @return {string} |
| 276 | */ |
| 277 | function 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. |
no test coverage detected