tor-browser

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

test_nsIEditor_beginningOfDocument.html (7231B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <head>
      4  <meta charset="utf-8">
      5  <title>Tests of nsIEditor#beginningOfDocument()</title>
      6  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      7  <link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
      8  <script>
      9    SimpleTest.waitForExplicitFinish();
     10    SimpleTest.waitForFocus(() => {
     11      const originalBody = document.body.innerHTML;
     12 
     13      (function test_with_text_editor() {
     14        for (const test of [
     15            {
     16              tag: "input",
     17              innerHTML: "<input>",
     18            },
     19            {
     20              tag: "textarea",
     21              innerHTML: "<textarea></textarea>",
     22            },
     23          ]) {
     24            document.body.innerHTML = test.innerHTML;
     25            const textControl = document.body.querySelector(test.tag);
     26            const editor = SpecialPowers.wrap(textControl).editor;
     27            editor.beginningOfDocument();
     28            is(textControl.selectionStart, 0,
     29              `nsIEditor.beginningOfDocument() should set selectionStart of empty <${test.tag}> to 0`);
     30            is(textControl.selectionEnd, 0,
     31              `nsIEditor.beginningOfDocument() should set selectionEnd of empty <${test.tag}> to 0`);
     32            textControl.value = "abc";
     33            textControl.selectionStart = 2;
     34            textControl.selectionEnd = 3;
     35            editor.beginningOfDocument();
     36            is(textControl.selectionStart, 0,
     37              `nsIEditor.beginningOfDocument() should set selectionStart of non-empty <${test.tag}> to 0`);
     38            is(textControl.selectionEnd, 0,
     39              `nsIEditor.beginningOfDocument() should set selectionEnd of non-empty <${test.tag}> to 0`);
     40          }
     41      })();
     42 
     43      function getHTMLEditor() {
     44        const editingSession = SpecialPowers.wrap(window).docShell.editingSession;
     45        if (!editingSession) {
     46          return null;
     47        }
     48        return editingSession.getEditorForWindow(window);
     49      }
     50 
     51      (function test_with_contenteditable() {
     52        document.body.innerHTML = "<div contenteditable><p>abc</p></div>";
     53        getSelection().removeAllRanges();
     54        getHTMLEditor().beginningOfDocument();
     55        is(getSelection().rangeCount, 0,
     56          "selection shouldn't be changed when there is no selection");
     57        getSelection().setBaseAndExtent(document.body.querySelector("p").firstChild, 1,
     58                                        document.body.querySelector("p").firstChild, 3);
     59        getHTMLEditor().beginningOfDocument();
     60        is(getSelection().isCollapsed, true,
     61          "selection should be collapsed after calling nsIEditor.beginningOfDocument() with contenteditable having one paragraph");
     62        is(getSelection().rangeCount, 1,
     63          "selection should has only one range after calling nsIEditor.beginningOfDocument() with contenteditable having one paragraph");
     64        is(getSelection().focusNode, document.body.querySelector("p").firstChild,
     65          "selection should be collapsed into the text node after calling nsIEditor.beggingOfDocument() with content editable having one paragraph");
     66        is(getSelection().focusOffset, 0,
     67          "selection should be collapsed to start of the text node after calling nsIEditor.beggingOfDocument() with content editable having one paragraph");
     68 
     69        document.body.innerHTML = "<div contenteditable><p contenteditable=\"false\">abc</p><p>def</p></div>";
     70        getSelection().setBaseAndExtent(document.body.querySelector("p + p").firstChild, 1,
     71                                        document.body.querySelector("p + p").firstChild, 3);
     72        getHTMLEditor().beginningOfDocument();
     73        is(getSelection().isCollapsed, true,
     74          "selection should be collapsed after calling nsIEditor.beginningOfDocument() with contenteditable having non-editable paragraph first");
     75        is(getSelection().rangeCount, 1,
     76          "selection should has only one range after calling nsIEditor.beginningOfDocument() with contenteditable having non-editable paragraph first");
     77        is(getSelection().focusNode, document.body.querySelector("div[contenteditable]"),
     78          "selection should be collapsed to start of the editing host after calling nsIEditor.beggingOfDocument() with content editable having non-editable paragraph first");
     79        is(getSelection().focusOffset, 0,
     80          "selection should be collapsed to start of the editing host after calling nsIEditor.beggingOfDocument() with content editable having non-editable paragraph first");
     81 
     82        document.body.innerHTML = "<div contenteditable>\n<p contenteditable=\"false\">abc</p>\n<p>def</p>\n</div>";
     83        getSelection().setBaseAndExtent(document.body.querySelector("p + p").firstChild, 1,
     84                                        document.body.querySelector("p + p").firstChild, 3);
     85        getHTMLEditor().beginningOfDocument();
     86        is(getSelection().isCollapsed, true,
     87          "selection should be collapsed after calling nsIEditor.beginningOfDocument() with contenteditable having non-editable paragraph first and some invisible line breaks");
     88        is(getSelection().rangeCount, 1,
     89          "selection should has only one range after calling nsIEditor.beginningOfDocument() with contenteditable having non-editable paragraph first and some invisible line breaks");
     90        is(getSelection().focusNode, document.body.querySelector("div[contenteditable]"),
     91          "selection should be collapsed to start of the editing host after calling nsIEditor.beggingOfDocument() with content editable having non-editable paragraph first and some invisible line breaks");
     92        is(getSelection().focusOffset, 0,
     93          "selection should be collapsed to start of the editing host after calling nsIEditor.beggingOfDocument() with content editable having non-editable paragraph first and some invisible line breaks");
     94 
     95        document.body.innerHTML = "<div contenteditable><p>abc</p></div>def<div contenteditable><p>ghi</p></div>";
     96        getSelection().setBaseAndExtent(document.body.querySelector("div + div > p").firstChild, 1,
     97                                        document.body.querySelector("div + div > p").firstChild, 3);
     98        getHTMLEditor().beginningOfDocument();
     99        is(getSelection().isCollapsed, true,
    100          "selection should be collapsed after calling nsIEditor.beginningOfDocument() with the 2nd contenteditable");
    101        is(getSelection().rangeCount, 1,
    102          "selection should has only one range after calling nsIEditor.beginningOfDocument() with the 2nd contenteditable");
    103        is(getSelection().focusNode, document.body.querySelector("div + div > p").firstChild,
    104          "selection should be collapsed to start of the first text node in the second editing host after calling nsIEditor.beggingOfDocument() with the 2nd contenteditable");
    105        is(getSelection().focusOffset, 0,
    106          "selection should be collapsed to start of the first text node in the second editing host after calling nsIEditor.beggingOfDocument() with the 2nd contenteditable");
    107      })();
    108 
    109      document.body.innerHTML = originalBody;
    110      SimpleTest.finish();
    111    });
    112  </script>
    113 </head>
    114 <body>
    115 <p id="display"></p>
    116 <div id="content" style="display: none"></div>
    117 <pre id="test"></pre>
    118 </body>
    119 </html>