|
6 | 6 | <link rel="help" href="https://www.w3.org/TR/cssom-view-1/#dom-document-caretpositionfrompoint"> |
7 | 7 | <script src="/resources/testharness.js"></script> |
8 | 8 | <script src="/resources/testharnessreport.js"></script> |
| 9 | +<link rel="stylesheet" href="/fonts/ahem.css"> |
| 10 | +<style> |
| 11 | +textarea { |
| 12 | + font: 20px/1 Ahem; |
| 13 | + border: none; |
| 14 | + padding: 0; |
| 15 | +} |
| 16 | +</style> |
| 17 | + |
9 | 18 | <div id="container"></div> |
10 | 19 | <script> |
11 | 20 |
|
|
44 | 53 | assert_equals(caretPosition.offset, 0); |
45 | 54 | }, "document.caretPositionFromPoint() should return a CaretPosition at the specified location pointing to an input element which is the offsetNode."); |
46 | 55 |
|
47 | | -test(() => { |
48 | | - container.setHTMLUnsafe(`<textarea rows="2" cols="4">12345678901234567890</textarea>`); |
| 56 | +promise_test(async () => { |
| 57 | + container.setHTMLUnsafe(`<textarea rows="3" cols="4">12345678\n901234567890</textarea>`); |
| 58 | + await document.fonts.ready; |
49 | 59 | const rect = container.firstChild.getBoundingClientRect(); |
50 | 60 | // Get x and y coordinate at "1234|5678..." |
51 | 61 | const x = rect.left + 1; |
52 | | - const y = rect.top + rect.height * 0.75; |
| 62 | + const y = rect.top + rect.height * 0.5; |
53 | 63 | const caretPosition = document.caretPositionFromPoint(x, y); |
54 | 64 | assert_true(caretPosition instanceof CaretPosition); |
55 | 65 | assert_true(caretPosition.offsetNode instanceof Node); |
|
58 | 68 | assert_equals(caretPosition.offset, 4); |
59 | 69 | }, "document.caretPositionFromPoint() should return a CaretPosition at the specified location pointing to a textarea element which is the offsetNode."); |
60 | 70 |
|
| 71 | +promise_test(async () => { |
| 72 | + container.setHTMLUnsafe(`<textarea rows="3" cols="4">12345678\n901234567890</textarea>`); |
| 73 | + await document.fonts.ready; |
| 74 | + const element = container.firstChild; |
| 75 | + const rect = element.getBoundingClientRect(); |
| 76 | + const fontSize = parseInt(getComputedStyle(element).fontSize.match(/\d+/)[0]); |
| 77 | + |
| 78 | + // Check a position after a forced break. |
| 79 | + const caretPosition = document.caretPositionFromPoint( |
| 80 | + rect.left + fontSize + 2, rect.bottom - fontSize / 2); |
| 81 | + // caretPosition should point between '9' and '0'. |
| 82 | + assert_equals(caretPosition.offsetNode, element); |
| 83 | + assert_equals(caretPosition.offset, 10, 'offset'); |
| 84 | + const caretRect = caretPosition.getClientRect(); |
| 85 | + assert_equals(caretRect.left, rect.left + fontSize, 'caretRect.left'); |
| 86 | + assert_greater_than(caretRect.bottom, rect.top + rect.height * 0.9, |
| 87 | + 'caretRect.bottom'); |
| 88 | +}, "document.caretPositionFromPoint() for a point after a forced break should return a CaretPosition at the specified location pointing to a textarea element which is the offsetNode."); |
| 89 | + |
61 | 90 | test(() => { |
62 | 91 | container.setHTMLUnsafe(`a<div id="host"></div>b`); |
63 | 92 | const shadowRoot = host.attachShadow({mode: 'closed'}); |
|
0 commit comments