summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/editing/edit-context/edit-context-basics.tentative.html
blob: 0011270c812fdd2ccea80699f0729ef2f7fc214e (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<!DOCTYPE html>
<html>
<head>
<title>EditContext: The HTMLElement.editContext property</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src='../../html/resources/common.js'></script>
</head>
<body>
  <div id="test"></div>
  <div id="contenteditableDiv" contenteditable></div>
  <script>
    test(function() {
      const editContextDict = {
        text: "Hello world",
        selectionStart: 11,
        selectionEnd: 11
      };
      const editContext = new EditContext(editContextDict);
      assert_not_equals(editContext, null);
      // Verify all the members of the EditContext
      assert_equals(editContext.text, "Hello world");
      assert_equals(editContext.selectionStart, 11);
      assert_equals(editContext.selectionEnd, 11);
    }, 'Testing EditContext Dictionary Init');

    test(function() {
      contenteditableDiv.editContext = new EditContext();
      contenteditableDiv.editContext = null;
      contenteditableDiv.focus();
      assert_equals(document.activeElement, contenteditableDiv);
    }, 'A contenteditable element should remain editable after attaching and detaching EditContext.');

    test(function() {
      const editContext = new EditContext();
      assert_not_equals(editContext, null);

      const disconnected_div = document.createElement("DIV");
      assert_equals(disconnected_div.editContext, null);

      disconnected_div.editContext = editContext;
      assert_equals(disconnected_div.editContext, editContext);
      assert_equals(editContext.attachedElements().length, 1);
      assert_equals(editContext.attachedElements()[0], disconnected_div);
    }, 'EditContext can be associated with an element that is not in the tree.');

    test(function() {
      const editContext = new EditContext();
      assert_not_equals(editContext, null);

      const div = document.createElement("DIV");
      assert_equals(div.editContext, null);

      document.body.appendChild(div);
      div.editContext = editContext;
      assert_equals(div.editContext, editContext);
      assert_equals(editContext.attachedElements().length, 1);
      assert_equals(editContext.attachedElements()[0], div);

      document.body.removeChild(div);
      assert_equals(div.editContext, editContext);
      assert_equals(editContext.attachedElements().length, 1);
      assert_equals(editContext.attachedElements()[0], div);
    }, 'If an element is removed from the tree, the associated EditContext remains connected to the element.');

    test(function() {
      const editContext = new EditContext();

      const div_parent = document.createElement("DIV");
      const div_child = document.createElement("DIV");
      document.body.appendChild(div_parent);
      div_parent.appendChild(div_child);

      div_child.editContext = editContext;
      assert_equals(div_child.editContext, editContext);
      assert_equals(div_parent.editContext, null);
      assert_equals(editContext.attachedElements().length, 1);
      assert_equals(editContext.attachedElements()[0], div_child);

      document.body.removeChild(div_parent);
      assert_equals(div_child.editContext, editContext);
      assert_equals(editContext.attachedElements().length, 1);
      assert_equals(editContext.attachedElements()[0], div_child);
    }, 'If an element\'s ancestor is removed from tree, the associated EditContext remains connected to the element.');

    test(function() {
      const editContext = new EditContext();
      const test = document.getElementById("test");

      test.editContext = editContext;

      assert_equals(test.editContext, editContext);
      assert_equals(editContext.attachedElements().length, 1);
      assert_equals(editContext.attachedElements()[0], test);

      test.editContext = null;

      assert_equals(editContext.attachedElements().length, 0);
    }, '.attachedElements() should return associated element');

    test(function() {
      const editContext = new EditContext();
      assert_not_equals(editContext, null);
      editContext.updateText(0, 3, "foo");
      assert_equals(editContext.text, "foo");
      const test = document.getElementById('test');
      // Update the layout of the |EditContext|
      var viewRect = test.getBoundingClientRect();
      viewRect.x = viewRect.left;
      viewRect.y = viewRect.top;
      var caretRect = test.getBoundingClientRect();
      caretRect.x = caretRect.left;
      caretRect.y = 2.2 * caretRect.top;
      caretRect.width = 1;
      editContext.updateSelection(0, 0);
      assert_equals(editContext.selectionStart, 0);
      assert_equals(editContext.selectionEnd, 0);
      editContext.updateSelection(1, 0);
      assert_equals(editContext.selectionStart, 1);
      assert_equals(editContext.selectionEnd, 0);
      editContext.updateSelection(0, 1);
      assert_equals(editContext.selectionStart, 0);
      assert_equals(editContext.selectionEnd, 1);
      editContext.updateSelection(1, 1);
      assert_equals(editContext.selectionStart, 1);
      assert_equals(editContext.selectionEnd, 1);
      editContext.updateControlBounds(viewRect);
      editContext.updateSelectionBounds(caretRect);
      editContext.updateCharacterBounds(0, [caretRect]);

      assert_throws_js(TypeError, function() { editContext.updateControlBounds(42); });
      assert_throws_js(TypeError, function() { editContext.updateSelectionBounds(42); });
      assert_throws_js(TypeError, function() { editContext.updateControlBounds(undefined); });
      assert_throws_js(TypeError, function() { editContext.updateSelectionBounds(undefined); });
      assert_throws_js(TypeError, function() { editContext.updateCharacterBounds(0); });
      assert_throws_js(TypeError, function() { editContext.updateCharacterBounds([caretRect]); });
      assert_throws_js(TypeError, function() { editContext.updateCharacterBounds(0, caretRect); });
      assert_throws_js(TypeError, function() { editContext.updateCharacterBounds(0, 42); });
      assert_throws_js(TypeError, function() { editContext.updateCharacterBounds(0, undefined); });
      assert_throws_js(TypeError, function() { editContext.updateCharacterBounds(0, [undefined]); });

      viewRect.x = viewRect.y = viewRect.width = viewRect.height = undefined;
      editContext.updateControlBounds(viewRect);
      editContext.updateSelectionBounds(viewRect);
      editContext.updateCharacterBounds(0, [viewRect]);
    }, 'Testing EditContext update text, selection and layout');

    test(function() {
      const editContext = new EditContext();
      const test = document.getElementById('test');
      var rect1 = DOMRect.fromRect({x:0, y:1, width:100, height:200});
      var rect2 = DOMRect.fromRect({x:2, y:3, width:300, height:400});
      var rectArray = [rect1, rect2];
      var rangeStart = 2;
      editContext.updateCharacterBounds(rangeStart, rectArray);
      assert_equals(editContext.characterBoundsRangeStart, 2);

      var actualRectArray = editContext.characterBounds();
      assert_equals(actualRectArray.length, 2);
      assert_equals(actualRectArray[0].x, 0);
      assert_equals(actualRectArray[0].y, 1);
      assert_equals(actualRectArray[0].width, 100);
      assert_equals(actualRectArray[0].height, 200);
      rect2.x=100;
      assert_equals(actualRectArray[1].x, 2);  // the cached value shouldn't change.
      assert_equals(actualRectArray[1].y, 3);
      assert_equals(actualRectArray[1].width, 300);
      assert_equals(actualRectArray[1].height, 400);
    }, 'updateCharacterBounds(), characterBounds(), and characterBoundsRangeStart should work properly');

    // The behavior in this test case is not well-defined in the spec.
    // See https://github.com/w3c/edit-context/issues/88
    // test(function() {
    //   const editContext = new EditContext();
    //   assert_not_equals(editContext, null);
    //   editContext.updateText(0, 3, "foo");
    //   assert_equals(editContext.text, "foo");
    //   assert_throws_dom("IndexSizeError", function() { editContext.updateSelection(10, 0); });
    //   assert_equals(editContext.selectionStart, 0);
    //   assert_equals(editContext.selectionEnd, 0);
    //   assert_throws_dom("IndexSizeError", function() { editContext.updateText(10, 1, "h"); });
    //   assert_equals(editContext.text, "foo");
    // }, 'Testing EditContext update text and selection with invalid values');

    test(function() {
      const editContext = new EditContext();
      assert_not_equals(editContext, null);
      editContext.updateText(0, 3, "foo");
      assert_equals(editContext.text, "foo");
      editContext.updateSelection(3, 0);
      assert_equals(editContext.selectionStart, 3);
      assert_equals(editContext.selectionEnd, 0);
    }, 'EditContext should allow a backwards selection');

    test(function() {
      const editContext = new EditContext();
      assert_not_equals(editContext, null);
      editContext.updateText(6, 0, "abcdef");
      assert_equals(editContext.text, "abcdef");

      editContext.updateText(2, 5, "ghi");
      assert_equals(editContext.text, "abghif");

      editContext.updateText(5, 2, "jkl");
      assert_equals(editContext.text, "abjklf");
    }, 'updateText can replace substrings including with backwards parameters');
  </script>
</body>
</html>