summaryrefslogtreecommitdiffstats
path: root/editor/libeditor/tests/test_nsIEditor_beginningOfDocument.html
blob: 119f35dbdd2824be71159be8243ba288f0e40914 (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
114
115
116
117
118
119
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Tests of nsIEditor#beginningOfDocument()</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;
            editor.beginningOfDocument();
            is(textControl.selectionStart, 0,
              `nsIEditor.beginningOfDocument() should set selectionStart of empty <${test.tag}> to 0`);
            is(textControl.selectionEnd, 0,
              `nsIEditor.beginningOfDocument() should set selectionEnd of empty <${test.tag}> to 0`);
            textControl.value = "abc";
            textControl.selectionStart = 2;
            textControl.selectionEnd = 3;
            editor.beginningOfDocument();
            is(textControl.selectionStart, 0,
              `nsIEditor.beginningOfDocument() should set selectionStart of non-empty <${test.tag}> to 0`);
            is(textControl.selectionEnd, 0,
              `nsIEditor.beginningOfDocument() should set selectionEnd of non-empty <${test.tag}> to 0`);
          }
      })();

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

      (function test_with_contenteditable() {
        document.body.innerHTML = "<div contenteditable><p>abc</p></div>";
        getSelection().removeAllRanges();
        getHTMLEditor().beginningOfDocument();
        is(getSelection().rangeCount, 0,
          "selection shouldn't be changed when there is no selection");
        getSelection().setBaseAndExtent(document.body.querySelector("p").firstChild, 1,
                                        document.body.querySelector("p").firstChild, 3);
        getHTMLEditor().beginningOfDocument();
        is(getSelection().isCollapsed, true,
          "selection should be collapsed after calling nsIEditor.beginningOfDocument() with contenteditable having one paragraph");
        is(getSelection().rangeCount, 1,
          "selection should has only one range after calling nsIEditor.beginningOfDocument() with contenteditable having one paragraph");
        is(getSelection().focusNode, document.body.querySelector("p").firstChild,
          "selection should be collapsed into the text node after calling nsIEditor.beggingOfDocument() with content editable having one paragraph");
        is(getSelection().focusOffset, 0,
          "selection should be collapsed to start of the text node after calling nsIEditor.beggingOfDocument() with content editable having one paragraph");

        document.body.innerHTML = "<div contenteditable><p contenteditable=\"false\">abc</p><p>def</p></div>";
        getSelection().setBaseAndExtent(document.body.querySelector("p + p").firstChild, 1,
                                        document.body.querySelector("p + p").firstChild, 3);
        getHTMLEditor().beginningOfDocument();
        is(getSelection().isCollapsed, true,
          "selection should be collapsed after calling nsIEditor.beginningOfDocument() with contenteditable having non-editable paragraph first");
        is(getSelection().rangeCount, 1,
          "selection should has only one range after calling nsIEditor.beginningOfDocument() with contenteditable having non-editable paragraph first");
        is(getSelection().focusNode, document.body.querySelector("div[contenteditable]"),
          "selection should be collapsed to start of the editing host after calling nsIEditor.beggingOfDocument() with content editable having non-editable paragraph first");
        is(getSelection().focusOffset, 0,
          "selection should be collapsed to start of the editing host after calling nsIEditor.beggingOfDocument() with content editable having non-editable paragraph first");

        document.body.innerHTML = "<div contenteditable>\n<p contenteditable=\"false\">abc</p>\n<p>def</p>\n</div>";
        getSelection().setBaseAndExtent(document.body.querySelector("p + p").firstChild, 1,
                                        document.body.querySelector("p + p").firstChild, 3);
        getHTMLEditor().beginningOfDocument();
        is(getSelection().isCollapsed, true,
          "selection should be collapsed after calling nsIEditor.beginningOfDocument() with contenteditable having non-editable paragraph first and some invisible line breaks");
        is(getSelection().rangeCount, 1,
          "selection should has only one range after calling nsIEditor.beginningOfDocument() with contenteditable having non-editable paragraph first and some invisible line breaks");
        is(getSelection().focusNode, document.body.querySelector("div[contenteditable]"),
          "selection should be collapsed to start of the editing host after calling nsIEditor.beggingOfDocument() with content editable having non-editable paragraph first and some invisible line breaks");
        is(getSelection().focusOffset, 0,
          "selection should be collapsed to start of the editing host after calling nsIEditor.beggingOfDocument() with content editable having non-editable paragraph first and some invisible line breaks");

        document.body.innerHTML = "<div contenteditable><p>abc</p></div>def<div contenteditable><p>ghi</p></div>";
        getSelection().setBaseAndExtent(document.body.querySelector("div + div > p").firstChild, 1,
                                        document.body.querySelector("div + div > p").firstChild, 3);
        getHTMLEditor().beginningOfDocument();
        is(getSelection().isCollapsed, true,
          "selection should be collapsed after calling nsIEditor.beginningOfDocument() with the 2nd contenteditable");
        is(getSelection().rangeCount, 1,
          "selection should has only one range after calling nsIEditor.beginningOfDocument() with the 2nd contenteditable");
        is(getSelection().focusNode, document.body.querySelector("div + div > p").firstChild,
          "selection should be collapsed to start of the first text node in the second editing host after calling nsIEditor.beggingOfDocument() with the 2nd contenteditable");
        is(getSelection().focusOffset, 0,
          "selection should be collapsed to start of the first text node in the second editing host after calling nsIEditor.beggingOfDocument() with the 2nd contenteditable");
      })();

      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>