source.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. module('plugins.source');
  2. //test('初始化进入源码模式',function(){
  3. // if(ua.browser.ie>0 && ua.browser.ie<8)
  4. // return 0;
  5. // var div = document.createElement('div');
  6. // document.body.appendChild(div);
  7. // div.id = 'e';
  8. // var editor = UE.getEditor('e');//,{sourceEditorFirst:true}
  9. // stop();
  10. //// editor.ready(function(){
  11. //// setTimeout(function(){
  12. ////// equal(editor.queryCommandState('source'),1,'源码高亮');
  13. //// equal(editor.queryCommandState('bold'),-1,'加粗灰色');
  14. //////// start();
  15. //// },100);
  16. //// });
  17. //});
  18. test('chrome删除后切换源码再切换回来,光标没了', function () {
  19. //opera 取不到range值
  20. if (ua.browser.opera) return 0;
  21. var editor = te.obj[0];
  22. var div = te.dom[0];
  23. editor.render(div);
  24. editor.setContent('hello');
  25. var range = editor.selection.getRange();
  26. range.selectNode(editor.body.firstChild).select();
  27. editor.execCommand('cleardoc');
  28. stop();
  29. expect(2);
  30. //source 包含超时操作,ie下必须有同步操作,否则会报错
  31. setTimeout(function () {
  32. editor.execCommand('source');
  33. setTimeout(function () {
  34. editor.execCommand('source');
  35. start();
  36. }, 20);
  37. }, 20);
  38. range = editor.selection.getRange();
  39. equal(range.startContainer.nodeType, 1, '光标定位在p里');
  40. equal(range.startContainer.tagName.toLowerCase(), 'p', 'startContainer为p');
  41. te.dom.push(div);
  42. });
  43. //TODO 1.2.6
  44. /*trace 986*/
  45. //test( '切换源码,视频地址被添加了网站前缀', function () {
  46. // if ( !ua.browser.ie ) {
  47. // var editor = te.obj[0];
  48. // var range = te.obj[1];
  49. // editor.setContent( '<p><br></p>' );
  50. // setTimeout(function(){
  51. // range.setStart( editor.body.firstChild, 0 ).collapse( 1 ).select();
  52. // /*涉及到video的一些特殊处理,因此直接设置编辑器的html不是很可行,所以这里用了video这个插件*/
  53. // editor.execCommand( 'insertvideo', {url:'www.baidu.com'} );
  54. // setTimeout( function () {
  55. // editor.execCommand( 'source' );
  56. // range.setStart( editor.body.firstChild, 0 ).collapse( 1 ).select();
  57. // setTimeout( function () {
  58. // editor.execCommand( 'source' );
  59. // start();
  60. // }, 50 );
  61. // }, 50 );
  62. //
  63. // var img = editor.document.getElementsByTagName( 'img' )[0];
  64. // equal( $( img ).attr( '_url' ), 'www.baidu.com', '检查超链接前是否添加了网站的路径' );
  65. // },50);
  66. // stop();
  67. // }
  68. // else
  69. // ok( true, 'ie里加了视频节点embed,在节点embed后加bookmark会出错' );
  70. //} );
  71. //trace 852
  72. test('切换源码,源码中多处空行', function () {
  73. var editor = te.obj[0];
  74. editor.setContent('<p>hello<a href="http://www.baidu.com/">baidu</a></p>');
  75. stop();
  76. setTimeout(function () {
  77. editor.execCommand('source');
  78. setTimeout(function () {
  79. editor.execCommand('source');
  80. setTimeout(function () {
  81. var html = editor.getContent();
  82. equal(html, '<p>hello<a href="http://www.baidu.com/">baidu</a></p>');
  83. start();
  84. }, 100);
  85. }, 100);
  86. }, 100);
  87. // var html = '<p>\nhello<a href="http://www.baidu.com/">\n\tbaidu\n</a>\n</p>';
  88. //无奈的验证,有不可见字符
  89. //多余不可见字符的的bug已经修改了,现在用例字符串长度:53
  90. // ok(html.length>=58&&html.length<=60,'切换源码不会多空行');
  91. });
  92. /*trace 710*/
  93. test('设置源码内容没有p标签,切换源码后会自动添加', function () {
  94. var editor = te.obj[0];
  95. editor.setContent('<strong><em>helloworld你好啊</em></strong>大家好,<strong><i>你在干嘛呢</i></strong><em><strong>。谢谢,不用谢</strong></em>~~%199<p>hello</p>');
  96. setTimeout(function () {
  97. editor.execCommand('source');
  98. setTimeout(function () {
  99. editor.execCommand('source');
  100. setTimeout(function () {
  101. editor.execCommand('source');
  102. setTimeout(function () {
  103. var childs = editor.body.childNodes;
  104. ok(childs.length, 3, '3个p');
  105. for (var index = 0; index < 3; index++) {
  106. equal(childs[0].tagName.toLowerCase(), 'p', '第' + index + '个孩子为p');
  107. }
  108. start();
  109. }, 100);
  110. }, 100);
  111. }, 100);
  112. }, 100);
  113. stop();
  114. });
  115. test('切换源码去掉空的span', function () {
  116. var editor = te.obj[0];
  117. editor.setContent('<p>切换源码<span>去掉空的span</span></p>');
  118. setTimeout(function () {
  119. editor.execCommand('source');
  120. setTimeout(function () {
  121. editor.execCommand('source');
  122. start();
  123. }, 100);
  124. }, 100);
  125. stop();
  126. equal(editor.getContent(), '<p>切换源码去掉空的span</p>');
  127. });
  128. test('b,i标签,切换源码后自动转换成strong和em', function () {
  129. var editor = te.obj[0];
  130. editor.setContent('<p><b>加粗的内容</b><i>斜体的内容<b>加粗且斜体</b></i></p>');
  131. setTimeout(function () {
  132. editor.execCommand('source');
  133. setTimeout(function () {
  134. editor.execCommand('source');
  135. start();
  136. }, 100);
  137. }, 100);
  138. stop();
  139. equal(editor.getContent(), '<p><strong>加粗的内容</strong><em>斜体的内容<strong>加粗且斜体</strong></em></p>');
  140. });
  141. test(' trace 3739 trace 1734 range的更新/特殊符号的转换', function () {
  142. var editor = te.obj[0];
  143. editor.setContent('<p>"<></p>');
  144. setTimeout(function () {
  145. editor.execCommand('source');
  146. setTimeout(function () {
  147. editor.execCommand('source');
  148. equal(editor.getContent(), '<p>&quot;&lt;&gt;</p>');
  149. editor.setContent("<p>'<img src='http://nsclick.baidu.com/u.gif?&asdf=\"sdf&asdfasdfs;asdf'></p>");
  150. // var range = te.obj[1];
  151. // range.setStart(editor.body.firstChild,0).collapse(1).select();
  152. setTimeout(function () {
  153. // var label = ua.browser.gecko ? 'html' : 'body';
  154. // var label = 'html';
  155. ua.manualDeleteFillData(editor.body);
  156. var sc = (ua.browser.ie==11)?editor.selection.getRange().startContainer.parentNode.tagName.toLowerCase():editor.selection.getRange().startContainer.parentNode.parentNode.tagName.toLowerCase();
  157. equal(sc, 'html', 'range的更新');
  158. editor.execCommand('source');
  159. setTimeout(function () {
  160. editor.execCommand('source');
  161. equal(editor.getContent(), "<p>&#39;<img src=\"http://nsclick.baidu.com/u.gif?&asdf=&quot;sdf&asdfasdfs;asdf\"/></p>");
  162. start();
  163. }, 100);
  164. }, 100);
  165. }, 100);
  166. }, 100);
  167. stop();
  168. });
  169. /*trace 1234 */
  170. test('默认插入的占位符', function () {
  171. var editor = te.obj[0];
  172. editor.setContent('');
  173. equal(editor.getContent(), '');
  174. });
  175. test('插入分页符,源码中显示:_baidu_page_break_tag_', function () {
  176. var editor = te.obj[0];
  177. var range = te.obj[1];
  178. editor.setContent('<p><br /></p>');
  179. setTimeout(function () {
  180. range.setStart(editor.body.firstChild, 0).collapse(1).select();
  181. editor.execCommand('pagebreak');
  182. ua.manualDeleteFillData(editor.body);
  183. var pagebreak = editor.body.getElementsByTagName('hr')[0];
  184. if (typeof pagebreak.attributes['class'] == "undefined") {
  185. equal(pagebreak.getAttribute('class'), 'pagebreak', 'pagebreak');
  186. }
  187. else {//适用于ie6,7
  188. equal(pagebreak.attributes['class'].nodeValue, 'pagebreak', 'pagebreak');
  189. }
  190. ua.manualDeleteFillData(editor.body);
  191. // var br = baidu.editor.browser.ie ? '&nbsp;' : '<br />';
  192. ok(editor.getContent().indexOf('_ueditor_page_break_tag_') >= 0, 'pagebreak被解析');
  193. // equal( editor.getContent(), '<p>' + br + '</p>_baidu_page_break_tag_<p>' + br + '</p>' );
  194. start();
  195. }, 200);
  196. stop();
  197. });
  198. //TODO 1.2.6
  199. //test( 'trace 1977 1949 插入代码,源码中对应的标签是pre', function () {
  200. // var div = document.body.appendChild( document.createElement( 'div' ) );
  201. // $( div ).css( 'width', '500px' ).css( 'height', '500px' ).css( 'border', '1px solid #ccc' );
  202. // var editor = te.obj[2];
  203. // editor.render(div);
  204. // var range = new baidu.editor.dom.Range( editor.document );
  205. // var body = editor.body;
  206. // stop();
  207. // setTimeout(function(){
  208. // editor.setContent( '<p><br></p>' );
  209. // range.setStart( body.firstChild, 0 ).collapse( 1 ).select();
  210. // editor.execCommand( 'highlightcode', '<a href="http://net.tutsplus.com" class="logo">Nettuts+</a>', 'html' );
  211. // ua.manualDeleteFillData( editor.body );
  212. //
  213. // var td_gutter = body.getElementsByTagName( 'td' )[0];
  214. // var td_code = body.getElementsByTagName( 'td' )[1];
  215. // equal( body.getElementsByTagName( 'td' ).length, 2, '显示代码的table分两列' );
  216. // if(td_gutter!=''){
  217. // if ( typeof td_gutter.attributes['class'] == "undefined" ) {
  218. // equal( td_gutter.getAttribute( 'class' ), 'gutter', '第一列class=gutter' );
  219. // equal( td_code.getAttribute( 'class' ), 'code', '第一列class=code' );
  220. // }
  221. // else {//适用于ie6,7
  222. // equal( td_gutter.attributes['class'].nodeValue, 'gutter', '第一列class=gutter' );
  223. // equal( td_code.attributes['class'].nodeValue, 'code', '第一列class=code' );
  224. // }
  225. // equal( editor.getContent().substring( 0, 119 ), '<pre class=\"brush: html;toolbar:false;\" >&lt;a href=\"http://net.tutsplus.com\" class=\"logo\"&gt;Nettuts+&lt;/a&gt; </pre>' );
  226. // //highlightcode空格问题
  227. //// equal( editor.getContent().substring( 0, 116 ), '<pre class=\"brush:html;toolbar:false;\" >&lt;a href=\"http://net.tutsplus.com\" class=\"logo\"&gt;Nettuts+&lt;/a&gt;</pre>' );
  228. // te.dom.push( div );
  229. // }
  230. // start();
  231. // },50);
  232. //} );
  233. test('不以http://开头的超链接绝对路径网址', function () {
  234. if (ua.browser.ie == 9)return 0;//TODO 1.2.6
  235. var editor = te.obj[0];
  236. editor.setContent('<p><a href="www.baidu.com">绝对路径网址</a></p>');
  237. setTimeout(function () {
  238. editor.execCommand('source');
  239. setTimeout(function () {
  240. editor.execCommand('source');
  241. equal(editor.getContent(), '<p><a href="www.baidu.com">绝对路径网址</a></p>');
  242. start();
  243. }, 100);
  244. }, 100);
  245. stop();
  246. });
  247. test('trace 1727:插入超链接后再插入空格,空格不能被删除', function () {
  248. var editor = te.obj[0];
  249. editor.setContent('<p> <a href="http://www.baidu.com/">绝对路径网址</a> ddd</p>');
  250. setTimeout(function () {
  251. editor.execCommand('source');
  252. setTimeout(function () {
  253. editor.execCommand('source');
  254. equal(editor.body.innerHTML.toLowerCase(), '<p><a href="http://www.baidu.com/" _href=\"http://www.baidu.com/\">绝对路径网址</a> &nbsp;ddd</p>', '查看空格是否被删除');
  255. start();
  256. }, 100);
  257. }, 100);
  258. stop();
  259. });
  260. //TODO 1.2.6 空style未删除
  261. //test( '关于空格的问题', function () {
  262. // var editor = te.obj[0];
  263. // var html = '<ol> <li> dd jj </li> <li> ll kdkd <a href = "http://www.baidu.com/"> baidu </a> </li> </ol>';
  264. // editor.setContent( html );
  265. // setTimeout(function(){
  266. // editor.execCommand( 'source' );
  267. // setTimeout( function () {
  268. // editor.execCommand( 'source' );
  269. // setTimeout( function () {
  270. // ua.manualDeleteFillData( editor.body );
  271. // equal( editor.body.innerHTML.toLowerCase().replace(/[\r\n\t]/g,''), '<ol class=\" list-paddingleft-2\"><li><p>dd&nbsp;jj</p></li><li><p>ll&nbsp;kdkd<a href="http://www.baidu.com/" >&nbsp;baidu&nbsp;</a></p></li></ol>' );
  272. // start();
  273. // }, 150 );
  274. // }, 100 );
  275. // },20);
  276. // stop();
  277. //} );
  278. //TODO 1.2.6
  279. //test('初始化进入源码模式',function(){
  280. // if(ua.browser.ie>0 && ua.browser.ie<8)
  281. // return 0;
  282. // var div = document.createElement('div');
  283. // document.body.appendChild(div);
  284. // var editor = UE.getEditor(div);//,{sourceEditorFirst:true}
  285. // stop();
  286. // editor.ready(function(){
  287. // setTimeout(function(){
  288. //// equal(editor.queryCommandState('source'),1,'源码高亮');
  289. // equal(editor.queryCommandState('bold'),-1,'加粗灰色');
  290. ////// start();
  291. // },100);
  292. // });
  293. //});
  294. test('在font,b,i标签中输入,会自动转换标签 ', function () {
  295. // if(!ua.browser.gecko){
  296. var editor = te.obj[0];
  297. editor.body.innerHTML = '<p><font size="3" color="red"><b><i>x</i></b></font></p>';
  298. setTimeout(function () {
  299. editor.execCommand('source');
  300. setTimeout(function () {
  301. editor.execCommand('source');
  302. equal(editor.body.firstChild.firstChild.tagName.toLowerCase(), 'span', 'font转换成span');
  303. if (ua.browser.gecko || ua.browser.ie)
  304. equal($(editor.body.firstChild.firstChild).css('font-size'), '16px', '检查style');
  305. else
  306. equal($(editor.body.firstChild.firstChild).css('font-size'), '16px', '检查style');
  307. var EMstyle = $(editor.body.firstChild.firstChild).css('color');
  308. ok(EMstyle == 'rgb(255, 0, 0)' || EMstyle == 'red' || EMstyle == '#ff0000', '检查style');
  309. equal(ua.getChildHTML(editor.body.firstChild.firstChild), '<strong><em>x</em></strong>', 'b转成strong,i转成em ');
  310. start();
  311. }, 20);
  312. }, 20);
  313. stop();
  314. // }
  315. });
  316. test('trace 3334:img和a之间不会产生多余空格', function () {
  317. var editor = te.obj[0];
  318. editor.setContent('<p><img src="http://img.baidu.com/hi/jx2/j_0001.gif" /><a href="http://www.baidu.com">http://www.baidu.com</a></p>');
  319. setTimeout(function () {
  320. editor.execCommand('source');
  321. setTimeout(function () {
  322. editor.execCommand('source');
  323. setTimeout(function () {
  324. editor.execCommand('source');
  325. ua.manualDeleteFillData(editor.body);
  326. var html = '<p><img src="http://img.baidu.com/hi/jx2/j_0001.gif" _src=\"http://img.baidu.com/hi/jx2/j_0001.gif\"><a href=\"http://www.baidu.com\" _href=\"http://www.baidu.com\">http://www.baidu.com</a></p>';
  327. ua.checkSameHtml(editor.body.innerHTML.toLowerCase(), html, '查看img和a之间是否会产生多余空格');
  328. start();
  329. }, 20);
  330. }, 20);
  331. }, 20);
  332. stop();
  333. });
  334. test('trace 3334:table中td不会产生多余空格', function () {
  335. if(ua.browser.ie)return ;//todo 1.3.0
  336. var editor = te.obj[0];
  337. editor.execCommand('inserttable');
  338. var br = ua.browser.ie ? '' : '<br>';
  339. setTimeout(function () {
  340. editor.execCommand('source');
  341. setTimeout(function () {
  342. editor.execCommand('source');
  343. ua.manualDeleteFillData(editor.body);
  344. equal(editor.body.getElementsByTagName('table').length, 1, '有1个table');
  345. equal(editor.body.getElementsByTagName('tr').length, 5, '有5个tr');
  346. equal(editor.body.getElementsByTagName('td').length, 25, '有25个td');
  347. equal(editor.body.getElementsByTagName('td')[12].innerHTML, br, '不会产生多余空格');
  348. start();
  349. }, 20);
  350. }, 20);
  351. stop();
  352. });
  353. test('trace 3349:带颜色的span切到源码再切回,不会丢失span', function () {
  354. var editor = te.obj[0];
  355. editor.setContent('<p><span style="color: rgb(255, 0, 0);"></span><br></p>');
  356. setTimeout(function () {
  357. editor.execCommand('source');
  358. setTimeout(function () {
  359. editor.execCommand('source');
  360. ua.checkSameHtml(editor.body.innerHTML, '<p><span style="color: rgb(255, 0, 0);"></span><br></p>');
  361. start();
  362. }, 20);
  363. }, 20);
  364. stop();
  365. });