summaryrefslogtreecommitdiffstats
path: root/editor/libeditor/tests/test_nsIEditor_documentIsEmpty.html
blob: 1e60e5819197d75a9fbaa3b82021f9e11fc5ca0a (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Tests of nsIEditor#documentIsEmpty</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
  <script>
    SimpleTest.waitForExplicitFinish();
    SimpleTest.waitForFocus(() => {
      const originalBody = document.body.innerHTML;

      (function test_with_text_editor() {
        for (const test of [
            {
              tag: "input",
              innerHTML: '<input><input value="abc"><input placeholder="abc">',
            },
            {
              tag: "textarea",
              innerHTML: '<textarea></textarea><textarea>abc</textarea><textarea placeholder="abc"></textarea>',
            },
          ]) {
            document.body.innerHTML = test.innerHTML;
            let textControl = document.body.querySelector(test.tag);
            is(SpecialPowers.wrap(textControl).editor.documentIsEmpty, true,
              `nsIEditor.documentIsEmpty should be true if value of <${test.tag}> is empty by default`);
            textControl.focus();
            is(SpecialPowers.wrap(textControl).editor.documentIsEmpty, true,
              `nsIEditor.documentIsEmpty should be true if value of <${test.tag}> is empty by default after getting focus`);
            textControl.value = "abc";
            is(SpecialPowers.wrap(textControl).editor.documentIsEmpty, false,
              `nsIEditor.documentIsEmpty should be false if <${test.tag}>.value is set to non-empty string`);
            textControl.value = "";
            is(SpecialPowers.wrap(textControl).editor.documentIsEmpty, true,
              `nsIEditor.documentIsEmpty should be true if <${test.tag}>.value is set to empty string`);

            textControl = textControl.nextSibling;
            is(SpecialPowers.wrap(textControl).editor.documentIsEmpty, false,
              `nsIEditor.documentIsEmpty should be false if value of <${test.tag}> is non-empty by default`);
            textControl.focus();
            is(SpecialPowers.wrap(textControl).editor.documentIsEmpty, false,
              `nsIEditor.documentIsEmpty should be false if value of <${test.tag}> is non-empty by default after getting focus`);
            textControl.value = "def";
            is(SpecialPowers.wrap(textControl).editor.documentIsEmpty, false,
              `nsIEditor.documentIsEmpty should be false if <${test.tag}>.value is set to different non-empty string`);
            textControl.value = "";
            is(SpecialPowers.wrap(textControl).editor.documentIsEmpty, true,
              `nsIEditor.documentIsEmpty should be true if <${test.tag}>.value is set to empty string from non-empty string`);

            textControl = textControl.nextSibling;
            is(SpecialPowers.wrap(textControl).editor.documentIsEmpty, true,
              `nsIEditor.documentIsEmpty should be true if value of <${test.tag}> is empty by default (placeholder isn't empty)`);
            textControl.focus();
            is(SpecialPowers.wrap(textControl).editor.documentIsEmpty, true,
              `nsIEditor.documentIsEmpty should be true if value of <${test.tag}> is empty by default after getting focus (placeholder isn't empty)`);
            textControl.value = "abc";
            is(SpecialPowers.wrap(textControl).editor.documentIsEmpty, false,
              `nsIEditor.documentIsEmpty should be false if <${test.tag}>.value is set to non-empty string (placeholder isn't empty)`);
            textControl.value = "";
            is(SpecialPowers.wrap(textControl).editor.documentIsEmpty, true,
              `nsIEditor.documentIsEmpty should be true if <${test.tag}>.value is set to empty string (placeholder isn't empty)`);
          }
      })();

      function getHTMLEditor() {
        const editingSession = SpecialPowers.wrap(window).docShell.editingSession;
        if (!editingSession) {
          return null;
        }
        return editingSession.getEditorForWindow(window);
      }

      (function test_with_contenteditable() {
        document.body.innerHTML = "<div contenteditable></div>";
        try {
          getHTMLEditor().documentIsEmpty;
          todo(false, "nsIEditor.documentIsEmpty should throw an exception when no editing host has focus");
        } catch (e) {
          ok(true, "nsIEditor.documentIsEmpty should throw an exception when no editing host has focus");
        }
        document.querySelector("div[contenteditable]").focus();
        is(getHTMLEditor().documentIsEmpty, true,
          "nsIEditor.documentIsEmpty should be true when editing host does not have contents");

        document.body.innerHTML = "<div contenteditable><br></div>";
        document.querySelector("div[contenteditable]").focus();
        is(getHTMLEditor().documentIsEmpty, false,
          "nsIEditor.documentIsEmpty should be false when editing host has only a <br> element");

        document.body.innerHTML = "<div contenteditable><p><br></p></div>";
        document.querySelector("div[contenteditable]").focus();
        is(getHTMLEditor().documentIsEmpty, false,
          "nsIEditor.documentIsEmpty should be false when editing host has only an empty paragraph");

        document.body.innerHTML = "<div contenteditable><p>abc</p></div>";
        document.querySelector("div[contenteditable]").focus();
        is(getHTMLEditor().documentIsEmpty, false,
          "nsIEditor.documentIsEmpty should be false when editing host has text in a paragraph");

        document.body.innerHTML = "<div contenteditable>abc</div>";
        document.querySelector("div[contenteditable]").focus();
        is(getHTMLEditor().documentIsEmpty, false,
          "nsIEditor.documentIsEmpty should be false when editing host has text directly");

        document.execCommand("selectall");
        document.execCommand("delete");
        todo_is(getHTMLEditor().documentIsEmpty, true,
          "nsIEditor.documentIsEmpty should be true when all contents in editing host are deleted");
      })();

      document.designMode = "on";
      (function test_with_designMode() {
        document.body.innerHTML = "";
        is(getHTMLEditor().documentIsEmpty, true,
          "nsIEditor.documentIsEmpty should be true when <body> is empty in designMode");
        document.body.focus();
        is(getHTMLEditor().documentIsEmpty, true,
          "nsIEditor.documentIsEmpty should be true when <body> is empty in designMode (after setting focus explicitly)");

        document.body.innerHTML = "<div><br></div>";
        is(getHTMLEditor().documentIsEmpty, false,
          "nsIEditor.documentIsEmpty should be false when <body> has only an empty paragraph in designMode");

        document.body.innerHTML = "<div>abc</div>";
        is(getHTMLEditor().documentIsEmpty, false,
          "nsIEditor.documentIsEmpty should be false when <body> has text in a paragraph in designMode");

        document.body.innerHTML = "abc";
        is(getHTMLEditor().documentIsEmpty, false,
          "nsIEditor.documentIsEmpty should be false when <body> has text directly in designMode");

        document.execCommand("selectall");
        document.execCommand("delete");
        todo_is(getHTMLEditor().documentIsEmpty, true,
          "nsIEditor.documentIsEmpty should be true when all contents in designMode are deleted");
      })();
      document.designMode = "off";

      document.body.innerHTML = originalBody;
      SimpleTest.finish();
    });
  </script>
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test"></pre>
</body>
</html>