File: async-navigator-clipboard-write-domstring.https.html

package info (click to toggle)
firefox 145.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,653,344 kB
  • sloc: cpp: 7,594,932; javascript: 6,459,612; ansic: 3,752,905; python: 1,403,433; xml: 629,811; asm: 438,677; java: 186,421; sh: 67,287; makefile: 19,169; objc: 13,086; perl: 12,982; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10
file content (112 lines) | stat: -rw-r--r-- 4,249 bytes parent folder | download | duplicates (11)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<!doctype html>
<meta charset="utf-8">
<title>Async Clipboard input type validation tests - DOMString input in write API</title>
<link rel="help" href="https://w3c.github.io/clipboard-apis/#typedefdef-clipboarditemdata">

<body>Body needed for test_driver.click()</body>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="resources/user-activation.js"></script>
<script>

  // Permissions are required in order to invoke navigator.clipboard functions in
  // an automated test.
  async function getPermissions() {
    await tryGrantReadPermission();
    await tryGrantWritePermission();
    await waitForUserActivation();
  }

  test(() => {
    assert_not_equals(navigator.clipboard, undefined);
    assert_true(navigator.clipboard instanceof Clipboard);
    assert_equals(navigator.clipboard, navigator.clipboard);
  }, 'navigator.clipboard exists');

  promise_test(async t => {
    await getPermissions();
    const text_plain = "This text was copied using `Clipboard.prototype.write`.";
    const html_text = "<p style='color: red; font-style: oblique;'>Test</p>";
    await navigator.clipboard.write([
      new ClipboardItem({
        "text/plain": text_plain,
        "text/html": html_text
      }),
    ]);
  }, 'navigator.clipboard.write(DOMString) succeeds');

  promise_test(async () => {
    await getPermissions();
    const promise_text_string = Promise.resolve('hello');
    const promise_html_string = Promise.resolve("<p style='color: red; font-style: oblique;'>hello</p>");
    const item = new ClipboardItem({
      'text/plain': promise_text_string,
      'text/html': promise_html_string
    });
    await navigator.clipboard.write([item]);
  }, 'navigator.clipboard.write(Promise<DOMString>) succeeds');

  promise_test(async () => {
    await getPermissions();
    const promise_html_string = `
    <table>
      <tbody>
        <tr>
          <td>0,00€</td>
        </tr>
        <tr>
          <td>0,00€</td>
        </tr>
      </tbody>
    </table>
  `;
    const item = new ClipboardItem({
      'text/html': promise_html_string
    });
    await navigator.clipboard.write([item]);
  }, 'navigator.clipboard.write(Promise<DOMString>) with utf-16 string succeeds');

  promise_test(async t => {
    await getPermissions();
    const text_plain = 'hello';
    const html_text = "<p style='color: red; font-style: oblique;'>hello</p>";
    const image = await fetch("/clipboard-apis/resources/greenbox.png");
    const item = new ClipboardItem({
      'text/plain': text_plain,
      'text/html': new Blob([html_text], {type: 'text/html'}),
      'image/png': image.blob(), // Promise<Blob>
      'web text/csv': 'hello,world'
    });
    await navigator.clipboard.write([item]);
  }, 'navigator.clipboard.write(web_custom_format) succeeds');

  promise_test(async () => {
    await getPermissions();
    const html_text = "<p style='color: red; font-style: oblique;'>Test</p>";
    const item = new ClipboardItem({
      'text/plain': 'hello',
      'text/html': new Blob([html_text], {type: 'text/html'})
    });
    const text = await item.getType('text/plain');
    const blob = await item.getType('text/html');
    assert_true(text instanceof Blob, "item.getType('text/plain') should return a Blob");
    assert_true(blob instanceof Blob, "item.getType('text/html') should return a Blob");
  }, 'validate GetType(type) on a constructed ClipboardItem returns Blob');

  promise_test(async () => {
    await getPermissions();
    // Test string with various non-Latin characters: Chinese, Arabic, Cyrillic, emoji
    const nonLatinText = "你好 مرحبا Привет 👋🌍";
    const item = new ClipboardItem({
      'text/plain': nonLatinText
    });
    await navigator.clipboard.write([item]);

    // Read back the text and verify it matches
    const readText = await navigator.clipboard.readText();
    assert_equals(readText, nonLatinText,
      "Text read from clipboard should match the non-Latin text that was written");
  }, 'write non-Latin characters with DOMString and verify readText returns the same string');
</script>