|
8 | 8 | #textDiv {
|
9 | 9 | display: inline-block;
|
10 | 10 | }
|
| 11 | + |
| 12 | + .user-select-none { |
| 13 | + -webkit-user-select: none; |
| 14 | + user-select: none; |
| 15 | + } |
11 | 16 | </style>
|
12 |
| -<div id="textDiv">aaa</div> |
| 17 | +<div id="container"></div> |
13 | 18 | <script>
|
14 | 19 | test(() => {
|
| 20 | + container.setHTMLUnsafe(`<div id="textDiv">aaa</div>`); |
15 | 21 | assert_throws_js(TypeError, () => { document.caretPositionFromPoint(); });
|
16 | 22 | assert_throws_js(TypeError, () => { document.caretPositionFromPoint(5); });
|
17 | 23 | assert_throws_js(TypeError, () => { document.caretPositionFromPoint("foo", 5); });
|
18 | 24 | }, "document.caretPositionFromPoint() throws when called without the correct parameters");
|
19 | 25 |
|
20 | 26 | test(() => {
|
| 27 | + container.setHTMLUnsafe(`<div id="textDiv">aaa</div>`); |
21 | 28 | const doc = document.implementation.createHTMLDocument("");
|
22 | 29 | assert_equals(doc.caretPositionFromPoint(0, 0), null);
|
23 | 30 | }, "document.caretPositionFromPoint() should return null for a document with no viewport");
|
24 | 31 |
|
25 | 32 | test(() => {
|
| 33 | + container.setHTMLUnsafe(`<div id="textDiv">aaa</div>`); |
26 | 34 | assert_equals(document.caretPositionFromPoint(-5, 5), null);
|
27 | 35 | assert_equals(document.caretPositionFromPoint(5, -5), null);
|
28 | 36 | assert_equals(document.caretPositionFromPoint(document.documentElement.clientWidth * 2, 5), null);
|
29 | 37 | assert_equals(document.caretPositionFromPoint(5, document.documentElement.clientHeight * 2), null);
|
30 | 38 | }, "document.caretPositionFromPoint() should return null if given coordinates outside of the viewport");
|
31 | 39 |
|
32 | 40 | test(() => {
|
| 41 | + container.setHTMLUnsafe(`<div id="textDiv">aaa</div>`); |
33 | 42 | const textDiv = document.getElementById("textDiv");
|
34 | 43 | const rect = textDiv.getBoundingClientRect();
|
35 | 44 | const characterWidth = rect.width / textDiv.textContent.length;
|
|
45 | 54 | }, "document.caretPositionFromPoint() should return a CaretPosition at the specified location");
|
46 | 55 |
|
47 | 56 | test(() => {
|
| 57 | + container.setHTMLUnsafe(`<div id="textDiv" class="user-select-none">aaa</div>`); |
| 58 | + const textDiv = document.getElementById("textDiv"); |
| 59 | + const rect = textDiv.getBoundingClientRect(); |
| 60 | + const characterWidth = rect.width / textDiv.textContent.length; |
| 61 | + const characterIndex = 2 |
| 62 | + const x = rect.left + characterWidth * characterIndex; |
| 63 | + const y = rect.top + rect.height / 2; |
| 64 | + const caretPosition = document.caretPositionFromPoint(x, y); |
| 65 | + assert_true(caretPosition instanceof CaretPosition); |
| 66 | + assert_true(caretPosition.offsetNode instanceof Text); |
| 67 | + assert_equals(typeof(caretPosition.offset), "number"); |
| 68 | + assert_equals(caretPosition.offsetNode, textDiv.firstChild); |
| 69 | + assert_equals(caretPosition.offset, characterIndex); |
| 70 | + }, "document.caretPositionFromPoint() should return a CaretPosition over elements with `user-select: none`"); |
| 71 | + |
| 72 | + test(() => { |
| 73 | + container.setHTMLUnsafe(`<video controls></video>`); |
| 74 | + const video = document.querySelector("video"); |
| 75 | + const caretPosition = document.caretPositionFromPoint(video.offsetLeft + video.offsetWidth / 2, video.offsetTop + video.offsetHeight / 2); |
| 76 | + assert_equals(caretPosition.offsetNode, container); |
| 77 | + assert_equals(caretPosition.offset, 0); |
| 78 | + }, "document.caretPositionFromPoint() should return a CaretPosition over video elements"); |
| 79 | + |
| 80 | + test(() => { |
| 81 | + container.setHTMLUnsafe(`<audio controls></audio>`); |
| 82 | + const audio = document.querySelector("audio"); |
| 83 | + const caretPosition = document.caretPositionFromPoint(audio.offsetLeft + audio.offsetWidth / 2, audio.offsetTop + audio.offsetHeight / 2); |
| 84 | + assert_equals(caretPosition.offsetNode, container); |
| 85 | + assert_equals(caretPosition.offset, 0); |
| 86 | + }, "document.caretPositionFromPoint() should return a CaretPosition over audio elements"); |
| 87 | + |
| 88 | + test(() => { |
| 89 | + container.setHTMLUnsafe(`<svg width=100 height=100><circle cx=50 cy=50 r=50 /></svg>`); |
| 90 | + const circle = document.querySelector("circle"); |
| 91 | + const caretPosition = document.caretPositionFromPoint(50, 50); |
| 92 | + assert_equals(caretPosition.offsetNode, circle); |
| 93 | + assert_equals(caretPosition.offset, 0); |
| 94 | + }, "document.caretPositionFromPoint() should return a CaretPosition over SVG elements"); |
| 95 | + |
| 96 | + test(() => { |
| 97 | + container.setHTMLUnsafe(`<div id="textDiv">aaa</div>`); |
48 | 98 | const textDiv = document.getElementById("textDiv");
|
49 | 99 | const rect = textDiv.getBoundingClientRect();
|
50 | 100 | const characterWidth = rect.width / textDiv.textContent.length;
|
|
0 commit comments