tor-browser

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

browser_markup_textcontent_edit_02.js (4611B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // Test that using UP/DOWN next to a number when editing a text node does not
      7 // increment or decrement but simply navigates inside the editable field.
      8 
      9 const TEST_URL = URL_ROOT + "doc_markup_edit.html";
     10 const SELECTOR = ".node6";
     11 
     12 add_task(async function () {
     13  const { inspector } = await openInspectorForURL(TEST_URL);
     14 
     15  info("Expanding all nodes");
     16  await inspector.markup.expandAll();
     17  await waitForMultipleChildrenUpdates(inspector);
     18 
     19  let nodeValue = await getFirstChildNodeValue(SELECTOR);
     20  let expectedValue = "line6";
     21  is(nodeValue, expectedValue, "The test node's text content is correct");
     22 
     23  info("Open editable field for .node6");
     24  const container = await focusNode(SELECTOR, inspector);
     25  const field = container.elt.querySelector("pre");
     26  field.focus();
     27  EventUtils.sendKey("return", inspector.panelWin);
     28  const editor = inplaceEditor(field);
     29 
     30  info("Initially, all the input content should be selected");
     31  checkSelectionPositions(editor, 0, expectedValue.length);
     32 
     33  info("Navigate using 'RIGHT': move the caret to the end");
     34  await sendKey("VK_RIGHT", {}, editor, inspector.panelWin);
     35  is(editor.input.value, expectedValue, "Value should not have changed");
     36  checkSelectionPositions(editor, expectedValue.length, expectedValue.length);
     37 
     38  info("Navigate using 'DOWN': no effect, already at the end");
     39  await sendKey("VK_DOWN", {}, editor, inspector.panelWin);
     40  is(editor.input.value, expectedValue, "Value should not have changed");
     41  checkSelectionPositions(editor, expectedValue.length, expectedValue.length);
     42 
     43  info("Navigate using 'UP': move to the start");
     44  await sendKey("VK_UP", {}, editor, inspector.panelWin);
     45  is(editor.input.value, expectedValue, "Value should not have changed");
     46  checkSelectionPositions(editor, 0, 0);
     47 
     48  info("Navigate using 'DOWN': move to the end");
     49  await sendKey("VK_DOWN", {}, editor, inspector.panelWin);
     50  is(editor.input.value, expectedValue, "Value should not have changed");
     51  checkSelectionPositions(editor, expectedValue.length, expectedValue.length);
     52 
     53  info("Type 'b' in the editable field");
     54  await sendKey("b", {}, editor, inspector.panelWin);
     55  expectedValue += "b";
     56  is(editor.input.value, expectedValue, "Value should be updated");
     57 
     58  info("Type 'a' in the editable field");
     59  await sendKey("a", {}, editor, inspector.panelWin);
     60  expectedValue += "a";
     61  is(editor.input.value, expectedValue, "Value should be updated");
     62 
     63  info("Create a new line using shift+RETURN");
     64  await sendKey("VK_RETURN", { shiftKey: true }, editor, inspector.panelWin);
     65  expectedValue += "\n";
     66  is(editor.input.value, expectedValue, "Value should have a new line");
     67  checkSelectionPositions(editor, expectedValue.length, expectedValue.length);
     68 
     69  info("Type '1' in the editable field");
     70  await sendKey("1", {}, editor, inspector.panelWin);
     71  expectedValue += "1";
     72  is(editor.input.value, expectedValue, "Value should be updated");
     73  checkSelectionPositions(editor, expectedValue.length, expectedValue.length);
     74 
     75  info("Navigate using 'UP': move back to the first line");
     76  await sendKey("VK_UP", {}, editor, inspector.panelWin);
     77  is(editor.input.value, expectedValue, "Value should not have changed");
     78  info("Caret should be back on the first line");
     79  checkSelectionPositions(editor, 1, 1);
     80 
     81  info("Commit the new value with RETURN, wait for the markupmutation event");
     82  const onMutated = inspector.once("markupmutation");
     83  await sendKey("VK_RETURN", {}, editor, inspector.panelWin);
     84  await onMutated;
     85 
     86  nodeValue = await getFirstChildNodeValue(SELECTOR);
     87  is(nodeValue, expectedValue, "The test node's text content is correct");
     88 });
     89 
     90 /**
     91 * Check that the editor selection is at the expected positions.
     92 */
     93 function checkSelectionPositions(editor, expectedStart, expectedEnd) {
     94  is(
     95    editor.input.selectionStart,
     96    expectedStart,
     97    "Selection should start at " + expectedStart
     98  );
     99  is(
    100    editor.input.selectionEnd,
    101    expectedEnd,
    102    "Selection should end at " + expectedEnd
    103  );
    104 }
    105 
    106 /**
    107 * Send a key and expect to receive a keypress event on the editor's input.
    108 */
    109 function sendKey(key, options, editor, win) {
    110  return new Promise(resolve => {
    111    info("Adding event listener for down|left|right|back_space|return keys");
    112    editor.input.addEventListener("keypress", function onKeypress() {
    113      if (editor.input) {
    114        editor.input.removeEventListener("keypress", onKeypress);
    115      }
    116      executeSoon(resolve);
    117    });
    118 
    119    EventUtils.synthesizeKey(key, options, win);
    120  });
    121 }