summaryrefslogtreecommitdiffstats
path: root/accessible/tests/mochitest/textrange/test_selection.html
blob: 2a5d4da5c23d98eb464876ee512793d4b9cb2c05 (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
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
<!DOCTYPE html>
<html>
<head>
  <title>Text Range selection tests</title>
  <link rel="stylesheet" type="text/css"
        href="chrome://mochikit/content/tests/SimpleTest/test.css" />

  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script type="application/javascript"
          src="../common.js"></script>
  <script type="application/javascript"
          src="../text.js"></script>
  <script type="application/javascript"
          src="../layout.js"></script>
  <script type="application/javascript">

    function doTest() {
      var sel = window.getSelection();
      var p = getNode("p1");
      var a = getNode("p2_a");

      var range = document.createRange();
      sel.addRange(range);

      // the accessible is contained by the range
      range.selectNode(p);

      var a11yranges = getAccessible(document, [nsIAccessibleText]).selectionRanges;
      var a11yrange = a11yranges.queryElementAt(0, nsIAccessibleTextRange);

      testTextRange(a11yrange, "selection range #1", document, 3, document, 4);

      ok(a11yrange.crop(getAccessible(a)), "Range failed to crop #1.");
      testTextRange(a11yrange, "cropped range #1", a, 0, a, 5);

      // the range is contained by the accessible
      range.selectNode(a);
      a11yranges = getAccessible(document, [nsIAccessibleText]).selectionRanges;
      a11yrange = a11yranges.queryElementAt(0, nsIAccessibleTextRange);

      testTextRange(a11yrange, "selection range #2", p, 5, p, 6);

      ok(a11yrange.crop(getAccessible(p)), "Range failed to crop #2.");
      testTextRange(a11yrange, "cropped range #2", p, 5, p, 6);

      // the range starts before the accessible and ends inside it
      range.setStart(p, 0);
      range.setEndAfter(a.firstChild, 4);
      a11yranges = getAccessible(document, [nsIAccessibleText]).selectionRanges;
      a11yrange = a11yranges.queryElementAt(0, nsIAccessibleTextRange);

      testTextRange(a11yrange, "selection range #3", p, 0, a, 4);

      ok(a11yrange.crop(getAccessible(a)), "Range failed to crop #3.");
      testTextRange(a11yrange, "cropped range #3", a, 0, a, 4);

      // the range starts inside the accessible and ends after it
      range.setStart(a.firstChild, 1);
      range.setEndAfter(p);
      a11yranges = getAccessible(document, [nsIAccessibleText]).selectionRanges;
      a11yrange = a11yranges.queryElementAt(0, nsIAccessibleTextRange);

      testTextRange(a11yrange, "selection range #4", a, 1, document, 4);

      ok(a11yrange.crop(getAccessible(a)), "Range failed to crop #4.");
      testTextRange(a11yrange, "cropped range #4", a, 1, a, 5);

      // the range ends before the accessible
      range.setStart(p.firstChild, 0);
      range.setEnd(p.firstChild, 4);
      a11yranges = getAccessible(document, [nsIAccessibleText]).selectionRanges;
      a11yrange = a11yranges.queryElementAt(0, nsIAccessibleTextRange);

      testTextRange(a11yrange, "selection range #5", p, 0, p, 4);
      ok(!a11yrange.crop(getAccessible(a)), "Crop #5 succeeded while it shouldn't");

      // the range starts after the accessible
      range.setStart(p.lastChild, 0);
      range.setEnd(p.lastChild, 4);
      a11yranges = getAccessible(document, [nsIAccessibleText]).selectionRanges;
      a11yrange = a11yranges.queryElementAt(0, nsIAccessibleTextRange);

      testTextRange(a11yrange, "selection range #6", p, 6, p, 10);

      ok(!a11yrange.crop(getAccessible(a)), "Crop #6 succeeded while it shouldn't");

      // crop a range by a table
      range.selectNode(getNode("c2"));
      a11yranges = getAccessible(document, [nsIAccessibleText]).selectionRanges;
      a11yrange = a11yranges.queryElementAt(0, nsIAccessibleTextRange);

      testTextRange(a11yrange, "selection range #7", document, 4, document, 5);

      ok(a11yrange.crop(getAccessible("table")), "Range failed to crop #7.");
      testTextRange(a11yrange, "cropped range #7", "table", 0, "table", 1);

      // test compare points for selection with start in nested node
      range.setStart(a.firstChild, 2);
      range.setEnd(p.lastChild, 3);
      a11yranges = getAccessible(document, [nsIAccessibleText]).selectionRanges;
      a11yrange = a11yranges.queryElementAt(0, nsIAccessibleTextRange);
      var res = a11yrange.compareEndPoints(EndPoint_Start, a11yrange, EndPoint_End);
      is(res, -1, "start must be lesser than end");

      res = a11yrange.compareEndPoints(EndPoint_End, a11yrange, EndPoint_Start);
      is(res, 1, "end must be greater than start");

      // Crop a range to its next sibling.
      range.selectNode(getNode("c3p1").firstChild);
      a11yranges = getAccessible(document, [nsIAccessibleText]).selectionRanges;
      a11yrange = a11yranges.queryElementAt(0, nsIAccessibleTextRange);
      testTextRange(a11yrange, "selection range #8", "c3p1", 0, "c3p1", 1);
      ok(!a11yrange.crop(getAccessible("c3p2")), "Crop #8 succeeded but shouldn't have.");
      // Crop a range to its previous sibling.
      range.selectNode(getNode("c3p2").firstChild);
      a11yranges = getAccessible(document, [nsIAccessibleText]).selectionRanges;
      a11yrange = a11yranges.queryElementAt(0, nsIAccessibleTextRange);
      testTextRange(a11yrange, "selection range #9", "c3p2", 0, "c3p2", 1);
      ok(!a11yrange.crop(getAccessible("c3p1")), "Crop #9 succeeded but shouldn't have.");

      SimpleTest.finish();
    }

    SimpleTest.waitForExplicitFinish();
    addA11yLoadEvent(doTest);
  </script>
</head>
<body>

  <a target="_blank"
     title="Implement IAccessible2_3::selectionRanges"
     href="https://bugzilla.mozilla.org/show_bug.cgi?id=1233118">Bug 1233118</a>
  <p id="display"></p>
  <div id="content" style="display: none"></div>
  <pre id="test">
  </pre>

  <p id="p1">text <a id="p2_a" href="www">link<img id="p2_img", src="../moz.png"></a> text</p>

  <div id="c2">start<table id="table"><tr><td>cell</td></tr></table>end</div>

  <div id="c3"><p id="c3p1">a</p><p id="c3p2">b</p></div>
</body>
</html>