summaryrefslogtreecommitdiffstats
path: root/dom/base/test/test_pasting_svg_image.html
blob: be22f92f3aebbc69fd0e9274008556f7d1e9fe81 (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
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
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test pasting &lt;svg&gt;&lt;image href&gt;</title>
  <script src="/tests/SimpleTest/EventUtils.js"></script>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
  <script>
    const kPasteTargetId = "pasteTarget";
    const kTestContentId = "testContent";

    function selectSVG() {
      const testContent = document.getElementById(kTestContentId);
      document.getSelection().selectAllChildren(testContent);
    }

    async function copyToClipboard() {
      function validatorFn(aData) {
        const testContent = document.getElementById(kTestContentId);

        let expectedData = testContent.outerHTML;
        if (navigator.platform.includes(kPlatformWindows)) {
          expectedData = kTextHtmlPrefixClipboardDataWindows +
            expectedData + kTextHtmlSuffixClipboardDataWindows;
        }

        return SimpleTest.stripLinebreaksAndWhitespaceAfterTags(aData) ==
          SimpleTest.stripLinebreaksAndWhitespaceAfterTags(expectedData);
      }

      function setupFn() {
        synthesizeKey("c", { accelKey: true } /* aEvent */);
      }

      const flavor = "text/html";

      await SimpleTest.promiseClipboardChange(validatorFn, setupFn, flavor);
    }

    async function pasteTo(aTargetElement) {
      document.getSelection().selectAllChildren(aTargetElement);

      const promiseInputEvent = new Promise(resolve => {
        document.addEventListener("input", resolve,
          { once: true } /* options */);
        synthesizeKey("v", { accelKey: true } /* aEvent */);
      });

      await promiseInputEvent;
    }

    async function runTest() {
      selectSVG();
      await copyToClipboard();

      // Get the test-content before pasting, because pasting will duplicate
      // ids.
      const expectedPastedInnerHTML =
        SimpleTest.stripLinebreaksAndWhitespaceAfterTags(
          document.getElementById(kTestContentId).outerHTML);

      const pasteTargetElement = document.getElementById(kPasteTargetId);
      await pasteTo(pasteTargetElement);

      const pastedInnerHTMLWithoutLinebreaksAndWhitespaceAfterTags =
        SimpleTest.stripLinebreaksAndWhitespaceAfterTags(
          pasteTargetElement.innerHTML);

      is(pastedInnerHTMLWithoutLinebreaksAndWhitespaceAfterTags,
        expectedPastedInnerHTML,
        "Pasted HTML is as expected.");

      SimpleTest.finish()
    }

    function onLoad() {
      SimpleTest.waitForExplicitFinish();
      SimpleTest.waitForFocus(runTest);
    };
  </script>
</head>
<body onload="onLoad()">
  <h6>
    Test for <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=1669050">bug 1669050</a>
  </h6>
<div id="testContent">
  foo
  <svg>
    <image
     href="http://mochi.test:8888/tests/dom/base/test/name_of_some_image_file.png"
     height="200" width="200">
    </image>
  </svg>
  bar
</div>
<div contenteditable id="pasteTarget"/>
</body>
</html>