tor-browser

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

test_nsIEditor_documentIsEmpty.html (7590B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <head>
      4  <meta charset="utf-8">
      5  <title>Tests of nsIEditor#documentIsEmpty</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><input value="abc"><input placeholder="abc">',
     18            },
     19            {
     20              tag: "textarea",
     21              innerHTML: '<textarea></textarea><textarea>abc</textarea><textarea placeholder="abc"></textarea>',
     22            },
     23          ]) {
     24            document.body.innerHTML = test.innerHTML;
     25            let textControl = document.body.querySelector(test.tag);
     26            is(SpecialPowers.wrap(textControl).editor.documentIsEmpty, true,
     27              `nsIEditor.documentIsEmpty should be true if value of <${test.tag}> is empty by default`);
     28            textControl.focus();
     29            is(SpecialPowers.wrap(textControl).editor.documentIsEmpty, true,
     30              `nsIEditor.documentIsEmpty should be true if value of <${test.tag}> is empty by default after getting focus`);
     31            textControl.value = "abc";
     32            is(SpecialPowers.wrap(textControl).editor.documentIsEmpty, false,
     33              `nsIEditor.documentIsEmpty should be false if <${test.tag}>.value is set to non-empty string`);
     34            textControl.value = "";
     35            is(SpecialPowers.wrap(textControl).editor.documentIsEmpty, true,
     36              `nsIEditor.documentIsEmpty should be true if <${test.tag}>.value is set to empty string`);
     37 
     38            textControl = textControl.nextSibling;
     39            is(SpecialPowers.wrap(textControl).editor.documentIsEmpty, false,
     40              `nsIEditor.documentIsEmpty should be false if value of <${test.tag}> is non-empty by default`);
     41            textControl.focus();
     42            is(SpecialPowers.wrap(textControl).editor.documentIsEmpty, false,
     43              `nsIEditor.documentIsEmpty should be false if value of <${test.tag}> is non-empty by default after getting focus`);
     44            textControl.value = "def";
     45            is(SpecialPowers.wrap(textControl).editor.documentIsEmpty, false,
     46              `nsIEditor.documentIsEmpty should be false if <${test.tag}>.value is set to different non-empty string`);
     47            textControl.value = "";
     48            is(SpecialPowers.wrap(textControl).editor.documentIsEmpty, true,
     49              `nsIEditor.documentIsEmpty should be true if <${test.tag}>.value is set to empty string from non-empty string`);
     50 
     51            textControl = textControl.nextSibling;
     52            is(SpecialPowers.wrap(textControl).editor.documentIsEmpty, true,
     53              `nsIEditor.documentIsEmpty should be true if value of <${test.tag}> is empty by default (placeholder isn't empty)`);
     54            textControl.focus();
     55            is(SpecialPowers.wrap(textControl).editor.documentIsEmpty, true,
     56              `nsIEditor.documentIsEmpty should be true if value of <${test.tag}> is empty by default after getting focus (placeholder isn't empty)`);
     57            textControl.value = "abc";
     58            is(SpecialPowers.wrap(textControl).editor.documentIsEmpty, false,
     59              `nsIEditor.documentIsEmpty should be false if <${test.tag}>.value is set to non-empty string (placeholder isn't empty)`);
     60            textControl.value = "";
     61            is(SpecialPowers.wrap(textControl).editor.documentIsEmpty, true,
     62              `nsIEditor.documentIsEmpty should be true if <${test.tag}>.value is set to empty string (placeholder isn't empty)`);
     63          }
     64      })();
     65 
     66      function getHTMLEditor() {
     67        const editingSession = SpecialPowers.wrap(window).docShell.editingSession;
     68        if (!editingSession) {
     69          return null;
     70        }
     71        return editingSession.getEditorForWindow(window);
     72      }
     73 
     74      (function test_with_contenteditable() {
     75        document.body.innerHTML = "<div contenteditable></div>";
     76        try {
     77          getHTMLEditor().documentIsEmpty;
     78          todo(false, "nsIEditor.documentIsEmpty should throw an exception when no editing host has focus");
     79        } catch (e) {
     80          ok(true, "nsIEditor.documentIsEmpty should throw an exception when no editing host has focus");
     81        }
     82        document.querySelector("div[contenteditable]").focus();
     83        is(getHTMLEditor().documentIsEmpty, true,
     84          "nsIEditor.documentIsEmpty should be true when editing host does not have contents");
     85 
     86        document.body.innerHTML = "<div contenteditable><br></div>";
     87        document.querySelector("div[contenteditable]").focus();
     88        is(getHTMLEditor().documentIsEmpty, false,
     89          "nsIEditor.documentIsEmpty should be false when editing host has only a <br> element");
     90 
     91        document.body.innerHTML = "<div contenteditable><p><br></p></div>";
     92        document.querySelector("div[contenteditable]").focus();
     93        is(getHTMLEditor().documentIsEmpty, false,
     94          "nsIEditor.documentIsEmpty should be false when editing host has only an empty paragraph");
     95 
     96        document.body.innerHTML = "<div contenteditable><p>abc</p></div>";
     97        document.querySelector("div[contenteditable]").focus();
     98        is(getHTMLEditor().documentIsEmpty, false,
     99          "nsIEditor.documentIsEmpty should be false when editing host has text in a paragraph");
    100 
    101        document.body.innerHTML = "<div contenteditable>abc</div>";
    102        document.querySelector("div[contenteditable]").focus();
    103        is(getHTMLEditor().documentIsEmpty, false,
    104          "nsIEditor.documentIsEmpty should be false when editing host has text directly");
    105 
    106        document.execCommand("selectall");
    107        document.execCommand("delete");
    108        todo_is(getHTMLEditor().documentIsEmpty, true,
    109          "nsIEditor.documentIsEmpty should be true when all contents in editing host are deleted");
    110      })();
    111 
    112      document.designMode = "on";
    113      (function test_with_designMode() {
    114        document.body.innerHTML = "";
    115        is(getHTMLEditor().documentIsEmpty, true,
    116          "nsIEditor.documentIsEmpty should be true when <body> is empty in designMode");
    117        document.body.focus();
    118        is(getHTMLEditor().documentIsEmpty, true,
    119          "nsIEditor.documentIsEmpty should be true when <body> is empty in designMode (after setting focus explicitly)");
    120 
    121        document.body.innerHTML = "<div><br></div>";
    122        is(getHTMLEditor().documentIsEmpty, false,
    123          "nsIEditor.documentIsEmpty should be false when <body> has only an empty paragraph in designMode");
    124 
    125        document.body.innerHTML = "<div>abc</div>";
    126        is(getHTMLEditor().documentIsEmpty, false,
    127          "nsIEditor.documentIsEmpty should be false when <body> has text in a paragraph in designMode");
    128 
    129        document.body.innerHTML = "abc";
    130        is(getHTMLEditor().documentIsEmpty, false,
    131          "nsIEditor.documentIsEmpty should be false when <body> has text directly in designMode");
    132 
    133        document.execCommand("selectall");
    134        document.execCommand("delete");
    135        todo_is(getHTMLEditor().documentIsEmpty, true,
    136          "nsIEditor.documentIsEmpty should be true when all contents in designMode are deleted");
    137      })();
    138      document.designMode = "off";
    139 
    140      document.body.innerHTML = originalBody;
    141      SimpleTest.finish();
    142    });
    143  </script>
    144 </head>
    145 <body>
    146 <p id="display"></p>
    147 <div id="content" style="display: none"></div>
    148 <pre id="test"></pre>
    149 </body>
    150 </html>