DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (95ddb3213aec)

VCS Links

Line Code
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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
<html>
<head>
  <title>Test for input event of text editor</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>
<div id="display">
  <input type="text" id="input">
  <textarea id="textarea"></textarea>
</div>
<div id="content" style="display: none">

</div>
<pre id="test">
</pre>

<script class="testbody" type="application/javascript">
"use strict";

SimpleTest.waitForExplicitFinish();
SimpleTest.expectAssertions(0, 1);  // In a11y module
SimpleTest.waitForFocus(runTests, window);

const kIsWin = navigator.platform.indexOf("Win") == 0;
const kIsMac = navigator.platform.indexOf("Mac") == 0;

async function runTests() {
  await SpecialPowers.pushPrefEnv({
    set: [["dom.input_events.beforeinput.enabled", true]],
  });

  const kWordSelectEatSpaceToNextWord = SpecialPowers.getBoolPref("layout.word_select.eat_space_to_next_word");

  function doTests(aElement, aDescription, aIsTextarea) {
    aDescription += ": ";
    aElement.focus();
    aElement.value = "";

    let cancelBeforeInput = false;
    let beforeInputEvent = null;
    let inputEvent = null;
    let action = "";
    let beforeInputHandler = (aEvent) => {
      ok(!beforeInputEvent, `${aDescription}Multiple "beforeinput" events are fired at ${action} (inputType: "${aEvent.inputType}", data: ${aEvent.data})`);
      if (cancelBeforeInput) {
        aEvent.preventDefault();
      }
      ok(aEvent.isTrusted, `${aDescription}"beforeinput" event at ${action} must be trusted`);
      is(aEvent.target, aElement, `${aDescription}"beforeinput" event at ${action} is fired on unexpected element: ${aEvent.target.tagName}`);
      ok(aEvent instanceof InputEvent, `${aDescription}"beforeinput" event at ${action} should be dispatched with InputEvent interface`);
      ok(aEvent.bubbles, `${aDescription}"beforeinput" event at ${action} must be bubbles`);
      beforeInputEvent = aEvent;
    };
    let inputHandler = (aEvent) => {
      ok(!inputEvent, `${aDescription}Multiple "input" events are fired at ${action} (inputType: "${aEvent.inputType}", data: ${aEvent.data})`);
      ok(aEvent.isTrusted, `${aDescription}"input" event at ${action} must be trusted`);
      is(aEvent.target, aElement, `"input" event at ${action} is fired on unexpected element: ${aEvent.target.tagName}`);
      ok(aEvent instanceof InputEvent, `${aDescription}"input" event at ${action} should be dispatched with InputEvent interface`);
      ok(!aEvent.cancelable, `${aDescription}"input" event at ${action} must not be cancelable`);
      ok(aEvent.bubbles, `${aDescription}"input" event at ${action} must be bubbles`);
      let duration = Math.abs(window.performance.now() - aEvent.timeStamp);
      ok(duration < 30 * 1000,
         `${aDescription}perhaps, timestamp wasn't set correctly :${aEvent.timeStamp} (expected it to be within 30s of ` +
         `the current time but it differed by ${duration}ms)`);
      inputEvent = aEvent;
    };

    aElement.addEventListener("beforeinput", beforeInputHandler, true);
    aElement.addEventListener("input", inputHandler, true);

    cancelBeforeInput = true;
    beforeInputEvent = null;
    inputEvent = null;
    action = 'typing "a"';
    sendString("a");
    is(aElement.value, "", `${aDescription}${action} shouldn't insert "a" since "beforeinput" was canceled"`);
    ok(beforeInputEvent, `${aDescription}"beforeinput" event should've been fired at ${action}`);
    is(beforeInputEvent.cancelable, true, `${aDescription}"beforeinput" event by ${action} should be cancelable`);
    is(beforeInputEvent.inputType, "insertText", `${aDescription}inputType of "beforeinput" event by ${action} should be "insertText"`);
    is(beforeInputEvent.data, "a", `${aDescription}data of "beforeinput" event by ${action} should be "a"`);
    is(beforeInputEvent.dataTransfer, null, `${aDescription}dataTransfer of "beforeinput" event by ${action} should be null`);
    is(beforeInputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "beforeinput" event by ${action} should return empty array`);
    ok(!inputEvent, `${aDescription}"input" event shouldn't have been fired at ${action} since "beforeinput" was canceled`);

    aElement.value = "";
    cancelBeforeInput = false;
    beforeInputEvent = null;
    inputEvent = null;
    action = 'typing "a"';
    sendString("a");
    is(aElement.value, "a", `${aDescription}${action} should've inserted "a"`);
    ok(beforeInputEvent, `${aDescription}"beforeinput" event should've been fired at ${action}`);
    is(beforeInputEvent.cancelable, true, `${aDescription}"beforeinput" event by ${action} should be cancelable`);
    is(beforeInputEvent.inputType, "insertText", `${aDescription}inputType of "beforeinput" event by ${action} should be "insertText"`);
    is(beforeInputEvent.data, "a", `${aDescription}data of "beforeinput" event by ${action} should be "a"`);
    is(beforeInputEvent.dataTransfer, null, `${aDescription}dataTransfer of "beforeinput" event by ${action} should be null`);
    is(beforeInputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "beforeinput" event by ${action} should return empty array`);
    ok(inputEvent, `${aDescription}"input" event should've been fired at ${action}`);
    is(inputEvent.inputType, "insertText", `${aDescription}inputType of "input" event by ${action} should be "insertText"`);
    is(inputEvent.data, "a", `${aDescription}data of "input" event by ${action} should be "a"`);
    is(inputEvent.dataTransfer, null, `${aDescription}dataTransfer of "input" event by ${action} should be null`);
    is(inputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "input" event by ${action} should return empty array`);

    cancelBeforeInput = true;
    beforeInputEvent = null;
    inputEvent = null;
    action = 'removing "a" with "Backspace"';
    synthesizeKey("KEY_Backspace");
    is(aElement.value, "a", `${aDescription}${action} shouldn't remove "a" since "beforeinput" was canceled"`);
    ok(beforeInputEvent, `${aDescription}"beforeinput" event should've been fired at ${action}`);
    is(beforeInputEvent.cancelable, true, `${aDescription}"beforeinput" event by ${action} should be cancelable`);
    is(beforeInputEvent.inputType, "deleteContentBackward", `${aDescription}inputType of "beforeinput" event by ${action} should be "deleteContentBackward"`);
    is(beforeInputEvent.data, null, `${aDescription}data of "beforeinput" event by ${action} should be null`);
    is(beforeInputEvent.dataTransfer, null, `${aDescription}dataTransfer of "beforeinput" event by ${action} should be null`);
    is(beforeInputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "beforeinput" event by ${action} should return empty array`);
    ok(!inputEvent, `${aDescription}"input" event shouldn't have been fired at ${action} since "beforeinput" was canceled`);

    aElement.value = "a";
    aElement.setSelectionStart = "a".length;
    cancelBeforeInput = false;
    beforeInputEvent = null;
    inputEvent = null;
    action = 'removing "a" with "Backspace"';
    synthesizeKey("KEY_Backspace");
    is(aElement.value, "", `${aDescription}${action} should've removed "a"`);
    ok(beforeInputEvent, `${aDescription}"beforeinput" event should've been fired at ${action}`);
    is(beforeInputEvent.cancelable, true, `${aDescription}"beforeinput" event by ${action} should be cancelable`);
    is(beforeInputEvent.inputType, "deleteContentBackward", `${aDescription}inputType of "beforeinput" event by ${action} should be "deleteContentBackward"`);
    is(beforeInputEvent.data, null, `${aDescription}data of "beforeinput" event by ${action} should be null`);
    is(beforeInputEvent.dataTransfer, null, `${aDescription}dataTransfer of "beforeinput" event by ${action} should be null`);
    is(beforeInputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "beforeinput" event by ${action} should return empty array`);
    ok(inputEvent, `${aDescription}"input" event should've been fired at ${action}`);
    is(inputEvent.inputType, "deleteContentBackward", `${aDescription}inputType of "input" event by ${action} should be "deleteContentBackward"`);
    is(inputEvent.data, null, `${aDescription}data of "input" event by ${action} should be null`);
    is(inputEvent.dataTransfer, null, `${aDescription}dataTransfer of "input" event by ${action} should be null`);
    is(inputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "input" event by ${action} should return empty array`);

    cancelBeforeInput = true;
    beforeInputEvent = null;
    inputEvent = null;
    action = 'typing "Enter"';
    synthesizeKey("KEY_Enter");
    if (aIsTextarea) {
      is(aElement.value, "", `${aDescription}${action} shouldn't insert a line break since "beforeinput" was canceled"`);
    } else {
      is(aElement.value, "", `${aDescription}${action} shouldn't insert a line break since it's a single line editor"`);
    }
    ok(beforeInputEvent, `${aDescription}"beforeinput" event should've been fired at ${action}`);
    is(beforeInputEvent.cancelable, true, `${aDescription}"beforeinput" event by ${action} should be cancelable`);
    is(beforeInputEvent.inputType, "insertLineBreak", `${aDescription}inputType of "beforeinput" event by ${action} should be "insertLineBreak"`);
    is(beforeInputEvent.data, null, `${aDescription}data of "beforeinput" event by ${action} should be null`);
    is(beforeInputEvent.dataTransfer, null, `${aDescription}dataTransfer of "beforeinput" event by ${action} should be null`);
    is(beforeInputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "beforeinput" event by ${action} should return empty array`);
    if (aIsTextarea) {
      ok(!inputEvent, `${aDescription}"input" event shouldn't have been fired at ${action} since "beforeinput" was canceled`);
    } else {
      ok(!inputEvent, `${aDescription}$"input" event shouldn't have been fired at ${action} since it's a single line editor"`);
    }

    aElement.value = "";
    cancelBeforeInput = false;
    beforeInputEvent = null;
    inputEvent = null;
    action = 'typing "Enter"';
    synthesizeKey("KEY_Enter");
    if (aIsTextarea) {
      is(aElement.value, "\n", `${aDescription}${action} should've inserted a line break"`);
    } else {
      is(aElement.value, "", `${aDescription}${action} shouldn't insert a line break since it's a single line editor"`);
    }
    ok(beforeInputEvent, `${aDescription}"beforeinput" event should've been fired at ${action}`);
    is(beforeInputEvent.cancelable, true, `${aDescription}"beforeinput" event by ${action} should be cancelable`);
    is(beforeInputEvent.inputType, "insertLineBreak", `${aDescription}inputType of "beforeinput" event by ${action} should be "insertLineBreak"`);
    is(beforeInputEvent.data, null, `${aDescription}data of "beforeinput" event by ${action} should be null`);
    is(beforeInputEvent.dataTransfer, null, `${aDescription}dataTransfer of "beforeinput" event by ${action} should be null`);
    is(beforeInputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "beforeinput" event by ${action} should return empty array`);
    if (aIsTextarea) {
      ok(inputEvent, `${aDescription}"input" event should've been fired at ${action}`);
      is(inputEvent.inputType, "insertLineBreak", `${aDescription}inputType of "input" event by ${action} should be "insertLineBreak"`);
      is(inputEvent.data, null, `${aDescription}data of "input" event by ${action} should be null`);
      is(inputEvent.dataTransfer, null, `${aDescription}dataTransfer of "input" event by ${action} should be null`);
      is(inputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "input" event by ${action} should return empty array`);
    } else {
      ok(!inputEvent, `${aDescription}$"input" event shouldn't have been fired at ${action} since it's a single line editor"`);
    }

    cancelBeforeInput = false;
    beforeInputEvent = null;
    inputEvent = null;
    aElement.value = "foo-bar";
    action = "setting value";
    is(aElement.value, "foo-bar", `${aDescription}value should've been set by ${action}`);
    ok(!beforeInputEvent, `${aDescription}"beforeinput" event shouldn't have been fired by ${action}`);
    ok(!inputEvent, `${aDescription}"input" event shouldn't have been fired by ${action}`);

    cancelBeforeInput = false;
    beforeInputEvent = null;
    inputEvent = null;
    aElement.value = "";
    action = "setting empty value";
    is(aElement.value, "", aDescription + "value wasn't set (empty)");
    is(aElement.value, "", `${aDescription}value should've been set to empy by ${action}`);
    ok(!beforeInputEvent, `${aDescription}"beforeinput" event shouldn't have been fired by ${action}`);
    ok(!inputEvent, `${aDescription}"input" event shouldn't have been fired by ${action}`);

    cancelBeforeInput = false;
    beforeInputEvent = null;
    inputEvent = null;
    action = "typing a space";
    sendString(" ");
    is(aElement.value, " ", `${aDescription}" " should've been inserted by ${action}`);
    ok(beforeInputEvent, `${aDescription}"beforeinput" event should've been fired at ${action}`);
    is(beforeInputEvent.cancelable, true, `${aDescription}"beforeinput" event by ${action} should be cancelable`);
    is(beforeInputEvent.inputType, "insertText", `${aDescription}inputType of "beforeinput" event by ${action} should be "insertText"`);
    is(beforeInputEvent.data, " ", `${aDescription}data of "beforeinput" event by ${action} should be " "`);
    is(beforeInputEvent.dataTransfer, null, `${aDescription}dataTransfer of "beforeinput" event by ${action} should be null`);
    is(beforeInputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "beforeinput" event by ${action} should return empty array`);
    ok(inputEvent, `${aDescription}"input" event should've been fired at ${action}`);
    is(inputEvent.inputType, "insertText", `${aDescription}inputType of "input" event by ${action} should be "insertText"`);
    is(inputEvent.data, " ", `${aDescription}data of "input" event by ${action} should be " "`);
    is(inputEvent.dataTransfer, null, `${aDescription}dataTransfer of "input" event by ${action} should be null`);
    is(inputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "input" event by ${action} should return empty array`);

    cancelBeforeInput = false;
    beforeInputEvent = null;
    inputEvent = null;
    action = 'typing "Delete" at end';
    synthesizeKey("KEY_Delete");
    is(aElement.value, " ", `${aDescription}${action} shouldn't remove anything since no removable content`);
    ok(beforeInputEvent, `${aDescription}"beforeinput" event should've been fired at ${action}`);
    is(beforeInputEvent.cancelable, true, `${aDescription}"beforeinput" event by ${action} should be cancelable`);
    is(beforeInputEvent.inputType, "deleteContentForward", `${aDescription}inputType of "beforeinput" event by ${action} should be "deleteContentForward"`);
    is(beforeInputEvent.data, null, `${aDescription}data of "beforeinput" event by ${action} should be null`);
    is(beforeInputEvent.dataTransfer, null, `${aDescription}dataTransfer of "beforeinput" event by ${action} should be null`);
    is(beforeInputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "beforeinput" event by ${action} should return empty array`);
    ok(!inputEvent, `${aDescription}"input" event shouldn't have been fired by ${action} since no removable content`);

    cancelBeforeInput = false;
    beforeInputEvent = null;
    inputEvent = null;
    action = 'typing "ArrowLeft"';
    synthesizeKey("KEY_ArrowLeft");
    is(aElement.value, " ", `${aDescription}${action} shouldn't remove anything`);
    ok(!beforeInputEvent, `${aDescription}"beforeinput" event shouldn't have been fired by ${action}`);
    ok(!inputEvent, `${aDescription}"input" event shouldn't have been fired by ${action}`);

    cancelBeforeInput = true;
    beforeInputEvent = null;
    inputEvent = null;
    action = 'typing "Delete"';
    synthesizeKey("KEY_Delete");
    is(aElement.value, " ", `${aDescription}${action} shouldn't remove the content since "beforeinput" was canceled`);
    ok(beforeInputEvent, `${aDescription}"beforeinput" event should've been fired at ${action}`);
    is(beforeInputEvent.cancelable, true, `${aDescription}"beforeinput" event by ${action} should be cancelable`);
    is(beforeInputEvent.inputType, "deleteContentForward", `${aDescription}inputType of "beforeinput" event by ${action} should be "deleteContentForward"`);
    is(beforeInputEvent.data, null, `${aDescription}data of "beforeinput" event by ${action} should be null`);
    is(beforeInputEvent.dataTransfer, null, `${aDescription}dataTransfer of "beforeinput" event by ${action} should be null`);
    is(beforeInputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "beforeinput" event by ${action} should return empty array`);
    ok(!inputEvent, `${aDescription}"input" event shouldn't have been fired at ${action} since "beforeinput" was canceled`);

    aElement.value = "";
    aElement.value = " ";
    aElement.selectionStart = 0;
    cancelBeforeInput = false;
    beforeInputEvent = null;
    inputEvent = null;
    action = 'typing "Delete"';
    synthesizeKey("KEY_Delete");
    is(aElement.value, "", `${aDescription}${action} should've removed " "`);
    ok(beforeInputEvent, `${aDescription}"beforeinput" event should've been fired at ${action}`);
    is(beforeInputEvent.cancelable, true, `${aDescription}"beforeinput" event by ${action} should be cancelable`);
    is(beforeInputEvent.inputType, "deleteContentForward", `${aDescription}inputType of "beforeinput" event by ${action} should be "deleteContentForward"`);
    is(beforeInputEvent.data, null, `${aDescription}data of "beforeinput" event by ${action} should be null`);
    is(beforeInputEvent.dataTransfer, null, `${aDescription}dataTransfer of "beforeinput" event by ${action} should be null`);
    is(beforeInputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "beforeinput" event by ${action} should return empty array`);
    ok(inputEvent, `${aDescription}"input" event should've been fired at ${action}`);
    is(inputEvent.inputType, "deleteContentForward", `${aDescription}inputType of "input" event by ${action} should be "deleteContentForward"`);
    is(inputEvent.data, null, `${aDescription}data of "input" event by ${action} should be null`);
    is(inputEvent.dataTransfer, null, `${aDescription}dataTransfer of "input" event by ${action} should be null`);
    is(inputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "input" event by ${action} should return empty array`);

    cancelBeforeInput = true;
    beforeInputEvent = null;
    inputEvent = null;
    action = 'doing "Undo"';
    synthesizeKey("z", {accelKey: true});
    is(aElement.value, "", `${aDescription}${action} shouldn't restore`);
    ok(beforeInputEvent, `${aDescription}"beforeinput" event should've been fired at ${action}`);
    is(beforeInputEvent.cancelable, true, `${aDescription}"beforeinput" event for ${action} should be cancelable`);
    is(beforeInputEvent.inputType, "historyUndo", `${aDescription}inputType of "beforeinput" event for ${action} should be "historyUndo"`);
    is(beforeInputEvent.data, null, `${aDescription}data of "beforeinput" event for ${action} should be null`);
    is(beforeInputEvent.dataTransfer, null, `${aDescription}dataTransfer of "beforeinput" event for ${action} should be null`);
    is(beforeInputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "beforeinput" event by ${action} should return empty array`);
    ok(!inputEvent, `${aDescription}"input" event shouldn't have been fired at ${action} since "beforeinput" was canceled`);

    cancelBeforeInput = false;
    beforeInputEvent = null;
    inputEvent = null;
    action = 'doing "Undo"';
    synthesizeKey("z", {accelKey: true});
    is(aElement.value, " ", `${aDescription}${action} should restore " "`);
    ok(beforeInputEvent, `${aDescription}"beforeinput" event should've been fired at ${action}`);
    is(beforeInputEvent.cancelable, true, `${aDescription}"beforeinput" event for ${action} should be cancelable`);
    is(beforeInputEvent.inputType, "historyUndo", `${aDescription}inputType of "beforeinput" event for ${action} should be "historyUndo"`);
    is(beforeInputEvent.data, null, `${aDescription}data of "beforeinput" event for ${action} should be null`);
    is(beforeInputEvent.dataTransfer, null, `${aDescription}dataTransfer of "beforeinput" event for ${action} should be null`);
    is(beforeInputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "beforeinput" event by ${action} should return empty array`);
    ok(inputEvent, `${aDescription}"input" event should've been fired at ${action}`);
    is(inputEvent.inputType, "historyUndo", `${aDescription}inputType of "input" event for ${action} should be "historyUndo"`);
    is(inputEvent.data, null, `${aDescription}data of "input" event for ${action} should be null`);
    is(inputEvent.dataTransfer, null, `${aDescription}dataTransfer of "input" event for ${action} should be null`);
    is(inputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "input" event by ${action} should return empty array`);

    cancelBeforeInput = false;
    beforeInputEvent = null;
    inputEvent = null;
    action = 'doing "Undo" again';
    synthesizeKey("z", {accelKey: true});
    is(aElement.value, " ", `${aDescription}${action} shouldn't modify the value since no undo transaction`);
    ok(!beforeInputEvent, `${aDescription}"beforeinput" event shouldn't have been fired at ${action} since no undo transaction`);
    ok(!inputEvent, `${aDescription}"input" event shouldn't have been fired at ${action} since no undo transaction`);

    cancelBeforeInput = true;
    beforeInputEvent = null;
    inputEvent = null;
    action = 'doing "Redo"';
    synthesizeKey("Z", {accelKey: true, shiftKey: true});
    is(aElement.value, " ", `${aDescription}${action} shouldn't restore`);
    ok(beforeInputEvent, `${aDescription}"beforeinput" event should've been fired at ${action}`);
    is(beforeInputEvent.cancelable, true, `${aDescription}"beforeinput" event for ${action} should be cancelable`);
    is(beforeInputEvent.inputType, "historyRedo", `${aDescription}inputType of "beforeinput" event for ${action} should be "historyRedo"`);
    is(beforeInputEvent.data, null, `${aDescription}data of "beforeinput" event for ${action} should be null`);
    is(beforeInputEvent.dataTransfer, null, `${aDescription}dataTransfer of "beforeinput" event for ${action} should be null`);
    is(beforeInputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "beforeinput" event by ${action} should return empty array`);
    ok(!inputEvent, `${aDescription}"input" event shouldn't have been fired at ${action} since "beforeinput" was canceled`);

    cancelBeforeInput = false;
    beforeInputEvent = null;
    inputEvent = null;
    action = 'doing "Redo"';
    synthesizeKey("Z", {accelKey: true, shiftKey: true});
    is(aElement.value, "", `${aDescription}${action} should remove " "`);
    ok(beforeInputEvent, `${aDescription}"beforeinput" event should've been fired at ${action}`);
    is(beforeInputEvent.cancelable, true, `${aDescription}"beforeinput" event for ${action} should be cancelable`);
    is(beforeInputEvent.inputType, "historyRedo", `${aDescription}inputType of "beforeinput" event for ${action} should be "historyRedo"`);
    is(beforeInputEvent.data, null, `${aDescription}data of "beforeinput" event for ${action} should be null`);
    is(beforeInputEvent.dataTransfer, null, `${aDescription}dataTransfer of "beforeinput" event for ${action} should be null`);
    is(beforeInputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "beforeinput" event by ${action} should return empty array`);
    ok(inputEvent, `${aDescription}"input" event should've been fired at ${action}`);
    is(inputEvent.inputType, "historyRedo", `${aDescription}inputType of "input" event for ${action} should be "historyRedo"`);
    is(inputEvent.data, null, `${aDescription}data of "input" event for ${action} should be null`);
    is(inputEvent.dataTransfer, null, `${aDescription}dataTransfer of "input" event for ${action} should be null`);
    is(inputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "input" event by ${action} should return empty array`);

    cancelBeforeInput = false;
    beforeInputEvent = null;
    inputEvent = null;
    action = 'doing "Redo" again';
    synthesizeKey("Z", {accelKey: true, shiftKey: true});
    is(aElement.value, "", `${aDescription}${action} shouldn't modify the value since no redo transaction`);
    ok(!beforeInputEvent, `${aDescription}"beforeinput" event shouldn't have been fired at ${action} since no redo transaction`);
    ok(!inputEvent, `${aDescription}"input" event shouldn't have been fired at ${action} since no redo transaction`);

    // Backspace/Delete with non-collapsed selection.
    aElement.value = "a";
    aElement.focus();
    aElement.select();
    cancelBeforeInput = true;
    beforeInputEvent = null;
    inputEvent = null;
    action = 'removing "a" with "Backspace" (with selection)';
    synthesizeKey("KEY_Backspace");
    is(aElement.value, "a", `${aDescription}"a" shouldn't have been removed by ${action} since "beforeinput" was canceled`);
    ok(beforeInputEvent, `${aDescription}"beforeinput" event should've been fired at ${action}`);
    ok(!inputEvent, `${aDescription}"input" event shouldn't been fired at ${action} since "beforeinput" was canceled`);

    aElement.value = "a";
    aElement.focus();
    aElement.select();
    cancelBeforeInput = false;
    beforeInputEvent = null;
    inputEvent = null;
    synthesizeKey("KEY_Backspace");
    is(aElement.value, "", `${aDescription}"a" should've been removed by ${action}`);
    ok(beforeInputEvent, `${aDescription}"beforeinput" event should've been fired at ${action}`);
    is(beforeInputEvent.cancelable, true, `${aDescription}"beforeinput" event for ${action} should be cancelable`);
    is(beforeInputEvent.inputType, "deleteContentBackward",
       `${aDescription}inputType of "beforeinput" event for ${action} should be "deleteContentBackward"`);
    is(beforeInputEvent.data, null, `${aDescription}data of "beforeinput" event for ${action} should be null`);
    is(beforeInputEvent.dataTransfer, null, `${aDescription}dataTransfer of "beforeinput" event for ${action} should be null`);
    is(beforeInputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "beforeinput" event by ${action} should return empty array`);
    ok(inputEvent, `${aDescription}"input" event should've been fired at ${action}`);
    is(inputEvent.inputType, "deleteContentBackward", `${aDescription}inputType of "input" event for ${action} should be "deleteContentBackward"`);
    is(inputEvent.data, null, `${aDescription}data of "input" event for ${action} should be null`);
    is(inputEvent.dataTransfer, null, `${aDescription}dataTransfer of "input" event for ${action} should be null`);
    is(inputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "input" event by ${action} should return empty array`);

    aElement.value = "a";
    aElement.focus();
    aElement.select();
    cancelBeforeInput = true;
    beforeInputEvent = null;
    inputEvent = null;
    action = 'removing "a" with "Delete" (with selection)';
    synthesizeKey("KEY_Delete");
    is(aElement.value, "a", `${aDescription}"a" should've been removed by ${action} since "beforeinput" was canceled`);
    ok(beforeInputEvent, `${aDescription}"beforeinput" event should be fired at ${action} even if it won't remove any content`);
    is(beforeInputEvent.cancelable, true, `${aDescription}"beforeinput" event for ${action} should be cancelable`);
    is(beforeInputEvent.inputType, "deleteContentForward", `${aDescription}inputType of "beforeinput" event for ${action} should be "deleteContentForward"`);
    is(beforeInputEvent.data, null, `${aDescription}data of "beforeinput" event for ${action} should be null`);
    is(beforeInputEvent.dataTransfer, null, `${aDescription}dataTransfer of "beforeinput" event for ${action} should be null`);
    is(beforeInputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "beforeinput" event by ${action} should return empty array`);
    ok(!inputEvent, `${aDescription}${action} should not fire "input" event since "beforeinput" was canceled`);

    aElement.value = "a";
    aElement.focus();
    aElement.select();
    cancelBeforeInput = false;
    beforeInputEvent = null;
    inputEvent = null;
    action = 'removing "a" with "Delete" (with selection)';
    synthesizeKey("KEY_Delete");
    is(aElement.value, "", `${aDescription}" " should've been removed by ${action}`);
    ok(beforeInputEvent, `${aDescription}"beforeinput" event should've been fired at ${action}`);
    is(beforeInputEvent.cancelable, true, `${aDescription}"beforeinput" event for ${action} should be cancelable`);
    is(beforeInputEvent.inputType, "deleteContentForward", `${aDescription}inputType of "beforeinput" event for ${action} should be "deleteContentForward"`);
    is(beforeInputEvent.data, null, `${aDescription}data of "beforeinput" event for ${action} should be null`);
    is(beforeInputEvent.dataTransfer, null, `${aDescription}dataTransfer of "beforeinput" event for ${action} should be null`);
    is(beforeInputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "beforeinput" event by ${action} should return empty array`);
    ok(inputEvent, `${aDescription}"input" event should've been fired at ${action}`);
    is(inputEvent.inputType, "deleteContentForward", `${aDescription}inputType of "input" event for ${action} should be "deleteContentForward"`);
    is(inputEvent.data, null, `${aDescription}data of "input" event for ${action} should be null`);
    is(inputEvent.dataTransfer, null, `${aDescription}dataTransfer of "input" event for ${action} should be null`);
    is(inputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "input" event by ${action} should return empty array`);

    // Delete to previous/next word boundary with collapsed selection.
    aElement.value = "abc def";
    aElement.focus();
    document.documentElement.scrollTop;  // XXX Needs reflow here for working with nsFrameSelection, must be a bug.
    aElement.setSelectionRange("abc def".length, "abc def".length);
    cancelBeforeInput = true;
    beforeInputEvent = null;
    inputEvent = null;
    action = 'removing last word, "def", with backward deletion from its end';
    SpecialPowers.doCommand(window, "cmd_deleteWordBackward");
    is(aElement.value, "abc def", `${aDescription}"def" shouldn't have been removed by ${action} since "beforeinput" was canceled`);
    ok(beforeInputEvent, `${aDescription}"beforeinput" event should've been fired at ${action}`);
    ok(!inputEvent, `${aDescription}"input" event shouldn't been fired at ${action} since "beforeinput" was canceled`);

    aElement.value = "abc def";
    aElement.focus();
    document.documentElement.scrollTop;  // XXX Needs reflow here for working with nsFrameSelection, must be a bug.
    aElement.setSelectionRange("abc def".length, "abc def".length);
    cancelBeforeInput = false;
    beforeInputEvent = null;
    inputEvent = null;
    SpecialPowers.doCommand(window, "cmd_deleteWordBackward");
    is(aElement.value, "abc ", `${aDescription}"def" should've been removed by ${action}`);
    ok(beforeInputEvent, `${aDescription}"beforeinput" event should've been fired at ${action}`);
    is(beforeInputEvent.cancelable, true, `${aDescription}"beforeinput" event for ${action} should be cancelable`);
    is(beforeInputEvent.inputType, "deleteWordBackward", `${aDescription}inputType of "beforeinput" event for ${action} should be "deleteWordBackward"`);
    is(beforeInputEvent.data, null, `${aDescription}data of "beforeinput" event for ${action} should be null`);
    is(beforeInputEvent.dataTransfer, null, `${aDescription}dataTransfer of "beforeinput" event for ${action} should be null`);
    is(beforeInputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "beforeinput" event by ${action} should return empty array`);
    ok(inputEvent, `${aDescription}"input" event should've been fired at ${action}`);
    is(inputEvent.inputType, "deleteWordBackward", `${aDescription}inputType of "input" event for ${action} should be "deleteWordBackward"`);
    is(inputEvent.data, null, `${aDescription}data of "input" event for ${action} should be null`);
    is(inputEvent.dataTransfer, null, `${aDescription}dataTransfer of "input" event for ${action} should be null`);
    is(inputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "input" event by ${action} should return empty array`);

    aElement.value = "abc def";
    aElement.focus();
    document.documentElement.scrollTop;  // XXX Needs reflow here for working with nsFrameSelection, must be a bug.
    aElement.setSelectionRange(0, 0);
    cancelBeforeInput = true;
    beforeInputEvent = null;
    inputEvent = null;
    action = `removing first word, "${kWordSelectEatSpaceToNextWord ? "abc" : "abc "}", with forward deletion from its start`;
    SpecialPowers.doCommand(window, "cmd_deleteWordForward");
    is(aElement.value, "abc def",
       `${aDescription}"${kWordSelectEatSpaceToNextWord ? "abc" : "abc "}" shouldn't have been removed by ${action} since "beforeinput" was canceled`);
    ok(beforeInputEvent, `${aDescription}"beforeinput" event should've been fired at ${action}`);
    ok(!inputEvent, `${aDescription}"input" event shouldn't been fired at ${action} since "beforeinput" was canceled`);

    aElement.value = "abc def";
    aElement.focus();
    document.documentElement.scrollTop;  // XXX Needs reflow here for working with nsFrameSelection, must be a bug.
    aElement.setSelectionRange(0, 0);
    cancelBeforeInput = false;
    beforeInputEvent = null;
    inputEvent = null;
    SpecialPowers.doCommand(window, "cmd_deleteWordForward");
    is(aElement.value, kWordSelectEatSpaceToNextWord ? "def" : " def",
       `${aDescription}"${kWordSelectEatSpaceToNextWord ? "abc" : "abc "}" should've been removed by ${action}`);
    ok(beforeInputEvent, `${aDescription}"beforeinput" event should've been fired at ${action}`);
    is(beforeInputEvent.cancelable, true, `${aDescription}"beforeinput" event for ${action} should be cancelable`);
    is(beforeInputEvent.inputType, "deleteWordForward", `${aDescription}inputType of "beforeinput" event for ${action} should be "deleteWordForward"`);
    is(beforeInputEvent.data, null, `${aDescription}data of "beforeinput" event for ${action} should be null`);
    is(beforeInputEvent.dataTransfer, null, `${aDescription}dataTransfer of "beforeinput" event for ${action} should be null`);
    is(beforeInputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "beforeinput" event by ${action} should return empty array`);
    ok(inputEvent, `${aDescription}"input" event should've been fired at ${action}`);
    is(inputEvent.inputType, "deleteWordForward", `${aDescription}inputType of "input" event for ${action} should be "deleteWordForward"`);
    is(inputEvent.data, null, `${aDescription}data of "input" event for ${action} should be null`);
    is(inputEvent.dataTransfer, null, `${aDescription}dataTransfer of "input" event for ${action} should be null`);
    is(inputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "input" event by ${action} should return empty array`);

    // Delete to previous/next word boundary with non-collapsed selection.
    aElement.value = "abc def";
    aElement.focus();
    document.documentElement.scrollTop;  // XXX Needs reflow here for working with nsFrameSelection, must be a bug.
    aElement.setSelectionRange("abc d".length, "abc de".length);
    cancelBeforeInput = false;
    beforeInputEvent = null;
    inputEvent = null;
    action = "removing characters backward from middle of second word";
    SpecialPowers.doCommand(window, "cmd_deleteWordBackward");
    // Only on Windows, we collapse selection to start before handling this command.
    let expectedInputType = kIsWin ? "deleteWordBackward" : "deleteContentBackward";
    is(aElement.value, kIsWin ? "abc ef" : "abc df",
      `${aDescription}${kIsWin ? "characters between current word start and selection start" : "selected characters"} should've been removed by ${action}`);
    ok(beforeInputEvent, `${aDescription}"beforeinput" event should've been fired at ${action}`);
    is(beforeInputEvent.cancelable, true, `${aDescription}"beforeinput" event for ${action} should be cancelable`);
    is(beforeInputEvent.inputType, expectedInputType, `${aDescription}inputType of "beforeinput" event for ${action} should be "${expectedInputType}"`);
    is(beforeInputEvent.data, null, `${aDescription}data of "beforeinput" event for ${action} should be null`);
    is(beforeInputEvent.dataTransfer, null, `${aDescription}dataTransfer of "beforeinput" event for ${action} should be null`);
    is(beforeInputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "beforeinput" event by ${action} should return empty array`);
    ok(inputEvent, `${aDescription}"input" event should've been fired at ${action}`);
    is(inputEvent.inputType, expectedInputType, `${aDescription}inputType of "input" event for ${action} should be "${expectedInputType}"`);
    is(inputEvent.data, null, `${aDescription}data of "input" event for ${action} should be null`);
    is(inputEvent.dataTransfer, null, `${aDescription}dataTransfer of "input" event for ${action} should be null`);
    is(inputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "input" event by ${action} should return empty array`);

    aElement.value = "abc def";
    aElement.focus();
    document.documentElement.scrollTop;  // XXX Needs reflow here for working with nsFrameSelection, must be a bug.
    aElement.setSelectionRange("a".length, "ab".length);
    cancelBeforeInput = false;
    beforeInputEvent = null;
    inputEvent = null;
    action = "removing characters forward from middle of first word";
    SpecialPowers.doCommand(window, "cmd_deleteWordForward");
    // Only on Windows, we collapse selection to start before handling this command.
    expectedInputType = kIsWin ? "deleteWordForward" : "deleteContentForward";
    let expectedValue = "ac def";
    if (kIsWin) {
      expectedValue = kWordSelectEatSpaceToNextWord ? "adef" : "a def";
    }
    is(aElement.value, expectedValue,
      `${aDescription}${kIsWin ? "characters between selection start and next word start" : "selected characters"} should've been removed by ${action}`);
    ok(beforeInputEvent, `${aDescription}"beforeinput" event should've been fired at ${action}`);
    is(beforeInputEvent.cancelable, true, `${aDescription}"beforeinput" event for ${action} should be cancelable`);
    is(beforeInputEvent.inputType, expectedInputType, `${aDescription}inputType of "beforeinput" event for ${action} should be "${expectedInputType}"`);
    is(beforeInputEvent.data, null, `${aDescription}data of "beforeinput" event for ${action} should be null`);
    is(beforeInputEvent.dataTransfer, null, `${aDescription}dataTransfer of "beforeinput" event for ${action} should be null`);
    is(beforeInputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "beforeinput" event by ${action} should return empty array`);
    ok(inputEvent, `${aDescription}"input" event should've been fired at ${action}`);
    is(inputEvent.inputType, expectedInputType, `${aDescription}inputType of "input" event for ${action} should be "${expectedInputType}"`);
    is(inputEvent.data, null, `${aDescription}data of "input" event for ${action} should be null`);
    is(inputEvent.dataTransfer, null, `${aDescription}dataTransfer of "input" event for ${action} should be null`);
    is(inputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "input" event by ${action} should return empty array`);

    // Delete to previous/next visual line boundary with collapsed selection.
    aElement.value = "abc def";
    aElement.focus();
    document.documentElement.scrollTop;  // XXX Needs reflow here for working with nsFrameSelection, must be a bug.
    aElement.setSelectionRange("abc d".length, "abc d".length);
    cancelBeforeInput = false;
    beforeInputEvent = null;
    inputEvent = null;
    action = "removing characters backward to start of line";
    SpecialPowers.doCommand(window, "cmd_deleteToBeginningOfLine");
    is(aElement.value, "ef", `${aDescription}characters between start of line and caret should've been removed by ${action}`);
    ok(beforeInputEvent, `${aDescription}"beforeinput" event should've been fired at ${action}`);
    is(beforeInputEvent.cancelable, true, `${aDescription}"beforeinput" event for ${action} should be cancelable`);
    is(beforeInputEvent.inputType, "deleteSoftLineBackward",
       `${aDescription}inputType of "beforeinput" event for ${action} should be "deleteSoftLineBackward"`);
    is(beforeInputEvent.data, null, `${aDescription}data of "beforeinput" event for ${action} should be null`);
    is(beforeInputEvent.dataTransfer, null, `${aDescription}dataTransfer of "beforeinput" event for ${action} should be null`);
    is(beforeInputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "beforeinput" event by ${action} should return empty array`);
    ok(inputEvent, `${aDescription}"input" event should've been fired at ${action}`);
    is(inputEvent.inputType, "deleteSoftLineBackward", `${aDescription}inputType of "input" event for ${action} should be "deleteSoftLineBackward"`);
    is(inputEvent.data, null, `${aDescription}data of "input" event for ${action} should be null`);
    is(inputEvent.dataTransfer, null, `${aDescription}dataTransfer of "input" event for ${action} should be null`);
    is(inputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "input" event by ${action} should return empty array`);

    aElement.value = "abc def";
    aElement.focus();
    document.documentElement.scrollTop;  // XXX Needs reflow here for working with nsFrameSelection, must be a bug.
    aElement.setSelectionRange("ab".length, "ab".length);
    cancelBeforeInput = false;
    beforeInputEvent = null;
    inputEvent = null;
    action = "removing characters forward to end of line";
    SpecialPowers.doCommand(window, "cmd_deleteToEndOfLine");
    is(aElement.value, "ab", `${aDescription}characters between caret and end of line should've been removed by ${action}`);
    ok(beforeInputEvent, `${aDescription}"beforeinput" event should've been fired at ${action}`);
    is(beforeInputEvent.cancelable, true, `${aDescription}"beforeinput" event for ${action} should be cancelable`);
    is(beforeInputEvent.inputType, "deleteSoftLineForward",
       `${aDescription}inputType of "beforeinput" event for ${action} should be "deleteSoftLineForward"`);
    is(beforeInputEvent.data, null, `${aDescription}data of "beforeinput" event for ${action} should be null`);
    is(beforeInputEvent.dataTransfer, null, `${aDescription}dataTransfer of "beforeinput" event for ${action} should be null`);
    is(beforeInputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "beforeinput" event by ${action} should return empty array`);
    ok(inputEvent, `${aDescription}"input" event should've been fired at ${action}`);
    is(inputEvent.inputType, "deleteSoftLineForward", `${aDescription}inputType of "input" event for ${action} should be "deleteSoftLineForward"`);
    is(inputEvent.data, null, `${aDescription}data of "input" event for ${action} should be null`);
    is(inputEvent.dataTransfer, null, `${aDescription}dataTransfer of "input" event for ${action} should be null`);
    is(inputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "input" event by ${action} should return empty array`);

    // Delete to previous/next visual line boundary with non-collapsed selection.
    aElement.value = "abc def";
    aElement.focus();
    document.documentElement.scrollTop;  // XXX Needs reflow here for working with nsFrameSelection, must be a bug.
    aElement.setSelectionRange("abc d".length, "abc_de".length);
    cancelBeforeInput = false;
    beforeInputEvent = null;
    inputEvent = null;
    action = "removing characters backward to start of line (with selection in second word)";
    SpecialPowers.doCommand(window, "cmd_deleteToBeginningOfLine");
    // Only on Windows, we collapse selection to start before handling this command.
    expectedInputType = kIsWin ? "deleteSoftLineBackward" : "deleteContentBackward";
    is(aElement.value, kIsWin ? "ef" : "abc df",
      `${aDescription}${kIsWin ? "characters between start of line and caret" : "selected characters"} should've been removed by ${action}`);
    ok(beforeInputEvent, `${aDescription}"beforeinput" event should've been fired at ${action}`);
    is(beforeInputEvent.cancelable, true, `${aDescription}"beforeinput" event for ${action} should be cancelable`);
    is(beforeInputEvent.inputType, expectedInputType, `${aDescription}inputType of "beforeinput" event for ${action} should be "${expectedInputType}"`);
    is(beforeInputEvent.data, null, `${aDescription}data of "beforeinput" event for ${action} should be null`);
    is(beforeInputEvent.dataTransfer, null, `${aDescription}dataTransfer of "beforeinput" event for ${action} should be null`);
    is(beforeInputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "beforeinput" event by ${action} should return empty array`);
    ok(inputEvent, `${aDescription}"input" event should've been fired at ${action}`);
    is(inputEvent.inputType, expectedInputType, `${aDescription}inputType of "input" event for ${action} should be "${expectedInputType}"`);
    is(inputEvent.data, null, `${aDescription}data of "input" event for ${action} should be null`);
    is(inputEvent.dataTransfer, null, `${aDescription}dataTransfer of "input" event for ${action} should be null`);
    is(inputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "input" event by ${action} should return empty array`);

    aElement.value = "abc def";
    aElement.focus();
    document.documentElement.scrollTop;  // XXX Needs reflow here for working with nsFrameSelection, must be a bug.
    aElement.setSelectionRange("a".length, "ab".length);
    cancelBeforeInput = false;
    beforeInputEvent = null;
    inputEvent = null;
    action = "removing characters forward to end of line (with selection in second word)";
    SpecialPowers.doCommand(window, "cmd_deleteToEndOfLine");
    // Only on Windows, we collapse selection to start before handling this command.
    expectedInputType = kIsWin ? "deleteSoftLineForward" : "deleteContentForward";
    is(aElement.value, kIsWin ? "a" : "ac def",
      `${aDescription}${kIsWin ? "characters between caret anc end of line" : "selected characters"} should've been removed by ${action}`);
    ok(beforeInputEvent, `${aDescription}"beforeinput" event should've been fired at ${action}`);
    is(beforeInputEvent.cancelable, true, `${aDescription}"beforeinput" event for ${action} should be cancelable`);
    is(beforeInputEvent.inputType, expectedInputType, `${aDescription}inputType of "beforeinput" event for ${action} should be "${expectedInputType}"`);
    is(beforeInputEvent.data, null, `${aDescription}data of "beforeinput" event for ${action} should be null`);
    is(beforeInputEvent.dataTransfer, null, `${aDescription}dataTransfer of "beforeinput" event for ${action} should be null`);
    is(beforeInputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "beforeinput" event by ${action} should return empty array`);
    ok(inputEvent, `${aDescription}"input" event should've been fired at ${action}`);
    is(inputEvent.inputType, expectedInputType, `${aDescription}inputType of "input" event for ${action} should be "${expectedInputType}"`);
    is(inputEvent.data, null, `${aDescription}data of "input" event for ${action} should be null`);
    is(inputEvent.dataTransfer, null, `${aDescription}dataTransfer of "input" event for ${action} should be null`);
    is(inputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "input" event by ${action} should return empty array`);

    // Toggling text direction
    aElement.focus();
    cancelBeforeInput = true;
    beforeInputEvent = null;
    inputEvent = null;
    action = 'switching text direction from "ltr" to "rtl"';
    SpecialPowers.doCommand(window, "cmd_switchTextDirection");
    is(aElement.getAttribute("dir"), null, `${aDescription}dir attribute of the element should not be set" by ${action} since "beforeinput" was canceled`);
    ok(beforeInputEvent, `${aDescription}"beforeinput" event should've been fired at ${action}`);
    is(beforeInputEvent.cancelable, true, `${aDescription}"beforeinput" event for ${action} should be cancelable`);
    is(beforeInputEvent.inputType, "formatSetBlockTextDirection",
       `${aDescription}inputType of "beforeinput" event for ${action} should be "formatSetBlockTextDirection"`);
    is(beforeInputEvent.data, "rtl", `${aDescription}data of "beforeinput" event for ${action} should be "rtl"`);
    is(beforeInputEvent.dataTransfer, null, `${aDescription}dataTransfer of "beforeinput" event for ${action} should be null`);
    is(beforeInputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "beforeinput" event by ${action} should return empty array`);
    ok(!inputEvent, `${aDescription}"input" event should not have been fired at ${action} since "beforeinput" was canceled`);

    aElement.setAttribute("dir", "rtl");
    aElement.scrollTop; // XXX Update the root frame
    aElement.focus();
    cancelBeforeInput = true;
    beforeInputEvent = null;
    inputEvent = null;
    action = 'switching text direction from "rtl" to "ltr"';
    SpecialPowers.doCommand(window, "cmd_switchTextDirection");
    is(aElement.getAttribute("dir"), "rtl",
       `${aDescription}dir attribute of the element should not have been modified by ${action} since "beforeinput" was canceled`);
    ok(beforeInputEvent, `${aDescription}"beforeinput" event should've been fired at ${action}`);
    is(beforeInputEvent.cancelable, true, `${aDescription}"beforeinput" event for ${action} should be cancelable`);
    is(beforeInputEvent.inputType, "formatSetBlockTextDirection",
       `${aDescription}inputType of "beforeinput" event for ${action} should be "formatSetBlockTextDirection"`);
    is(beforeInputEvent.data, "ltr", `${aDescription}data of "beforeinput" event for ${action} should be "ltr"`);
    is(beforeInputEvent.dataTransfer, null, `${aDescription}dataTransfer of "beforeinput" event for ${action} should be null`);
    is(beforeInputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "beforeinput" event by ${action} should return empty array`);
    ok(!inputEvent, `${aDescription}"input" event should not have been fired at ${action} since "beforeinput" was canceled`);

    aElement.removeAttribute("dir");
    aElement.scrollTop; // XXX Update the root frame
    aElement.focus();
    cancelBeforeInput = false;
    beforeInputEvent = null;
    inputEvent = null;
    action = 'switching text direction from "ltr" to "rtl"';
    SpecialPowers.doCommand(window, "cmd_switchTextDirection");
    is(aElement.getAttribute("dir"), "rtl", `${aDescription}dir attribute of the element should've been set to "rtl" by ${action}`);
    ok(beforeInputEvent, `${aDescription}"beforeinput" event should've been fired at ${action}`);
    is(beforeInputEvent.cancelable, true, `${aDescription}"beforeinput" event for ${action} should be cancelable`);
    is(beforeInputEvent.inputType, "formatSetBlockTextDirection",
       `${aDescription}inputType of "beforeinput" event for ${action} should be "formatSetBlockTextDirection"`);
    is(beforeInputEvent.data, "rtl", `${aDescription}data of "beforeinput" event for ${action} should be "rtl"`);
    is(beforeInputEvent.dataTransfer, null, `${aDescription}dataTransfer of "beforeinput" event for ${action} should be null`);
    is(beforeInputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "beforeinput" event by ${action} should return empty array`);
    ok(inputEvent, `${aDescription}"input" event should've been fired at ${action}`);
    is(inputEvent.inputType, "formatSetBlockTextDirection", `${aDescription}inputType of "input" event for ${action} should be "formatSetBlockTextDirection"`);
    is(inputEvent.data, "rtl", `${aDescription}data of "input" event for ${action} should be "rtl"`);
    is(inputEvent.dataTransfer, null, `${aDescription}dataTransfer of "input" event for ${action} should be null`);
    is(inputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "input" event by ${action} should return empty array`);

    aElement.focus();
    cancelBeforeInput = false;
    beforeInputEvent = null;
    inputEvent = null;
    action = 'switching text direction from "rtl" to "ltr"';
    SpecialPowers.doCommand(window, "cmd_switchTextDirection");
    is(aElement.getAttribute("dir"), "ltr", `${aDescription}dir attribute of the element should've been set to "ltr" by ${action}`);
    ok(beforeInputEvent, `${aDescription}"beforeinput" event should've been fired at ${action}`);
    is(beforeInputEvent.cancelable, true, `${aDescription}"beforeinput" event for ${action} should be cancelable`);
    is(beforeInputEvent.inputType, "formatSetBlockTextDirection",
       `${aDescription}inputType of "beforeinput" event for ${action} should be "formatSetBlockTextDirection"`);
    is(beforeInputEvent.data, "ltr", `${aDescription}data of "beforeinput" event for ${action} should be "ltr"`);
    is(beforeInputEvent.dataTransfer, null, `${aDescription}dataTransfer of "beforeinput" event for ${action} should be null`);
    is(beforeInputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "beforeinput" event by ${action} should return empty array`);
    ok(inputEvent, `${aDescription}"input" event should've been fired at ${action}`);
    is(inputEvent.inputType, "formatSetBlockTextDirection", `${aDescription}inputType of "input" event for ${action} should be "formatSetBlockTextDirection"`);
    is(inputEvent.data, "ltr", `${aDescription}data of "input" event for ${action} should be "ltr"`);
    is(inputEvent.dataTransfer, null, `${aDescription}dataTransfer of "input" event for ${action} should be null`);
    is(inputEvent.getTargetRanges().length, 0, `${aDescription}getTargetRanges() of "input" event by ${action} should return empty array`);

    aElement.removeEventListener("beforeinput", beforeInputHandler, true);
    aElement.removeEventListener("input", inputHandler, true);
  }

  doTests(document.getElementById("input"), "<input type=\"text\">", false);
  doTests(document.getElementById("textarea"), "<textarea>", true);

  SimpleTest.finish();
}

</script>
</body>

</html>