summaryrefslogtreecommitdiffstats
path: root/editor/libeditor/tests/test_nsIHTMLEditor_selectElement.html
blob: 1ac21635eadcca0353ada191e1453d5c6e5c09f1 (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
120
121
122
123
124
125
126
127
128
129
130
131
<!DOCTYPE>
<html>
<head>
  <title>Test for nsIHTMLEditor.selectElement()</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css">
</head>
<body>
<div id="display">
</div>
<div id="content" contenteditable></div>
<pre id="test">
</pre>

<script class="testbody" type="application/javascript">

SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(function() {
  let editor = document.getElementById("content");
  let selection = window.getSelection();

  editor.innerHTML = "<p>p1<b>b1</b><i>i1</i></p><p><b>b2</b><i>i2</i>p2</p>";

  editor.focus();
  try {
    getHTMLEditor().selectElement(editor.firstChild.firstChild);
    ok(false, "nsIHTMLEditor.selectElement() should throw an exception if given node is not an element");
  } catch (e) {
    ok(true, `nsIHTMLEditor.selectElement() should throw an exception if given node is not an element: ${e}`);
  }

  editor.focus();
  try {
    getHTMLEditor().selectElement(editor.firstChild.firstChild.nextSibling);
    is(selection.anchorNode, editor.firstChild,
       "nsIHTMLEditor.selectElement() should set anchor node to parent of <b> element in the first paragraph");
    is(selection.anchorOffset, 1,
       "nsIHTMLEditor.selectElement() should set anchor offset to the index of <b> element in the first paragraph");
    is(selection.focusNode, editor.firstChild,
       "nsIHTMLEditor.selectElement() should set focus node to parent of <b> element in the first paragraph");
    is(selection.focusOffset, 2,
       "nsIHTMLEditor.selectElement() should set anchor offset to the index of <b> element + 1 in the first paragraph");
  } catch (e) {
    ok(false, `nsIHTMLEditor.selectElement() shouldn't throw exception when selecting an element in focused editor #1: ${e}`);
  }

  editor.focus();
  try {
    getHTMLEditor().selectElement(editor.firstChild.nextSibling.firstChild);
    is(selection.anchorNode, editor.firstChild.nextSibling,
       "nsIHTMLEditor.selectElement() should set anchor node to parent of <b> element in the second paragraph");
    is(selection.anchorOffset, 0,
       "nsIHTMLEditor.selectElement() should set anchor offset to the index of <b> element in the second paragraph");
    is(selection.focusNode, editor.firstChild.nextSibling,
       "nsIHTMLEditor.selectElement() should set focus node to parent of <b> element in the second paragraph");
    is(selection.focusOffset, 1,
       "nsIHTMLEditor.selectElement() should set anchor offset to the index of <b> element + 1 in the second paragraph");
  } catch (e) {
    ok(false, `nsIHTMLEditor.selectElement() shouldn't throw exception when selecting an element in focused editor #2: ${e}`);
  }

  editor.focus();
  try {
    getHTMLEditor().selectElement(editor);
    ok(false, "nsIHTMLEditor.selectElement() should throw an exception if given node is the editing host");
  } catch (e) {
    ok(true, `nsIHTMLEditor.selectElement() should throw an exception if given node is the editing host: ${e}`);
  }

  editor.focus();
  try {
    getHTMLEditor().selectElement(editor.parentElement);
    ok(false, "nsIHTMLEditor.selectElement() should throw an exception if given node is outside of the editing host");
  } catch (e) {
    ok(true, `nsIHTMLEditor.selectElement() should throw an exception if given node is outside of the editing host: ${e}`);
  }

  selection.removeAllRanges();
  editor.blur();
  try {
    getHTMLEditor().selectElement(editor.firstChild.nextSibling.firstChild);
    ok(false, "nsIHTMLEditor.selectElement() should throw an exception if there is no active editing host");
  } catch (e) {
    ok(true, `nsIHTMLEditor.selectElement() should throw an exception if there is no active editing host: ${e}`);
  }

  editor.focus();
  editor.firstChild.firstChild.nextSibling.nextSibling.setAttribute("contenteditable", "false");
  try {
    getHTMLEditor().selectElement(editor.firstChild.firstChild.nextSibling.nextSibling);
    is(selection.anchorNode, editor.firstChild,
       "nsIHTMLEditor.selectElement() should set anchor node to parent of <i contenteditable=\"false\"> element in the first paragraph");
    is(selection.anchorOffset, 2,
       "nsIHTMLEditor.selectElement() should set anchor offset to the index of <i contenteditable=\"false\"> element in the first paragraph");
    is(selection.focusNode, editor.firstChild,
       "nsIHTMLEditor.selectElement() should set focus node to parent of <i contenteditable=\"false\"> element in the first paragraph");
    is(selection.focusOffset, 3,
       "nsIHTMLEditor.selectElement() should set anchor offset to the index of <i contenteditable=\"false\"> element + 1 in the first paragraph");
  } catch (e) {
    ok(false, `nsIHTMLEditor.selectElement() shouldn't throw exception when selecting an element in focused editor #3: ${e}`);
  }

  editor.focus();
  editor.firstChild.nextSibling.setAttribute("contenteditable", "false");
  try {
    getHTMLEditor().selectElement(editor.firstChild.nextSibling.firstChild.nextSibling);
    is(selection.anchorNode, editor.firstChild.nextSibling,
       "nsIHTMLEditor.selectElement() should set anchor node to parent of <i> element in the second paragraph which is not editable");
    is(selection.anchorOffset, 1,
       "nsIHTMLEditor.selectElement() should set anchor offset to the index of <i> element in the second paragraph which is not editable");
    is(selection.focusNode, editor.firstChild.nextSibling,
       "nsIHTMLEditor.selectElement() should set focus node to parent of <i> element in the second paragraph which is not editable");
    is(selection.focusOffset, 2,
       "nsIHTMLEditor.selectElement() should set anchor offset to the index of <i> element + 1 in the second paragraph which is not editable");
  } catch (e) {
    ok(false, `nsIHTMLEditor.selectElement() shouldn't throw exception when selecting an element in focused editor #4: ${e}`);
  }

  SimpleTest.finish();
});

function getHTMLEditor() {
  var Ci = SpecialPowers.Ci;
  var editingSession = SpecialPowers.wrap(window).docShell.editingSession;
  return editingSession.getEditorForWindow(window).QueryInterface(Ci.nsIHTMLEditor);
}

</script>
</body>

</html>