summaryrefslogtreecommitdiffstats
path: root/editor/libeditor/tests/test_cut_copy_delete_command_enabled.html
blob: 9e70178b11fd06699b160112738bbe6f9e2be7f5 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<!DOCTYPE HTML>
<!-- This Source Code Form is subject to the terms of the Mozilla Public
   - License, v. 2.0. If a copy of the MPL was not distributed with this
   - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1067255
-->

<head>
  <title>Test for enabled state of cut/copy/delete commands</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  <script src="/tests/SimpleTest/EventUtils.js"></script>
</head>

<body onload="doTest();">
  <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1067255">Mozilla Bug 1067255</a>

  <pre id="test">
    <script type="application/javascript">
      /** Test for Bug 1067255 **/
      SimpleTest.waitForExplicitFinish();

      function doTest() {
        var text = $("text-field");
        var textWithHandlers = $("text-field-2");
        var password = $("password-field");
        var passwordWithHandlers = $("password-field-2");
        var textWithParentHandlers = $("text-field-3");
        var passwordWithParentHandlers = $("password-field-3");
        var textarea1 = $("text-area-1");
        var textarea2 = $("text-area-2");
        var textarea3 = $("text-area-3");

        var editor1 = SpecialPowers.wrap(text).editor;
        var editor2 = SpecialPowers.wrap(password).editor;
        var editor3 = SpecialPowers.wrap(textWithHandlers).editor;
        var editor4 = SpecialPowers.wrap(passwordWithHandlers).editor;
        var editor5 = SpecialPowers.wrap(textWithParentHandlers).editor;
        var editor6 = SpecialPowers.wrap(passwordWithParentHandlers).editor;
        var editor7 = SpecialPowers.wrap(textarea1).editor;
        var editor8 = SpecialPowers.wrap(textarea2).editor;
        var editor9 = SpecialPowers.wrap(textarea3).editor;

        text.focus();

        ok(!editor1.canCopy(),
           "nsIEditor.canCopy() should return false in <input type=text> with no selection");
        ok(!editor1.canCut(),
           "nsIEditor.canCut() should return false in <input type=text> with no selection");
        ok(!SpecialPowers.isCommandEnabled(window, "cmd_copy"),
           "cmd_copy command should be disabled in <input type=text> with no selection");
        ok(!SpecialPowers.isCommandEnabled(window, "cmd_cut"),
           "cmd_cut command should be disabled in <input type=text> with no selection");
        ok(!SpecialPowers.isCommandEnabled(window, "cmd_delete"),
           "cmd_delete command should be disabled in <input type=text> with no selection");

        text.select();

        ok(editor1.canCopy(),
           "nsIEditor.canCopy() should return true in <input type=text> with selection");
        ok(editor1.canCut(),
           "nsIEditor.canCut() should return true in <input type=text> with selection");
        ok(SpecialPowers.isCommandEnabled(window, "cmd_copy"),
           "cmd_copy command should be enabled in <input type=text> with selection");
        ok(SpecialPowers.isCommandEnabled(window, "cmd_cut"),
           "cmd_cut command should be enabled in <input type=text> with selection");
        ok(SpecialPowers.isCommandEnabled(window, "cmd_delete"),
           "cmd_delete command should be enabled in <input type=text> with selection");

        password.focus();

        ok(!editor2.canCopy(),
           "nsIEditor.canCopy() should return false in <input type=password> with no selection");
        ok(!editor2.canCut(),
           "nsIEditor.canCut() should return false in <input type=password> with no selection");
        ok(!SpecialPowers.isCommandEnabled(window, "cmd_copy"),
           "cmd_copy command should be disabled in <input type=password> with no selection");
        ok(!SpecialPowers.isCommandEnabled(window, "cmd_cut"),
           "cmd_cut command should be disabled in <input type=password> with no selection");
        ok(!SpecialPowers.isCommandEnabled(window, "cmd_delete"),
           "cmd_delete command should be disabled in <input type=password> with no selection");

        // Copy and cut commands don't do anything on password fields by default,
        // so they remain disabled even when there is a selection...
        password.select();

        ok(!editor2.canCopy(),
           "nsIEditor.canCopy() should return false in <input type=password> with selection");
        ok(!editor2.canCut(),
           "nsIEditor.canCut() should return false in <input type=password> with selection");
        ok(!SpecialPowers.isCommandEnabled(window, "cmd_copy"),
           "cmd_copy command should be disabled in <input type=password> with selection");
        ok(!SpecialPowers.isCommandEnabled(window, "cmd_cut"),
           "cmd_cut command should be disabled in <input type=password> with selection");
        // Delete, on the other hand, does apply to password fields.
        ok(SpecialPowers.isCommandEnabled(window, "cmd_delete"),
           "cmd_delete command should be enabled in <input type=password with selection>");

        // ...but webpages can hook up event handlers to cut/copy events, so we have to
        // keep the cut and copy commands enabled if event handlers are attached,
        // for both regular edit fields and password fields (even when there's no
        // selection, as we don't know what the handler might want to do).
        textWithHandlers.focus();

        ok(editor3.canCopy(),
           "nsIEditor.canCopy() should return true in <input type=text> with event handler");
        ok(editor3.canCut(),
           "nsIEditor.canCut() should return true in <input type=text> with event handler");
        ok(SpecialPowers.isCommandEnabled(window, "cmd_copy"),
           "cmd_copy command should be enabled in <input type=text> with event handler");
        ok(SpecialPowers.isCommandEnabled(window, "cmd_cut"),
           "cmd_cut command should be enabled in <input type=text> with event handler");

        passwordWithHandlers.focus();

        ok(editor4.canCopy(),
           "nsIEditor.canCopy() should return true in <input type=password> with event handler");
        ok(editor4.canCut(),
           "nsIEditor.canCut() should return true in <input type=password> with event handler");
        ok(SpecialPowers.isCommandEnabled(window, "cmd_copy"),
           "cmd_copy command should be enabled in <input type=password> with event handler");
        ok(SpecialPowers.isCommandEnabled(window, "cmd_cut"),
           "cmd_cut command should be enabled in <input type=password> with event handler");

        // Also check that the commands are enabled if there's a handler on a parent element.
        textWithParentHandlers.focus();

        ok(editor5.canCopy(),
           "nsIEditor.canCopy() should return true in <input type=text> with event handler on an ancestor");
        ok(editor5.canCut(),
           "nsIEditor.canCut() should return true in <input type=text> with event handler on an ancestor");
        ok(SpecialPowers.isCommandEnabled(window, "cmd_copy"),
           "cmd_copy command should be enabled in <input type=text> with event handler on an ancestor");
        ok(SpecialPowers.isCommandEnabled(window, "cmd_cut"),
           "cmd_cut command should be enabled in <input type=text> with event handler on an ancestor");

        passwordWithParentHandlers.focus();

        ok(editor6.canCopy(),
           "nsIEditor.canCopy() should return true in <input type=password> with event handler on an ancestor");
        ok(editor6.canCut(),
           "nsIEditor.canCut() should return true in <input type=password> with event handler on an ancestor");
        ok(SpecialPowers.isCommandEnabled(window, "cmd_copy"),
           "cmd_copy command should be enabled in <input type=password> with event handler on an ancestor");
        ok(SpecialPowers.isCommandEnabled(window, "cmd_cut"),
           "cmd_cut command should be enabled in <input type=password> with event handler on an ancestor");

        // TEXTAREA tests

        textarea1.focus();

        ok(!editor7.canCopy(),
           "nsIEditor.canCopy() should return false in <textarea> with no selection");
        ok(!editor7.canCut(),
           "nsIEditor.canCut() should return false in <textarea> with no selection");
        ok(!SpecialPowers.isCommandEnabled(window, "cmd_copy"),
           "cmd_copy command should be disabled in <textarea> with no selection");
        ok(!SpecialPowers.isCommandEnabled(window, "cmd_cut"),
           "cmd_cut command should be disabled in <textarea> with no selection");
        ok(!SpecialPowers.isCommandEnabled(window, "cmd_delete"),
           "cmd_delete command should be disabled in <textarea> with no selection");

        textarea1.select();

        ok(editor7.canCopy(),
           "nsIEditor.canCopy() should return true in <textarea> with selection");
        ok(editor7.canCut(),
           "nsIEditor.canCut() should return true in <textarea> with selection");
        ok(SpecialPowers.isCommandEnabled(window, "cmd_copy"),
           "cmd_copy command should be enabled in <textarea> with selection");
        ok(SpecialPowers.isCommandEnabled(window, "cmd_cut"),
           "cmd_cut command should be enabled in <textarea> with selection");
        ok(SpecialPowers.isCommandEnabled(window, "cmd_delete"),
           "cmd_delete command should be enabled in <textarea> with selection");

        textarea2.focus();

        ok(!editor8.canCopy(),
           "nsIEditor.canCopy() should return false in <textarea> with only a 'cut' handler");
        ok(editor8.canCut(),
           "nsIEditor.canCut() should return true in <textarea> with only a 'cut' handler");
        ok(!SpecialPowers.isCommandEnabled(window, "cmd_copy"),
           "cmd_copy command should be disabled in <textarea> with only a 'cut' handler");
        ok(SpecialPowers.isCommandEnabled(window, "cmd_cut"),
           "cmd_cut command should be enabled in <textarea> with only a 'cut' handler");
        ok(!SpecialPowers.isCommandEnabled(window, "cmd_delete"),
           "cmd_delete command should be disabled in <textarea> with only a 'cut' handler");

        textarea3.focus();

        ok(editor9.canCopy(),
           "nsIEditor.canCopy() should return true in <textarea> with only a 'copy' handler on ancestor");
        ok(!editor9.canCut(),
           "nsIEditor.canCut() should return false in <textarea> with only a 'copy' handler on ancestor");
        ok(SpecialPowers.isCommandEnabled(window, "cmd_copy"),
           "cmd_copy command should be enabled in <textarea> with only a 'copy' handler on ancestor");
        ok(!SpecialPowers.isCommandEnabled(window, "cmd_cut"),
           "cmd_cut command should be disabled in <textarea> with only a 'copy' handler on ancestor");
        ok(!SpecialPowers.isCommandEnabled(window, "cmd_delete"),
           "cmd_delete command should be disabled in <textarea> with only a 'copy' handler on ancestor");

        SimpleTest.finish();
      }
   </script>
  </pre>

  <input type="text" value="Gonzo says hi" id="text-field" />
  <input type="password" value="Jan also" id="password-field" />
  <input type="text" value="Hi says Gonzo" id="text-field-2" oncut="cut()" oncopy="copy()" ondelete="delete()"/>
  <input type="password" value="Also Jan" id="password-field-2" oncut="cut()" oncopy="copy()" ondelete="delete()"/>
  <div oncut="cut()">
    <ul oncopy="copy()">
      <li><input type="text" value="Hi again" id="text-field-3"/></li>
      <li><input type="password" value="And again, hi" id="password-field-3"/></li>
    </ul>
  </div>
  <textarea id="text-area-1">textarea</textarea>
  <textarea oncut="cut()" id="text-area-2">textarea with cut handler</textarea>
  <div oncopy="copy()">
    <blockquote>
      <p><textarea id="text-area-3">textarea with copy handler on parent</textarea></p>
    </blockquote>
  </div>
</body>
</html>