File: stringifier_editable_element.tentative.html

package info (click to toggle)
thunderbird 1%3A140.5.0esr-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 4,609,180 kB
  • sloc: cpp: 7,672,739; javascript: 5,901,898; ansic: 3,898,899; python: 1,413,347; xml: 653,997; asm: 462,284; java: 180,927; sh: 113,491; makefile: 20,463; perl: 14,288; objc: 13,059; yacc: 4,583; pascal: 3,352; lex: 1,720; ruby: 1,222; exp: 762; sql: 715; awk: 580; php: 436; lisp: 430; sed: 70; csh: 10
file content (187 lines) | stat: -rw-r--r-- 5,564 bytes parent folder | download | duplicates (12)
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
<!DOCTYPE HTML>
<meta charset=utf-8>
<title>Selection: stringifier for editable elements</title>
<!--
There are two open issues about how this should behave
https://github.com/w3c/selection-api/issues/83
https://github.com/w3c/selection-api/issues/7
-->
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/resources/testdriver-actions.js"></script>
<body>
  <input id="dummyInput"></input>

  <input id="textInput" value="This is a text">
  <textarea id="textArea" rows="5" cols="40">

  Line one
  Line two

  </textarea>

  <button id="button">Button</button>
  <a id="anchor">Anchor</a>
  <span id="text">Text</span>
</body>
<script>

function reset() {
  window.getSelection().empty();
  dummyInput.focus();
}

window.onload = () => {
  test(() => {
    reset();
    textInput.select();

    assert_equals(document.activeElement, textInput);
    assert_equals(window.getSelection().toString().replace(/\r\n/g, "\n"), "This is a text");
  }, "select the entire input should result all the content");

  test(() => {
    reset();
    textInput.select();
    dummyInput.focus();

    assert_equals(document.activeElement, dummyInput);
    assert_equals(window.getSelection().toString().replace(/\r\n/g, "\n"), "");
  }, "toString() should return empty when the focus is not on the editable content");

  test(() => {
    reset();

    textInput.selectionStart = 3;
    textInput.selectionEnd = 7;

    assert_equals(document.activeElement, dummyInput);
    assert_equals(window.getSelection().toString().replace(/\r\n/g, "\n"), "");

    textInput.focus();
    assert_equals(document.activeElement, textInput);
    assert_equals(window.getSelection().toString().replace(/\r\n/g, "\n"), "s is");
  }, "toString() works with selectionStart and selectionEnd for input");

  test(() => {
    reset();

    textArea.select();

    assert_equals(document.activeElement, textArea);
    assert_equals(window.getSelection().toString().replace(/\r\n/g, "\n"), "\n  Line one\n  Line two\n\n  ");
  }, "select the entire textarea should result all the content");

  test(() => {
    reset();

    textArea.selectionStart = 3;
    textArea.selectionEnd = 12;

    assert_equals(document.activeElement, dummyInput);
    assert_equals(window.getSelection().toString().replace(/\r\n/g, "\n"), "");

    textArea.focus();
    assert_equals(document.activeElement, textArea);
    assert_equals(window.getSelection().toString().replace(/\r\n/g, "\n"), "Line one\n");
  }, "toString() works with selectionStart and selectionEnd for textarea");

  test(() => {
    reset();
    textInput.select();

    button.focus();

    assert_equals(document.activeElement, button);

    assert_equals(window.getSelection().toString().replace(/\r\n/g, "\n"), "This is a text");
  }, "toString() works even if a click just occured on a button");

  promise_test((t) => {
    reset();
    textInput.select();
    return new Promise(r => {
      anchor.addEventListener("click", function() {
        assert_equals(window.getSelection().toString().replace(/\r\n/g, "\n"), "This is a text");
        r();
      }, {once: true});

      anchor.click();
    });
  }, "toString() works for programatically calling .click() on anchor (without href)");

  promise_test((t) => {
    reset();
    textInput.select();
    return new Promise(r => {
      anchor.addEventListener("click", function() {
        assert_equals(window.getSelection().toString().replace(/\r\n/g, "\n"), "");
        r();
      }, {once : true});

      test_driver.click(anchor);
    });
  }, "toString() doesn't work for actual clicking the anchor (without href)");

  promise_test((t) => {
    reset();
    textInput.select();
    anchor.href = "#";
    return new Promise(r => {
      anchor.addEventListener("click", function() {
        assert_equals(window.getSelection().toString().replace(/\r\n/g, "\n"), "This is a text");
        r();
      }, {once: true});

      anchor.click(); // anchor has href now
    });
  }, "toString() works for programatically calling .click() on anchor (with href)");

  promise_test((t) => {
    reset();
    textInput.select();
    return new Promise(r => {
      anchor.addEventListener("click", function() {
        assert_equals(window.getSelection().toString().replace(/\r\n/g, "\n"), "This is a text");
        r();
      }, {once : true});

      test_driver.click(anchor); // anchor has href now
    });
  }, "toString() also works for actual clicking the anchor (with href)");

  promise_test((t) => {
    reset();
    textInput.select();

    return new Promise(async r => {
      anchor.addEventListener("click", function() {
        assert_equals(window.getSelection().toString().replace(/\r\n/g, "\n"), "");
        r();
      }, {once : true});

      await test_driver.click(text);
      test_driver.click(anchor);
    });
  }, "Click on a text prior to toString() moves the seleciton");

  promise_test((t) => {
    reset();
    textInput.select();

    text.style = "user-select: none";
    return new Promise(async r => {
      anchor.addEventListener("click", function() {
        assert_equals(window.getSelection().toString().replace(/\r\n/g, "\n"), "This is a text");
        r();
      }, {once : true});

      await test_driver.click(text);
      test_driver.click(anchor);
    });
  }, "Click on a `user-select:none` text prior to toString() doesn't move the seleciton");
};
</script>
</html>