summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/editing/run/caretnavigation.html
blob: c0a8eca4ac23e179ab7e8d7df741417564c4fda1 (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
<!DOCTYPE html>
<meta charset="utf-8">
<title>Caret navigation</title>

<link rel="stylesheet" href="../support/reset.css">

<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>

<script>
const KEY_CODE_MAP = {
  'ArrowLeft':  '\uE012',
  'ArrowUp':    '\uE013',
  'ArrowRight': '\uE014',
  'ArrowDown':  '\uE015',
};

/**
 * Send key event to the target element using test driver. Supports human
 * friendly key names for common keyboard scroll operations e.g., arrow keys,
 * page keys, etc.
 * @param {Node} target
 * @param {string} key
 * @returns {Promise}
 */
function keyPress(target, key) {
  const code = KEY_CODE_MAP[key];
  return test_driver.send_keys(target, code);
}
</script>

<div id="container">
  <div contenteditable data-title="Move with ArrowLeft to BR following a div">
    <div>line 1</div>
    <br>
    <div class="caret">line 2</div>
  </div>
  <div contenteditable data-title="Move with ArrowLeft to BR following an img">
    <img style="display:block;width:100px;height:100px">
    <br>
    <div class="caret">line 2</div>
  </div>
</div>

<script>
const container = document.getElementById("container");

for (const test of container.children) {
  promise_test(async t => {
    test.focus();
    getSelection().collapse(test.querySelector(".caret"));
    await keyPress(test, "ArrowLeft");

    const range = getSelection().getRangeAt(0);

    assert_equals(range.commonAncestorContainer.localName, "div");
    assert_equals(range.startOffset, 3);
    assert_equals(range.commonAncestorContainer.childNodes[range.startOffset].localName, "br");
  }, test.dataset.title);
}
</script>