summaryrefslogtreecommitdiffstats
path: root/editor/libeditor/tests/test_nsIEditor_documentCharacterSet.html
blob: 18f948fdd960c13152af6c9471940192e45a0d9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<!DOCTYPE HTML>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html,charset=utf-8">
  <title>Tests of nsIEditor#documentCharacterSet</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
  <script>
    SimpleTest.waitForExplicitFinish();
    SimpleTest.waitForFocus(() => {
      const originalBody = document.body.innerHTML;

      (function test_with_text_editor() {
        for (const test of [
            {
              tag: "input",
              innerHTML: "<input>",
            },
            {
              tag: "textarea",
              innerHTML: "<textarea></textarea>",
            },
          ]) {
            document.body.innerHTML = test.innerHTML;
            const textControl = document.body.querySelector(test.tag);
            const editor = SpecialPowers.wrap(textControl).editor;
            try {
              editor.documentCharacterSet;
              ok(false, `TextEditor::GetDocumentCharacterSet() for <${test.tag}> should throw an exception`);
            } catch (e) {
              ok(true, `TextEditor::GetDocumentCharacterSet() for <${test.tag}> should throw an exception`);
            }
            try {
              editor.documentCharacterSet = "windows-1252";
              ok(false, `TextEditor::SetDocumentCharacterSet() for <${test.tag}> should throw an exception`);
            } catch (e) {
              ok(true, `TextEditor::SetDocumentCharacterSet() for <${test.tag}> should throw an exception`);
            }
          }
      })();

      function getHTMLEditor(win = window) {
        const editingSession = SpecialPowers.wrap(win).docShell.editingSession;
        if (!editingSession) {
          return null;
        }
        return editingSession.getEditorForWindow(win);
      }

      (function test_with_contenteditable() {
        document.body.innerHTML = "<div contenteditable><p>abc</p></div>";
        const editor = getHTMLEditor();
        is(editor.documentCharacterSet, "UTF-8",
          "HTMLEditor::GetDocumentCharacterSet() should return \"UTF-8\"");
        editor.documentCharacterSet = "windows-1252";
        is(document.querySelector("meta[http-equiv]").getAttribute("content"), "text/html,charset=windows-1252",
          "HTMLEditor::SetDocumentCharacterSet() should add <meta> element whose \"http-equiv\" attribute has \"windows-1252\"");
        is(editor.documentCharacterSet, "windows-1252",
          "HTMLEditor::GetDocumentCharacterSet() should return \"windows-1252\" after setting the value");
        editor.documentCharacterSet = "utf-8";
        is(document.querySelector("meta[http-equiv]").getAttribute("content"), "text/html,charset=utf-8",
          "HTMLEditor::SetDocumentCharacterSet() should add <meta> element whose \"http-equiv\" attribute has \"utf-8\"");
        is(editor.documentCharacterSet, "UTF-8",
          "HTMLEditor::GetDocumentCharacterSet() should return \"UTF-8\" after setting the value");
      })();

      (function test_with_designMode() {
        while (document.querySelector("meta")) {
          document.querySelector("meta").remove();
        }
        document.body.innerHTML = "<iframe></iframe>";
        const editdoc = document.querySelector("iframe").contentDocument;
        editdoc.designMode = "on";
        const editor = getHTMLEditor(document.querySelector("iframe").contentWindow);

        editor.documentCharacterSet = "us-ascii";
        const metaWithHttpEquiv = editdoc.getElementsByTagName("meta")[0];
        is(metaWithHttpEquiv.getAttribute("http-equiv"), "Content-Type",
          "meta element should have http-equiv");
        is(metaWithHttpEquiv.getAttribute("content"), "text/html;charset=us-ascii",
          "charset should be set as us-ascii");

        const dummyMeta = editdoc.createElement("meta");
        dummyMeta.setAttribute("name", "keywords");
        dummyMeta.setAttribute("content", "test");
        metaWithHttpEquiv.parentNode.insertBefore(dummyMeta, metaWithHttpEquiv);

        editor.documentCharacterSet = "utf-8";

        is(dummyMeta, editdoc.getElementsByTagName("meta")[0],
          "<meta> element shouldn't be touched");
        isnot(dummyMeta.getAttribute("http-equiv"), "Content-Type",
          "first meta element shouldn't have http-equiv");

        is(metaWithHttpEquiv, editdoc.getElementsByTagName("meta")[1],
          "The second <meta> element should be reused");
        is(metaWithHttpEquiv.getAttribute("http-equiv"), "Content-Type",
          "second meta element should have http-equiv");
        is(metaWithHttpEquiv.getAttribute("content"), "text/html;charset=utf-8",
          "charset should be set as utf-8");
      })();

      document.body.innerHTML = originalBody;
      SimpleTest.finish();
    });
  </script>
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test"></pre>
</body>
</html>