tor-browser

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

Range_collapse.html (13281B)


      1 <!doctype html>
      2 <meta charset=utf-8>
      3 <title>focus move tests caused by a call of Range.collapse()</title>
      4 <script src="/resources/testharness.js"></script>
      5 <script src="/resources/testharnessreport.js"></script>
      6 <body>
      7 <div style="height: 3000px;">Spacer to check whether or not page was scrolled down to focused editor</div>
      8 <p id="staticBefore">static text</p>
      9 <div id="editor" contenteditable><p>content of editor</p></div>
     10 <div id="outerEditor" contenteditable
     11 ><p>content of outer editor</p><div id="staticInEditor" contenteditable="false"
     12 ><p>static content of outer editor</p><div id="innerEditor" contenteditable
     13 ><p>content of inner editor</p></div></div></div>
     14 <p id="staticAfter">static text</p>
     15 <p><a id="anchor" href="about:blank">anchor</a></p>
     16 <script>
     17 "use strict";
     18 
     19 var staticBefore = {
     20    element: document.getElementById("staticBefore"),
     21    textNode: document.getElementById("staticBefore").firstChild,
     22    textLength: document.getElementById("staticBefore").firstChild.length
     23 };
     24 var editor = {
     25    element: document.getElementById("editor"),
     26    textNode: document.getElementById("editor").firstChild.firstChild,
     27    textLength: document.getElementById("editor").firstChild.firstChild.length
     28 };
     29 var outerEditor = {
     30    element: document.getElementById("outerEditor"),
     31    textNode: document.getElementById("outerEditor").firstChild.firstChild,
     32    textLength: document.getElementById("outerEditor").firstChild.firstChild.length
     33 };
     34 var staticInEditor = {
     35    element: document.getElementById("staticInEditor"),
     36    textNode: document.getElementById("staticInEditor").firstChild,
     37    textLength: document.getElementById("staticInEditor").firstChild.length
     38 };
     39 var innerEditor = {
     40    element: document.getElementById("innerEditor"),
     41    textNode: document.getElementById("innerEditor").firstChild.firstChild,
     42    textLength: document.getElementById("innerEditor").firstChild.firstChild.length
     43 };
     44 var staticAfter = {
     45    element: document.getElementById("staticAfter"),
     46    textNode: document.getElementById("staticAfter").firstChild,
     47    textLength: document.getElementById("staticAfter").firstChild.length
     48 };
     49 var anchor = {
     50    element: document.getElementById("anchor"),
     51    textNode: document.getElementById("anchor").firstChild,
     52    textLength: document.getElementById("anchor").firstChild.length
     53 };
     54 
     55 function resetFocusAndSelectionRange(aFocus)
     56 {
     57    document.getSelection().removeAllRanges();
     58    if (document.activeElement) {
     59        document.activeElement.blur();
     60    }
     61    if (aFocus) {
     62        aFocus.element.focus();
     63        document.getSelection().collapse(aFocus.textNode, 0);
     64    } else {
     65        document.getSelection().collapse(staticBefore.textNode, 0);
     66    }
     67    document.documentElement.scrollTop = 0;
     68 }
     69 
     70 test(function() {
     71    resetFocusAndSelectionRange();
     72    document.getSelection().setBaseAndExtent(anchor.textNode, 0,
     73                                             anchor.textNode, anchor.textLength);
     74    document.getSelection().getRangeAt(0).collapse(true);
     75    assert_equals(document.activeElement, document.body);
     76    assert_equals(document.documentElement.scrollTop, 0);
     77 }, "Active element should be the <body> after Range.collapse(true) of selection between start of the first text node of 'anchor' and end of the first text node of 'anchor' when active element is the <body>");
     78 test(function() {
     79    resetFocusAndSelectionRange();
     80    document.getSelection().setBaseAndExtent(anchor.textNode, 0,
     81                                             anchor.textNode, anchor.textLength);
     82    document.getSelection().getRangeAt(0).collapse(false);
     83    assert_equals(document.activeElement, document.body);
     84    assert_equals(document.documentElement.scrollTop, 0);
     85 }, "Active element should be the <body> after Range.collapse(false) of selection between start of the first text node of 'anchor' and end of the first text node of 'anchor' when active element is the <body>");
     86 test(function() {
     87    resetFocusAndSelectionRange();
     88    document.getSelection().setBaseAndExtent(staticBefore.textNode, 0,
     89                                             anchor.textNode, anchor.textLength);
     90    document.getSelection().getRangeAt(0).collapse(false);
     91    assert_equals(document.activeElement, document.body);
     92    assert_equals(document.documentElement.scrollTop, 0);
     93 }, "Active element should be the <body> after Range.collapse(false) of selection between start of the first text node of 'staticBefore' and end of the first text node of 'anchor' when active element is the <body>");
     94 test(function() {
     95    resetFocusAndSelectionRange(editor);
     96    document.getSelection().setBaseAndExtent(editor.textNode, 0,
     97                                             editor.textNode, editor.textLength);
     98    document.getSelection().getRangeAt(0).collapse(false);
     99    assert_equals(document.activeElement, editor.element);
    100    assert_equals(document.documentElement.scrollTop, 0);
    101 }, "Active element should be 'editor' after Range.collapse(false) of selection between start of the first text node of 'editor' and end of the first text node of 'editor' when active element is 'editor'");
    102 test(function() {
    103    resetFocusAndSelectionRange(editor);
    104    document.getSelection().setBaseAndExtent(editor.textNode, 0,
    105                                             editor.textNode, editor.textLength);
    106    document.getSelection().getRangeAt(0).collapse(true);
    107    assert_equals(document.activeElement, editor.element);
    108    assert_equals(document.documentElement.scrollTop, 0);
    109 }, "Active element should be 'editor' after Range.collapse(true) of selection between start of the first text node of 'editor' and end of the first text node of 'editor' when active element is 'editor'");
    110 test(function() {
    111    resetFocusAndSelectionRange(innerEditor);
    112    document.getSelection().setBaseAndExtent(innerEditor.textNode, 0,
    113                                             innerEditor.textNode, innerEditor.textLength);
    114    document.getSelection().getRangeAt(0).collapse(false);
    115    assert_equals(document.activeElement, innerEditor.element);
    116    assert_equals(document.documentElement.scrollTop, 0);
    117 }, "Active element should be 'innerEditor' after Range.collapse(false) of selection between start of the first text node of 'innerEditor' and end of the first text node of 'innerEditor' when active element is 'innerEditor'");
    118 test(function() {
    119    resetFocusAndSelectionRange(innerEditor);
    120    document.getSelection().setBaseAndExtent(innerEditor.textNode, 0,
    121                                             innerEditor.textNode, innerEditor.textLength);
    122    document.getSelection().getRangeAt(0).collapse(true);
    123    assert_equals(document.activeElement, innerEditor.element);
    124    assert_equals(document.documentElement.scrollTop, 0);
    125 }, "Active element should be 'innerEditor' after Range.collapse(true) of selection between start of the first text node of 'innerEditor' and end of the first text node of 'innerEditor' when active element is 'innerEditor'");
    126 test(function() {
    127    resetFocusAndSelectionRange(anchor);
    128    document.getSelection().setBaseAndExtent(anchor.textNode, 0,
    129                                             anchor.textNode, anchor.textLength);
    130    document.getSelection().getRangeAt(0).collapse(false);
    131    assert_equals(document.activeElement, anchor.element);
    132    assert_equals(document.documentElement.scrollTop, 0);
    133 }, "Active element should be 'anchor' after Range.collapse(false) of selection between start of the first text node of 'anchor' and end of the first text node of 'anchor' when active element is 'anchor'");
    134 test(function() {
    135    resetFocusAndSelectionRange(anchor);
    136    document.getSelection().setBaseAndExtent(anchor.textNode, 0,
    137                                             anchor.textNode, anchor.textLength);
    138    document.getSelection().getRangeAt(0).collapse(true);
    139    assert_equals(document.activeElement, anchor.element);
    140    assert_equals(document.documentElement.scrollTop, 0);
    141 }, "Active element should be 'anchor' after Range.collapse(true) of selection between start of the first text node of 'anchor' and end of the first text node of 'anchor' when active element is 'anchor'");
    142 
    143 test(function() {
    144    resetFocusAndSelectionRange();
    145    document.getSelection().setBaseAndExtent(staticBefore.textNode, 0,
    146                                             editor.textNode, editor.textLength);
    147    document.getSelection().getRangeAt(0).collapse(true);
    148    assert_equals(document.activeElement, document.body);
    149    assert_equals(document.documentElement.scrollTop, 0);
    150 }, "Active element should be the <body> after Range.collapse(true) of selection between start of the first text node of 'staticBefore' and end of the first text node of 'editor' when active element is the <body>");
    151 test(function() {
    152    resetFocusAndSelectionRange();
    153    document.getSelection().setBaseAndExtent(staticBefore.textNode, 0,
    154                                             editor.textNode, editor.textLength);
    155    document.getSelection().getRangeAt(0).collapse(false);
    156    assert_equals(document.activeElement, editor.element);
    157    assert_equals(document.documentElement.scrollTop, 0);
    158 }, "Active element should be 'editor' after Range.collapse(false) of selection between start of the first text node of 'staticBefore' and end of the first text node of 'editor' when active element is the <body>");
    159 test(function() {
    160    resetFocusAndSelectionRange();
    161    document.getSelection().setBaseAndExtent(editor.textNode, 0,
    162                                             outerEditor.textNode, outerEditor.textLength);
    163    document.getSelection().getRangeAt(0).collapse(true);
    164    assert_equals(document.activeElement, editor.element);
    165    assert_equals(document.documentElement.scrollTop, 0);
    166 }, "Active element should be 'editor' after Range.collapse(true) of selection between start of the first text node of 'editor' and end of the first text node of 'outerEditor' when active element is the <body>");
    167 test(function() {
    168    resetFocusAndSelectionRange();
    169    document.getSelection().setBaseAndExtent(editor.textNode, 0,
    170                                             outerEditor.textNode, outerEditor.textLength);
    171    document.getSelection().getRangeAt(0).collapse(false);
    172    assert_equals(document.activeElement, outerEditor.element);
    173    assert_equals(document.documentElement.scrollTop, 0);
    174 }, "Active element should be 'outerEditor' after Range.collapse(false) of selection between start of the first text node of 'editor' and end of the first text node of 'outerEditor' when active element is the <body>");
    175 test(function() {
    176    resetFocusAndSelectionRange(outerEditor);
    177    document.getSelection().setBaseAndExtent(outerEditor.textNode, 0,
    178                                             staticInEditor.textNode, staticInEditor.textLength);
    179    document.getSelection().getRangeAt(0).collapse(true);
    180    assert_equals(document.activeElement, outerEditor.element);
    181    assert_equals(document.documentElement.scrollTop, 0);
    182 }, "Active element should be 'outerEditor' after Range.collapse(true) of selection between start of the first text node of 'outerEditor' and end of the first text node of 'staticInEditor' when active element is 'outerEditor'");
    183 test(function() {
    184    resetFocusAndSelectionRange(outerEditor);
    185    document.getSelection().setBaseAndExtent(outerEditor.textNode, 0,
    186                                             staticInEditor.textNode, staticInEditor.textLength);
    187    document.getSelection().getRangeAt(0).collapse(false);
    188    assert_equals(document.activeElement, outerEditor.element);
    189    assert_equals(document.documentElement.scrollTop, 0);
    190 }, "Active element should be 'outerEditor' after Range.collapse(false) of selection between start of the first text node of 'outerEditor' and end of the first text node of 'staticInEditor' when active element is 'outerEditor'");
    191 test(function() {
    192    resetFocusAndSelectionRange(outerEditor);
    193    document.getSelection().setBaseAndExtent(outerEditor.textNode, 0,
    194                                             innerEditor.textNode, innerEditor.textLength);
    195    document.getSelection().getRangeAt(0).collapse(true);
    196    assert_equals(document.activeElement, outerEditor.element);
    197    assert_equals(document.documentElement.scrollTop, 0);
    198 }, "Active element should be 'outerEditor' after Range.collapse(true) of selection between start of the first text node of 'outerEditor' and end of the first text node of 'innerEditor' when active element is 'outerEditor'");
    199 test(function() {
    200    resetFocusAndSelectionRange(outerEditor);
    201    document.getSelection().setBaseAndExtent(outerEditor.textNode, 0,
    202                                             innerEditor.textNode, innerEditor.textLength);
    203    document.getSelection().getRangeAt(0).collapse(false);
    204    assert_equals(document.activeElement, innerEditor.element);
    205    assert_equals(document.documentElement.scrollTop, 0);
    206 }, "Active element should be 'innerEditor' after Range.collapse(false) of selection between start of the first text node of 'outerEditor' and end of the first text node of 'innerEditor' when active element is 'outerEditor'");
    207 </script>