tor-browser

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

test_nsIHTMLEditor_selectElement.html (6185B)


      1 <!DOCTYPE>
      2 <html>
      3 <head>
      4  <title>Test for nsIHTMLEditor.selectElement()</title>
      5  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      6  <link rel="stylesheet" href="/tests/SimpleTest/test.css">
      7 </head>
      8 <body>
      9 <div id="display">
     10 </div>
     11 <div id="content" contenteditable></div>
     12 <pre id="test">
     13 </pre>
     14 
     15 <script class="testbody" type="application/javascript">
     16 
     17 SimpleTest.waitForExplicitFinish();
     18 SimpleTest.waitForFocus(function() {
     19  let editor = document.getElementById("content");
     20  let selection = window.getSelection();
     21 
     22  editor.innerHTML = "<p>p1<b>b1</b><i>i1</i></p><p><b>b2</b><i>i2</i>p2</p>";
     23 
     24  editor.focus();
     25  try {
     26    getHTMLEditor().selectElement(editor.firstChild.firstChild);
     27    ok(false, "nsIHTMLEditor.selectElement() should throw an exception if given node is not an element");
     28  } catch (e) {
     29    ok(true, `nsIHTMLEditor.selectElement() should throw an exception if given node is not an element: ${e}`);
     30  }
     31 
     32  editor.focus();
     33  try {
     34    getHTMLEditor().selectElement(editor.firstChild.firstChild.nextSibling);
     35    is(selection.anchorNode, editor.firstChild,
     36       "nsIHTMLEditor.selectElement() should set anchor node to parent of <b> element in the first paragraph");
     37    is(selection.anchorOffset, 1,
     38       "nsIHTMLEditor.selectElement() should set anchor offset to the index of <b> element in the first paragraph");
     39    is(selection.focusNode, editor.firstChild,
     40       "nsIHTMLEditor.selectElement() should set focus node to parent of <b> element in the first paragraph");
     41    is(selection.focusOffset, 2,
     42       "nsIHTMLEditor.selectElement() should set anchor offset to the index of <b> element + 1 in the first paragraph");
     43  } catch (e) {
     44    ok(false, `nsIHTMLEditor.selectElement() shouldn't throw exception when selecting an element in focused editor #1: ${e}`);
     45  }
     46 
     47  editor.focus();
     48  try {
     49    getHTMLEditor().selectElement(editor.firstChild.nextSibling.firstChild);
     50    is(selection.anchorNode, editor.firstChild.nextSibling,
     51       "nsIHTMLEditor.selectElement() should set anchor node to parent of <b> element in the second paragraph");
     52    is(selection.anchorOffset, 0,
     53       "nsIHTMLEditor.selectElement() should set anchor offset to the index of <b> element in the second paragraph");
     54    is(selection.focusNode, editor.firstChild.nextSibling,
     55       "nsIHTMLEditor.selectElement() should set focus node to parent of <b> element in the second paragraph");
     56    is(selection.focusOffset, 1,
     57       "nsIHTMLEditor.selectElement() should set anchor offset to the index of <b> element + 1 in the second paragraph");
     58  } catch (e) {
     59    ok(false, `nsIHTMLEditor.selectElement() shouldn't throw exception when selecting an element in focused editor #2: ${e}`);
     60  }
     61 
     62  editor.focus();
     63  try {
     64    getHTMLEditor().selectElement(editor);
     65    ok(false, "nsIHTMLEditor.selectElement() should throw an exception if given node is the editing host");
     66  } catch (e) {
     67    ok(true, `nsIHTMLEditor.selectElement() should throw an exception if given node is the editing host: ${e}`);
     68  }
     69 
     70  editor.focus();
     71  try {
     72    getHTMLEditor().selectElement(editor.parentElement);
     73    ok(false, "nsIHTMLEditor.selectElement() should throw an exception if given node is outside of the editing host");
     74  } catch (e) {
     75    ok(true, `nsIHTMLEditor.selectElement() should throw an exception if given node is outside of the editing host: ${e}`);
     76  }
     77 
     78  selection.removeAllRanges();
     79  editor.blur();
     80  try {
     81    getHTMLEditor().selectElement(editor.firstChild.nextSibling.firstChild);
     82    ok(false, "nsIHTMLEditor.selectElement() should throw an exception if there is no active editing host");
     83  } catch (e) {
     84    ok(true, `nsIHTMLEditor.selectElement() should throw an exception if there is no active editing host: ${e}`);
     85  }
     86 
     87  editor.focus();
     88  editor.firstChild.firstChild.nextSibling.nextSibling.setAttribute("contenteditable", "false");
     89  try {
     90    getHTMLEditor().selectElement(editor.firstChild.firstChild.nextSibling.nextSibling);
     91    is(selection.anchorNode, editor.firstChild,
     92       "nsIHTMLEditor.selectElement() should set anchor node to parent of <i contenteditable=\"false\"> element in the first paragraph");
     93    is(selection.anchorOffset, 2,
     94       "nsIHTMLEditor.selectElement() should set anchor offset to the index of <i contenteditable=\"false\"> element in the first paragraph");
     95    is(selection.focusNode, editor.firstChild,
     96       "nsIHTMLEditor.selectElement() should set focus node to parent of <i contenteditable=\"false\"> element in the first paragraph");
     97    is(selection.focusOffset, 3,
     98       "nsIHTMLEditor.selectElement() should set anchor offset to the index of <i contenteditable=\"false\"> element + 1 in the first paragraph");
     99  } catch (e) {
    100    ok(false, `nsIHTMLEditor.selectElement() shouldn't throw exception when selecting an element in focused editor #3: ${e}`);
    101  }
    102 
    103  editor.focus();
    104  editor.firstChild.nextSibling.setAttribute("contenteditable", "false");
    105  try {
    106    getHTMLEditor().selectElement(editor.firstChild.nextSibling.firstChild.nextSibling);
    107    is(selection.anchorNode, editor.firstChild.nextSibling,
    108       "nsIHTMLEditor.selectElement() should set anchor node to parent of <i> element in the second paragraph which is not editable");
    109    is(selection.anchorOffset, 1,
    110       "nsIHTMLEditor.selectElement() should set anchor offset to the index of <i> element in the second paragraph which is not editable");
    111    is(selection.focusNode, editor.firstChild.nextSibling,
    112       "nsIHTMLEditor.selectElement() should set focus node to parent of <i> element in the second paragraph which is not editable");
    113    is(selection.focusOffset, 2,
    114       "nsIHTMLEditor.selectElement() should set anchor offset to the index of <i> element + 1 in the second paragraph which is not editable");
    115  } catch (e) {
    116    ok(false, `nsIHTMLEditor.selectElement() shouldn't throw exception when selecting an element in focused editor #4: ${e}`);
    117  }
    118 
    119  SimpleTest.finish();
    120 });
    121 
    122 function getHTMLEditor() {
    123  var Ci = SpecialPowers.Ci;
    124  var editingSession = SpecialPowers.wrap(window).docShell.editingSession;
    125  return editingSession.getEditorForWindow(window).QueryInterface(Ci.nsIHTMLEditor);
    126 }
    127 
    128 </script>
    129 </body>
    130 
    131 </html>