DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (ef8f91e97c99)

VCS Links

Line Code
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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
<!DOCTYPE>
<html>
<head>
  <title>Test for nsITableEditor.getNextSelectedCell()</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css">
</head>
<body>
<div id="display">
</div>
<div id="content" contenteditable></div>
<pre id="test">
</pre>

<script class="testbody" type="application/javascript">

SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(function() {
  let editor = document.getElementById("content");
  let selection = document.getSelection();
  let rangeWrapper = {};

  editor.innerHTML =
    '<table id="table">' +
      '<tr id="r1"><td id="c1-1">cell1-1</td><td id="c1-2">cell1-2</td><td id="c1-3">cell1-3</td><td id="c1-4" colspan="2" rowspan="2">cell1-4</td></tr>' +
      '<tr id="r2"><th id="c2-1" rowspan="2">cell2-1</th><td id="c2-2">cell2-2<td id="c2-3">cell2-3</td></tr>' +
      '<tr id="r3"><td id="c3-2">cell3-2</td><td id="c3-3">cell3-3</td><td id="c3-4" colspan="2">cell3-4</td></tr>' +
      '<tr id="r4"><td id="c4-1" rowspan="4">cell4-1</td><td id="c4-2">cell4-2</td><td id="c4-3">cell4-3</td><th id="c4-4">cell4-4</th><td id="c4-5">cell4-5</td></tr>' +
      '<tr id="r5"><th id="c5-2">cell5-2</th><th id="c5-3" colspan="2">' +
        '<table><tr id="r2-1"><td id="c2-1-1">cell2-1-1</td></tr></table>' +
        '</th><td id="c5-5">cell5-5</td></tr>' +
      '<tr id="r6"><td id="c6-2">cell6-2</td><td id="c6-3">cell6-3</td><td id="c6-4"><p>cell6-4</p></td><td id="c6-5">cell6-5</td></tr>' +
      '<tr id="r7"><td id="c7-2" colspan="4">cell7-2</td></tr>' +
    "</table>";

  let tr = document.getElementById("r1");
  selection.setBaseAndExtent(tr, 0, tr, 1);
  let cell = SpecialPowers.unwrap(getTableEditor().getFirstSelectedCell(rangeWrapper));
  is(cell.getAttribute("id"), "c1-1",
     "#1-1 nsITableEditor.getFirstSelectedCell() should return the first cell element in the first row");

  cell = SpecialPowers.unwrap(getTableEditor().getNextSelectedCell(rangeWrapper));
  is(cell, null,
     "Next of #1-1 nsITableEditor.getNextSelectedCell() should return null if there is only one selected range");

  selection.removeAllRanges();
  tr = document.getElementById("r1");
  let range = document.createRange();
  range.setStart(tr, 1);
  range.setEnd(tr, 2);
  selection.addRange(range);
  range = document.createRange();
  range.setStart(tr, 2);
  range.setEnd(tr, 3);
  selection.addRange(range);
  range = document.createRange();
  range.setStart(tr, 3);
  range.setEnd(tr, 4);
  selection.addRange(range);
  tr = document.getElementById("r2");
  range = document.createRange();
  range.setStart(tr, 0);
  range.setEnd(tr, 1);
  selection.addRange(range);
  range = document.createRange();
  range.setStart(tr, 1);
  range.setEnd(tr, 2);
  selection.addRange(range);

  cell = SpecialPowers.unwrap(getTableEditor().getFirstSelectedCell(rangeWrapper));
  is(cell.getAttribute("id"), "c1-2",
     "#1-2 nsITableEditor.getFirstSelectedCell() should return the second cell element in the first row");

  cell = SpecialPowers.unwrap(getTableEditor().getNextSelectedCell(rangeWrapper));
  is(cell.getAttribute("id"), "c1-3",
     "#1-3 nsITableEditor.getNextSelectedCell() should return the third cell element in the first row");
  is(rangeWrapper.value.startContainer, document.getElementById("r1"),
     "#1-3 nsITableEditor.getNextSelectedCell() should return a range whose startContainer is the first row");
  is(rangeWrapper.value.startOffset, 2,
     "#1-3 nsITableEditor.getNextSelectedCell() should return a range whose startOffset is 2");
  is(rangeWrapper.value.endContainer, document.getElementById("r1"),
     "#1-3 nsITableEditor.getNextSelectedCell() should return a range whose endContainer is the first row");
  is(rangeWrapper.value.endOffset, 3,
     "#1-3 nsITableEditor.getNextSelectedCell() should return a range whose endOffset is 3");

  cell = SpecialPowers.unwrap(getTableEditor().getNextSelectedCell(rangeWrapper));
  is(cell.getAttribute("id"), "c1-4",
     "#1-4 nsITableEditor.getNextSelectedCell() should return the forth cell element in the first row");
  is(rangeWrapper.value.startContainer, document.getElementById("r1"),
     "#1-4 nsITableEditor.getNextSelectedCell() should return a range whose startContainer is the first row");
  is(rangeWrapper.value.startOffset, 3,
     "#1-4 nsITableEditor.getNextSelectedCell() should return a range whose startOffset is 3");
  is(rangeWrapper.value.endContainer, document.getElementById("r1"),
     "#1-4 nsITableEditor.getNextSelectedCell() should return a range whose endContainer is the first row");
  is(rangeWrapper.value.endOffset, 4,
     "#1-4 nsITableEditor.getNextSelectedCell() should return a range whose endOffset is 4");

  cell = SpecialPowers.unwrap(getTableEditor().getNextSelectedCell(rangeWrapper));
  is(cell.getAttribute("id"), "c2-1",
     "#2-1 nsITableEditor.getNextSelectedCell() should return the first cell element in the second row");
  is(rangeWrapper.value.startContainer, document.getElementById("r2"),
     "#2-1 nsITableEditor.getNextSelectedCell() should return a range whose startContainer is the second row");
  is(rangeWrapper.value.startOffset, 0,
     "#2-1 nsITableEditor.getNextSelectedCell() should return a range whose startOffset is 0");
  is(rangeWrapper.value.endContainer, document.getElementById("r2"),
     "#2-1 nsITableEditor.getNextSelectedCell() should return a range whose endContainer is the second row");
  is(rangeWrapper.value.endOffset, 1,
     "#2-1 nsITableEditor.getNextSelectedCell() should return a range whose endOffset is 1");

  cell = SpecialPowers.unwrap(getTableEditor().getNextSelectedCell(rangeWrapper));
  is(cell.getAttribute("id"), "c2-2",
     "#2-2 nsITableEditor.getNextSelectedCell() should return the second cell element in the second row");
  is(rangeWrapper.value.startContainer, document.getElementById("r2"),
     "#2-2 nsITableEditor.getNextSelectedCell() should return a range whose startContainer is the second row");
  is(rangeWrapper.value.startOffset, 1,
     "#2-2 nsITableEditor.getNextSelectedCell() should return a range whose startOffset is 1");
  is(rangeWrapper.value.endContainer, document.getElementById("r2"),
     "#2-2 nsITableEditor.getNextSelectedCell() should return a range whose endContainer is the second row");
  is(rangeWrapper.value.endOffset, 2,
     "#2-2 nsITableEditor.getNextSelectedCell() should return a range whose endOffset is 2");

  cell = SpecialPowers.unwrap(getTableEditor().getNextSelectedCell(rangeWrapper));
  is(cell, null,
     "Next of #2-2 nsITableEditor.getNextSelectedCell() should return null if we reached the last cell");

  selection.removeAllRanges();
  tr = document.getElementById("r6");
  range = document.createRange();
  range.setStart(tr, 1);
  range.setEnd(tr, 2);
  selection.addRange(range);
  range = document.createRange();
  range.setStart(document.getElementById("c6-4").firstChild, 0);
  range.setEnd(document.getElementById("c6-4").firstChild, 1);
  selection.addRange(range);
  range = document.createRange();
  range.setStart(tr, 3);
  range.setEnd(tr, 4);
  selection.addRange(range);

  cell = SpecialPowers.unwrap(getTableEditor().getFirstSelectedCell(rangeWrapper));
  is(cell.getAttribute("id"), "c6-3",
     "#6-3 nsITableEditor.getFirstSelectedCell() should return the second cell element in the sixth row");

  // The <p> element in c6-4 is selected.  In this case, the range is ignored
  // by getNextSelectedCell().  So, next call should return the last range.

  cell = SpecialPowers.unwrap(getTableEditor().getNextSelectedCell(rangeWrapper));
  is(cell.getAttribute("id"), "c6-5",
     "#6-5 nsITableEditor.getNextSelectedCell() should return the third cell element in the sixth row");
  is(rangeWrapper.value.startContainer, document.getElementById("r6"),
     "#6-5 nsITableEditor.getNextSelectedCell() should return a range whose startContainer is the sixth row");
  is(rangeWrapper.value.startOffset, 3,
     "#6-5 nsITableEditor.getNextSelectedCell() should return a range whose startOffset is 3");
  is(rangeWrapper.value.endContainer, document.getElementById("r6"),
     "#6-5 nsITableEditor.getNextSelectedCell() should return a range whose endContainer is the sixth row");
  is(rangeWrapper.value.endOffset, 4,
     "#6-5 nsITableEditor.getNextSelectedCell() should return a range whose endOffset is 4");

  cell = SpecialPowers.unwrap(getTableEditor().getNextSelectedCell(rangeWrapper));
  is(cell, null,
     "Next of #6-5 nsITableEditor.getNextSelectedCell() should return null if we reached the last cell");

  selection.removeAllRanges();
  tr = document.getElementById("r2");
  range = document.createRange();
  range.setStart(tr, 2);
  range.setEnd(tr, 3);
  selection.addRange(range);
  range = document.createRange();
  range.setStart(document.getElementById("c4-1").firstChild, 0);
  range.setEnd(document.getElementById("c4-1").firstChild, 7);
  selection.addRange(range);
  tr = document.getElementById("r7");
  range = document.createRange();
  range.setStart(tr, 0);
  range.setEnd(tr, 1);
  selection.addRange(range);

  cell = SpecialPowers.unwrap(getTableEditor().getFirstSelectedCell(rangeWrapper));
  is(cell.getAttribute("id"), "c2-3",
     "#2-3 nsITableEditor.getFirstSelectedCell() should return the third cell element in the second row");

  // c4-1 is not selected even though it contains a range of Selection.
  // In this case, getNextSelectedCell() returns null.
  cell = SpecialPowers.unwrap(getTableEditor().getNextSelectedCell(rangeWrapper));
  is(cell, null,
     "#4-1 nsITableEditor.getNextSelectedCell() should return null if the range does not select <td> nor <th> element");
  is(rangeWrapper.value, null,
     "#4-1 nsITableEditor.getNextSelectedCell() should return null to the range if the range does not select <td> nor <th> element");

  // Although c7-2 is selected, but if getNextSelectedCell() meets a range which
  // does not select <td> nor <th>, it stops incrementing its internal counter.
  // So, following getNextSelectedCell() should return null.
  cell = SpecialPowers.unwrap(getTableEditor().getNextSelectedCell(rangeWrapper));
  is(cell, null,
     "#7-2 nsITableEditor.getNextSelectedCell() should return null if it reached a range which does not select <td> nor <th>");

  selection.removeAllRanges();
  tr = document.getElementById("r3");
  range = document.createRange();
  range.setStart(tr, 0);
  range.setEnd(tr, 1);
  selection.addRange(range);
  tr = document.getElementById("r2-1");
  range = document.createRange();
  range.setStart(tr, 0);
  range.setEnd(tr, 1);
  selection.addRange(range);
  tr = document.getElementById("r7");
  range = document.createRange();
  range.setStart(tr, 0);
  range.setEnd(tr, 1);
  selection.addRange(range);

  cell = SpecialPowers.unwrap(getTableEditor().getFirstSelectedCell(rangeWrapper));
  is(cell.getAttribute("id"), "c3-2",
     "#3-2 nsITableEditor.getFirstSelectedCell() should return the first cell element in the third row");

  // c2-1-1 is in another <table>, however, getNextSelectedCell() returns it
  // since it works only with ranges of Selection.
  cell = SpecialPowers.unwrap(getTableEditor().getNextSelectedCell(rangeWrapper));
  is(cell.getAttribute("id"), "c2-1-1",
     "#2-1-1 nsITableEditor.getNextSelectedCell() should return the cell element in the child <table> element");
  is(rangeWrapper.value.startContainer, document.getElementById("r2-1"),
     "#2-1-1 nsITableEditor.getNextSelectedCell() should return a range whose startContainer is the row in the child <table> element");
  is(rangeWrapper.value.startOffset, 0,
     "#2-1-1 nsITableEditor.getNextSelectedCell() should return a range whose startOffset is 0");
  is(rangeWrapper.value.endContainer, document.getElementById("r2-1"),
     "#2-1-1 nsITableEditor.getNextSelectedCell() should return a range whose endContainer is the row in the child <table> element");
  is(rangeWrapper.value.endOffset, 1,
     "#2-1-1 nsITableEditor.getNextSelectedCell() should return a range whose endOffset is 1");

  cell = SpecialPowers.unwrap(getTableEditor().getNextSelectedCell(rangeWrapper));
  is(cell.getAttribute("id"), "c7-2",
     "#7-2 nsITableEditor.getNextSelectedCell() should return the cell element in the last row");
  is(rangeWrapper.value.startContainer, document.getElementById("r7"),
     "#7-2 nsITableEditor.getNextSelectedCell() should return a range whose startContainer is the last row");
  is(rangeWrapper.value.startOffset, 0,
     "#7-2 nsITableEditor.getNextSelectedCell() should return a range whose startOffset is 0");
  is(rangeWrapper.value.endContainer, document.getElementById("r7"),
     "#7-2 nsITableEditor.getNextSelectedCell() should return a range whose endContainer is the last row");
  is(rangeWrapper.value.endOffset, 1,
     "#7-2 nsITableEditor.getNextSelectedCell() should return a range whose endOffset is 1");

  cell = SpecialPowers.unwrap(getTableEditor().getNextSelectedCell(rangeWrapper));
  is(cell, null,
     "Next of #7-2 nsITableEditor.getNextSelectedCell() should return null if we reached the last cell");

  SimpleTest.finish();
});

function getTableEditor() {
  var Ci = SpecialPowers.Ci;
  var editingSession = SpecialPowers.wrap(window).docShell.editingSession;
  return editingSession.getEditorForWindow(window).QueryInterface(Ci.nsITableEditor);
}

</script>
</body>

</html>