insertcode.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. module('plugins.insertcode');
  2. //test('',function(){stop();})
  3. test('trace 3343:插入代码中有空行', function () {
  4. var editor = te.obj[0];
  5. var range = te.obj[1];
  6. editor.setContent('hello');
  7. ua.keydown(editor.body, {'keyCode':65, 'ctrlKey':true});
  8. editor.execCommand('insertcode', 'Java');
  9. range.setStart(editor.body.firstChild, 0).collapse(true).select();
  10. ua.keydown(editor.body, {'keyCode':13});
  11. range.setStart(editor.body.firstChild, 0).collapse(true).select();
  12. ua.keydown(editor.body, {'keyCode':13});
  13. range.setStart(editor.body.firstChild, 0).collapse(true).select();
  14. range.insertNode(editor.document.createTextNode('hello'));
  15. var br = ua.browser.ie ? '' : '<br>';
  16. if (ua.browser.ie)
  17. ua.checkSameHtml(editor.body.innerHTML, '<PRE class=brush:Java;toolbar:false>​<BR>hello​​<BR>​​hello</PRE><P></P>', '插入代码');
  18. else if (ua.browser.gecko||ua.browser.webkit)
  19. ua.checkSameHtml(editor.body.innerHTML, '<pre class="brush:Java;toolbar:false">hello<br><br>hello</pre>', '插入代码');
  20. else
  21. ua.checkSameHtml(editor.body.innerHTML, '<pre class="brush:Java;toolbar:false">hello<br><br>hello</pre><p>' + br + '</p>', '插入代码');
  22. setTimeout(function () {
  23. editor.execCommand('source');
  24. setTimeout(function () {
  25. editor.execCommand('source');
  26. var br = ua.browser.ie ? '' : '<br>';
  27. if (ua.browser.ie&&ua.browser.ie<9)
  28. ua.checkSameHtml(editor.body.innerHTML, "<PRE class=brush:Java;toolbar:false>hello<BR>hello<BR></PRE><P>&nbsp;</P>", '样式不变');
  29. else if (ua.browser.gecko||ua.browser.webkit)
  30. ua.checkSameHtml(editor.body.innerHTML, '<pre class="brush:Java;toolbar:false">hello<br><br>hello<br></pre>', '样式不变');
  31. start();
  32. }, 20);
  33. }, 20);
  34. stop();
  35. });
  36. test('trace 3355:不闭合选区插入代码', function () {
  37. var editor = te.obj[0];
  38. var code = '&lt;div id=&quot;upload&quot; style=&quot;display: none&quot; &gt;&lt;img id=&quot;uploadBtn&quot;&gt;&lt;/div&gt;';
  39. editor.setContent(code);
  40. setTimeout(function () {
  41. ua.keydown(editor.body, {'keyCode': 65, 'ctrlKey': true});
  42. editor.execCommand('insertcode', 'html');
  43. var br = ua.browser.ie ? '' : '<br>';
  44. if (ua.browser.gecko || ua.browser.opera ||ua.browser.webkit)
  45. ua.checkSameHtml(editor.body.innerHTML, '<pre class="brush:html;toolbar:false">&lt;div id=\"upload\" style=\"display: none\" &gt;&lt;img id=\"uploadBtn\"&gt;&lt;/div&gt;</pre>', '检查插入了html');
  46. else
  47. ua.checkSameHtml(editor.body.innerHTML, '<pre class="brush:html;toolbar:false">&lt;div id=\"upload\" style=\"display: none\" &gt;&lt;img id=\"uploadBtn\"&gt;&lt;/div&gt;</pre><p>' + br + '</p>', '检查插入了html');
  48. start();
  49. }, 50);
  50. stop();
  51. });
  52. test('trace 3395:插入代码为空时,清空编辑器', function () {
  53. var editor = te.obj[0];
  54. var range = te.obj[1];
  55. editor.setContent('');
  56. editor.execCommand('insertcode', 'html');
  57. var br = ua.browser.ie ? '&nbsp;' : '<br>';
  58. if (ua.browser.gecko||ua.browser.ie > 10)
  59. ua.checkSameHtml(editor.body.firstChild.outerHTML, '<pre class="brush:html;toolbar:false"><br></pre>', '检查插入了html');
  60. else if (ua.browser.ie > 8)
  61. ua.checkSameHtml(editor.body.firstChild.outerHTML, '<pre class="brush:html;toolbar:false"></pre>', '检查插入了html');
  62. else
  63. ua.checkSameHtml(editor.body.firstChild.outerHTML, '<pre class="brush:html;toolbar:false">' + br + '</pre>', '检查插入了html');
  64. range.setStart(editor.body.firstChild, 0).collapse(true).select();
  65. range.insertNode(editor.document.createTextNode('hello'));//TODO bug修复把此行删除
  66. ua.keydown(editor.body, {'keyCode':65, 'ctrlKey':true});
  67. ua.keydown(editor.body, {'keyCode':8});
  68. br = ua.browser.ie ? '' : '<br>';
  69. ua.checkSameHtml(editor.body.innerHTML, '<p>' + br + '</p>', '检查编辑器清空');
  70. if(!ua.browser.ie){
  71. editor.setContent('<pre class="brush:html;toolbar:false"></pre>');
  72. editor.fireEvent('delkeydown',{});
  73. ua.checkSameHtml(editor.body.innerHTML, '<p>' + br + '</p>', '检查编辑器清空');
  74. }
  75. editor.setContent('<pre class="brush:html;toolbar:false"></pre>');
  76. ua.keydown(editor.body, {'keyCode':40});
  77. if(ua.browser.ie==8||ua.browser.ie==11)
  78. br = '<br>'
  79. if(ua.browser.ie==8)return;
  80. ua.checkSameHtml(editor.body.innerHTML,'<pre class="brush:html;toolbar:false">'+br+'</pre>','方向键处理');
  81. });
  82. test('trace 3396:多次切换源码,不会产生空行', function () {
  83. var editor = te.obj[0];
  84. editor.setContent('<p>&lt;body&gt;</p><p>&lt;/body&gt;</p>');
  85. ua.keydown(editor.body, {'keyCode':65, 'ctrlKey':true});
  86. editor.execCommand('insertcode', 'html');
  87. var br = (ua.browser.ie==9||ua.browser.ie==10) ? '\n' : '<br>';
  88. var p = editor.body.firstChild.outerHTML.toLowerCase();
  89. var x ='\"';
  90. if(ua.browser.ie<9&&ua.browser.ie)x='';
  91. equal(p, '<pre class='+x+'brush:html;toolbar:false'+x+'>&lt;body&gt;'+br+'&lt;/body&gt;</pre>', '检查插入了html')
  92. ua.checkSameHtml(editor.body.firstChild.outerHTML, '<pre class="brush:html;toolbar:false">&lt;body&gt;'+br+'&lt;/body&gt;</pre>', '检查插入了html');
  93. //todo 1.3.6 3853
  94. setTimeout(function () {
  95. editor.execCommand('source');
  96. setTimeout(function () {
  97. editor.execCommand('source');
  98. var end = (ua.browser.ie==9||ua.browser.ie==10) ?'':'<br>';
  99. br =(ua.browser.ie==9||ua.browser.ie==10) ?'\n':'<br>';
  100. var Bbr =( ua.browser.ie&&ua.browser.ie<9)?'\n':'';
  101. ua.checkSameHtml(editor.body.firstChild.innerHTML, '&lt;body&gt;'+Bbr+br+'&lt;/body&gt;'+end, '切回源码无影响');
  102. // setTimeout(function() {//TODO bug修复后去掉注释
  103. // editor.execCommand('source');
  104. // setTimeout(function() {
  105. // editor.execCommand('source');
  106. // ua.checkSameHtml(editor.body.firstChild.innerHTML,'&lt;body&gt;<br>&lt;/body&gt;<br>','切回源码无影响');
  107. // setTimeout(function () {
  108. // UE.delEditor('ue');
  109. // document.getElementById('ue')&&te.dom.push(document.getElementById('ue'));
  110. start();
  111. }, 20);
  112. }, 20);
  113. stop();
  114. });
  115. test('trace 3407:表格中插入代码', function () {
  116. var editor = te.obj[0];
  117. var range = te.obj[1];
  118. editor.setContent('');
  119. editor.execCommand('inserttable');
  120. stop();
  121. setTimeout(function () {
  122. var tds = editor.body.getElementsByTagName('td');
  123. tds[1].innerHTML = 'asd';
  124. range.setStart(tds[1], 0).setEnd(tds[1], 1).select();
  125. editor.execCommand('insertcode', 'Javascript');
  126. var br = ua.browser.ie ? '&nbsp;' : '<br>';
  127. ua.checkSameHtml(tds[1].innerHTML, '<pre class="brush:Javascript;toolbar:false">asd</pre>', '检查插入了html');
  128. // stop();
  129. // setTimeout(function() {//TODO bug
  130. // editor.execCommand('source');
  131. // setTimeout(function() {
  132. // editor.execCommand('source');
  133. // ua.checkSameHtml(tds[1].innerHTML,'<pre class="brush:Javascript;toolbar:false">asd</pre><br>','切回源码无影响');
  134. // start();
  135. // },20);
  136. // },20);
  137. start();
  138. }, 50);
  139. });
  140. test('test-beforeInsertHTML', function(){
  141. var editor = te.obj[0];
  142. var range = te.obj[1];
  143. editor.setContent('<pre class="brush:html;toolbar:false"><br/></pre>');
  144. //闭合
  145. range.setStart(editor.body.firstChild,0).collapse(true).select();
  146. var insert = 'text';
  147. editor.execCommand('inserthtml', insert);
  148. if(ua.browser.ie==9||ua.browser.ie==10)
  149. ua.checkSameHtml(editor.body.innerHTML, '<pre class="brush:html;toolbar:false">'+insert+'</pre>', '插入IE');
  150. else
  151. ua.checkSameHtml(editor.body.innerHTML, '<pre class="brush:html;toolbar:false">'+insert+'<br></pre>', '插入chrome/ff/ie11+');
  152. ua.manualDeleteFillData(editor.body);
  153. //插入非br element
  154. range.setStart(editor.body.firstChild.firstChild,0).collapse(true).select();
  155. insert='<p>I</p>';
  156. editor.execCommand('inserthtml', insert);
  157. if(ua.browser.ie==9||ua.browser.ie==10)
  158. ua.checkSameHtml(editor.body.innerHTML, '<pre class="brush:html;toolbar:false">Itext</pre>', '插入IE');
  159. else
  160. ua.checkSameHtml(editor.body.innerHTML, '<pre class="brush:html;toolbar:false">Itext<br></pre>', '插入chrome/ff/ie11+');
  161. ua.manualDeleteFillData(editor.body);
  162. //插入br element
  163. range.setStart(editor.body.firstChild.firstChild,1).collapse(true).select();
  164. insert='<br>br';
  165. editor.execCommand('inserthtml', insert);
  166. if(ua.browser.ie==9||ua.browser.ie==10){
  167. ua.checkSameHtml(editor.body.innerHTML, '<pre class="brush:html;toolbar:false">\nbrItext</pre>', '插入IE');
  168. }
  169. else if(ua.browser.ie>10){
  170. ua.checkSameHtml(editor.body.innerHTML, '<pre class="brush:html;toolbar:false">I\nbrtext<br></pre>', '插入IE11+');
  171. }
  172. else{
  173. ua.checkSameHtml(editor.body.innerHTML, '<pre class="brush:html;toolbar:false">I​<br>brtext<br></pre>', '插入chrome/ff');}
  174. ua.manualDeleteFillData(editor.body);
  175. //混合标签
  176. range.setStart(editor.body.firstChild,0).collapse(true).select();
  177. insert='<p>PPP<p>222</p><span>SSS</span><br>BBB</p>';
  178. editor.execCommand('inserthtml', insert);
  179. if(ua.browser.ie==9||ua.browser.ie==10){
  180. // if(ua.browser.ie<11){
  181. ua.checkSameHtml(editor.body.innerHTML, '<pre class="brush:html;toolbar:false">PPP222SSS\nBBB\nbrItext</pre>', '插入IE');
  182. // }else{
  183. // ua.checkSameHtml(editor.body.innerHTML, '<pre class="brush:html;toolbar:false">PPP222SSS\nBBBI\nbrtext</pre>', '插入IE');
  184. // }
  185. }
  186. else if(ua.browser.ie>10){
  187. ua.checkSameHtml(editor.body.innerHTML, '<pre class="brush:html;toolbar:false">PPP222SSS\nBBBI\nbrtext<br></pre>', '插入IE11+');
  188. }
  189. else{
  190. ua.checkSameHtml(editor.body.innerHTML, '<pre class="brush:html;toolbar:false">PPP222SSS<br>BBBI<br>brtext<br></pre>', '插入chrome/ff/ie11+');
  191. }
  192. ua.manualDeleteFillData(editor.body);
  193. //非闭合
  194. //插入非element
  195. range.setStart(editor.body.firstChild, 0).setEnd(editor.body.firstChild, 4).select();
  196. insert = 'replace';
  197. editor.execCommand('inserthtml', insert);
  198. if(ua.browser.ie==9||ua.browser.ie==10)
  199. ua.checkSameHtml(editor.body.innerHTML, '<pre class="brush:html;toolbar:false">replace</pre>', '插入IE');
  200. else if(ua.browser.ie>10){
  201. ua.checkSameHtml(editor.body.innerHTML, '<pre class="brush:html;toolbar:false">replace<br></pre>', '插入IE11+');
  202. }
  203. else
  204. ua.checkSameHtml(editor.body.innerHTML, '<pre class="brush:html;toolbar:false">replaceBBBI<br>brtext<br></pre>', '插入chrome/ff/ie11+');
  205. ua.manualDeleteFillData(editor.body);
  206. //插入element
  207. range.setStart(editor.body.firstChild, 0).setEnd(editor.body.firstChild, 0).select();
  208. insert = '<p>PPP</p>';
  209. editor.execCommand('inserthtml', insert);
  210. if(ua.browser.ie==9||ua.browser.ie==10)
  211. ua.checkSameHtml(editor.body.innerHTML, '<pre class="brush:html;toolbar:false">PPPreplace</pre>', '插入IE');
  212. else if(ua.browser.ie>10){
  213. ua.checkSameHtml(editor.body.innerHTML, '<pre class="brush:html;toolbar:false">PPPreplace<br></pre>', '插入IE11+');
  214. }
  215. else
  216. ua.checkSameHtml(editor.body.innerHTML, '<pre class="brush:html;toolbar:false">PPPreplaceBBBI<br>brtext<br></pre>', '插入chrome/ff/ie11+');
  217. ua.manualDeleteFillData(editor.body);
  218. });
  219. test('关于pre中的tabKey',function(){
  220. var editor = te.obj[0];
  221. var range = te.obj[1];
  222. editor.setContent('<pre class="brush:Javascript;toolbar:false">function a(){var a = true;}</pre>');
  223. var text = editor.body.firstChild.firstChild;
  224. range.setStart(text,13).setEnd(text,16).select();
  225. ua.keydown(editor.body,{'shiftKey':false,'keyCode':9});
  226. ua.keyup(editor.body,{'shiftKey':false,'keyCode':9});
  227. if(ua.browser.ie==8||ua.browser.ie==9){
  228. equal(editor.getContent(),'<pre class=\"brush:Javascript;toolbar:false\">&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;a(){var&nbsp;a&nbsp;=&nbsp;true;}</pre>');
  229. }else if(ua.browser.ie>9){
  230. equal(editor.getContent(),'<pre class=\"brush:Javascript;toolbar:false\">&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;a(){var&nbsp;a&nbsp;=&nbsp;true;}</pre>','验证pre下tabKey1');
  231. }else{
  232. equal(editor.getContent(),'<pre class="brush:Javascript;toolbar:false">&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;a(){var&nbsp;a&nbsp;=&nbsp;true;}</pre>','验证pre下tabKey1');
  233. }
  234. editor.setContent('<pre class="brush:Javascript;toolbar:false"><br>function a(){var a = true;}</pre>');
  235. var text = editor.body.firstChild.firstChild;
  236. range.setStart(text,13).setEnd(text,16).select();
  237. ua.keydown(editor.body,{'shiftKey':false,'keyCode':9});
  238. ua.keyup(editor.body,{'shiftKey':false,'keyCode':9});
  239. if(ua.browser.ie==8||ua.browser.ie==9){
  240. var x = '\n';
  241. if(ua.browser.ie==9){
  242. x = '';
  243. }
  244. equal(editor.getContent(),'<pre class=\"brush:Javascript;toolbar:false\">&nbsp;&nbsp;&nbsp;&nbsp;'+x+'function&nbsp;a(){var&nbsp;a&nbsp;=&nbsp;true;}</pre>','验证pre下tabKey2');
  245. }else if(ua.browser.ie>9){
  246. var x2 = '';
  247. var x3 = '&nbsp;&nbsp;&nbsp;&nbsp;';
  248. if(ua.browser.ie==11){
  249. x2 = '&nbsp;&nbsp;&nbsp;&nbsp;';
  250. x3='\n';
  251. }
  252. equal(editor.getContent(),'<pre class=\"brush:Javascript;toolbar:false\">'+x3+'function&nbsp;a(){var&nbsp;a&nbsp;=&nbsp;true;}</pre>'+x2,'验证pre下tabKey2');
  253. }else{
  254. equal(editor.getContent(),'<pre class="brush:Javascript;toolbar:false">\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;a(){var&nbsp;a&nbsp;=&nbsp;true;}</pre>','验证pre下tabKey2');
  255. }
  256. editor.setContent('<pre class="brush:Javascript;toolbar:false">function a(){<br>var a = true;}</pre>');
  257. var text = editor.body.firstChild.firstChild;
  258. range.setStart(text,13).setEnd(text,16).select();
  259. ua.keydown(editor.body,{'shiftKey':false,'keyCode':9});
  260. ua.keyup(editor.body,{'shiftKey':false,'keyCode':9});
  261. if(ua.browser.ie==8){
  262. equal(editor.getContent(),'<pre class=\"brush:Javascript;toolbar:false\">function&nbsp;a(){&nbsp;&nbsp;&nbsp;&nbsp;\nvar&nbsp;a&nbsp;=&nbsp;true;}</pre>','验证pre下tabKey3');
  263. }else if(ua.browser.ie>8){
  264. var xx = '&nbsp;&nbsp;&nbsp;&nbsp;';
  265. var xx2 = '';
  266. if(ua.browser.ie==11){
  267. xx = '';
  268. xx2 = '&nbsp;&nbsp;&nbsp;&nbsp;';
  269. }
  270. equal(editor.getContent(),'<pre class=\"brush:Javascript;toolbar:false\">'+xx+'function&nbsp;a(){'+xx2+'\nvar&nbsp;a&nbsp;=&nbsp;true;}</pre>','验证pre下tabKey3');
  271. }else{
  272. equal(editor.getContent(),'<pre class="brush:Javascript;toolbar:false">function&nbsp;a(){&nbsp;&nbsp;&nbsp;&nbsp;\nvar&nbsp;a&nbsp;=&nbsp;true;}</pre>','验证pre下tabKey3');
  273. }
  274. });