summaryrefslogtreecommitdiffstats
path: root/editor/libeditor/tests/test_pasteImgFromTransferable.html
blob: a45bb5b574dc5f2e77900c6dfdfbe1c29b195569 (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
<!DOCTYPE html>
<html>
<head>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>

<body>
<div id="edit" contenteditable></div>

<script>
const Cc = SpecialPowers.Cc;
const Ci = SpecialPowers.Ci;

function getHTMLEditor(aWindow) {
  let editingSession = SpecialPowers.wrap(aWindow).docShell.editingSession;
  if (!editingSession) {
    return null;
  }
  let editor = editingSession.getEditorForWindow(aWindow);
  if (!editor) {
    return null;
  }
  return editor.QueryInterface(Ci.nsIHTMLEditor);
}

const TESTS = [
  {
    mimeType: "image/gif",
    base64: "R0lGODdhAQACAPABAAD/AP///ywAAAAAAQACAAACAkQKADs="
  },
  {
    mimeType: "image/jpeg",
    base64: "/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAP//////////////////////////////////////////////////////////////////////////////////////wgALCAABAAEBAREA/8QAFBABAAAAAAAAAAAAAAAAAAAAAP/aAAgBAQABPxA="
  },
  {
    mimeType: "image/png",
    base64: "iVBORw0KGgoAAAANSUhEUgAAAAYAAAAGCAIAAABvrngfAAAAFklEQVQImWMwjWhCQwxECoW3oCHihAB0LyYv5/oAHwAAAABJRU5ErkJggg=="
  },
];

add_task(async function() {
  await new Promise(resolve => SimpleTest.waitForFocus(resolve, window));

  let edit = document.getElementById("edit");
  edit.focus();

  await new Promise(resolve => SimpleTest.executeSoon(resolve));

  for (const test of TESTS) {
    let bin = window.atob(test.base64);
    let stringStream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(
      Ci.nsIStringInputStream
    );
    stringStream.setData(bin, bin.length);

    let trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(
      Ci.nsITransferable
    );
    trans.init(null);
    trans.setTransferData(test.mimeType, stringStream);

    let evt = new Promise(resolve =>
      edit.addEventListener("input", resolve, {once: true}));

    getHTMLEditor(window).pasteTransferable(trans);

    await evt;

    is(edit.innerHTML,
       "<img src=\"data:" + test.mimeType + ";base64," + test.base64 + "\" alt=\"\">",
       "pastedTransferable pastes image as data URL");
    edit.innerHTML = "";
  }
});
</script>
</body>
</html>