summaryrefslogtreecommitdiffstats
path: root/editor/libeditor/tests/test_nsITableEditor_getTableSize.html
blob: 986d8e3e499b74964c1db62c99222cf6bebdfe52 (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
<!DOCTYPE>
<html>
<head>
  <title>Test for nsITableEditor.getTableSize()</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 rowCount = {}, columnCount = {};

  try {
    getTableEditor().getTableSize(undefined, rowCount, columnCount);
    ok(false, "nsITableEditor.getTableSize(undefined) should cause throwing an exception");
  } catch (e) {
    ok(true, "nsITableEditor.getTableSize(undefined) should cause throwing an exception");
  }

  try {
    getTableEditor().getTableSize(null, rowCount, columnCount);
    ok(false, "nsITableEditor.getTableSize(null) should cause throwing an exception");
  } catch (e) {
    ok(true, "nsITableEditor.getTableSize(null) should cause throwing an exception");
  }

  try {
    getTableEditor().getTableSize(editor, rowCount, columnCount);
    ok(false, "nsITableEditor.getTableSize() should cause throwing an exception if given node is not in a <table>");
  } catch (e) {
    ok(true, "nsITableEditor.getTableSize() should cause throwing an exception if given node is not in a <table>");
  }

  // Set id to "test" for the argument for getTableSize().
  // Set data-rows and data-cols to expected count of them.
  const kTests = [
    '<table><tr><td id="test" data-rows="2" data-cols="3">cell1-1</td><td>cell1-2</td><td>cell1-3</td></tr><tr><td>cell2-1</td><td>cell2-2</td><td>cell2-3</td></tr></table>',
    '<table><tr id="test" data-rows="2" data-cols="3"><td>cell1-1</td><td>cell1-2</td><td>cell1-3</td></tr><tr><td>cell2-1</td><td>cell2-2</td><td>cell2-3</td></tr></table>',
    '<table id="test" data-rows="2" data-cols="3"><tr><td>cell1-1</td><td>cell1-2</td><td>cell1-3</td></tr><tr><td>cell2-1</td><td>cell2-2</td><td>cell2-3</td></tr></table>',
    '<table><tr><td>cell1-1</td><td>cell1-2</td><td>cell1-3</td></tr><tr><td>cell2-1</td><td>cell2-2</td><td><p id="test" data-rows="2" data-cols="3">cell2-3</p></td></tr></table>',
    '<table><caption id="test" data-rows="2" data-cols="3">caption</caption><tr><td>cell1-1</td><td>cell1-2</td><td>cell1-3</td></tr><tr><td>cell2-1</td><td>cell2-2</td><td>cell2-3</td></tr></table>',
    '<table id="test" data-rows="0" data-cols="0"></table>',
    '<table id="test" data-rows="0" data-cols="0"><caption>caption</caption></table>',
    '<table id="test" data-rows="1" data-cols="1"><td>cell1-1</td></table>',
    // rowspan does not affect, but colspan affects...
    '<table id="test" data-rows="1" data-cols="12"><tr><td rowspan="8" colspan="12">cell1-1</td></tr></table>',
    '<table id="test" data-rows="1" data-cols="1"><tr><td><table><tr><td>cell1-1</td><td>cell1-2</td></tr><tr><td>cell2-1</td><td>cell2-2</td></tr><tr><td>cell3-1</td><td>cell3-2</td></tr></table></td></tr></table>',
    '<table><tr><td id="test" data-rows="1" data-cols="1"><table><tr><td>cell1-1</td><td>cell1-2</td></tr><tr><td>cell2-1</td><td>cell2-2</td></tr><tr><td>cell3-1</td><td>cell3-2</td></tr></table></td></tr></table>',
    '<table><tr><td><table id="test" data-rows="3" data-cols="2"><tr><td>cell1-1</td><td>cell1-2</td></tr><tr><td>cell2-1</td><td>cell2-2</td></tr><tr><td>cell3-1</td><td>cell3-2</td></tr></table></td></tr></table>',
    '<table><tr><td><table><tr><td id="test" data-rows="3" data-cols="2">cell1-1</td><td>cell1-2</td></tr><tr><td>cell2-1</td><td>cell2-2</td></tr><tr><td>cell3-1</td><td>cell3-2</td></tr></table></td></tr></table>',
    '<table><tr><td><table><tr><td>cell1-1</td><td>cell1-2</td></tr><tr><td>cell2-1</td><td><p id="test" data-rows="3" data-cols="2">cell2-2</p></td></tr><tr><td>cell3-1</td><td>cell3-2</td></tr></table></td></tr></table>',
  ];

  for (const kTest of kTests) {
    editor.innerHTML = kTest;
    let element = document.getElementById("test");
    getTableEditor().getTableSize(element, rowCount, columnCount);
    is(rowCount.value.toString(10), element.getAttribute("data-rows"),
       `Specified an element in a <table> directly, its parent table row count should be retrieved: ${kTest}`);
    is(columnCount.value.toString(10), element.getAttribute("data-cols"),
       `Specified an element in a <table> directly, its parent table column count should be retrieved: ${kTest}`);
    if (element.firstChild && element.firstChild.nodeType == Node.TEXT_NODE) {
      selection.collapse(element.firstChild, 0);
      getTableEditor().getTableSize(null, rowCount, columnCount);
      is(rowCount.value.toString(10), element.getAttribute("data-rows"),
         `Selection is collapsed in a cell element, its parent table row count should be retrieved: ${kTest}`);
      is(columnCount.value.toString(10), element.getAttribute("data-cols"),
         `Selection is collapsed in a cell element, its parent table column count should be retrieved: ${kTest}`);
    }
  }

  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>