section.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /**
  2. * Created by JetBrains PhpStorm.
  3. * User: dongyancen
  4. * Date: 12-9-28
  5. * Time: 下午1:34
  6. * To change this template use File | Settings | File Templates.
  7. */
  8. module('plugins.section');
  9. test('getsections命令',function(){
  10. var editor = te.obj[0];
  11. var html = '<h1>一级标题1</h1><p>段落a</p><h2>二级标题2</h2><p>段落</p><h2>二级标题3</h2><p>段落</p><h3>三级标题4</h3><p>段落</p><h1>一级标题5</h1><p>段落</p>';
  12. editor.setContent(html);
  13. var sectionTree = editor.execCommand('getsections');
  14. equal(sectionTree.children.length, 2, '内容里有两个一级标题');
  15. equal(sectionTree.children[1].title, '一级标题5', '验证章节内容');
  16. same(sectionTree.children[1].startAddress, [8], '验证章节开始位置');
  17. same(sectionTree.children[1].endAddress, [9], '验证章节结束位置');
  18. });
  19. test('deletesection命令',function(){
  20. var editor = te.obj[0];
  21. var html = '<h1>一级标题1</h1><p>段落a</p><h2>二级标题2</h2><p>段落</p><h2>二级标题3</h2><p>段落</p><h3>三级标题4</h3><p>段落</p><h1>一级标题5</h1><p>段落b</p>';
  22. editor.setContent(html);
  23. var sectionTree = editor.execCommand('getsections');
  24. editor.execCommand('deleteSection', sectionTree.children[1]);
  25. equal(sectionTree.children[1].parentNode, null, '验证章节标题是否已删除');
  26. notEqual(editor.body.children[0].innerHTML, '段落a', '不传入keepChild参数时,验证章节内容是否已删除');
  27. editor.setContent(html);
  28. sectionTree = editor.execCommand('getsections');
  29. editor.execCommand('deleteSection', sectionTree.children[0], true);
  30. equal(editor.body.children[0].innerHTML, '段落a', '传入keepChild参数为true时,验证章节内容是否已保留');
  31. });
  32. test('movesection命令',function(){
  33. var editor = te.obj[0];
  34. var html = '<h1>一级标题1</h1><p>段落a</p><h2>二级标题2</h2><p>段落</p><h2>二级标题3</h2><p>段落</p><h3>三级标题4</h3><p>段落</p><h1>一级标题5</h1><p>段落b</p>';
  35. editor.setContent(html);
  36. var sectionTree = editor.execCommand('getsections');
  37. editor.execCommand('movesection', sectionTree.children[1], sectionTree.children[0]);
  38. equal(editor.body.children[0].innerHTML, '一级标题5', ' 移动章节移动到目标章节之前,验证章节是否移动正确');
  39. equal(editor.body.children[1].innerHTML, '段落b', ' 验证移动章节移动到目标章节之前,验证章节内容是否移动正确');
  40. editor.setContent(html);
  41. sectionTree = editor.execCommand('getsections');
  42. editor.execCommand('movesection', sectionTree.children[0], sectionTree.children[1], true);
  43. var len = editor.body.children.length;
  44. equal(editor.body.children[len-2].innerHTML, '一级标题1', ' 移动章节移动到目标章节之前,验证章节是否移动正确');
  45. equal(editor.body.children[len-1].innerHTML, '段落a', ' 验证移动章节移动到目标章节之前,验证章节内容是否移动正确');
  46. });
  47. test('selectsection命令',function(){
  48. var editor = te.obj[0];
  49. var html = '<h1>一级标题1</h1><p>段落a</p><h2>二级标题2</h2><p>段落</p><h2>二级标题3</h2><p>段落</p><h3>三级标题4</h3><p>段落</p><h1>一级标题5</h1><p>段落b</p>';
  50. editor.setContent(html);
  51. var sectionTree = editor.execCommand('getsections');
  52. editor.execCommand('selectsection', sectionTree.children[1]);
  53. var range = editor.selection.getRange();
  54. ua.checkSameHtml($('<div>').append(range.cloneContents()).html(), '<h1>一级标题5</h1><p>段落b</p>', '判断选区内容是否正确');
  55. });