defaultfilter.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /**
  2. * Created by JetBrains PhpStorm.
  3. * User: dongyancen
  4. * Date: 13-2-28
  5. * Time: 下午3:20
  6. * To change this template use File | Settings | File Templates.
  7. */
  8. module( 'plugins.defaultfilter' );
  9. //test('',function(){
  10. // stop();
  11. //});
  12. test( 'p标签禁止嵌套', function () {
  13. var editor = te.obj[0];
  14. editor.setContent( '<p align ="center" ><p>123</p><p>456</p></p><dl><dd></dd></dl>' );
  15. var br = ua.browser.ie?'&nbsp;':'<br>';
  16. console.log(editor.getContent());
  17. var html = '<p style=\"text-align:center;\">'+br+'</p><p>123</p><p>456</p><ul class=" list-paddingleft-2"><li><p>'+br+'</p></li></ul>';
  18. ua.checkSameHtml(editor.body.innerHTML,html, 'p标签禁止嵌套');
  19. } );
  20. test( '对代码的行号不处理', function () {
  21. var editor = te.obj[0];
  22. editor.setContent( '<td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div></td>');
  23. // var br = ua.browser.ie?'':'<br>';
  24. var html = '<table><tbody><tr><td class=\"gutter\"><div class=\"line number1 index0 alt2\">1</div><div class=\"line number2 index1 alt1\">2</div></td></tr></tbody></table>';
  25. ua.checkSameHtml(editor.body.innerHTML,html,'table补全,对代码的行号不处理')
  26. } );
  27. test( '空td,th,caption', function () {
  28. var editor = te.obj[0];
  29. editor.setContent( '<table><caption></caption><tbody><tr><th></th><th></th></tr><tr><td></td><td></td></tr><tr><td></td><td></td></tr></tbody></table>' );
  30. var br = ua.browser.ie&&ua.browser.ie<11?'':'<br>';
  31. var html = '<table><caption>'+br+'</caption><tbody><tr><th>'+br+'</th><th>'+br+'</th></tr><tr><td>'+br+'</td><td>'+br+'</td></tr><tr><td>'+br+'</td><td>'+br+'</td></tr></tbody></table>';
  32. ua.checkSameHtml(editor.body.innerHTML,html,'空td,th,caption,添加text')
  33. } );
  34. test( '转换a标签', function () {
  35. var editor = te.obj[0];
  36. editor.setContent( '<a href="http://elearning.baidu.com/url/RepositoryEntry/68616197" target="_blank">' );
  37. var br = ua.browser.ie?'&nbsp;':'<br>';
  38. var html = '<p><a href="http://elearning.baidu.com/url/RepositoryEntry/68616197" target="_blank" _href="http://elearning.baidu.com/url/RepositoryEntry/68616197"></a></p>';
  39. ua.checkSameHtml(html,editor.body.innerHTML,'转换a标签');
  40. } );
  41. test( '转换img标签', function () {
  42. var editor = te.obj[0];
  43. editor.setContent( '<img src="http://www.baidu.com/img/shouye_b5486898c692066bd2cbaeda86d74448.gif" width="270" height="129" style="border: 0px;" />' );
  44. // var html = '<p><img src="http://www.baidu.com/img/shouye_b5486898c692066bd2cbaeda86d74448.gif" width="270" height="129" style="border: 0px;" _src="http://www.baidu.com/img/shouye_b5486898c692066bd2cbaeda86d74448.gif" /></p>';
  45. equal(editor.body.getElementsByTagName('img')[0].getAttribute('_src'),"http://www.baidu.com/img/shouye_b5486898c692066bd2cbaeda86d74448.gif");
  46. } );
  47. test( '删span中的white-space标签', function () {
  48. if(ua.browser.webkit){
  49. var editor = te.obj[0];
  50. editor.setContent( '<span style=" display: block; white-space: nowrap " >sadfsadf</span>' );
  51. var html = '<p><span style=" display: block; ">sadfsadf</span></p>';
  52. ua.checkSameHtml(html,editor.body.innerHTML,'删span中的white-space标签');
  53. }
  54. } );
  55. //TODO 1.2.6
  56. //test( '删p中的margin|padding标签', function () {
  57. // var editor = te.obj[0];
  58. // editor.setContent( '<p style="margin-left: 1em; list-style: none;" >hello</p>' );
  59. // var html = '<p style="list-style: none;">hello</p>';
  60. // ua.checkSameHtml(html,editor.body.innerHTML,'删p中的margin|padding标签');
  61. //} );
  62. test( '给空p加br&&转对齐样式', function () {
  63. var editor = te.obj[0];
  64. editor.setContent( '<p align ="center" ></p>' );
  65. var br = ua.browser.ie?'&nbsp;':'<br>';
  66. // "<p style=\"text-align:center;list-style: none;\"><br></p>"
  67. var html = '<p style=\"text-align:center;\">'+br+'</p>';
  68. ua.checkSameHtml(editor.body.innerHTML,html, '给空p加br&&转对齐样式');
  69. } );
  70. test( '删div', function () {
  71. var editor = te.obj[0];
  72. editor.setContent( '<div class="socore" ><div class="sooption" style="padding: 1px;" ><p>视频</p></div></div>' );
  73. var html = '<p>视频</p>';
  74. ua.checkSameHtml(html,editor.body.innerHTML,'删div');
  75. } );
  76. test( 'allowDivTransToP--false 不转div', function () {
  77. var div = document.body.appendChild(document.createElement('div'));
  78. div.id ='ue';
  79. var editor = UE.getEditor('ue',{allowDivTransToP:false});
  80. stop();
  81. editor.ready(function(){
  82. var html = '<div class="socore" ><div class="sooption" style="padding: 1px;" >视频</div></div>';
  83. editor.setContent( html );
  84. var padding = (ua.browser.ie&&ua.browser.ie<9)?'PADDING-BOTTOM: 1px; PADDING-LEFT: 1px; PADDING-RIGHT: 1px; PADDING-TOP: 1px':'padding: 1px;';
  85. var html_a = '<div class="socore" ><div class="sooption" style="'+padding+'" >视频</div></div>';
  86. ua.checkSameHtml(html_a,editor.body.innerHTML,'不转div');
  87. UE.delEditor('ue');
  88. start();
  89. });
  90. } );
  91. test( 'li', function () {
  92. var editor = te.obj[0];
  93. editor.setContent( '<li style="margin: 0px 0px 0px 6px;" ><a href="http://www.baidu.com/p/pistachio%E5%A4%A9?from=zhidao" class="user-name" >天<i class="i-arrow-down"></i></a></li>' );
  94. var html = '<ul class=" list-paddingleft-2"><li><p><a href="http://www.baidu.com/p/pistachio%E5%A4%A9?from=zhidao" class="user-name" _href="http://www.baidu.com/p/pistachio%E5%A4%A9?from=zhidao">天<em class="i-arrow-down"></em></a></p></li></ul>';
  95. ua.checkSameHtml(html,editor.body.innerHTML,'li');
  96. } );
  97. //<li style="margin: 0px 0px 0px 6px;" ><a href="http://www.baidu.com/p/pistachio%E5%A4%A9?from=zhidao" class="user-name" >pistachio天<i class="i-arrow-down"></i></a></li>
  98. //TODO 现在在过滤机制里面去除无用的标签
  99. test( "getContent--去除无用的空标签:autoClearEmptyNode==true", function() {
  100. var div = document.body.appendChild(document.createElement('div'));
  101. div.id = 'ue';
  102. var editor = UE.getEditor('ue',{autoClearEmptyNode:true,'autoFloatEnabled':false});
  103. //
  104. stop();
  105. editor.ready(function () {
  106. te.dom.push(div);
  107. editor.focus();
  108. var innerHTML = '<span><span></span><strong>xx</strong><em>em</em><em></em></span><div>xxxx</div>';
  109. editor.setContent(innerHTML);
  110. editor.execCommand('source');
  111. setTimeout(function () {
  112. editor.execCommand('source');
  113. setTimeout(function () {
  114. equal(editor.getContent(), '<p><strong>xx</strong><em>em</em></p><p>xxxx</p>', "span style空,套空的em和不空的em");
  115. //style="color:#c4bd97;"
  116. innerHTML = '<span style="color:#c4bd97"><span></span><strong>xx</strong><em>em</em><em></em></span>';
  117. editor.setContent(innerHTML);
  118. if (ua.browser.ie>8) {
  119. ua.checkSameHtml(editor.getContent().toLowerCase(), '<p><span style="color: rgb(196, 189, 151);" ><strong>xx</strong><em>em</em></span></p>', "span style不空,套空的em和不空的em");
  120. }
  121. else {
  122. ua.checkSameHtml(editor.getContent().toLowerCase(),'<p><span style="color:#c4bd97" ><strong>xx</strong><em>em</em></span></p>', "span style不空,套空的em和不空的em");
  123. }
  124. innerHTML = '<span style="color:#c4bd97"></span><strong>xx</strong><em>em</em><em></em>';
  125. editor.setContent(innerHTML);
  126. /*inline标签上只要有属性就不清理*/
  127. if (ua.browser.ie >8) {
  128. ua.checkSameHtml(editor.getContent().toLowerCase(), '<p><span style="color: rgb(196, 189, 151);" ></span><strong>xx</strong><em>em</em></p>', "span 有style但内容为空");
  129. }
  130. else {
  131. ua.checkSameHtml(editor.getContent().toLowerCase(), '<p><span style="color:#c4bd97" ></span><strong>xx</strong><em>em</em></p>', "span 有style但内容为空");
  132. }
  133. innerHTML = '<span style="color:#c4bd97">asdf<strong>xx</strong><em>em</em><em></em></span>';
  134. editor.setContent(innerHTML);
  135. if (ua.browser.ie >8) {
  136. ua.checkSameHtml(editor.getContent().toLowerCase(), '<p><span style="color: rgb(196, 189, 151);" >asdf<strong>xx</strong><em>em</em></span></p>', "span 有style内容不空");
  137. }
  138. else {
  139. ua.checkSameHtml(editor.getContent().toLowerCase(), '<p><span style="color:#c4bd97" >asdf<strong>xx</strong><em>em</em></span></p>', "span 有style内容不空");
  140. }
  141. innerHTML = '<a href="http://www.baidu.com"></a><a>a</a><strong>xx</strong><em>em</em><em></em>';
  142. editor.setContent(innerHTML);
  143. ua.checkSameHtml(editor.getContent(), '<p><a href="http://www.baidu.com" ></a><a>a</a><strong>xx</strong><em>em</em></p>', "a 有href但内容为空,不过滤a标签");
  144. setTimeout(function () {
  145. UE.delEditor('ue');
  146. start()
  147. },300);
  148. }, 50);
  149. }, 50);
  150. });
  151. });
  152. //editor.options.autoClearEmptyNode
  153. test("getContent--不去除无用的空标签:autoClearEmptyNode==false", function() {
  154. var div = document.body.appendChild(document.createElement('div'));
  155. div.id = 'ue';
  156. var editor = UE.getEditor('ue',{autoClearEmptyNode:false,'autoFloatEnabled':false});
  157. stop();
  158. editor.ready(function () {
  159. te.dom.push(div);
  160. editor.focus();
  161. var innerHTML = '<span><span></span><strong>xx</strong><em>em</em><em></em><strong></strong></span>';
  162. editor.setContent(innerHTML);
  163. equal(editor.getContent().toLowerCase(), '<p><span><span></span><strong>xx</strong><em>em</em><em></em><strong></strong></span></p>', "span style空,套空的em和不空的em");
  164. innerHTML = '<span style="color:#c4bd97"></span><strong>xx</strong><em>em</em><em></em><strong></strong>';
  165. editor.setContent(innerHTML);
  166. ua.manualDeleteFillData(editor.body);
  167. if (ua.browser.ie >8) {
  168. ua.checkSameHtml(editor.getContent().toLowerCase(), '<p><span style="color: rgb(196, 189, 151);" ></span><strong>xx</strong><em>em</em><em></em><strong></strong></p>', "span 有style但内容为空");
  169. }
  170. else {
  171. ua.checkSameHtml(editor.getContent().toLowerCase(), '<p><span style="color:#c4bd97" ></span><strong>xx</strong><em>em</em><em></em><strong></strong></p>', "span 有style但内容为空");
  172. }
  173. setTimeout(function () {
  174. UE.delEditor('ue');
  175. start()
  176. }, 500);
  177. });
  178. });
  179. test("getContent--转换空格,nbsp与空格相间显示", function() {
  180. var editor = te.obj[0];
  181. editor.focus();
  182. //策略改变,原nbsp不做处理,类似:'<p> d </p>'中的空格会被过滤
  183. var innerHTML = '<div>x x x &nbsp;x&nbsp;&nbsp; &nbsp;</div>';
  184. editor.setContent(innerHTML);
  185. equal(editor.getContent(), '<p>x &nbsp;x &nbsp; x &nbsp;x&nbsp;&nbsp; &nbsp;&nbsp;</p>', "转换空格,nbsp与空格相间显示");
  186. });
  187. test( '转换script标签', function () {
  188. var editor = te.obj[0];
  189. var br = ua.browser.ie?'<p>&nbsp;</p>':('<p><br></p>');
  190. editor.setContent( '<script type="text/javascript">ueditor</script>' );
  191. var html = br+'<div type="text/javascript" cdata_tag=\"script\" cdata_data=\"ueditor\" _ue_custom_node_=\"true\"></div>';
  192. ua.checkSameHtml(editor.body.innerHTML,html,'转换script标签');
  193. } );
  194. test( 'trace 3698 1.3.0 版本修复: script(style)标签里面的内容不转码', function () {
  195. var editor = te.obj[0];
  196. editor.setContent('<script type="text/plain" id="myEditor" name="myEditor">var ue=UE.getEditor("editor");</script>');
  197. equal(editor.document.getElementById('myEditor').innerHTML,'','内容不保留');//1.3.6 针对ie下标签不能隐藏问题的修复
  198. // todo 1.3.0 trace 3698
  199. editor.setContent('<style type="text/css" id="myEditor"> .clear { clear: both; } </style>');
  200. var br = ua.browser.ie?'<p>&nbsp;</p>':('<p><br></p>');
  201. ua.checkSameHtml(editor.getContent(),br+'<style type="text/css" id="myEditor">.clear { clear: both; }</style>','内容不转码');
  202. } );
  203. test( '转换style标签:style data不为空', function () {
  204. var editor = te.obj[0];
  205. editor.setContent( '<style type="text/css">sdf</style>' );
  206. var br = ua.browser.ie?'<p>&nbsp;</p>':('<p><br></p>');
  207. var html = br+'<div type="text/css" cdata_tag="style" cdata_data=\"sdf\" _ue_custom_node_=\"true\"></div>';
  208. ua.checkSameHtml(editor.body.innerHTML,html,'转换script标签');
  209. } );
  210. test( '转换style标签:style data不空', function () {
  211. var editor = te.obj[0];
  212. editor.setContent( '<style type="text/css"></style>' );
  213. var br = ua.browser.ie?'<p>&nbsp;</p>':('<p><br></p>');
  214. var html = br+'<div type="text/css" cdata_tag="style" _ue_custom_node_=\"true\"></div>';
  215. ua.checkSameHtml(editor.body.innerHTML,html,'转换script标签');
  216. } );
  217. test( 'div出编辑器转换', function () {
  218. var editor = te.obj[0];
  219. var str = '<script type="text/javascript">ueditor</script>' ;
  220. var html = '<div type="text/javascript" cdata_tag=\"script\" >ueditor</div>';
  221. editor.body.innerHTML = html;
  222. editor.execCommand( 'source' );
  223. stop();
  224. setTimeout(function(){
  225. equal(editor.getContent(),str,'div出编辑器转换');
  226. start();
  227. },20);
  228. } );
  229. test( 'img出编辑器转换', function () {
  230. var editor = te.obj[0];
  231. var str = ua.browser.ie? '<p><img src="http://www.baidu.com/img/shouye_b5486898c692066bd2cbaeda86d74448.gif" width="270" height="129" style="border: 0px currentColor;"/></p>':'<p><img src="http://www.baidu.com/img/shouye_b5486898c692066bd2cbaeda86d74448.gif" width="270" height="129" style="border: 0px"/></p>' ;
  232. if(ua.browser.ie==8)
  233. str ='<p><img src="http://www.baidu.com/img/shouye_b5486898c692066bd2cbaeda86d74448.gif" width="270" height="129" style=\"BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; BORDER-TOP: 0px; BORDER-RIGHT: 0px\"/></p>';
  234. if(ua.browser.ie==11)
  235. str = '<p><img src="http://www.baidu.com/img/shouye_b5486898c692066bd2cbaeda86d74448.gif" width="270" height="129" style="border: 0px currentColor;border-image: none;"/></p>';
  236. var html = '<p><img src="http://www.baidu.com/img/shouye_b5486898c692066bd2cbaeda86d74448.gif" width="270" height="129" style="border: 0px;" _src="http://www.baidu.com/img/shouye_b5486898c692066bd2cbaeda86d74448.gif" /></p>';
  237. editor.body.innerHTML = html;
  238. editor.execCommand( 'source' );
  239. stop();
  240. setTimeout(function(){
  241. ua.checkSameHtml(editor.getContent(),str,'img出编辑器转换');
  242. start();
  243. },20);
  244. } );
  245. //ue.setContent('<a href="http://elearning.baidu.com/url/RepositoryEntry/68616197" target="_blank">');