elementpath.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. module('plugins.elementpath');
  2. /*
  3. <li>表格
  4. <li>列表
  5. <li>文本
  6. <li>图片
  7. <li>超链接
  8. <li>加粗加斜
  9. <li>下划线,删除线
  10. * */
  11. //1.2的版本中,表格的外面会自动套一个带格式的div
  12. test('表格', function () {
  13. var div = document.body.appendChild(document.createElement('div'));
  14. div.id = 'ue';
  15. var editor = UE.getEditor('ue', {'initialContent': '<p>欢迎使用ueditor</p>', 'elementPathEnabled': true, 'autoFloatEnabled': false});
  16. editor.ready(function () {
  17. var range = new baidu.editor.dom.Range(editor.document);
  18. editor.setContent('<table><tbody><tr><td>hello1</td><td><strong>strongText</strong>hello2<span style="text-decoration: underline">spanText</span></td></tr></tbody></table>');
  19. var body = editor.body;
  20. /*选中整个表格*/
  21. range.selectNode(body.firstChild).select();
  22. var eles = editor.queryCommandValue('elementpath');
  23. ua.checkElementPath(eles, ['body', 'table', 'tbody', 'tr', 'td'], '选中整个表格');
  24. /*在单元格中单击*/
  25. var tds = body.getElementsByTagName('td');
  26. range.setStart(tds[0].firstChild, 0).collapse(true).select();
  27. ua.checkElementPath(eles, ['body', 'table', 'tbody', 'tr', 'td'], '在单元格中单击');
  28. /*在单元格中的加粗文本中单击*/
  29. ua.manualDeleteFillData(editor.body);
  30. range.setStart(tds[1].firstChild.firstChild, 1).collapse(true).select();
  31. eles = editor.queryCommandValue('elementpath');
  32. ua.checkElementPath(eles, ['body', 'table', 'tbody', 'tr', 'td', 'strong'], '在单元格中的加粗文本中单击');
  33. /*在单元格中的下划线文本中单击*/
  34. ua.manualDeleteFillData(editor.body);
  35. range.setStart(tds[1].lastChild.firstChild, 1).collapse(true).select();
  36. eles = editor.queryCommandValue('elementpath');
  37. ua.checkElementPath(eles, ['body', 'table', 'tbody', 'tr', 'td', 'span'], '在单元格中的下划线文本中单击');
  38. /*选中有下划线的文本*/
  39. ua.manualDeleteFillData(editor.body);
  40. range.setStart(tds[1].lastChild.lastChild, 1).setEnd(tds[1].lastChild.lastChild, 4).select();
  41. eles = editor.queryCommandValue('elementpath');
  42. ua.checkElementPath(eles, ['body', 'table', 'tbody', 'tr', 'td', 'span'], '选中有下划线的文本');
  43. setTimeout(function(){
  44. UE.delEditor('ue');
  45. te.dom.push(document.getElementById('ue'));
  46. start();
  47. },200);
  48. });
  49. stop();
  50. });
  51. test(' 通过选区路径取range', function () {
  52. var editor = te.obj[0];
  53. var range = te.obj[1];
  54. editor.options.elementPathEnabled = true;
  55. editor.setContent('<table><tbody><tr><td>1</td><td>2</td></tr><tr><td>3</td><td>45</td></tr></tbody></table>');
  56. stop();
  57. setTimeout(function () {
  58. var tds = editor.body.getElementsByTagName('td');
  59. var trs = editor.body.getElementsByTagName('tr');
  60. var tbodys = editor.body.getElementsByTagName('tbody');
  61. var table = editor.body.getElementsByTagName('table');
  62. range.setStart(tds[3].firstChild, 0).collapse(true).select();
  63. editor.queryCommandValue('elementpath');
  64. editor.execCommand('elementpath', '4');
  65. setTimeout(function () {
  66. range = editor.selection.getRange();
  67. if (ua.browser.gecko||ua.browser.webkit) {
  68. ua.checkResult(range, trs[1], trs[1], 1, 2, false, '取range--td');
  69. } else {
  70. if (ua.browser.ie)
  71. ua.checkResult(range, tds[3].firstChild, tds[3].lastChild, 0, 2, false, '取range--td');
  72. else
  73. ua.checkResult(range, tds[3].firstChild, editor.body, 0, 1, false, '取range--td');
  74. }
  75. range.setStart(tds[3].firstChild, 1).collapse(1).select();
  76. editor.execCommand('elementpath', '3');
  77. setTimeout(function () {
  78. range = editor.selection.getRange();
  79. if (ua.browser.gecko||ua.browser.webkit) {
  80. ua.checkResult(range, tbodys[0], tbodys[0], 1, 2, false, '取range--tr');
  81. } else {
  82. if (ua.browser.ie)
  83. ua.checkResult(range, tds[2].firstChild, tds[3].lastChild, 0, 2, false, '取range--tr');
  84. else
  85. ua.checkResult(range, tds[2].firstChild, editor.body, 0, 1, false, '取range--tr');
  86. }
  87. range.setStart(tds[3].firstChild, 0).collapse(1).select();
  88. editor.execCommand('elementpath', '2');
  89. setTimeout(function () {
  90. range = editor.selection.getRange();
  91. if (ua.browser.gecko||ua.browser.webkit) {
  92. ua.checkResult(range, table[0], table[0], 0, 1, false, '取range--tbody');
  93. } else {
  94. if (ua.browser.ie)
  95. ua.checkResult(range, tds[0].firstChild, tds[3].lastChild, 0, 2, false, '取range--tbody');
  96. else
  97. ua.checkResult(range, editor.body, editor.body, 0, 1, false, '取range--tbody');
  98. }
  99. editor.setContent('<p>45645</p>');
  100. range.selectNode(editor.body.firstChild).select();
  101. editor.queryCommandValue('elementpath');
  102. editor.execCommand('elementpath', 1);
  103. setTimeout(function () {
  104. range = editor.selection.getRange();
  105. var p = editor.body.firstChild;
  106. if (ua.browser.gecko||ua.browser.webkit) {
  107. ua.checkResult(range, editor.body, editor.body, 0, 1, false, '取range--p');
  108. } else {
  109. ua.checkResult(range, p.firstChild, p.firstChild, 0, 5, false, '取range--p');
  110. }
  111. start();
  112. }, 20);
  113. }, 20);
  114. }, 100);
  115. }, 20);
  116. }, 50);
  117. });
  118. test('trace 1539:列表', function () {
  119. var div = document.body.appendChild(document.createElement('div'));
  120. var editor = new baidu.editor.Editor({'initialContent': '<p>欢迎使用ueditor</p>', 'elementPathEnabled': true, 'autoFloatEnabled': false});
  121. stop();
  122. setTimeout(function () {
  123. editor.render(div);
  124. editor.ready(function () {
  125. var range = new baidu.editor.dom.Range(editor.document);
  126. editor.setContent('<ol><li>hello1</li><li>hello2<br><table><tbody><tr><td>hello3</td></tr></tbody></table></li></ol>');
  127. var body = editor.body;
  128. /*选中所有列表*/
  129. range.selectNode(body.firstChild).select();
  130. var eles = editor.queryCommandValue('elementpath');
  131. ua.checkElementPath(eles, ['body', 'ol', 'li', 'p'], '选中整个列表');
  132. /*选中列表中的表格*/
  133. range.selectNode(body.firstChild.getElementsByTagName('table')[0]).select();
  134. eles = editor.queryCommandValue('elementpath');
  135. ua.checkElementPath(eles, ['body', 'ol', 'li', 'table', 'tbody', 'tr', 'td'], '选中列表中的表格');
  136. /*选中列表中的br*/
  137. range.setStart(body.firstChild.firstChild.nextSibling.firstChild.firstChild, 6).collapse(true).select();
  138. eles = editor.queryCommandValue('elementpath');
  139. ua.checkElementPath(eles, ['body', 'ol', 'li', 'p'], '选中列表中的br');
  140. div.parentNode.removeChild(div);
  141. start();
  142. });
  143. }, 20);
  144. });
  145. test('文本和超链接', function () {
  146. var div = document.body.appendChild(document.createElement('div'));
  147. var editor = new baidu.editor.Editor({'initialContent': '<p>欢迎使用ueditor</p>', 'elementPathEnabled': true, 'autoFloatEnabled': false});
  148. editor.render(div);
  149. stop();
  150. editor.ready(function () {
  151. var range = new baidu.editor.dom.Range(editor.document);
  152. editor.setContent('<div><p>hello<a>a_link</a></p></div>');
  153. var body = editor.body;
  154. /*选中文本hello*/
  155. range.selectNode(body.firstChild.firstChild).select();
  156. var eles = editor.queryCommandValue('elementpath');
  157. ua.checkElementPath(eles, ['body', 'p'], '选中文本');
  158. /*选中超链接*/
  159. range.selectNode(body.firstChild.lastChild.firstChild).select();
  160. eles = editor.queryCommandValue('elementpath');
  161. ua.checkElementPath(eles, ['body', 'p', 'a'], '选中文本');
  162. div.parentNode.removeChild(div);
  163. start();
  164. });
  165. });
  166. //在版本1.2中,如果没有setTimeout在FF(3.6和9都是)中range会出错,其他浏览器没问题
  167. test('图片', function () {
  168. if(ua.browser.ie>8)return;//todo 1.3.6 #3847
  169. var div = document.body.appendChild(document.createElement('div'));
  170. div.id = "ue";
  171. var editor = UE.getEditor("ue",{'initialContent': '<p>欢迎使用ueditor</p>', 'elementPathEnabled': true, 'autoFloatEnabled': false});
  172. stop();
  173. editor.ready(function () {
  174. var range = new baidu.editor.dom.Range(editor.document);
  175. editor.setContent('<div><p>hello<img /></p></div>');
  176. var body = editor.body;
  177. /*选中图片*/
  178. setTimeout(function () {
  179. range.selectNode(body.firstChild.lastChild).select();
  180. var eles = editor.queryCommandValue('elementpath');
  181. ua.checkElementPath(eles, ['body', 'p', 'img'], '选中图片');
  182. setTimeout(function () {
  183. UE.delEditor('ue');
  184. te.dom.push(document.getElementById('ue'));
  185. start();
  186. }, 200);
  187. }, 20)
  188. });
  189. });
  190. test('锚点', function () {
  191. if(ua.browser.ie>8)return;//todo 1.3.6 #3847
  192. var div = document.body.appendChild(document.createElement('div'));
  193. var editor = new baidu.editor.Editor({'initialContent': '<p>欢迎使用ueditor</p>', 'elementPathEnabled': true, 'autoFloatEnabled': false});
  194. editor.render(div);
  195. stop();
  196. editor.ready(function () {
  197. var range = new baidu.editor.dom.Range(editor.document);
  198. editor.setContent('<div><p>hello<img anchorname="hello" class="anchorclass"></p></div>');
  199. var body = editor.body;
  200. /*选中图片*/
  201. setTimeout(function () {
  202. range.selectNode(body.firstChild.lastChild).select();
  203. var eles = editor.queryCommandValue('elementpath');
  204. ua.checkElementPath(eles, ['body', 'p', 'anchor'], '选中锚点');
  205. div.parentNode.removeChild(div);
  206. start();
  207. }, 20)
  208. });
  209. });
  210. test('文本', function () {
  211. var div = document.body.appendChild(document.createElement('div'));
  212. var editor = new baidu.editor.Editor({'initialContent': '<p>欢迎使用ueditor</p>', 'elementPathEnabled': true, 'autoFloatEnabled': false});
  213. editor.render(div);
  214. stop();
  215. editor.ready(function () {
  216. var range = new baidu.editor.dom.Range(editor.document);
  217. editor.setContent('hello');
  218. var body = editor.body;
  219. /*选中图片*/
  220. setTimeout(function () {
  221. range.setStart(body.firstChild.firstChild, 1).setEnd(body.firstChild.firstChild, 3).select();
  222. var eles = editor.queryCommandValue('elementpath');
  223. ua.checkElementPath(eles, ['body', 'p'], '选中文本');
  224. div.parentNode.removeChild(div);
  225. start();
  226. }, 20)
  227. });
  228. });
  229. test('trace 3995表格和文本', function () {
  230. if(ua.browser.ie==11)return;//todo dev1.4.0
  231. var div = document.body.appendChild(document.createElement('div'));
  232. var editor = new baidu.editor.Editor({'initialContent': '<p>欢迎使用ueditor</p>', 'elementPathEnabled': true, 'autoFloatEnabled': false});
  233. editor.render(div);
  234. stop();
  235. editor.ready(function () {
  236. var range = new baidu.editor.dom.Range(editor.document);
  237. var body = editor.body;
  238. range.setStart(body.firstChild.firstChild, 2).collapse(true).select();
  239. editor.execCommand('inserttable');
  240. /*选中图片*/
  241. setTimeout(function () {
  242. range.selectNode(body).select();
  243. var eles = editor.queryCommandValue('elementpath');
  244. editor.execCommand('elementpath', 1);
  245. ua.checkElementPath(eles, ['body', 'p'], '选中文本和表格');
  246. range.selectNode(body.firstChild.nextSibling).select();
  247. eles = editor.queryCommandValue('elementpath');
  248. ua.checkElementPath(eles, ['body', 'table'], '选中表格');
  249. editor.execCommand('elementpath', 4);
  250. eles = editor.queryCommandValue('elementpath');
  251. ua.checkElementPath(eles, ['body', 'table', 'tbody', 'tr', 'td'], '选中表格');
  252. div.parentNode.removeChild(div);
  253. start();
  254. }, 20);
  255. });
  256. });