autolink.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. module('plugins.autolink');
  2. //test('', function () {
  3. // stop()
  4. //});
  5. //自动添加的功能是针对非ie的,单测用例同样只针对非ie,仍需手动测试检验ie与非ie下效果是否一致
  6. test('输入超链接后回车', function () {
  7. if (!ua.browser.ie) {
  8. var editor = te.obj[0];
  9. var range = te.obj[1];
  10. var body = editor.body;
  11. editor.setContent('<p>http://www.baidu.com</p>');
  12. stop();
  13. setTimeout(function () {
  14. range.setStart(body.firstChild.firstChild, body.firstChild.firstChild.length).collapse(1).select();
  15. setTimeout(function () {
  16. ua.keydown(editor.body, {'keyCode': 13});
  17. ua.keyup(editor.body, {'keyCode': 13});
  18. var a = body.firstChild.getElementsByTagName('a')[0];
  19. if(a){
  20. equal(ua.getChildHTML(a), 'http://www.baidu.com', '检查a的内容');
  21. ok(a && $(a).attr('href').indexOf('http://www.baidu.com') != -1, '检查a的href');
  22. ok(a && $(a).attr('_src').indexOf('http://www.baidu.com') != -1, '检查a的_src');
  23. }else{
  24. var text = editor.getContent();
  25. equal(text, '<p>http://www.baidu.com</p>', '检查p的内容');
  26. }
  27. start();
  28. }, 20);
  29. }, 20);
  30. }
  31. });
  32. test('输入超链接后按空格', function () {
  33. if (!ua.browser.ie) {
  34. var editor = te.obj[0];
  35. var range = te.obj[1];
  36. var body = editor.body;
  37. setTimeout(function () {
  38. editor.setContent('<p>http://www.baidu.com</p>');
  39. range.setStart(body.firstChild, 1).collapse(1).select();
  40. ua.keydown(editor.body, {'keyCode': 32});
  41. setTimeout(function () {
  42. var a = body.firstChild.getElementsByTagName('a')[0];
  43. if(a){
  44. equal(ua.getChildHTML(a), 'http://www.baidu.com', '检查a的内容');
  45. ok(a && $(a).attr('href').indexOf('http://www.baidu.com') != -1, '检查a的href');
  46. ok(a && $(a).attr('_src').indexOf('http://www.baidu.com') != -1, '检查a的_src');
  47. }else{
  48. var text = editor.getContent();
  49. equal(text, '<p>http://www.baidu.com</p>', '检查p的内容');
  50. }
  51. start();
  52. }, 20);
  53. }, 20);
  54. stop();
  55. }
  56. });
  57. test('字符前面有内容', function () {
  58. if (!ua.browser.ie) {
  59. var editor = te.obj[0];
  60. var range = te.obj[1];
  61. var body = editor.body;
  62. editor.setContent('<p><img src="" alt=""><span style="color: red">http://www.baidu.com</span></p>');
  63. range.setStart(body.firstChild, 2).collapse(1).select();
  64. stop();
  65. setTimeout(function () {
  66. ua.keydown(editor.body, {'keyCode': 32});
  67. setTimeout(function () {
  68. var a = body.firstChild.getElementsByTagName('a')[0];
  69. if(a){
  70. ok(a && $(a).attr('href').indexOf('http://www.baidu.com') != -1, '检查a的href');
  71. var html = 'http://www.baidu.com';
  72. equal(ua.getChildHTML(a), 'http://www.baidu.com', '检查a的内容');
  73. }else{
  74. var x= body.firstChild.firstChild.nextSibling.innerHTML;
  75. equal(x, 'http://www.baidu.com', '检查a的内容');
  76. }
  77. start();
  78. }, 20);
  79. }, 20);
  80. }
  81. });
  82. test('在p后面回车', function () {
  83. if (!UE.browser.ie) {
  84. var editor = te.obj[0];
  85. var range = te.obj[1];
  86. var body = editor.body;
  87. editor.setContent('<p>www.baidu.com</p>');
  88. setTimeout(function () {
  89. range.setStart(body.firstChild, 1).collapse(1).select();
  90. ua.keydown(editor.body, {'keyCode': 13});
  91. setTimeout(function () {
  92. var a = body.firstChild.getElementsByTagName('a')[0];
  93. if(a){
  94. ok(a && $(a).attr('href').indexOf('http://www.baidu.com') != -1, '检查a的href');
  95. equal(ua.getChildHTML(a), 'www.baidu.com', '检查a的内容');
  96. }else{
  97. var p =body.firstChild.innerHTML;
  98. equal(p,'www.baidu.com', '检查a的内容');
  99. }
  100. start();
  101. }, 20);
  102. }, 20);
  103. stop();
  104. }
  105. });
  106. ///*trace 1709 在“你好http://www.baidu.com”后回车/空格,各浏览器表现不一致*/
  107. ////这种情况,在ie中可以生成自动连接,非ie不可,现在以生成连接为期望结果
  108. test('trace 1709 在与其他文本相连的链接后空格', function () {
  109. if (!UE.browser.ie) {
  110. var editor = te.obj[0];
  111. var range = te.obj[1];
  112. var body = editor.body;
  113. editor.setContent('<p>你好http://www.baidu.com</p>');
  114. setTimeout(function () {
  115. range.setStart(body.firstChild, 1).collapse(1).select();
  116. ua.keydown(editor.body, {'keyCode': 32});
  117. setTimeout(function () {
  118. var a = body.firstChild.getElementsByTagName('a')[0];
  119. if(a){
  120. equal(ua.getChildHTML(a), 'http://www.baidu.com', '检查a的内容');
  121. ok(a && $(a).attr('href').indexOf('http://www.baidu.com') != -1, '检查a的href');
  122. }else{
  123. var p =body.firstChild.innerHTML;
  124. equal(p,'你好http://www.baidu.com', '检查a的内容');
  125. }
  126. start();
  127. }, 20);
  128. }, 20);
  129. stop();
  130. }
  131. });
  132. ////修改:对P中的文字内容,原:<p>你好htp://ww.baidu.com</p>
  133. test('你好htp://ww.baidu.com 后面回车', function () {
  134. if (!UE.browser.ie) {
  135. var editor = te.obj[0];
  136. var range = te.obj[1];
  137. var body = editor.body;
  138. editor.setContent('<p>你好http://www.baidu.com</p>');
  139. setTimeout(function () {
  140. range.setStart(body.firstChild, 1).collapse(1).select();
  141. ua.keydown(editor.body, {'keyCode': 32});
  142. setTimeout(function () {
  143. equal(body.firstChild.firstChild.nodeValue, '你好', '你好http://www.baidu.com 转换成文字+超链接');
  144. var a = body.firstChild.getElementsByTagName('a')[0];
  145. if(a){
  146. equal(ua.getChildHTML(a), 'http://www.baidu.com', '检查a的内容');
  147. ok(a && $(a).attr('href').indexOf('http://www.baidu.com') != -1, '检查a的href');
  148. }else{
  149. var p =body.firstChild.innerHTML;
  150. equal(p,'你好http://www.baidu.com', '检查a的内容');
  151. }
  152. start();
  153. }, 20);
  154. }, 20);
  155. stop();
  156. }
  157. });
  158. //<p>欢迎<strong>使用</strong>ueditor!</p>
  159. test('trace 2121', function () {
  160. if (!UE.browser.ie) {
  161. var editor = te.obj[0];
  162. var range = te.obj[1];
  163. var body = editor.body;
  164. editor.setContent('<p><span style="color:#ff0000;">欢迎<strong>使用</strong></span>ueditor!www.baidu.com</p>');
  165. stop();
  166. setTimeout(function () {
  167. range.setStart(body.firstChild.lastChild, body.firstChild.lastChild.length).collapse(1).select();
  168. setTimeout(function () {
  169. ua.keydown(editor.body, {'keyCode': 13});
  170. var a = body.firstChild.getElementsByTagName('a')[0];
  171. if(a){
  172. equal(ua.getChildHTML(a), 'www.baidu.com', '检查a的内容');
  173. ok(a && $(a).attr('href').indexOf('http://www.baidu.com') != -1, '检查a的href');
  174. ok(a && $(a).attr('_src').indexOf('http://www.baidu.com') != -1, '检查a的_src');
  175. }else{
  176. var p = body.firstChild.innerHTML;
  177. equal(p,'<span style="color:#ff0000;">欢迎<strong>使用</strong></span>ueditor!www.baidu.com','内容未改变');
  178. }
  179. start();
  180. }, 20);
  181. }, 20);
  182. }
  183. });
  184. test('autofloat:false 禁用IE中的自动加超链接功能', function () {
  185. if(ua.browser.ie==8)return;
  186. //在IE中回车/空格自动加连接,这里模拟加连接以后,测试keyup时把添加的链接去掉
  187. if (ua.browser.ie>8) {//这个用例中,ie8不好模拟startContainer.nodeName = p,用下面的用例测是一样的
  188. var div = document.body.appendChild(document.createElement('div'));
  189. div.id = 'ue';
  190. var editor = UE.getEditor('ue', {autolink: false});
  191. editor.ready(function () {
  192. var range = new baidu.editor.dom.Range(editor.document);
  193. var body = editor.body;
  194. editor.body.innerHTML = '<p><a href="http://www.baidu.com">www.baidu.com</a></p><p>&nbsp;</p>';
  195. setTimeout(function () {
  196. range.selectNode(body.lastChild.firstChild).select();
  197. setTimeout(function () {
  198. ua.keyup(editor.body, {'keyCode': 13});
  199. setTimeout(function () {
  200. equal(body.firstChild.getElementsByTagName('a').length, 0, 'a 标签被去掉了');
  201. equal(body.childNodes.length, 2, '结果正确');
  202. equal(body.firstChild.tagName.toLowerCase(), 'p', '结果正确');
  203. equal(body.firstChild.innerHTML, 'www.baidu.com', '结果正确');
  204. UE.delEditor('ue');
  205. te.dom.push(document.getElementById('ue'));
  206. start();
  207. }, 60);
  208. }, 20);
  209. }, 20);
  210. });
  211. stop();
  212. }
  213. });
  214. test('autofloat:false 禁用IE中的自动加超链接功能--回车', function () {
  215. if(ua.browser.ie==8)return;
  216. //在IE中回车/空格自动加连接,这里模拟加连接以后,测试keyup时把添加的链接去掉
  217. if (ua.browser.ie) {
  218. var div = document.body.appendChild(document.createElement('div'));
  219. div.id = 'ue';
  220. var editor = UE.getEditor('ue', {autolink: false});
  221. editor.ready(function () {
  222. var range = new baidu.editor.dom.Range(editor.document);
  223. var body = editor.body;
  224. editor.body.innerHTML = '<p><a href="http://www.baidu.com">www.baidu.com</a></p><p><img></p>';
  225. setTimeout(function () {
  226. range.selectNode(body.lastChild.firstChild).select();
  227. setTimeout(function () {
  228. ua.keyup(editor.body, {'keyCode': 13});
  229. setTimeout(function () {
  230. equal(body.firstChild.getElementsByTagName('a').length, 0, 'a 标签被去掉了');
  231. equal(body.childNodes.length, 2, '结果正确');
  232. equal(body.firstChild.tagName.toLowerCase(), 'p', '结果正确');
  233. equal(body.firstChild.innerHTML, 'www.baidu.com', '结果正确');
  234. UE.delEditor('ue');
  235. te.dom.push(document.getElementById('ue'));
  236. start();
  237. }, 60);
  238. }, 20);
  239. }, 20);
  240. });
  241. stop();
  242. }
  243. });
  244. test('autofloat:false 禁用IE中的自动加超链接功能--空格', function () {
  245. if(ua.browser.ie==8)return;
  246. //在IE中回车/空格自动加连接,这里模拟加连接以后,测试keyup时把添加的链接去掉
  247. if (ua.browser.ie) {
  248. var div = document.body.appendChild(document.createElement('div'));
  249. div.id = 'ue';
  250. var editor = UE.getEditor('ue', {autolink: false});
  251. editor.ready(function () {
  252. var range = new baidu.editor.dom.Range(editor.document);
  253. var body = editor.body;
  254. var space = ua.browser.ie>8?'&nbsp;':' ';
  255. editor.body.innerHTML = '<p><a href="http://www.baidu.com">www.baidu.com</a>'+space+'<img></p>';
  256. setTimeout(function () {
  257. // if(ua.browser.ie>8){
  258. // range.setStart(body.firstChild.childNodes[1], 1).collapse(true).select();
  259. // }
  260. // else{
  261. range.selectNode(body.firstChild.childNodes[1]).select();
  262. // }
  263. setTimeout(function () {
  264. ua.keyup(editor.body, {'keyCode': 32});
  265. equal(body.firstChild.getElementsByTagName('a').length, 0, 'a 标签被去掉了');
  266. equal(body.childNodes.length, 1, '结果正确');
  267. equal(body.firstChild.tagName.toLowerCase(), 'p', '结果正确');
  268. equal(body.firstChild.innerHTML.toLowerCase(), 'www.baidu.com'+space+'<img>', '结果正确');
  269. setTimeout(function () {
  270. UE.delEditor('ue');
  271. te.dom.push(document.getElementById('ue'));
  272. start();
  273. }, 100);
  274. }, 20);
  275. }, 20);
  276. });
  277. stop();
  278. }
  279. });
  280. //对于手动添加的,不会误删
  281. test('autofloat:false 禁用IE中的自动加超链接功能--对于手动添加的,不会误删', function () {
  282. //在IE中回车/空格自动加连接,这里模拟加连接以后,测试keyup时把添加的链接去掉
  283. if (ua.browser.ie) {
  284. var div = document.body.appendChild(document.createElement('div'));
  285. div.id = 'ue';
  286. var editor = UE.getEditor('ue', {autolink: false});
  287. editor.ready(function () {
  288. var range = new baidu.editor.dom.Range(editor.document);
  289. var body = editor.body;
  290. var space = ua.browser.ie>8?'&nbsp;':' ';
  291. editor.setContent('<p><a href="http://www.baidu.com">www.baidu.com</a>'+space+'<img></p>');
  292. setTimeout(function () {
  293. if(ua.browser.ie>8){
  294. range.setStart(body.firstChild.childNodes[1], 1).collapse(true).select();
  295. }
  296. else{
  297. range.selectNode(body.firstChild.childNodes[1]).select();
  298. }
  299. setTimeout(function () {
  300. ua.keyup(editor.body, {'keyCode': 32});
  301. var a = body.firstChild.getElementsByTagName('a');
  302. ok(a&&a.length==1, 'a 标签没去掉');
  303. ok(a[0] && $(a[0]).attr('href').indexOf('http://www.baidu.com') != -1, '检查a的href');
  304. setTimeout(function () {
  305. editor.execCommand('cleardoc');
  306. editor.setContent('<p><a href="http://www.baidu.com">www.baidu.com</a></p><p><img></p>');
  307. setTimeout(function () {
  308. range.selectNode(body.lastChild.firstChild).select();
  309. setTimeout(function () {
  310. ua.keyup(editor.body, {'keyCode': 13});
  311. setTimeout(function () {
  312. a = body.firstChild.getElementsByTagName('a');
  313. ok(a&&a.length==1, 'a 标签没去掉');
  314. ok(a[0] && $(a[0]).attr('href').indexOf('http://www.baidu.com') != -1, '检查a的href');
  315. UE.delEditor('ue');
  316. te.dom.push(document.getElementById('ue'));
  317. start();
  318. }, 500);
  319. }, 20);
  320. }, 20);
  321. }, 20);
  322. }, 20);
  323. }, 20);
  324. });
  325. stop();
  326. }
  327. });
  328. //test( '粘贴进来的http文本后回车', function() {
  329. // var editor = te.obj[0];
  330. // var range = te.obj[1];
  331. // var body = editor.body;
  332. // setTimeout( function() {
  333. // editor.setContent( '<p><br></p>' );
  334. // editor.focus();
  335. // range.setStart( body.firstChild, 0 ).collapse( 1 ).select();
  336. // te.setClipData( "http://www.google.com" );
  337. // setTimeout( function() {
  338. // editor.focus();
  339. // setTimeout( function() {
  340. // editor.focus();
  341. // te.presskey( "ctrl", "v" );
  342. // editor.focus();
  343. // setTimeout( function() {
  344. // te.presskey( "enter", "" );
  345. // editor.focus();
  346. // setTimeout( function() {
  347. // var a = body.firstChild.getElementsByTagName( 'a' )[0];
  348. // equal( ua.getChildHTML( a ), 'http://www.google.com', '检查a的内容' );
  349. // start();
  350. // }, 100 );
  351. //
  352. // }, 100 );
  353. // }, 100 );
  354. // }, 100 );
  355. // } );
  356. // stop();
  357. //} );
  358. //