summaryrefslogtreecommitdiffstats
path: root/dom/base/test/test_htmlcopyencoder.xhtml
blob: 8740a54f0298e56e39c0f68545b41236957ec9b6 (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
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
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml">
<!--
-->
<head>
  <title>Test the html copy encoder with XHTML</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=422403">Mozilla Bug </a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
//<![CDATA[
function testHtmlCopyEncoder () {
  const de = SpecialPowers.Ci.nsIDocumentEncoder;
  var encoder = SpecialPowers.Cu.createHTMLCopyEncoder();
  var out, expected;

  var node = document.getElementById('draggable');


  // in the following tests, we must use the OutputLFLineBreak flag, to avoid
  // to have the default line break of the platform in the result, so the test
  // can pass on all platform


  encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly);
  encoder.setContainerNode(node);
  out = encoder.encodeToString();
  expected = 'This is a <em>draggable</em> <br>bit of text.';
  is(out, expected, "test container node ");

  encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly);
  encoder.setNode(node);
  out = encoder.encodeToString();
  expected = "<div id=\"draggable\" ondragstart=\"doDragStartSelection(event)\">This is a <em>draggable</em> <br>bit of text.</div>";
  is(out, expected, "test node");

  var select = window.getSelection();
  select.selectAllChildren(node);
  
  encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly);
  encoder.setSelection(select);
  out = encoder.encodeToString();
  expected = "<div style=\"display: none;\">\n\n<div id=\"draggable\" ondragstart=\"doDragStartSelection(event)\">This is a <em>draggable</em> <br>bit of text.</div>\n\n</div>";
  todo_is(out, expected, "test selection");

  node.nextSibling.data="\nfoo bar\n";
  encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly);
  encoder.setSelection(select);
  out = encoder.encodeToString();
  expected = "<div id=\"draggable\" ondragstart=\"doDragStartSelection(event)\">This is a <em>draggable</em>\n <br>bit of text.</div>";
  todo_is(out, expected, "test selection with additional data");

  node = document.getElementById('aList');

  var select = window.getSelection();
  select.selectAllChildren(node);  

  encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly);
  encoder.setSelection(select);
  out = encoder.encodeToString();
  expected = '<ol id=\"aList\">\n   <li>Lorem ipsum dolor</li>\n  <li>sit amet, <strong>consectetuer</strong> </li>\n  <li>adipiscing elit</li>\n  <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus \naliquet lectus. Nunc vitae eros. Class</li>\n  <li>aptent taciti</li>\n</ol>';
  todo_is(out, expected, "test list selection");

  encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly);
  encoder.setContainerNode(node);
  out = encoder.encodeToString();
  expected = '\n   <li>Lorem ipsum dolor</li>\n  <li>sit amet, <strong>consectetuer</strong> </li>\n  <li>adipiscing elit</li>\n  <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus aliquet lectus. Nunc vitae eros. Class</li>\n  <li>aptent taciti</li>\n';
  is(out, expected, "test list container node");

  encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly);
  encoder.setNode(node);
  out = encoder.encodeToString();
  expected = "<ol id=\"aList\">\n   <li>Lorem ipsum dolor</li>\n  <li>sit amet, <strong>consectetuer</strong> </li>\n  <li>adipiscing elit</li>\n  <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus aliquet lectus. Nunc vitae eros. Class</li>\n  <li>aptent taciti</li>\n</ol>";
  is(out, expected, "test list node");

  var liList = node.getElementsByTagName("li");
  var range = document.createRange();

  // selection start at the first child of the ol, and end after the element ol
  range.setStart(node, 1);
  range.setEnd(node.parentNode, 2);
  select.removeAllRanges();
  select.addRange(range);
  encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly);
  encoder.setSelection(select);
  out = encoder.encodeToString();
  expected = '<ol id="aList">\n   <li>Lorem ipsum dolor</li>\n  <li>sit amet, <strong>consectetuer</strong> </li>\n  <li>adipiscing elit</li>\n  <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus aliquet lectus. Nunc vitae eros. Class</li>\n  <li>aptent taciti</li>\n</ol>';
  todo_is(out, expected, "test list selection with range: selection start at the first child of the ol, and end after the element ol");

  // selection start at the third child of the ol, and end after the element ol
  range.setStart(node, 3);
  range.setEnd(node.parentNode, 2);
  encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly);
  encoder.setSelection(select);
  out = encoder.encodeToString();
  expected = '<ol id="aList"><li value=\"2\">sit amet, <strong>consectetuer</strong> </li>\n  <li>adipiscing elit</li>\n  <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus aliquet lectus. Nunc vitae eros. Class</li>\n  <li>aptent taciti</li>\n</ol>';
  todo_is(out, expected, "test list selection with range: selection start at the third child of the ol, and end after the element ol");


  // selection start at the third child of the ol, and end after the element ol + ol start at the value 5
  range.setStart(node, 3);
  range.setEnd(node.parentNode, 2);
  node.setAttribute("start","5");
  encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly);
  encoder.setSelection(select);
  out = encoder.encodeToString();
  expected = '<ol start=\"5\" id=\"aList\"><li value=\"6\">sit amet, <strong>consectetuer</strong>\n </li>\n  <li>adipiscing elit</li>\n  <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus aliquet lectus. Nunc vitae eros. Class</li>\n  <li>aptent taciti</li>\n</ol>';
  todo_is(out, expected, "test list selection with range: selection start at the third child of the ol, and end after the element ol + ol start at the value 5");


  // selection contains only some child of the ol
  node.removeAttribute("start");
  range.setStart(node, 3);
  range.setEnd(node, 5);
  encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly);
  encoder.setSelection(select);
  out = encoder.encodeToString();
  expected = '<li>sit amet, <strong>consectetuer</strong> </li>\n  ';
  todo_is(out, expected, "test list selection with range: selection contains only some child of the ol");

  // selection contains only some child of the ol  + ol start at the value 5
  node.setAttribute("start","5");
  encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly);
  encoder.setSelection(select);
  out = encoder.encodeToString();
  expected = '<li>sit amet, <strong>consectetuer</strong> </li>\n  ';
  todo_is(out, expected, "test list selection with range: selection contains only some child of the ol + ol start at the value 5");


  // selection contains only some child of the ol  + a value is set on the first li
  node.removeAttribute("start");
  liList[0].setAttribute("value","8");
  encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly);
  encoder.setSelection(select);
  out = encoder.encodeToString();
  expected = '<li>sit amet, <strong>consectetuer</strong> </li>\n  ';
  todo_is(out, expected, "test list selection: contains only some child of the ol  + a value is set on the first li");

  select.selectAllChildren(node);
  encoder.init(document, "text/html",de.OutputLFLineBreak | de.OutputSelectionOnly);
  encoder.setSelection(select);
  out = encoder.encodeToString();
  expected = '<ol id=\"aList\">\n   <li value=\"8\">Lorem ipsum dolor</li>\n  <li>sit amet, <strong>consectetuer</strong> </li>\n  <li>adipiscing elit</li>\n  <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus \naliquet lectus. Nunc vitae eros. Class</li>\n  <li>aptent taciti</li>\n</ol>';
  todo_is(out, expected, "test list selection with a value on a LI");

  SimpleTest.finish();
}


SimpleTest.waitForExplicitFinish();

addLoadEvent(testHtmlCopyEncoder);
//]]>
</script>
</pre>
<div style="display: none">

<div id="draggable" ondragstart="doDragStartSelection(event)">This is a <em>draggable</em> <br/>bit of text.</div>

</div>
<div style="display: none">

<ol id="aList">
   <li>Lorem ipsum dolor</li>
  <li>sit amet, <strong>consectetuer</strong> </li>
  <li>adipiscing elit</li>
  <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus aliquet lectus. Nunc vitae eros. Class</li>
  <li>aptent taciti</li>
</ol>
foo bar
</div>
</body>
</html>