Debian Codegraph @

closure-library/closure/goog/editor/plugins/firststrong_test.js

raw
1 // Copyright 2012 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.editor.plugins.FirstStrongTest');
16 goog.setTestOnly('goog.editor.plugins.FirstStrongTest');
17
18 goog.require('goog.dom.Range');
19 goog.require('goog.editor.Command');
20 goog.require('goog.editor.Field');
21 goog.require('goog.editor.plugins.FirstStrong');
22 goog.require('goog.editor.range');
23 goog.require('goog.events.KeyCodes');
24 goog.require('goog.html.testing');
25 goog.require('goog.testing.MockClock');
26 goog.require('goog.testing.editor.TestHelper');
27 goog.require('goog.testing.events');
28 goog.require('goog.testing.jsunit');
29 goog.require('goog.userAgent');
30
31 // The key code for the Hebrew א, a strongly RTL letter.
32 var ALEPH_KEYCODE = 1488;
33 var field;
34 var fieldElement;
35 var dom;
36 var helper;
37 var triggeredCommand = null;
38 var clock;
39
40 function setUp() {
41 field = new goog.editor.Field('field');
42 field.registerPlugin(new goog.editor.plugins.FirstStrong());
43 field.makeEditable();
44
45 fieldElement = field.getElement();
46
47 helper = new goog.testing.editor.TestHelper(fieldElement);
48
49 dom = field.getEditableDomHelper();
50
51 // Mock out execCommand to see if a direction change has been triggered.
52 field.execCommand = function(command) {
53 if (command == goog.editor.Command.DIR_LTR ||
54 command == goog.editor.Command.DIR_RTL)
55 triggeredCommand = command;
56 };
57 }
58
59 function tearDown() {
60 goog.dispose(field);
61 goog.dispose(helper);
62 triggeredCommand = null;
63 goog.dispose(clock); // Make sure clock is disposed.
64 }
65
66 function testFirstCharacter_RTL() {
67 field.setSafeHtml(
68 false,
69 goog.html.testing.newSafeHtmlForTest('<div id="text">&nbsp;</div>'));
70 field.focusAndPlaceCursorAtStart();
71 goog.testing.events.fireNonAsciiKeySequence(
72 fieldElement, goog.events.KeyCodes.T, ALEPH_KEYCODE);
73 assertRTL();
74 }
75
76 function testFirstCharacter_LTR() {
77 field.setSafeHtml(
78 false,
79 goog.html.testing.newSafeHtmlForTest(
80 '<div dir="rtl" id="text">&nbsp;</div>'));
81 field.focusAndPlaceCursorAtStart();
82 goog.testing.events.fireKeySequence(fieldElement, goog.events.KeyCodes.A);
83 assertLTR();
84 }
85
86 function testFirstStrongCharacter_RTL() {
87 field.setSafeHtml(
88 false,
89 goog.html.testing.newSafeHtmlForTest(
90 '<div id="text">123.7 3121, <b><++{}></b> - $45</div>'));
91 field.focusAndPlaceCursorAtStart();
92 goog.testing.events.fireNonAsciiKeySequence(
93 fieldElement, goog.events.KeyCodes.T, ALEPH_KEYCODE);
94 assertRTL();
95 }
96
97 function testFirstStrongCharacter_LTR() {
98 field.setSafeHtml(
99 false,
100 goog.html.testing.newSafeHtmlForTest(
101 '<div dir="rtl" id="text">123.7 3121, <b><++{}></b> - $45</div>'));
102 field.focusAndPlaceCursorAtStart();
103 goog.testing.events.fireKeySequence(fieldElement, goog.events.KeyCodes.A);
104 assertLTR();
105 }
106
107 function testNotStrongCharacter_RTL() {
108 field.setSafeHtml(
109 false,
110 goog.html.testing.newSafeHtmlForTest(
111 '<div id="text">123.7 3121, - $45</div>'));
112 field.focusAndPlaceCursorAtStart();
113 goog.testing.events.fireKeySequence(fieldElement, goog.events.KeyCodes.NINE);
114 assertNoCommand();
115 }
116
117 function testNotStrongCharacter_LTR() {
118 field.setSafeHtml(
119 false,
120 goog.html.testing.newSafeHtmlForTest(
121 '<div dir="rtl" id="text">123.7 3121 $45</div>'));
122 field.focusAndPlaceCursorAtStart();
123 goog.testing.events.fireKeySequence(fieldElement, goog.events.KeyCodes.NINE);
124 assertNoCommand();
125 }
126
127 function testNotFirstStrongCharacter_RTL() {
128 field.setSafeHtml(
129 false,
130 goog.html.testing.newSafeHtmlForTest(
131 '<div id="text">123.7 3121, <b>English</b> - $45</div>'));
132 field.focusAndPlaceCursorAtStart();
133 goog.testing.events.fireNonAsciiKeySequence(
134 fieldElement, goog.events.KeyCodes.T, ALEPH_KEYCODE);
135 assertNoCommand();
136 }
137
138 function testNotFirstStrongCharacter_LTR() {
139 field.setSafeHtml(
140 false,
141 goog.html.testing.newSafeHtmlForTest(
142 '<div dir="rtl" id="text">123.7 3121, <b>עברית</b> - $45</div>'));
143 field.focusAndPlaceCursorAtStart();
144 goog.testing.events.fireKeySequence(fieldElement, goog.events.KeyCodes.A);
145 assertNoCommand();
146 }
147
148 function testFirstStrongCharacterWithInnerDiv_RTL() {
149 field.setSafeHtml(
150 false,
151 goog.html.testing.newSafeHtmlForTest(
152 '<div id="text">123.7 3121, <b id="b"><++{}></b>' +
153 '<div id="inner">English</div>' +
154 '</div>'));
155 field.focusAndPlaceCursorAtStart();
156 goog.testing.events.fireNonAsciiKeySequence(
157 fieldElement, goog.events.KeyCodes.T, ALEPH_KEYCODE);
158 assertRTL();
159 }
160
161 function testFirstStrongCharacterWithInnerDiv_LTR() {
162 field.setSafeHtml(
163 false,
164 goog.html.testing.newSafeHtmlForTest(
165 '<div dir="rtl" id="text">123.7 3121, <b id="b"><++{}></b>' +
166 '<div id="inner">English</div>' +
167 '</div>'));
168 field.focusAndPlaceCursorAtStart();
169 goog.testing.events.fireKeySequence(fieldElement, goog.events.KeyCodes.A);
170 assertLTR();
171 }
172
173
174 /**
175 * Regression test for {@link http://b/7549696}
176 */
177 function testFirstStrongCharacterInNewLine_RTL() {
178 field.setSafeHtml(
179 false,
180 goog.html.testing.newSafeHtmlForTest(
181 '<div><b id="cur">English<br>1</b></div>'));
182 goog.dom.Range.createCaret(dom.$('cur'), 2).select();
183
184 goog.testing.events.fireNonAsciiKeySequence(
185 fieldElement, goog.events.KeyCodes.T, ALEPH_KEYCODE);
186 // Only GECKO treats <br> as a new paragraph.
187 if (goog.userAgent.GECKO) {
188 assertRTL();
189 } else {
190 assertNoCommand();
191 }
192 }
193
194 function testFirstStrongCharacterInParagraph_RTL() {
195 field.setSafeHtml(
196 false,
197 goog.html.testing.newSafeHtmlForTest(
198 '<div id="text1">1&gt; English</div>' +
199 '<div id="text2">2&gt;</div>' +
200 '<div id="text3">3&gt;</div>'));
201 goog.dom.Range.createCaret(dom.$('text2'), 0).select();
202
203 goog.testing.events.fireNonAsciiKeySequence(
204 fieldElement, goog.events.KeyCodes.T, ALEPH_KEYCODE);
205 assertRTL();
206 }
207
208 function testFirstStrongCharacterInParagraph_LTR() {
209 field.setSafeHtml(
210 false,
211 goog.html.testing.newSafeHtmlForTest(
212 '<div dir="rtl" id="text1">1&gt; עברית</div>' +
213 '<div dir="rtl" id="text2">2&gt;</div>' +
214 '<div dir="rtl" id="text3">3&gt;</div>'));
215 goog.dom.Range.createCaret(dom.$('text2'), 0).select();
216
217 goog.testing.events.fireKeySequence(fieldElement, goog.events.KeyCodes.A);
218 assertLTR();
219 }
220
221 function testFirstStrongCharacterInList_RTL() {
222 field.setSafeHtml(
223 false,
224 goog.html.testing.newSafeHtmlForTest(
225 '<div id="text1">1&gt; English</div>' +
226 '<ul id="list">' +
227 '<li>10&gt;</li>' +
228 '<li id="li2"></li>' +
229 '<li>30</li>' +
230 '</ul>' +
231 '<div id="text3">3&gt;</div>'));
232 goog.editor.range.placeCursorNextTo(dom.$('li2'), true);
233
234 goog.testing.events.fireNonAsciiKeySequence(
235 fieldElement, goog.events.KeyCodes.T, ALEPH_KEYCODE);
236 assertRTL();
237 }
238
239 function testFirstStrongCharacterInList_LTR() {
240 field.setSafeHtml(
241 false,
242 goog.html.testing.newSafeHtmlForTest(
243 '<div dir="rtl" id="text1">1&gt; English</div>' +
244 '<ul dir="rtl" id="list">' +
245 '<li>10&gt;</li>' +
246 '<li id="li2"></li>' +
247 '<li>30</li>' +
248 '</ul>' +
249 '<div dir="rtl" id="text3">3&gt;</div>'));
250 goog.editor.range.placeCursorNextTo(dom.$('li2'), true);
251
252 goog.testing.events.fireKeySequence(fieldElement, goog.events.KeyCodes.A);
253 assertLTR();
254 }
255
256 function testNotFirstStrongCharacterInList_RTL() {
257 field.setSafeHtml(
258 false,
259 goog.html.testing.newSafeHtmlForTest(
260 '<div id="text1">1</div>' +
261 '<ul id="list">' +
262 '<li>10&gt;</li>' +
263 '<li id="li2"></li>' +
264 '<li>30<b>3<i>Hidden English</i>32</b></li>' +
265 '</ul>' +
266 '<div id="text3">3&gt;</div>'));
267 goog.editor.range.placeCursorNextTo(dom.$('li2'), true);
268
269 goog.testing.events.fireNonAsciiKeySequence(
270 fieldElement, goog.events.KeyCodes.T, ALEPH_KEYCODE);
271 assertNoCommand();
272 }
273
274 function testNotFirstStrongCharacterInList_LTR() {
275 field.setSafeHtml(
276 false,
277 goog.html.testing.newSafeHtmlForTest(
278 '<div dir="rtl" id="text1">1&gt; English</div>' +
279 '<ul dir="rtl" id="list">' +
280 '<li>10&gt;</li>' +
281 '<li id="li2"></li>' +
282 '<li>30<b>3<i>עברית סמויה</i>32</b></li>' +
283 '</ul>' +
284 '<div dir="rtl" id="text3">3&gt;</div>'));
285 goog.editor.range.placeCursorNextTo(dom.$('li2'), true);
286
287 goog.testing.events.fireKeySequence(fieldElement, goog.events.KeyCodes.A);
288 assertNoCommand();
289 }
290
291 function testFirstStrongCharacterWithBR_RTL() {
292 field.setSafeHtml(
293 false,
294 goog.html.testing.newSafeHtmlForTest(
295 '<div id="container">' +
296 '<div id="text1">ABC</div>' +
297 '<div id="text2">' +
298 '1<br>' +
299 '2<b id="inner">3</b><i>4<u>5<br>' +
300 '6</u>7</i>8</b>9<br>' +
301 '10' +
302 '</div>' +
303 '<div id="text3">11</div>' +
304 '</div>'));
305
306 goog.editor.range.placeCursorNextTo(dom.$('inner'), true);
307 goog.testing.events.fireNonAsciiKeySequence(
308 fieldElement, goog.events.KeyCodes.T, ALEPH_KEYCODE);
309 assertRTL();
310 }
311
312 function testFirstStrongCharacterWithBR_LTR() {
313 field.setSafeHtml(
314 false,
315 goog.html.testing.newSafeHtmlForTest(
316 '<div dir="rtl" id="container">' +
317 '<div dir="rtl" id="text1">אבג</div>' +
318 '<div dir="rtl" id="text2">' +
319 '1<br>' +
320 '2<b id="inner">3</b><i>4<u>5<br>' +
321 '6</u>7</i>8</b>9<br>' +
322 '10' +
323 '</div>' +
324 '<div dir="rtl" id="text3">11</div>' +
325 '</div>'));
326
327 goog.editor.range.placeCursorNextTo(dom.$('inner'), true);
328 goog.testing.events.fireKeySequence(fieldElement, goog.events.KeyCodes.A);
329 assertLTR();
330 }
331
332 function testNotFirstStrongCharacterInBR_RTL() {
333 field.setSafeHtml(
334 false,
335 goog.html.testing.newSafeHtmlForTest(
336 '<div id="container">' +
337 '<div id="text1">ABC</div>' +
338 '<div id="text2">' +
339 '1<br>' +
340 '2<b id="inner">3</b><i><em>4G</em><u>5<br>' +
341 '6</u>7</i>8</b>9<br>' +
342 '10' +
343 '</div>' +
344 '<div id="text3">11</div>' +
345 '</div>'));
346
347 goog.editor.range.placeCursorNextTo(dom.$('inner'), true);
348 goog.testing.events.fireNonAsciiKeySequence(
349 fieldElement, goog.events.KeyCodes.T, ALEPH_KEYCODE);
350 assertNoCommand();
351 }
352
353 function testNotFirstStrongCharacterInBR_LTR() {
354 field.setSafeHtml(
355 false,
356 goog.html.testing.newSafeHtmlForTest(
357 '<div dir="rtl" id="container">' +
358 '<div dir="rtl" id="text1">ABC</div>' +
359 '<div dir="rtl" id="text2">' +
360 '1<br>' +
361 '2<b id="inner">3</b><i><em>4G</em><u>5<br>' +
362 '6</u>7</i>8</b>9<br>' +
363 '10' +
364 '</div>' +
365 '<div dir="rtl" id="text3">11</div>' +
366 '</div>'));
367
368 goog.editor.range.placeCursorNextTo(dom.$('inner'), true);
369 goog.testing.events.fireKeySequence(fieldElement, goog.events.KeyCodes.A);
370 assertNoCommand();
371 }
372
373
374 /**
375 * Regression test for {@link http://b/7530985}
376 */
377 function testFirstStrongCharacterWithPreviousBlockSibling_RTL() {
378 field.setSafeHtml(
379 false,
380 goog.html.testing.newSafeHtmlForTest(
381 '<div>Te<div>xt</div>1<b id="cur">2</b>3</div>'));
382 goog.editor.range.placeCursorNextTo(dom.$('cur'), true);
383 goog.testing.events.fireNonAsciiKeySequence(
384 fieldElement, goog.events.KeyCodes.T, ALEPH_KEYCODE);
385 assertRTL();
386 }
387
388 function testFirstStrongCharacterWithPreviousBlockSibling_LTR() {
389 field.setSafeHtml(
390 false,
391 goog.html.testing.newSafeHtmlForTest(
392 '<div dir="rtl">טק<div>סט</div>1<b id="cur">2</b>3</div>'));
393 goog.editor.range.placeCursorNextTo(dom.$('cur'), true);
394 goog.testing.events.fireKeySequence(fieldElement, goog.events.KeyCodes.A);
395 assertLTR();
396 }
397
398 function testFirstStrongCharacterWithFollowingBlockSibling_RTL() {
399 field.setSafeHtml(
400 false,
401 goog.html.testing.newSafeHtmlForTest(
402 '<div>1<b id="cur">2</b>3<div>Te</div>xt</div>'));
403 goog.editor.range.placeCursorNextTo(dom.$('cur'), true);
404 goog.testing.events.fireNonAsciiKeySequence(
405 fieldElement, goog.events.KeyCodes.T, ALEPH_KEYCODE);
406 assertRTL();
407 }
408
409 function testFirstStrongCharacterWithFollowingBlockSibling_RTL() {
410 field.setSafeHtml(
411 false,
412 goog.html.testing.newSafeHtmlForTest(
413 '<div dir="rtl">1<b id="cur">2</b>3<div>א</div>ב</div>'));
414 goog.editor.range.placeCursorNextTo(dom.$('cur'), true);
415 goog.testing.events.fireKeySequence(fieldElement, goog.events.KeyCodes.A);
416 assertLTR();
417 }
418
419 function testFirstStrongCharacterFromIME_RTL() {
420 field.setSafeHtml(
421 false,
422 goog.html.testing.newSafeHtmlForTest(
423 '<div id="text">123.7 3121, </div>'));
424 field.focusAndPlaceCursorAtStart();
425 var attributes = {};
426 attributes[goog.editor.plugins.FirstStrong.INPUT_ATTRIBUTE] = 'אבג';
427 goog.testing.events.fireNonAsciiKeySequence(fieldElement, 0, 0, attributes);
428 if (goog.userAgent.IE) {
429 // goog.testing.events.fireNonAsciiKeySequence doesn't send KEYPRESS event
430 // so no command is expected.
431 assertNoCommand();
432 } else {
433 assertRTL();
434 }
435 }
436
437 function testFirstCharacterFromIME_LTR() {
438 field.setSafeHtml(
439 false,
440 goog.html.testing.newSafeHtmlForTest(
441 '<div dir="rtl" id="text"> 1234 </div>'));
442 field.focusAndPlaceCursorAtStart();
443 var attributes = {};
444 attributes[goog.editor.plugins.FirstStrong.INPUT_ATTRIBUTE] = 'ABC';
445 goog.testing.events.fireNonAsciiKeySequence(fieldElement, 0, 0, attributes);
446 if (goog.userAgent.IE) {
447 // goog.testing.events.fireNonAsciiKeySequence doesn't send KEYPRESS event
448 // so no command is expected.
449 assertNoCommand();
450 } else {
451 assertLTR();
452 }
453 }
454
455
456 /**
457 * Regression test for {@link http://b/19297723}
458 */
459 function testLTRShortlyAfterRTLAndEnter() {
460 clock = new goog.testing.MockClock();
461 field.focusAndPlaceCursorAtStart();
462 goog.testing.events.fireNonAsciiKeySequence(
463 fieldElement, goog.events.KeyCodes.T, ALEPH_KEYCODE);
464 assertRTL();
465 clock.tick(1000); // Make sure no pending selection change event.
466 goog.testing.events.fireKeySequence(fieldElement, goog.events.KeyCodes.ENTER);
467 assertRTL();
468 goog.testing.events.fireKeySequence(fieldElement, goog.events.KeyCodes.A);
469 assertLTR();
470 // Verify no RTL for first keypress on already-striong paragraph after
471 // delayed selection change event.
472 clock.tick(1000); // Let delayed selection change event fire.
473 goog.testing.events.fireNonAsciiKeySequence(
474 fieldElement, goog.events.KeyCodes.T, ALEPH_KEYCODE);
475 assertLTR();
476 }
477
478 function testRTLShortlyAfterLTRAndEnter() {
479 clock = new goog.testing.MockClock();
480 field.focusAndPlaceCursorAtStart();
481 goog.testing.events.fireKeySequence(fieldElement, goog.events.KeyCodes.A);
482 assertLTR();
483 clock.tick(1000); // Make sure no pending selection change event.
484 goog.testing.events.fireKeySequence(fieldElement, goog.events.KeyCodes.ENTER);
485 assertLTR();
486 goog.testing.events.fireNonAsciiKeySequence(
487 fieldElement, goog.events.KeyCodes.T, ALEPH_KEYCODE);
488 assertRTL();
489 // Verify no LTR for first keypress on already-strong paragraph after
490 // delayed selection change event.
491 clock.tick(1000); // Let delayed selection change event fire.
492 goog.testing.events.fireKeySequence(fieldElement, goog.events.KeyCodes.A);
493 assertRTL();
494 }
495
496 function assertRTL() {
497 assertEquals(goog.editor.Command.DIR_RTL, triggeredCommand);
498 }
499
500 function assertLTR() {
501 assertEquals(goog.editor.Command.DIR_LTR, triggeredCommand);
502 }
503
504 function assertNoCommand() {
505 assertNull(triggeredCommand);
506 }