summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/fs/resources/messaging-blob-helpers.js
blob: 852f2e2d3213075ce738fbc2bb5ac4a1f4008ad6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
'use strict';

// Creates a blob URL with the contents of 'message-target.html'. Use the
// blob as an iframe src or a window.open() URL, which creates a same origin
// message target.
async function create_message_target_blob_url(test) {
  const html = await create_message_target_html_without_subresources(test);
  const blob = new Blob([html], { type: 'text/html' });
  return URL.createObjectURL(blob);
}

// Creates a data URI with the contents of 'message-target.html'. Use the
// data URI as an iframe src, which creates a cross origin message target.
async function create_message_target_data_uri(test) {
  const iframe_html =
    await create_message_target_html_without_subresources(test);
  return `data:text/html,${encodeURIComponent(iframe_html)}`;
}

// Constructs a version of 'message-target.html' without any subresources.
// Enables the creation of blob URLs, data URIs and iframe srcdocs re-using
// the contents of 'message-target.html'.
async function create_message_target_html_without_subresources(test) {
  const test_helpers_script = await fetch_text('resources/test-helpers.js');

  const messaging_helpers_script =
    await fetch_text('resources/messaging-helpers.js');

  const messaging_serialize_helpers_script =
    await fetch_text('resources/messaging-serialize-helpers.js');

  const message_target_script =
    await fetch_text('resources/message-target.js');

  // Get the inline script code from 'message-target.html'.
  const iframe = await add_iframe(test, { src: 'resources/message-target.html' });
  const iframe_script =
    iframe.contentWindow.document.getElementById('inline_script').outerHTML;
  iframe.remove();

  return '<!DOCTYPE html>' +
    `<script>${test_helpers_script}</script>` +
    `<script>${messaging_serialize_helpers_script}</script>` +
    `<script>${message_target_script}</script>` +
    `${iframe_script}`;
}

async function fetch_text(url) {
  const response = await fetch(url);
  return await response.text();
}