summaryrefslogtreecommitdiffstats
path: root/editor/libeditor/tests/test_htmleditor_tab_key_handling.html
blob: 2d428611af3b82556bc8cc5580c99ac4933e1a8f (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
100
101
102
103
104
105
106
107
108
109
110
111
112
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Testing indentation with `Tab` key</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script src="/tests/SimpleTest/EventUtils.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<script>
"use strict";

SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(() => {
  function initEditor(aEditingHostTag) {
    const editor = document.createElement(aEditingHostTag);
    editor.setAttribute("contenteditable", "true");
    document.body.insertBefore(editor, document.body.firstChild);
    editor.getBoundingClientRect();
    const htmlEditor = SpecialPowers.wrap(window).docShell.editingSession.getEditorForWindow(window);
    htmlEditor.flags &= ~SpecialPowers.Ci.nsIEditor.eEditorAllowInteraction;
    return editor;
  }

  (function test_tab_in_paragraph() {
    const editor = initEditor("div");
    editor.innerHTML = "<p>abc</p><p>def</p>";
    editor.getBoundingClientRect();
    editor.focus();
    getSelection().collapse(editor.querySelector("p").firstChild, 1);
    synthesizeKey("KEY_Tab");
    is(
      editor.innerHTML.replace(/&nbsp;/g, " "),
      `<p>a    bc</p><p>def</p>`,
      "4 white-spaces should be inserted into the paragraph by Tab"
    );

    let blurred = false;
    editor.addEventListener("blur", () => {
      blurred = true;
    }, {once: true});
    synthesizeKey("KEY_Tab", {shiftKey: true});
    is(
      editor.innerHTML.replace(/&nbsp;/g, " "),
      `<p>a    bc</p><p>def</p>`,
      "The spaces in the paragraph shouldn't be removed by Shift-Tab"
    );
    ok(blurred, "Shift-Tab should cause moving focus");
    editor.remove();
  })();

  (function test_tab_in_body_text() {
    const editor = initEditor("div");
    editor.innerHTML = "abc";
    editor.getBoundingClientRect();
    editor.focus();
    getSelection().collapse(editor.firstChild, 1);
    synthesizeKey("KEY_Tab");
    is(
      editor.innerHTML.replace(/&nbsp;/g, " "),
      `a    bc`,
      "4 white-spaces should be inserted into the body text by Tab"
    );
    editor.remove();
  })();

  (function test_tab_in_li() {
    const editor = initEditor("div");
    for (const list of ["ol", "ul"]) {
      editor.innerHTML = `abc<${list}><li>def</li><li>ghi</li><li>jkl</li></${list}>`;
      editor.getBoundingClientRect();
      editor.focus();
      getSelection().collapse(editor.querySelector("li + li").firstChild, 1);
      synthesizeKey("KEY_Tab");
      is(
        editor.innerHTML,
        `abc<${list}><li>def</li><${list}><li>ghi</li></${list}><li>jkl</li></${list}>`,
        `The list item containing caret should be moved into new sub-<${list}> by Tab`
      );
      synthesizeKey("KEY_Tab", {shiftKey: true});
      is(
        editor.innerHTML,
        `abc<${list}><li>def</li><li>ghi</li><li>jkl</li></${list}>`,
        `The list item containing caret should be moved into parent <${list}> by Shift-Tab`
      );
    }
    editor.remove();
  })();

  (function test_tab_in_nested_editing_host_in_li() {
    const editor = initEditor("div");
    editor.innerHTML = `abc<ul><li>def</li><li><span contenteditable="false">g<span contenteditable="true">h</span>i</span></li><li>jkl</li></ul>`;
    editor.getBoundingClientRect();
    editor.focus();
    getSelection().collapse(editor.querySelector("span[contenteditable=true]").firstChild, 1);
    synthesizeKey("KEY_Tab");
    is(
      editor.innerHTML,
      `abc<ul><li>def</li><li><span contenteditable="false">g<span contenteditable="true">h</span>i</span></li><li>jkl</li></ul>`,
      `The list item containing caret should be modified by Tab`
    );
    editor.remove();
  })();

  // TODO: Add table cell cases.

  SimpleTest.finish();
});
</script>
</body>
</html>