tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_bug1644511.html (4073B)


      1 <!DOCTYPE html>
      2 <meta charset="utf-8">
      3 <title>Test for Bug 1644511</title>
      4 <script src="/tests/SimpleTest/SimpleTest.js"></script>
      5 <script src="/tests/SimpleTest/EventUtils.js"></script>
      6 <link rel="stylesheet" href="/tests/SimpleTest/test.css">
      7 <style>
      8  [contenteditable] {
      9    padding: .5em 40%;
     10  }
     11 </style>
     12 
     13 <div id="host" contenteditable="" dir="rtl">مرحبا عالم!<br>Hello World</div>
     14 
     15 <button onclick="subtestLTR()"></button>
     16 
     17 <script>
     18 const caretMovementStyleFlag = "bidi.edit.caret_movement_style";
     19 
     20 /**
     21 * Can't use synthesizeKey("KEY_Arrow*") as it triggers
     22 * nsFrameSelection::PhysicalMove() instead of CharacterMove() and thus
     23 * suppresses the flag. See also Bug 1644489.
     24 */
     25 function moveCaret(aRight, aSelect) {
     26  const select = aSelect ? "selectChar" : "char";
     27  const dir = aRight ? "Next" : "Previous";
     28  SpecialPowers.doCommand(window, `cmd_${select}${dir}`);
     29 }
     30 
     31 /**
     32 * Safer to directly select text node when the paragraph ends with LTR text as
     33 * the left edge is the end of the paragraph and then also the start of the LTR
     34 * text.
     35 */
     36 function putCaretInLastLine(div) {
     37  const { lastChild } = div;
     38  getSelection().collapse(lastChild, 1); // Pass 1 because of bug 1645877
     39 }
     40 
     41 // LTR behavior should be always same regardless of the flag
     42 function subtestLTR() {
     43  putCaretInLastLine(host);
     44  moveCaret(true, true);
     45  is(getSelection().anchorOffset, 1, "Shift+ArrowRight should select from left");
     46  is(getSelection().focusOffset, 2, "Shift+ArrowRight should select to right");
     47  moveCaret(true, false);
     48  is(getSelection().anchorOffset, 2, "Collapsing by ArrowRight should put the caret to the right side");
     49 
     50  putCaretInLastLine(host);
     51  moveCaret(true, true);
     52  moveCaret(false, false);
     53  is(getSelection().anchorOffset, 1, "Collapsing by ArrowLeft should put the caret to the left side");
     54 }
     55 
     56 async function testLogicalMovement() {
     57  await SpecialPowers.pushPrefEnv({
     58    set: [[caretMovementStyleFlag, 0]]
     59  });
     60  getSelection().collapse(host);
     61  moveCaret(true, true);
     62  is(getSelection().anchorOffset, 0, "Shift+ArrowRight should select from right");
     63  is(getSelection().focusOffset, 1, "Shift+ArrowRight should select to left");
     64  moveCaret(true, false);
     65  is(getSelection().anchorOffset, 1, "Collapsing by ArrowRight should put the caret to the left side");
     66 
     67  getSelection().collapse(host);
     68  moveCaret(true, true);
     69  moveCaret(false, false);
     70  is(getSelection().anchorOffset, 0, "Collapsing by ArrowLeft should put the caret to the right side");
     71 
     72  subtestLTR();
     73 }
     74 
     75 async function testVisualMovement() {
     76  await SpecialPowers.pushPrefEnv({
     77    set: [[caretMovementStyleFlag, 1]]
     78  });
     79  getSelection().collapse(host);
     80  moveCaret(false, true);
     81  is(getSelection().anchorOffset, 0, "Shift+ArrowLeft should select from right");
     82  is(getSelection().focusOffset, 1, "Shift+ArrowLeft should select to left");
     83  moveCaret(false, false);
     84  is(getSelection().anchorOffset, 1, "Collapsing by ArrowLeft should put the caret to the left side");
     85 
     86  getSelection().collapse(host);
     87  moveCaret(false, true);
     88  moveCaret(true, false);
     89  is(getSelection().anchorOffset, 0, "Collapsing by ArrowRight should put the caret to the right side");
     90 
     91  subtestLTR();
     92 }
     93 
     94 async function testHybridMovement() {
     95  await SpecialPowers.pushPrefEnv({
     96    set: [[caretMovementStyleFlag, 2]]
     97  });
     98  getSelection().collapse(host);
     99  moveCaret(true, true);
    100  is(getSelection().anchorOffset, 0, "Shift+ArrowRight should select from right");
    101  is(getSelection().focusOffset, 1, "Shift+ArrowRight should select to left");
    102  moveCaret(false, false);
    103  is(getSelection().anchorOffset, 1, "Collapsing by ArrowLeft should put the caret to the left side");
    104 
    105  getSelection().collapse(host);
    106  moveCaret(true, true);
    107  moveCaret(true, false);
    108  is(getSelection().anchorOffset, 0, "Collapsing by ArrowRight should put the caret to the right side");
    109 
    110  subtestLTR();
    111 }
    112 
    113 SimpleTest.waitForExplicitFinish();
    114 SimpleTest.promiseFocus().then(async () => {
    115  await testLogicalMovement();
    116  await testVisualMovement();
    117  await testHybridMovement();
    118  SimpleTest.finish();
    119 });
    120 </script>