Editor.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. module("core.Editor");
  2. //test('getContent--2个参数,第一个参数为参数为函数', function () {
  3. // var editor = te.obj[1];
  4. // var div = te.dom[0];
  5. // editor.render(div);
  6. // stop();
  7. // setTimeout(function () {
  8. // editor.focus();
  9. // editor.setContent("<p><br/>dd</p>");
  10. // equal(editor.getContent(), "<p><br/>dd</p>", 'hasContents判断不为空');
  11. // equal(editor.getContent("", function () {
  12. // return false
  13. // }), "", '为空');
  14. // setTimeout(function () {
  15. // UE.delEditor('test1');
  16. // setTimeout(function () {
  17. // start();
  18. // }, 50);
  19. // }, 100);
  20. // }, 50);
  21. //});
  22. //test('', function () {
  23. // stop();
  24. //});
  25. test('contentchange在命令调用时的触发机制', function () {
  26. var editor = te.obj[1];
  27. var container = te.dom[0];
  28. $(container).css('width', '500px').css('height', '500px').css('border', '1px solid #ccc');
  29. editor.render(container);
  30. editor.ready(function () {
  31. editor.commands['test1'] = {
  32. execCommand: function () {
  33. editor.body.innerHTML = '1123';
  34. }
  35. };
  36. var count = 0;
  37. editor.on('contentchange', function () {
  38. count++;
  39. });
  40. editor.commands['test'] = {
  41. execCommand: function () {
  42. editor.execCommand('test1')
  43. },
  44. ignoreContentChange: true
  45. };
  46. setTimeout(function () {
  47. editor.execCommand('test1');
  48. equals(count, 1);
  49. count = 0;
  50. editor.execCommand('test');
  51. equals(count, 0);
  52. start();
  53. }, 200);
  54. });
  55. stop();
  56. });
  57. test("initialStyle", function () {
  58. if(ua.browser.gecko)return;//todo 1.4.0
  59. var div = document.body.appendChild(document.createElement('div'));
  60. div.id = 'ue';
  61. var editor = UE.getEditor('ue', {initialStyle: "body{font-family: arial black;}.testCss{ color: rgb(192, 0, 0); }", initialContent: "<p><span class='testCss'>测试样式,红色,字体: arial black</span></p>", autoHeightEnabled: false});
  62. editor.ready(function () {
  63. equal(ua.formatColor(ua.getComputedStyle(editor.body.firstChild.firstChild).color), '#c00000', 'initialStyle中设置的class样式有效');
  64. ok(/arial black/.test(ua.getComputedStyle(editor.body.firstChild.firstChild).fontFamily), 'initialStyle中设置的body样式有效');
  65. setTimeout(function () {
  66. UE.delEditor('ue');
  67. te.dom.push(document.getElementById('ue'));
  68. start();
  69. }, 200);
  70. });
  71. stop();
  72. });
  73. test("autoSyncData:true,textarea容器(由setcontent触发的)", function () {
  74. var div = document.body.appendChild(document.createElement('div'));
  75. div.innerHTML = '<form id="form" method="post" target="_blank"><textarea id="myEditor" name="myEditor">这里的内容将会和html,body等标签一块提交</textarea></form>';
  76. equal(document.getElementById('form').childNodes.length, 1, 'form里只有一个子节点');
  77. var editor_a = UE.getEditor('myEditor', {autoHeightEnabled: false});
  78. stop();
  79. editor_a.ready(function () {
  80. equal(document.getElementById('form').childNodes.length, 2, 'form里有2个子节点');
  81. editor_a.setContent('<p>设置内容autoSyncData 1<br/></p>');
  82. setTimeout(function () {
  83. var form = document.getElementById('form');
  84. equal(form.childNodes.length, 2, '失去焦点,form里多了textarea');
  85. equal(form.lastChild.tagName.toLowerCase(), 'textarea', '失去焦点,form里多了textarea');
  86. equal(form.lastChild.value, '<p>设置内容autoSyncData 1<br/></p>', 'textarea内容正确');
  87. setTimeout(function () {
  88. UE.delEditor('myEditor');
  89. document.getElementById('form').parentNode.removeChild(document.getElementById('form'));
  90. document.getElementById('test1') && te.dom.push(document.getElementById('test1'));
  91. start();
  92. }, 200);
  93. }, 100);
  94. });
  95. });
  96. test("autoSyncData:true(由blur触发的)", function () {
  97. //todo ie8里事件触发有问题,暂用手动测
  98. if (ua.browser.ie > 8 || !ua.browser.ie) {
  99. var div = document.body.appendChild(document.createElement('div'));
  100. div.innerHTML = '<form id="form" method="post" ><script type="text/plain" id="myEditor" name="myEditor"></script></form>';
  101. var editor_a = UE.getEditor('myEditor', {autoHeightEnabled: false});
  102. stop();
  103. editor_a.ready(function () {
  104. editor_a.body.innerHTML = '<p>设置内容autoSyncData 2<br/></p>';
  105. equal(document.getElementsByTagName('textarea').length, 0, '内容空没有textarea');
  106. ua.blur(editor_a.body);
  107. stop();
  108. setTimeout(function () {
  109. var form = document.getElementById('form');
  110. equal(form.childNodes.length, 2, '失去焦点,form里多了textarea');
  111. equal(form.lastChild.tagName.toLowerCase(), 'textarea', '失去焦点,form里多了textarea');
  112. equal(form.lastChild.value, '<p>设置内容autoSyncData 2<br/></p>', 'textarea内容正确');
  113. UE.delEditor('myEditor');
  114. form.parentNode.removeChild(form);
  115. start();
  116. }, 200);
  117. });
  118. }
  119. });
  120. test("sync", function () {
  121. var div = document.body.appendChild(document.createElement('div'));
  122. div.innerHTML = '<form id="form" method="post" target="_blank"><textarea id="myEditor" name="myEditor">这里的内容将会和html,body等标签一块提交</textarea></form>';
  123. var editor_a = UE.getEditor('myEditor', {autoHeightEnabled: false});
  124. stop();
  125. editor_a.ready(function () {
  126. editor_a.body.innerHTML = '<p>hello</p>';
  127. editor_a.sync("form");
  128. setTimeout(function () {
  129. var form = document.getElementById('form');
  130. equal(form.lastChild.value, '<p>hello</p>', '同步内容正确');
  131. div = form.parentNode;
  132. UE.delEditor('myEditor');
  133. div.parentNode.removeChild(div);
  134. start();
  135. }, 100);
  136. });
  137. });
  138. test("hide,show", function () {
  139. var editor = te.obj[1];
  140. var container = te.dom[0];
  141. $(container).css('width', '500px').css('height', '500px').css('border', '1px solid #ccc');
  142. editor.render(container);
  143. editor.ready(function () {
  144. equal(editor.body.getElementsByTagName('span').length, 0, '初始没有书签');
  145. editor.hide();
  146. setTimeout(function () {
  147. equal($(editor.container).css('display'), 'none', '隐藏编辑器');
  148. equal(editor.body.getElementsByTagName('span').length, 1, '插入书签');
  149. ok(/_baidu_bookmark_start/.test(editor.body.getElementsByTagName('span')[0].id), '书签');
  150. editor.show();
  151. setTimeout(function () {
  152. equal($(te.dom[0]).css('display'), 'block', '显示编辑器');
  153. var br = ua.browser.ie ? '' : '<br>';
  154. equal(ua.getChildHTML(editor.body), '<p>tool</p>', '删除书签');
  155. start();
  156. }, 50);
  157. }, 50);
  158. });
  159. stop();
  160. });
  161. test("_setDefaultContent--focus", function () {
  162. var editor = te.obj[1];
  163. var container = te.dom[0];
  164. $(container).css('width', '500px').css('height', '500px').css('border', '1px solid #ccc');
  165. editor.render(container);
  166. editor.ready(function () {
  167. editor._setDefaultContent('hello');
  168. editor.fireEvent('focus');
  169. setTimeout(function () {
  170. var br = ua.browser.ie ? '' : '<br>';
  171. equal(ua.getChildHTML(editor.body), '<p>' + br + '</p>', 'focus');
  172. start();
  173. }, 50);
  174. });
  175. stop();
  176. });
  177. test("_setDefaultContent--firstBeforeExecCommand", function () {
  178. var editor = te.obj[1];
  179. var container = te.dom[0];
  180. $(container).css('width', '500px').css('height', '500px').css('border', '1px solid #ccc');
  181. editor.render(container);
  182. editor.ready(function () {
  183. editor._setDefaultContent('hello');
  184. editor.fireEvent('firstBeforeExecCommand');
  185. setTimeout(function () {
  186. var br = ua.browser.ie ? '' : '<br>';
  187. equal(ua.getChildHTML(editor.body), '<p>' + br + '</p>', 'firstBeforeExecCommand');
  188. start();
  189. }, 50);
  190. });
  191. stop();
  192. });
  193. test("setDisabled,setEnabled", function () {
  194. var editor = te.obj[1];
  195. var container = te.dom[0];
  196. $(container).css('width', '500px').css('height', '500px').css('border', '1px solid #ccc');
  197. editor.render(container);
  198. editor.ready(function () {
  199. editor.setContent('<p>欢迎使用ueditor!</p>');
  200. editor.focus();
  201. setTimeout(function () {
  202. var startContainer = editor.selection.getRange().startContainer.outerHTML;
  203. var startOffset = editor.selection.getRange().startOffset;
  204. var collapse = editor.selection.getRange().collapsed;
  205. editor.setDisabled();
  206. setTimeout(function () {
  207. equal(editor.body.contentEditable, 'false', 'setDisabled');
  208. equal(editor.body.firstChild.firstChild.tagName.toLowerCase(), 'span', '插入书签');
  209. equal($(editor.body.firstChild.firstChild).css('display'), 'none', '检查style');
  210. equal($(editor.body.firstChild.firstChild).css('line-height'), '0px', '检查style');
  211. ok(/_baidu_bookmark_start/.test(editor.body.firstChild.firstChild.id), '书签');///_baidu_bookmark_start/.test()
  212. editor.setEnabled();
  213. setTimeout(function () {
  214. equal(editor.body.contentEditable, 'true', 'setEnabled');
  215. equal(ua.getChildHTML(editor.body), '<p>欢迎使用ueditor!</p>', '内容恢复');
  216. if (!ua.browser.ie || ua.browser.ie < 9) {// ie9,10改range 之后,ie9,10这里的前后range不一致,focus时是text,setEnabled后是p
  217. equal(editor.selection.getRange().startContainer.outerHTML, startContainer, '检查range');
  218. }
  219. equal(editor.selection.getRange().startOffset, startOffset, '检查range');
  220. equal(editor.selection.getRange().collapsed, collapse, '检查range');
  221. start();
  222. }, 50);
  223. }, 50);
  224. }, 50);
  225. });
  226. stop();
  227. });
  228. test("render-- element", function () {
  229. var editor = new baidu.editor.Editor({'UEDITOR_HOME_URL': '../../../', 'autoFloatEnabled': false});
  230. var div = document.body.appendChild(document.createElement('div'));
  231. equal(div.innerHTML, "", "before render");
  232. editor.render(div);
  233. equal(div.firstChild.tagName.toLocaleLowerCase(), 'iframe', 'check iframe');
  234. ok(/ueditor_/.test(div.firstChild.id), 'check iframe id');
  235. te.dom.push(div);
  236. });
  237. test("render-- elementid", function () {
  238. var editor = te.obj[1];
  239. var div = te.dom[0];
  240. editor.render(div.id);
  241. equal(div.firstChild.tagName.toLocaleLowerCase(), 'iframe', 'check iframe');
  242. ok(/ueditor_/.test(div.firstChild.id), 'check iframe id');
  243. });
  244. test("render-- options", function () {
  245. var options = {'initialContent': '<span class="span">xxx</span><div>xxx<p></p></div>', 'UEDITOR_HOME_URL': '../../../', autoClearinitialContent: false, 'autoFloatEnabled': false};
  246. var editor = new baidu.editor.Editor(options);
  247. var div = document.body.appendChild(document.createElement('div'));
  248. editor.render(div);
  249. /*会自动用p标签包围*/
  250. var space = baidu.editor.browser.ie ? '&nbsp;' : '<br>';
  251. //策略变化,自1.2.6,div 标签都会被过滤
  252. stop();
  253. editor.ready(function () {
  254. equal(ua.getChildHTML(editor.body), '<p><span class="span">xxx</span></p><p>xxx</p><p>' + space + '</p>', 'check initialContent');
  255. te.dom.push(div);
  256. start();
  257. });
  258. });
  259. test('destroy', function () {
  260. // var editor = new baidu.editor.Editor( {'autoFloatEnabled':false} );
  261. var editor = new UE.ui.Editor({'autoFloatEnabled': false});
  262. editor.key = 'ed';
  263. var div = document.body.appendChild(document.createElement('div'));
  264. div.id = 'ed';
  265. editor.render(div);
  266. editor.ready(function () {
  267. setTimeout(function () {
  268. editor.destroy();
  269. equal(document.getElementById('ed').tagName.toLowerCase(), 'textarea', '容器被删掉了');
  270. document.getElementById('ed') && te.dom.push(document.getElementById('ed'));
  271. start();
  272. }, 200);
  273. });
  274. stop();
  275. });
  276. //test( "setup--ready event", function() {
  277. // //todo
  278. //} );
  279. //
  280. test("testBindshortcutKeys", function () {
  281. var editor = te.obj[1];
  282. var container = te.dom[0];
  283. $(container).css('width', '500px').css('height', '500px').css('border', '1px solid #ccc');
  284. editor.render(container);
  285. expect(1);
  286. editor.ready(function () {
  287. editor.addshortcutkey({
  288. "testBindshortcutKeys": "ctrl+67"//^C
  289. });
  290. editor.commands["testbindshortcutkeys"] = {
  291. execCommand: function (cmdName) {
  292. ok(1, '')
  293. },
  294. queryCommandState: function () {
  295. return 0;
  296. }
  297. }
  298. ua.keydown(editor.body, {keyCode: 67, ctrlKey: true});
  299. setTimeout(function () {
  300. start();
  301. }, 200);
  302. });
  303. stop();
  304. });
  305. test("getContent--转换空格,nbsp与空格相间显示", function () {
  306. var editor = te.obj[1];
  307. var div = te.dom[0];
  308. editor.render(div);
  309. stop();
  310. editor.ready(function () {
  311. setTimeout(function () {
  312. editor.focus();
  313. var innerHTML = '<div> x x x&nbsp;&nbsp;&nbsp;&nbsp;x&nbsp;&nbsp; &nbsp;</div>';
  314. editor.setContent(innerHTML);
  315. equal(editor.getContent(), '<p>x &nbsp;x &nbsp; x&nbsp;&nbsp;&nbsp;&nbsp;x&nbsp;&nbsp; &nbsp;&nbsp;</p>', "转换空格,nbsp与空格相间显示,原nbsp不变");
  316. setTimeout(function () {
  317. // UE.delEditor('test1');
  318. start();
  319. }, 100);
  320. }, 100);
  321. });
  322. });
  323. test('getContent--参数为函数', function () {
  324. var editor = te.obj[1];
  325. var div = te.dom[0];
  326. editor.render(div);
  327. stop();
  328. editor.ready(function () {
  329. editor.focus();
  330. editor.setContent("<p><br/>dd</p>");
  331. equal(editor.getContent(), "<p><br/>dd</p>", 'hasContents判断不为空');
  332. equal(editor.getContent(function () {
  333. return false
  334. }), "", '为空');
  335. setTimeout(function () {
  336. // UE.delEditor('test1');
  337. setTimeout(function () {
  338. start();
  339. }, 50);
  340. }, 100);
  341. });
  342. });
  343. test('getContent--2个参数,第一个参数为参数为函数', function () {
  344. var editor = te.obj[1];
  345. var div = te.dom[0];
  346. editor.render(div);
  347. stop();
  348. editor.ready(function () {
  349. editor.focus();
  350. editor.setContent("<p><br/>dd</p>");
  351. equal(editor.getContent(), "<p><br/>dd</p>", 'hasContents判断不为空');
  352. equal(editor.getContent("", function () {
  353. return false
  354. }), "", '为空');
  355. setTimeout(function () {
  356. // UE.delEditor('test1');
  357. // setTimeout(function () {
  358. start();
  359. // }, 50);
  360. }, 100);
  361. });
  362. });
  363. /*ie自动把左边的空格去掉,所以就不测这个了*/
  364. //test( "getContent--空格不会被去掉", function() {
  365. // var editor = te.obj[1];
  366. // var div = te.dom[0];
  367. // editor.render( div );
  368. // editor.focus();
  369. // var innerHTML = '你好 ';
  370. // editor.setContent( innerHTML );
  371. // equal( editor.getContent().toLowerCase(), '<p>你好 </p>', "删除不可见字符" );
  372. //} );
  373. test("setContent", function () {
  374. var editor = te.obj[1];
  375. var container = te.dom[0];
  376. $(container).css('width', '500px').css('height', '500px').css('border', '1px solid #ccc');
  377. editor.render(container);
  378. stop();
  379. editor.ready(function () {
  380. editor.focus();
  381. expect(2);
  382. editor.addListener("beforesetcontent", function () {
  383. ok(true, "beforesetcontent");
  384. });
  385. editor.addListener("aftersetcontent", function () {
  386. ok(true, "aftersetcontent");
  387. });
  388. var html = '<span><span></span><strong>xx</strong><em>em</em><em></em><u></u></span><div>xxxx</div>';
  389. editor.setContent(html);
  390. var div_new = document.createElement('div');
  391. div_new.innerHTML = '<p><span><span></span><strong>xx</strong><em>em</em><em></em><span style="text-decoration: underline"></span></span></p><div>xxxx</div>';
  392. var div2 = document.createElement('div');
  393. div2.innerHTML = editor.body.innerHTML;
  394. ua.haveSameAllChildAttribs(div2, div_new, 'check contents');
  395. start();
  396. });
  397. });
  398. test("setContent 追加", function () {
  399. var editor = te.obj[1];
  400. var container = te.dom[0];
  401. $(container).css('width', '500px').css('height', '500px').css('border', '1px solid #ccc');
  402. editor.render(container);
  403. stop();
  404. editor.ready(function () {
  405. editor.focus();
  406. expect(2);
  407. editor.addListener("beforesetcontent", function () {
  408. ok(true, "beforesetcontent");
  409. });
  410. editor.addListener("aftersetcontent", function () {
  411. ok(true, "aftersetcontent");
  412. });
  413. var html = '<span><span></span><strong>xx</strong><em>em</em><em></em><u></u></span><div>xxxx</div>';
  414. editor.setContent(html);
  415. var div_new = document.createElement('div');
  416. div_new.innerHTML = '<p><span><span></span><strong>xx</strong><em>em</em><em></em><span style="text-decoration: underline"></span></span></p><div>xxxx</div>';
  417. var div2 = document.createElement('div');
  418. div2.innerHTML = editor.body.innerHTML;
  419. ua.haveSameAllChildAttribs(div2, div_new, 'check contents');
  420. start();
  421. }, 50);
  422. });
  423. //test( "focus", function() {
  424. // var editor = te.obj[1];
  425. // expect( 1 );
  426. // /*设置onfocus事件,必须同步处理,否则在ie下onfocus会在用例执行结束后才会触发*/
  427. // stop();
  428. // editor.window.onfocus = function() {
  429. // ok( true, 'onfocus event dispatched' );
  430. // start();
  431. // };
  432. // editor.focus();
  433. //} );
  434. test("focus(false)", function () {
  435. var editor = te.obj[1];
  436. var container = te.dom[0];
  437. $(container).css('width', '500px').css('height', '500px').css('border', '1px solid #ccc');
  438. editor.render(container);
  439. stop();
  440. editor.ready(function () {
  441. editor.setContent("<p>hello1</p><p>hello2</p>");
  442. setTimeout(function () {
  443. editor.focus(false);
  444. setTimeout(function () {
  445. var range = editor.selection.getRange();
  446. equal(range.startOffset, 0, "focus(false)焦点在最前面");
  447. equal(range.endOffset, 0, "focus(false)焦点在最前面");
  448. if (ua.browser.gecko||ua.browser.webkit) {
  449. equal(range.startContainer, editor.body.firstChild, "focus(false)焦点在最前面");
  450. equal(range.collapsed, true, "focus(false)焦点在最前面");
  451. }
  452. else {
  453. equal(range.startContainer, editor.body.firstChild.firstChild, "focus(false)焦点在最前面");
  454. equal(range.endContainer, editor.body.firstChild.firstChild, "focus(false)焦点在最前面");
  455. }
  456. start();
  457. }, 200);
  458. }, 100);
  459. });
  460. });
  461. test("focus(true)", function () {
  462. var editor = te.obj[1];
  463. var container = te.dom[0];
  464. $(container).css('width', '500px').css('height', '500px').css('border', '1px solid #ccc');
  465. editor.render(container);
  466. stop();
  467. editor.ready(function () {
  468. editor.setContent("<p>hello1</p><p>hello2</p>");
  469. setTimeout(function () {
  470. editor.focus(true);
  471. setTimeout(function () {
  472. if (ua.browser.gecko||ua.browser.webkit) {
  473. equal(editor.selection.getRange().startContainer, editor.body.lastChild, "focus( true)焦点在最后面");
  474. equal(editor.selection.getRange().endContainer, editor.body.lastChild, "focus( true)焦点在最后面");
  475. equal(editor.selection.getRange().startOffset, editor.body.lastChild.childNodes.length, "focus( true)焦点在最后面");
  476. equal(editor.selection.getRange().endOffset, editor.body.lastChild.childNodes.length, "focus( true)焦点在最后面");
  477. }
  478. else {
  479. equal(editor.selection.getRange().startContainer, editor.body.lastChild.lastChild, "focus( true)焦点在最后面");
  480. equal(editor.selection.getRange().endContainer, editor.body.lastChild.lastChild, "focus( true)焦点在最后面");
  481. equal(editor.selection.getRange().startOffset, editor.body.lastChild.lastChild.length, "focus( true)焦点在最后面");
  482. equal(editor.selection.getRange().endOffset, editor.body.lastChild.lastChild.length, "focus( true)焦点在最后面");
  483. }
  484. start();
  485. }, 200);
  486. }, 100);
  487. });
  488. });
  489. test("isFocus()", function () {
  490. var editor = te.obj[1];
  491. var container = te.dom[0];
  492. $(container).css('width', '500px').css('height', '500px').css('border', '1px solid #ccc');
  493. editor.render(container);
  494. stop();
  495. editor.ready(function () {
  496. editor.focus();
  497. setTimeout(function () {
  498. ok(editor.isFocus());
  499. start();
  500. }, 200);
  501. });
  502. });
  503. test("blur()", function () {
  504. var editor = te.obj[1];
  505. var container = te.dom[0];
  506. $(container).css('width', '500px').css('height', '500px').css('border', '1px solid #ccc');
  507. editor.render(container);
  508. stop();
  509. editor.ready(function () {
  510. editor.focus();
  511. ok(editor.isFocus());
  512. editor.blur();
  513. ok(!editor.isFocus());
  514. editor.blur();//多次使用不报错
  515. ok(!editor.isFocus());
  516. start();
  517. });
  518. });
  519. test("_initEvents,_proxyDomEvent--click", function () {
  520. var editor = te.obj[1];
  521. var container = te.dom[0];
  522. $(container).css('width', '500px').css('height', '500px').css('border', '1px solid #ccc');
  523. editor.render(container);
  524. stop();
  525. editor.ready(function () {
  526. editor.focus();
  527. expect(1);
  528. stop();
  529. editor.addListener('click', function () {
  530. ok(true, 'click event dispatched');
  531. start();
  532. });
  533. ua.click(editor.document);
  534. });
  535. });
  536. //test("_initEvents,_proxyDomEvent--focus", function() {
  537. // var editor = te.obj[1];
  538. //
  539. // expect(1); stop();
  540. // editor.addListener('focus', function() {
  541. // ok(true, 'focus event dispatched');
  542. // start();
  543. // });
  544. // editor.setContent("<p>hello1</p><p>hello2</p>");
  545. // editor.focus();
  546. //});
  547. ////TODO
  548. //test( "_selectionChange--测试event是否被触发", function() {
  549. // var editor = te.obj[1];
  550. // var div = te.dom[0];
  551. // editor.render( div );
  552. // editor.focus();
  553. // expect( 2 );
  554. // stop();
  555. // editor.addListener( 'beforeselectionchange', function() {
  556. // ok( true, 'before selection change' );
  557. // } );
  558. // editor.addListener( 'selectionchange', function() {
  559. // ok( true, 'selection changed' );
  560. // } );
  561. //
  562. // ua.mousedown( editor.document, {clientX:0,clientY:0} );
  563. // setTimeout( function() {
  564. // ua.mouseup( editor.document, {clientX:0,clientY:0} );
  565. // }, 50 );
  566. //
  567. // /*_selectionChange有一定的延时才会触发,所以需要等一会*/
  568. // setTimeout( function() {
  569. // start();
  570. // }, 200 );
  571. //} );
  572. //test("_selectionChange--fillData", function() {
  573. // var editor = te.obj[1];
  574. // var div = te.dom[0];
  575. // editor.focus();
  576. // //TODO fillData干嘛用的
  577. //});
  578. /*按钮高亮、正常和灰色*/
  579. test("queryCommandState", function () {
  580. var editor = te.obj[1];
  581. var container = te.dom[0];
  582. $(container).css('width', '500px').css('height', '500px').css('border', '1px solid #ccc');
  583. editor.render(container);
  584. stop();
  585. editor.ready(function () {
  586. editor.focus();
  587. editor.setContent("<p><b>xxx</b>xxx</p>");
  588. var p = editor.document.getElementsByTagName('p')[0];
  589. var r = new baidu.editor.dom.Range(editor.document);
  590. r.setStart(p.firstChild, 0).setEnd(p.firstChild, 1).select();
  591. equal(editor.queryCommandState('bold'), 1, '加粗状态为1');
  592. r.setStart(p, 1).setEnd(p, 2).select();
  593. setTimeout(function () {
  594. equal(editor.queryCommandState('bold'), 0, '加粗状态为0');
  595. start();
  596. }, 100);
  597. });
  598. });
  599. test("queryCommandValue", function () {
  600. var editor = te.obj[1];
  601. var container = te.dom[0];
  602. $(container).css('width', '500px').css('height', '500px').css('border', '1px solid #ccc');
  603. editor.render(container);
  604. stop();
  605. editor.ready(function () {
  606. editor.focus();
  607. editor.setContent('<p style="text-align:left">xxx</p>');
  608. var range = new baidu.editor.dom.Range(editor.document);
  609. var p = editor.document.getElementsByTagName("p")[0];
  610. range.selectNode(p).select();
  611. equal(editor.queryCommandValue('justify'), 'left', 'text align is left');
  612. start();
  613. });
  614. });
  615. test("execCommand", function () {
  616. var editor = te.obj[1];
  617. var container = te.dom[0];
  618. $(container).css('width', '500px').css('height', '500px').css('border', '1px solid #ccc');
  619. editor.render(container);
  620. stop();
  621. editor.ready(function () {
  622. editor.focus();
  623. editor.setContent("<p>xx</p><p>xxx</p>");
  624. var doc = editor.document;
  625. var range = new baidu.editor.dom.Range(doc);
  626. var p = doc.getElementsByTagName('p')[1];
  627. range.setStart(p, 0).setEnd(p, 1).select();
  628. editor.execCommand('justify', 'right');
  629. equal($(p).css('text-align'), 'right', 'execCommand align');
  630. /*给span加style不会重复添加span*/
  631. range.selectNode(p).select();
  632. editor.execCommand("forecolor", "red");
  633. /*span发生了变化,需要重新获取*/
  634. var span = doc.getElementsByTagName('span')[0];
  635. equal(span.style['color'], 'red', 'check execCommand color');
  636. var div_new = document.createElement('div');
  637. div_new.innerHTML = '<p><span style="color: red; ">xx</span></p><p style="text-align: right; ">xxx</p>';
  638. var div1 = document.createElement('div');
  639. div1.innerHTML = editor.body.innerHTML;
  640. ok(ua.haveSameAllChildAttribs(div_new, div1), 'check style');
  641. start();
  642. });
  643. });
  644. test("hasContents", function () {
  645. var editor = te.obj[1];
  646. var container = te.dom[0];
  647. $(container).css('width', '500px').css('height', '500px').css('border', '1px solid #ccc');
  648. editor.render(container);
  649. stop();
  650. editor.ready(function () {
  651. editor.focus();
  652. editor.setContent('');
  653. ok(!editor.hasContents(), "have't content");
  654. editor.setContent("xxx");
  655. ok(editor.hasContents(), "has contents");
  656. editor.setContent('<p><br/></p>');
  657. ok(!editor.hasContents(), '空p认为是空');
  658. start();
  659. });
  660. });
  661. //test( "hasContents--只有空格", function() {
  662. // var editor = te.obj[1];
  663. // editor.focus();
  664. // editor.setContent( ' ' );
  665. // ok( editor.hasContents(), "空格不被过滤" );
  666. // editor.setContent( "<p> \t\n </p>" );
  667. // ok( editor.hasContents(), "空格不过滤" );
  668. //} );
  669. /*参数是对原有认为是空的标签的一个扩展,即原来的dtd认为br为空,加上这个参数可以认为br存在时body也不是空*/
  670. test("hasContents--有参数", function () {
  671. var editor = te.obj[1];
  672. var container = te.dom[0];
  673. $(container).css('width', '500px').css('height', '500px').css('border', '1px solid #ccc');
  674. editor.render(container);
  675. stop();
  676. editor.ready(function () {
  677. editor.focus();
  678. editor.setContent('<p><img src="" alt="">你好<ol><li>ddd</li></ol></p>');
  679. ok(editor.hasContents(['ol', 'li', 'table']), "有ol和li");
  680. ok(editor.hasContents(['td', 'li', 'table']), "有li");
  681. editor.setContent('<p><br></p>');
  682. ok(!editor.hasContents(['']), "为空");
  683. ok(editor.hasContents(['br']), "不为空");
  684. start();
  685. });
  686. });
  687. //test( 'getContentTxt--文本前后中间有空格', function() {
  688. // var editor = te.obj[1];
  689. // editor.focus();
  690. // editor.setContent( '你 好\t\n' );
  691. // equal( editor.getContentTxt(), '你 好\t\n' )
  692. // equal( editor.getContentTxt().length, 3, '3个字符,空格不会被过滤' )
  693. //} );
  694. test('trace 1964 getPlainTxt--得到有格式的编辑器的纯文本内容', function () {
  695. var editor = te.obj[1];
  696. var container = te.dom[0];
  697. $(container).css('width', '500px').css('height', '500px').css('border', '1px solid #ccc');
  698. editor.render(container);
  699. stop();
  700. editor.ready(function () {
  701. editor.focus();
  702. editor.setContent('<p>&nbsp;</p><p>&nbsp; hell\no<br/>hello</p>');
  703. var html = (ua.browser.ie > 0 && ua.browser.ie < 9) ? "\n hell o\nhello\n" : "\n hello\nhello\n";
  704. equal(editor.getPlainTxt(), html, '得到编辑器的纯文本内容,但会保留段落格式');
  705. start();
  706. });
  707. });
  708. test('getContentTxt--文本前后的空格,&nbs p转成空格', function () {
  709. var editor = te.obj[1];
  710. var container = te.dom[0];
  711. $(container).css('width', '500px').css('height', '500px').css('border', '1px solid #ccc');
  712. editor.render(container);
  713. stop();
  714. editor.ready(function () {
  715. editor.focus();
  716. editor.setContent('&nbsp;&nbsp;你 好&nbsp;&nbsp; ');
  717. equal(editor.getContentTxt(), ' 你 好 ');
  718. equal(editor.getContentTxt().length, 8, '8个字符,空格不被过滤');
  719. start();
  720. });
  721. });
  722. test('getAllHtml', function () {
  723. var editor = te.obj[1];
  724. var container = te.dom[0];
  725. $(container).css('width', '500px').css('height', '500px').css('border', '1px solid #ccc');
  726. editor.render(container);
  727. stop();
  728. editor.ready(function () {
  729. editor.focus();
  730. var html = editor.getAllHtml();
  731. ok(/iframe.css/.test(html), '引入样式');
  732. start();
  733. });
  734. });
  735. test('2个实例采用2个配置文件', function () {
  736. var head = document.getElementsByTagName('head')[0];
  737. var script = document.createElement('script');
  738. script.type = 'text/javascript';
  739. script.src = '../../editor_config.js';
  740. head.appendChild(script);
  741. expect(6);
  742. stop();
  743. /*动态加载js需要时间,用这个ueditor.config.js覆盖默认的配置文件*/
  744. setTimeout(function () {
  745. var div1 = document.body.appendChild(document.createElement('div'));
  746. div1.id = 'div1';
  747. div1.style.height = '200px';
  748. var div2 = document.body.appendChild(document.createElement('div'));
  749. div2.id = 'div2';
  750. var editor1 = UE.getEditor('div1', {'UEDITOR_HOME_URL': '../../../', 'initialContent': '欢迎使用ueditor', 'autoFloatEnabled': false});
  751. editor1.ready(function () {
  752. var editor2 = UE.getEditor('div2', UEDITOR_CONFIG2);
  753. editor2.ready(function () {
  754. //1.2.6 高度是iframe容器的高度
  755. equal(editor1.ui.getDom('iframeholder').style.height, '200px', '编辑器高度为200px');
  756. equal(editor2.ui.getDom('iframeholder').style.height, '400px', '自定义div高度为400px');
  757. var html = UEDITOR_CONFIG2.initialContent;
  758. ua.checkHTMLSameStyle(html, editor2.document, editor2.body.firstChild, '初始内容为自定制的');
  759. equal(editor2.options.enterTag, 'br', 'enterTag is br');
  760. html = '欢迎使用ueditor';
  761. equal(html, editor1.body.firstChild.innerHTML, '内容和ueditor.config一致');
  762. equal(editor1.options.enterTag, 'p', 'enterTag is p');
  763. setTimeout(function () {
  764. UE.delEditor('div1');
  765. UE.delEditor('div2');
  766. document.getElementById('div1') && te.dom.push(document.getElementById('div1'));
  767. document.getElementById('div2') && te.dom.push(document.getElementById('div2'));
  768. start();
  769. }, 500);
  770. });
  771. });
  772. }, 300);
  773. });
  774. test('绑定事件', function () {
  775. document.onmouseup = function (event) {
  776. ok(true, "mouseup is fired");
  777. };
  778. document.onmousedown = function (event) {
  779. ok(true, "mousedown is fired");
  780. };
  781. document.onmouseover = function (event) {
  782. ok(true, "mouseover is fired");
  783. };
  784. document.onkeydown = function (event) {
  785. ok(true, "keydown is fired");
  786. };
  787. document.onkeyup = function (event) {
  788. ok(true, "keyup is fired");
  789. };
  790. var editor = new baidu.editor.Editor({'autoFloatEnabled': false});
  791. var div = document.body.appendChild(document.createElement('div'));
  792. editor.render(div);
  793. expect(5);
  794. editor.ready(function () {
  795. setTimeout(function () {
  796. editor.focus();
  797. ua.mousedown(document.body);
  798. ua.mouseup(document.body);
  799. ua.mouseover(document.body);
  800. ua.keydown(document.body, {'keyCode': 13});
  801. ua.keyup(document.body, {'keyCode': 13});
  802. setTimeout(function () {
  803. document.getElementById('div') && te.dom.push(document.getElementById('div'));
  804. start();
  805. }, 1000);
  806. }, 50);
  807. });
  808. stop();
  809. });
  810. ////.fireMouseEvent(target, "contextmenu", options);
  811. //test('dragover',function(){
  812. // var editor = new baidu.editor.Editor({'autoFloatEnabled':false});
  813. // var div = document.body.appendChild(document.createElement('div'));
  814. // editor.render(div);
  815. // editor.ready(function(){
  816. // editor.focus();
  817. // ua.fireMouseEvent(document.body, "dragover");
  818. // setTimeout(function(){
  819. // expect(5);
  820. // start();
  821. // },100);
  822. // });
  823. //});