tor-browser

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

test_nsIEditor_documentCharacterSet.html (4940B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <head>
      4  <meta http-equiv="Content-Type" content="text/html,charset=utf-8">
      5  <title>Tests of nsIEditor#documentCharacterSet</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            try {
     28              editor.documentCharacterSet;
     29              ok(false, `TextEditor::GetDocumentCharacterSet() for <${test.tag}> should throw an exception`);
     30            } catch (e) {
     31              ok(true, `TextEditor::GetDocumentCharacterSet() for <${test.tag}> should throw an exception`);
     32            }
     33            try {
     34              editor.documentCharacterSet = "windows-1252";
     35              ok(false, `TextEditor::SetDocumentCharacterSet() for <${test.tag}> should throw an exception`);
     36            } catch (e) {
     37              ok(true, `TextEditor::SetDocumentCharacterSet() for <${test.tag}> should throw an exception`);
     38            }
     39          }
     40      })();
     41 
     42      function getHTMLEditor(win = window) {
     43        const editingSession = SpecialPowers.wrap(win).docShell.editingSession;
     44        if (!editingSession) {
     45          return null;
     46        }
     47        return editingSession.getEditorForWindow(win);
     48      }
     49 
     50      (function test_with_contenteditable() {
     51        document.body.innerHTML = "<div contenteditable><p>abc</p></div>";
     52        const editor = getHTMLEditor();
     53        is(editor.documentCharacterSet, "UTF-8",
     54          "HTMLEditor::GetDocumentCharacterSet() should return \"UTF-8\"");
     55        editor.documentCharacterSet = "windows-1252";
     56        is(document.querySelector("meta[http-equiv]").getAttribute("content"), "text/html,charset=windows-1252",
     57          "HTMLEditor::SetDocumentCharacterSet() should add <meta> element whose \"http-equiv\" attribute has \"windows-1252\"");
     58        is(editor.documentCharacterSet, "windows-1252",
     59          "HTMLEditor::GetDocumentCharacterSet() should return \"windows-1252\" after setting the value");
     60        editor.documentCharacterSet = "utf-8";
     61        is(document.querySelector("meta[http-equiv]").getAttribute("content"), "text/html,charset=utf-8",
     62          "HTMLEditor::SetDocumentCharacterSet() should add <meta> element whose \"http-equiv\" attribute has \"utf-8\"");
     63        is(editor.documentCharacterSet, "UTF-8",
     64          "HTMLEditor::GetDocumentCharacterSet() should return \"UTF-8\" after setting the value");
     65      })();
     66 
     67      (function test_with_designMode() {
     68        while (document.querySelector("meta")) {
     69          document.querySelector("meta").remove();
     70        }
     71        document.body.innerHTML = "<iframe></iframe>";
     72        const editdoc = document.querySelector("iframe").contentDocument;
     73        editdoc.designMode = "on";
     74        const editor = getHTMLEditor(document.querySelector("iframe").contentWindow);
     75 
     76        editor.documentCharacterSet = "us-ascii";
     77        const metaWithHttpEquiv = editdoc.getElementsByTagName("meta")[0];
     78        is(metaWithHttpEquiv.getAttribute("http-equiv"), "Content-Type",
     79          "meta element should have http-equiv");
     80        is(metaWithHttpEquiv.getAttribute("content"), "text/html;charset=us-ascii",
     81          "charset should be set as us-ascii");
     82 
     83        const dummyMeta = editdoc.createElement("meta");
     84        dummyMeta.setAttribute("name", "keywords");
     85        dummyMeta.setAttribute("content", "test");
     86        metaWithHttpEquiv.parentNode.insertBefore(dummyMeta, metaWithHttpEquiv);
     87 
     88        editor.documentCharacterSet = "utf-8";
     89 
     90        is(dummyMeta, editdoc.getElementsByTagName("meta")[0],
     91          "<meta> element shouldn't be touched");
     92        isnot(dummyMeta.getAttribute("http-equiv"), "Content-Type",
     93          "first meta element shouldn't have http-equiv");
     94 
     95        is(metaWithHttpEquiv, editdoc.getElementsByTagName("meta")[1],
     96          "The second <meta> element should be reused");
     97        is(metaWithHttpEquiv.getAttribute("http-equiv"), "Content-Type",
     98          "second meta element should have http-equiv");
     99        is(metaWithHttpEquiv.getAttribute("content"), "text/html;charset=utf-8",
    100          "charset should be set as utf-8");
    101      })();
    102 
    103      document.body.innerHTML = originalBody;
    104      SimpleTest.finish();
    105    });
    106  </script>
    107 </head>
    108 <body>
    109 <p id="display"></p>
    110 <div id="content" style="display: none"></div>
    111 <pre id="test"></pre>
    112 </body>
    113 </html>