rowspacing.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. module( 'plugins.rowspacing' );
  2. /*trace 1029*/
  3. test( '设置段前距查看状态反射', function () {
  4. var editor = te.obj[0];
  5. var range = te.obj[1];
  6. editor.setContent( '<p>hello1</p><p>hello2</p>' );
  7. stop();
  8. setTimeout(function () {
  9. range.setStart( editor.body.firstChild, 0 ).setEnd( editor.body.lastChild, 1 ).select();
  10. editor.execCommand( 'rowspacing', 15 ,'top');
  11. equal( editor.queryCommandValue( 'rowspacing' ,'top'), 15, '查看段前距' );
  12. /*光标闭合时查看状态反射*/
  13. range.setStart( editor.body.firstChild.firstChild, 1 ).collapse( true ).select();
  14. equal( editor.queryCommandValue( 'rowspacing','top' ), 15, '查看段前距' );
  15. start();
  16. }, 50);
  17. } );
  18. /*trace 1035*/
  19. test( '非闭合清除段前距等样式,查看状态反射', function () {
  20. var editor = te.obj[0];
  21. var range = te.obj[1];
  22. editor.setContent( '<p>hello1</p><p>hello2</p>' );
  23. range.setStart(editor.body.firstChild, 0).setEnd(editor.body.lastChild, 1).select();
  24. editor.execCommand('rowspacing', 20, 'top');
  25. equal(editor.queryCommandValue('rowspacing', 'top'), 20, '段前距为2.0');
  26. } );
  27. test( '闭合清除段前距等样式,查看状态反射', function () {
  28. var editor = te.obj[0];
  29. var range = te.obj[1];
  30. editor.setContent( '<p>hello1</p><p>hello2</p>' );
  31. range.setStart( editor.body.firstChild, 0 ).collapse( true ).select();
  32. editor.execCommand( 'rowspacing', 20 ,'top');
  33. equal( editor.queryCommandValue( 'rowspacing','top' ), 20, '段前距为2.0' );
  34. range.setStart( editor.body.firstChild, 0 ).collapse( true ).select();
  35. editor.execCommand( 'removeformat' );
  36. equal( editor.queryCommandValue( 'rowspacing' ,'top'), 5, '闭合清除格式后,段前距为5' );
  37. } );
  38. /*trace 1026*/
  39. test( '设置段后距后设置字体颜色', function () {
  40. var editor = te.obj[0];
  41. var range = te.obj[1];
  42. // var editor = te.obj[2];
  43. // var div = document.body.appendChild( document.createElement( 'div' ) );
  44. // $( div ).css( 'width', '500px' ).css( 'height', '500px' ).css( 'border', '1px solid #ccc' );
  45. // editor.render(div);
  46. stop();
  47. // setTimeout(function(){
  48. // var range = new baidu.editor.dom.Range( editor.document );
  49. editor.setContent( '<p>hello1<a href="">hello</a></p><p>hello2</p>' );
  50. range.setStart( editor.body.firstChild, 0 ).setEnd( editor.body.lastChild, 1 ).select();
  51. editor.execCommand( 'rowspacing', 15 ,'bottom');
  52. setTimeout(function(){
  53. editor.execCommand( 'forecolor', 'rgb(255,0,0)' );
  54. equal( editor.queryCommandValue( 'rowspacing' ,'bottom'), 15, '查看段后距' );
  55. /*闭合的方式去查看行距的状态反射*/
  56. range.setStart( editor.body.firstChild.firstChild, 1 ).collapse( true ).select();
  57. equal( editor.queryCommandValue( 'rowspacing' ,'bottom'), 15, '查看段后距' );
  58. // div.parentNode.removeChild(div);
  59. start();
  60. },50);
  61. // },50);
  62. } );
  63. test( '设置段后距后设置加粗等多种样式', function () {
  64. var editor = te.obj[0];
  65. var range = te.obj[1];
  66. // var editor = te.obj[2];
  67. // var div = document.body.appendChild( document.createElement( 'div' ) );
  68. // $( div ).css( 'width', '500px' ).css( 'height', '500px' ).css( 'border', '1px solid #ccc' );
  69. // editor.render(div);
  70. stop();
  71. // editor.ready(function(){
  72. setTimeout(function(){
  73. // var range = new baidu.editor.dom.Range( editor.document );
  74. editor.setContent( '<p>hello1</p><p>hello2</p>' );
  75. setTimeout(function(){
  76. range.setStart( editor.body.firstChild, 0 ).setEnd( editor.body.lastChild, 1 ).select();
  77. editor.execCommand( 'rowspacing', 15 ,'bottom');
  78. setTimeout(function(){
  79. editor.execCommand( 'bold' );
  80. editor.execCommand( 'underline' );
  81. equal( editor.queryCommandValue( 'rowspacing' ,'bottom'), 15, '查看段后距' );
  82. // div.parentNode.removeChild(div);
  83. start();
  84. },50);
  85. },50);
  86. },100);
  87. // });
  88. } );
  89. test( '非闭合去除加粗等样式', function () {
  90. var editor = te.obj[0];
  91. var range = te.obj[1];
  92. // var editor = te.obj[2];
  93. // var div = document.body.appendChild( document.createElement( 'div' ) );
  94. // $( div ).css( 'width', '500px' ).css( 'height', '500px' ).css( 'border', '1px solid #ccc' );
  95. // editor.render(div);
  96. stop();
  97. // editor.ready(function(){
  98. // setTimeout(function(){
  99. // var range = new baidu.editor.dom.Range( editor.document );
  100. editor.setContent( '<p>hello1</p><p>hello2</p>' );
  101. setTimeout(function(){
  102. range.setStart( editor.body.firstChild, 0 ).setEnd( editor.body.lastChild, 1 ).select();
  103. editor.execCommand( 'rowspacing', 15 ,'bottom');
  104. setTimeout(function(){
  105. editor.execCommand( 'bold' );
  106. editor.execCommand( 'underline' );
  107. equal( editor.queryCommandValue( 'rowspacing' ,'bottom'), 15, '查看段后距' );
  108. editor.execCommand( 'removeformat' );
  109. equal( editor.queryCommandValue( 'rowspacing' ,'bottom'), 5, '去除样式后查看段后距' );
  110. // div.parentNode.removeChild(div);
  111. start();
  112. },50);
  113. },50);
  114. // },100);
  115. // });
  116. } );
  117. test( '闭合去除样式', function () {
  118. var editor = te.obj[0];
  119. var range = te.obj[1];
  120. // var editor = te.obj[2];
  121. // var div = document.body.appendChild( document.createElement( 'div' ) );
  122. // $( div ).css( 'width', '500px' ).css( 'height', '500px' ).css( 'border', '1px solid #ccc' );
  123. // editor.render(div);
  124. stop();
  125. // editor.ready(function(){
  126. //
  127. // var range = new baidu.editor.dom.Range( editor.document );
  128. editor.setContent( '<p>hello1</p><p>hello2</p>' );
  129. setTimeout(function(){
  130. range.setStart( editor.body.firstChild, 0 ).setEnd( editor.body.lastChild, 1 ).select();
  131. editor.execCommand( 'rowspacing', 15 ,'bottom');
  132. setTimeout(function(){
  133. editor.execCommand( 'bold' );
  134. editor.execCommand( 'underline' );
  135. /*采用闭合的方式查询段后距,
  136. 介个好像用手选不太能选的出来,总是会选到<strong><span>里面去*/
  137. range.setStart( editor.body.firstChild, 0 ).collapse( true ).select();
  138. equal( editor.queryCommandValue( 'rowspacing','bottom' ), 15, '查看段后距' );
  139. /*闭合方式鼠标放在第二个p中*/
  140. range.setStart( editor.body.lastChild, 0 ).collapse( true ).select();
  141. equal( editor.queryCommandValue( 'rowspacing' ,'bottom'), 15, '查看段后距' );
  142. editor.execCommand( 'removeformat' );
  143. //1.2后改
  144. equal( editor.queryCommandValue( 'rowspacing' ,'bottom'), 5, '去除样式后查看段后距' );
  145. /*第一行的样式应当仍然在*/
  146. range.setStart( editor.body.firstChild, 0 ).collapse( true ).select();
  147. equal( editor.queryCommandValue( 'rowspacing' ,'bottom'), 15, '查看段后距' );
  148. // div.parentNode.removeChild(div);
  149. start();
  150. },50);
  151. },50);
  152. // });
  153. } );
  154. test( '表格中设置段距', function () {
  155. var editor = te.obj[0];
  156. var range = te.obj[1];
  157. editor.setContent( '<table><tbody><tr><td>hello1</td><td>hello2</td></tr><tr><td></td><td></td></tr></tbody></table>' );
  158. stop();
  159. setTimeout(function () {
  160. var tds = editor.body.firstChild.getElementsByTagName('td');
  161. /*选中表格中的文本设置段前距*/
  162. range.selectNode(tds[0].firstChild).select();
  163. editor.execCommand('rowspacing', 20, 'top');
  164. equal(editor.queryCommandValue('rowspacing', 'top'), 20, '设置表格中文本段前距为2');
  165. /*采用闭合的方式查询段前距*/
  166. setTimeout(function () {
  167. range.setStart(tds[0].firstChild.firstChild, 1).collapse(true).select();
  168. equal(editor.queryCommandValue('rowspacing', 'top'), 20, '设置表格中文本段前距为2');
  169. /*闭合在表格中的文本设置段后距*/
  170. range.setStart(tds[1].firstChild, 1).collapse(true).select();
  171. editor.execCommand('rowspacing', 15, 'bottom');
  172. /*选中整个单元格查询段后距*/
  173. range.selectNode(tds[1]).select();
  174. equal(editor.queryCommandValue('rowspacing', 'bottom'), 15, '设置表格中文本段后距为1.5');
  175. /*闭合在空白单元格中设置段后距*/
  176. range.setStart(tds[2], 0).collapse(true).select();
  177. editor.execCommand('rowspacing', 25, 'bottom');
  178. equal(editor.queryCommandValue('rowspacing', 'bottom'), 25, '设置表格中文本段后距为2.5');
  179. start();
  180. }, 50);
  181. }, 50);
  182. });
  183. test('跨多个单元格设置段前距', function () {
  184. var editor = te.obj[0];
  185. var range = te.obj[1];
  186. editor.setContent('<table><tbody><tr><td>hello1</td><td>hello2<img /></td></tr><tr><td><div>hello3</div></td><td><p>hello4</p></td></tr></tbody></table>');
  187. stop();
  188. setTimeout(function () {
  189. var tds = editor.body.firstChild.getElementsByTagName('td');
  190. var p = editor.body.getElementsByTagName('p');
  191. range.selectNode(editor.body.firstChild).select();
  192. editor.execCommand('rowspacing', 15, 'top');
  193. for (var index = 0; index < tds.length; index++) {
  194. range.selectNode(tds[index]).select();
  195. equal(editor.queryCommandValue('rowspacing', 'top'), 15, '设置表格中文本段前距为1.5');
  196. /*会自动在非block元素外面套p*/
  197. //1.2版本,加在p上
  198. equal(p[index].style['marginTop'], '15px', '段前距属性都是加在第一个孩子节点上');
  199. }
  200. start();
  201. }, 50);
  202. } );
  203. /*trace 1052*/
  204. test( '对插入的代码设置多倍段前距', function () {
  205. var editor = te.obj[0];
  206. var range = te.obj[1];
  207. editor.setContent( '<p></p>' );
  208. stop();
  209. setTimeout(function () {
  210. range.setStart( editor.body.firstChild, 0 ).collapse( true ).select();
  211. var stylecode = "var tds = editor.body.firstChild.getElementsByTagName( 'td' );\n range.selectNode( editor.body.firstChild ).select();";
  212. editor.execCommand( 'inserthtml', "<pre style='background-color: #F8F8F8;border: 1px solid #CCCCCC;padding:10px 10px'>" + stylecode + "</pre>" );
  213. equal( editor.body.firstChild.tagName.toLowerCase(), 'pre', '第一个孩子节点为pre' );
  214. range.selectNode( editor.body.firstChild ).select();
  215. editor.execCommand( 'rowspacing', 20,'top' );
  216. var pre = editor.body.firstChild;
  217. equal( pre.tagName.toLowerCase(), 'pre', '不允许将p换成pre' );
  218. equal( pre.style['borderWidth'], '1px', '宽度' );
  219. ok( pre.style['borderColor'].toUpperCase() == '#CCCCCC' || pre.style['borderColor'] == 'rgb(204, 204, 204)', '颜色' );
  220. start();
  221. }, 50);
  222. } );
  223. test( '在合并单元格中设置多倍段前距', function () {
  224. var editor = new baidu.editor.Editor({'autoFloatEnabled':false});
  225. stop();
  226. setTimeout( function () {
  227. var div = document.body.appendChild( document.createElement( 'div' ) );
  228. te.dom.push( div );
  229. editor.render( div );
  230. setTimeout(function () {
  231. editor.setContent('<p></p>');
  232. var range = new baidu.editor.dom.Range(editor.document);
  233. range.setStart(editor.body.firstChild, 0).collapse(true).select();
  234. editor.execCommand('inserttable', {numCols:3, numRows:3});
  235. stop();
  236. /*insertHTML有一个200ms的超时函数*/
  237. setTimeout(function () {
  238. ua.manualDeleteFillData(editor.body);
  239. var trs = editor.body.getElementsByTagName('tr');
  240. range.setStart(trs[0].firstChild, 0).setEnd(trs[1].firstChild, 0).select();
  241. editor.currentSelectedArr = [trs[0].firstChild, trs[1].firstChild];
  242. editor.execCommand('mergecells');
  243. /*合并单元格后设置这个单元格多倍段前距*/
  244. ua.manualDeleteFillData(editor.body);
  245. range.setStart(trs[0].firstChild, 0).collapse(true).select();
  246. editor.execCommand('rowspacing', 20, 'top');
  247. ua.manualDeleteFillData(editor.body);
  248. equal(trs[0].firstChild.firstChild.tagName.toLowerCase(), 'p', 'td下面创建了一个p');
  249. equal(trs[0].firstChild.firstChild.style['marginTop'], '20px', 'p设置了2倍行距');
  250. trs = editor.body.firstChild.getElementsByTagName('tr');
  251. equal(trs.length, 3, '3行');
  252. var tbodyChild = editor.body.getElementsByTagName('tbody')[0].childNodes;
  253. for (var index = 0; index < tbodyChild.length; index++) {
  254. equal(tbodyChild[index].tagName.toLowerCase(), 'tr', 'tbody下面都是tr');
  255. }
  256. start();
  257. }, 30);
  258. }, 300);
  259. },50);
  260. } );
  261. /*trace 1079*/
  262. test( '合并单元格后设置多个单元格多倍段前距', function () {
  263. var editor = new baidu.editor.Editor( {'plugins':['table'],'autoFloatEnabled':false} );
  264. stop();
  265. setTimeout(function () {
  266. var div = document.body.appendChild(document.createElement('div'));
  267. te.dom.push(div);
  268. editor.render(div);
  269. setTimeout(function () {
  270. editor.setContent('<p></p>');
  271. var range = new baidu.editor.dom.Range(editor.document);
  272. range.setStart(editor.body.firstChild, 0).collapse(true).select();
  273. editor.execCommand('inserttable', {numCols:3, numRows:3});
  274. setTimeout(function () {
  275. ua.manualDeleteFillData(editor.body);
  276. var trs = editor.body.firstChild.getElementsByTagName('tr');
  277. /*合并第一列前2个单元格*/
  278. range.setStart(trs[0].firstChild, 0).setEnd(trs[1].firstChild, 0).select();
  279. editor.currentSelectedArr = [trs[0].firstChild, trs[1].firstChild];
  280. editor.execCommand('mergecells');
  281. /*设置多倍段前距*/
  282. range.setStart(trs[0].firstChild, 0).setEnd(trs[2].firstChild, 0).select();
  283. editor.currentSelectedArr = [trs[0].firstChild, trs[1].firstChild, trs[2].firstChild];
  284. editor.execCommand('rowspacing', 20, 'top');
  285. trs = editor.body.firstChild.getElementsByTagName('tr');
  286. equal(trs.length, 3, '3行');
  287. var tbodyChild = editor.body.getElementsByTagName('tbody')[0].childNodes;
  288. for (var index = 0; index < tbodyChild.length; index++) {
  289. equal(tbodyChild[index].tagName.toLowerCase(), 'tr', 'tbody下面都是tr');
  290. }
  291. start();
  292. }, 50);
  293. }, 300);
  294. },50);
  295. } );