summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/css/css-pseudo/text-selection.html
blob: 42acd0aa52a25b3f92c0cfc47089fcd79c71c75c (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
<!DOCTYPE html>
<meta charset="utf-8">
<title>CSS Pseudo-Elements Test: Text selection</title>
<link rel="help" href="https://drafts.csswg.org/css-pseudo-4/#treelike">
<link rel="help" href="https://drafts.csswg.org/css-ui/#user-interaction">
<link rel="author" title="Oriol Brufau" href="mailto:obrufau@igalia.com">
<meta name="assert" content="This test checks that text in a ::before or ::marker pseudo-element can't be selected." />
<link rel="stylesheet" type="text/css" href="/fonts/ahem.css" />
<style>
.test {
  font: 10px/1 Ahem;
  margin-left: 200px;
}
#before::before {
  content: "::before";
  display: inline-block;
  margin-left: -80px;
}
#marker {
  display: list-item;
  list-style-type: "::marker";
}
#before-marker::before {
  content: "";
  display: list-item;
  list-style-type: "::marker";
  height: 0;
}
</style>
<div class="test" id="before"><span>helloworld</span></div>
<div class="test" id="marker"><span>helloworld</span></div>
<div class="test" id="before-marker"><span>helloworld</span></div>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-actions.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script>
function createSelection(element, start, end, step) {
  getSelection().empty();
  step = Math.abs(step);
  const actions = new test_driver.Actions();
  actions.pointerMove(start, 0, {origin: element});
  actions.pointerDown();
  if (start < end) {
    for (let x = start + step; x < end; x += step)
      actions.pointerMove(x, 0, {origin: element});
  } else {
    for (let x = start - step; x > end; x -= step)
      actions.pointerMove(x, 0, {origin: element});
  }
  actions.pointerMove(end, 0, {origin: element});
  actions.pointerUp();
  return actions.send();
}
(async function() {
  setup({ explicit_done: true });
  for (let target of document.querySelectorAll(".test")) {
    const contents = target.querySelector("span");
    const text = contents.firstChild;
    const s = getSelection();

    // Create a selection that starts in the middle of the element contents
    // and ends in the middle of the pseudo-element
    await createSelection(contents, 0, -90, 10);
    test(function(t) {
      assert_equals(s.toString(), "hello", "toString");
      assert_equals(s.anchorNode, text, "anchorNode");
      assert_equals(s.anchorOffset, 5, "anchorOffset");
      assert_equals(s.focusNode, text, "focusNode");
      assert_equals(s.focusOffset, 0, "focusOffset");
    }, "Selection ending in ::" + target.id);

    // Create a selection that starts and ends inside the pseudo-element.
    await createSelection(contents, -80, -110, 10);
    test(function(t) {
      assert_equals(s.toString(), "", "toString");
      assert_in_array(s.anchorNode, [text, null], "anchorNode");
      assert_equals(s.anchorOffset, 0, "anchorOffset");
      assert_in_array(s.focusNode, [text, null], "focusNode");
      assert_equals(s.focusOffset, 0, "focusOffset");
    }, "Selection contained in ::" + target.id);
  }
  done();
})();
</script>