formatmatch.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. module('plugins.formatmatch');
  2. /*trace 973*/
  3. test('为一行无格式的文字刷2种不同的格式', function () {
  4. var editor = te.obj[0];
  5. var range = te.obj[1];
  6. editor.setContent('<p><strong>first</strong></p><p><em>second</em></p><p>third</p>');
  7. setTimeout(function () {
  8. var body = editor.body;
  9. range.setStart(body.firstChild.firstChild.firstChild, 2).collapse(true).select();
  10. editor.execCommand('formatmatch');
  11. range.selectNode(body.lastChild.firstChild).select();
  12. ua.mouseup(body);
  13. editor.addListener('mouseup', function () {
  14. equal(editor.queryCommandState('formatmatch'), 0, '刷后状态为0');
  15. equal(body.lastChild.innerHTML.toLowerCase(), '<em>third</em>');
  16. start();
  17. });
  18. range.setStart(body.lastChild.previousSibling.firstChild.firstChild, 2).collapse(true).select();
  19. editor.execCommand('formatmatch');
  20. range.selectNode(body.lastChild.firstChild).select();
  21. ua.mouseup(body);
  22. /*editor自身还挂了一个mouseup侦听器,必须在用例执行前调用,否则_selectionChange方法调用无法取到window,会报错*/
  23. }, 50);
  24. stop();
  25. });
  26. /*trace 971*/
  27. test('trace 971:有格式文字刷自己', function () {
  28. var editor = te.obj[0];
  29. var range = te.obj[1];
  30. editor.setContent('<p><strong>欢迎光临</strong></p>');
  31. setTimeout(function () {
  32. var body = editor.body;
  33. var text = body.firstChild.firstChild.firstChild;
  34. range.setStart(text, 2).collapse(true).select();
  35. editor.addListener('mouseup', function () {
  36. equal(editor.getContent(), '<p><strong>欢</strong><strong>迎光临</strong></p>');
  37. start();
  38. });
  39. editor.execCommand('formatmatch');
  40. range.setStart(text, 0).setEnd(text, 1).select();
  41. ua.mouseup(editor.body);
  42. }, 50);
  43. stop();
  44. });
  45. //TODO 1.2.6
  46. //test( 'trace 1553:居中的标题自己刷自己', function () {
  47. // var editor = te.obj[0];
  48. // var range = te.obj[1];
  49. // editor.setContent( '<h2 style="text-align:center">欢迎使用UEditor编辑器</h2>' );
  50. // setTimeout( function () {
  51. // var body = editor.body;
  52. // var text = body.firstChild.firstChild;
  53. // range.setStart( text, 2 ).setEnd( text, 4 ).select();
  54. // editor.addListener( 'mouseup', function () {
  55. // if ( (ua.browser.gecko && ua.browser.gecko < 2)||ua.browser.ie ==9)
  56. // equal( editor.getContent(), '<h2 style="text-align:center;" >欢迎使用UEditor编辑器</h2>' );
  57. // else
  58. // equal( editor.getContent(), '<h2 style="text-align:center" >欢迎使用UEditor编辑器</h2>' );
  59. // } );
  60. // editor.execCommand( 'formatmatch' );
  61. // range.setStart( text, 5 ).setEnd( text, 6 ).select();
  62. // ua.mouseup( editor.body );
  63. // setTimeout( function () {
  64. // start();
  65. // }, 500 );
  66. // }, 50 );
  67. // stop();
  68. //} );
  69. /*trace:969*/
  70. test('格式刷的状态反射:非闭合区间', function () {
  71. var editor = te.obj[0];
  72. var range = te.obj[1];
  73. editor.setContent('hello');
  74. setTimeout(function () {
  75. var body = editor.body;
  76. range.setStart(body.firstChild.firstChild, 2).collapse().select();
  77. editor.addListener('mouseup', function () {
  78. equal(editor.queryCommandState('formatmatch'), 0, '刷后状态为0');
  79. start();
  80. });
  81. editor.execCommand('formatmatch');
  82. equal(editor.queryCommandState('formatmatch'), 1, '刷前状态为1');
  83. range.setStart(body.firstChild.firstChild, 0).setEnd(body.firstChild.firstChild, 2).select();
  84. /*格式刷侦听mouseup事件,select方法不能触发mouseup,因此必须手动触发*/
  85. ua.mouseup(editor.body);
  86. }, 50);
  87. stop();
  88. });
  89. /*trace 964*/
  90. test('默认格式图片刷有格式的图片', function () {
  91. var editor = te.obj[0];
  92. var range = te.obj[1];
  93. editor.setContent('hello<img width="200px" height="200px" style="float: left;background-color: red"/><img width="100px" height="200px"/>');
  94. setTimeout(function () {
  95. var img = editor.body.firstChild.lastChild;
  96. var img_new = img.previousSibling;
  97. range.selectNode(img).select();
  98. editor.addListener('mouseup', function () {
  99. equal(editor.queryCommandState('formatmatch'), 0, '刷后状态为0');
  100. if (!ua.browser.opera) {
  101. equal(img_new.style.cssFloat || img_new.style.styleFloat, 'none', 'check style float', 'float');
  102. }
  103. equal(img_new.style.backgroundColor, 'red', 'check background color');
  104. start();
  105. });
  106. editor.execCommand('formatmatch');
  107. range.selectNode(img_new).select();
  108. ua.mouseup(editor.body);
  109. }, 50);
  110. stop();
  111. });
  112. /*trace 965*/
  113. test('有浮动方式图片刷默认的图片', function () {
  114. var editor = te.obj[0];
  115. var range = te.obj[1];
  116. editor.setContent('hello<img width="200px" height="200px" style="float: left;background-color: red;width: 200px"/><img width="100px" height="200px"/>');
  117. setTimeout(function () {
  118. var img = editor.body.firstChild.lastChild.previousSibling;
  119. var img_new = img.nextSibling;
  120. range.selectNode(img).select();
  121. editor.addListener('mouseup', function () {
  122. equal(editor.queryCommandState('formatmatch'), 0, '刷后状态为0');
  123. if (!ua.browser.opera) {
  124. equal(img_new.style.cssFloat || img_new.style.styleFloat, "left", 'check style float');
  125. }
  126. /*只有浮动方式会刷,其他都不刷*/
  127. equal(img_new.style.backgroundColor, '', 'check background color');
  128. equal(img_new.style.width, '', 'check style width');
  129. equal($(img_new).attr('width'), 100, 'check width');
  130. start();
  131. });
  132. editor.execCommand('formatmatch');
  133. range.selectNode(img_new).select();
  134. ua.mouseup(editor.body);
  135. }, 50);
  136. stop();
  137. });
  138. /*trace 1068*/
  139. test('独占一行图片刷默认的图片', function () {
  140. var editor = te.obj[0];
  141. var range = te.obj[1];
  142. editor.setContent('hello<img width="200px" height="200px" style="background-color: red;width: 200px;display:block;"/><img width="100px" height="200px"/>');
  143. setTimeout(function () {
  144. var img = editor.body.firstChild.lastChild.previousSibling;
  145. var img_new = img.nextSibling;
  146. range.selectNode(img).select();
  147. editor.addListener('mouseup', function () {
  148. equal(editor.queryCommandState('formatmatch'), 0, '刷后状态为0');
  149. if (!ua.browser.opera) {
  150. equal(img_new.style.display, "block", 'check display block');
  151. }
  152. start();
  153. });
  154. editor.execCommand('formatmatch');
  155. range.selectNode(img_new).select();
  156. ua.mouseup(editor.body);
  157. }, 50);
  158. stop();
  159. });
  160. /*trace 1068*/
  161. test('默认的图片图片刷独占一行图片', function () {
  162. var editor = te.obj[0];
  163. var range = te.obj[1];
  164. editor.setContent('hello<img width="200px" height="200px" style="background-color: red;width: 200px;display:block;"/><img width="100px" height="200px"/>');
  165. setTimeout(function () {
  166. var img = editor.body.firstChild.lastChild;
  167. var img_new = img.previousSibling;
  168. range.selectNode(img).select();
  169. editor.addListener('mouseup', function () {
  170. equal(editor.queryCommandState('formatmatch'), 0, '刷后状态为0');
  171. if (!ua.browser.opera) {
  172. equal(img_new.style.display, "inline", 'check display block');
  173. }
  174. start();
  175. });
  176. editor.execCommand('formatmatch');
  177. range.selectNode(img_new).select();
  178. ua.mouseup(editor.body);
  179. }, 50);
  180. stop();
  181. });
  182. /*trace 939*/
  183. test('trace 939:字母列表刷表格内的字母列表', function () {
  184. var editor = te.obj[0];
  185. var range = te.obj[1];
  186. editor.setContent('<ol style="list-style-type: lower-alpha;"><li>first</li><li>second</li></ol> <table><tbody><tr><td><ol style="list-style-type: lower-alpha;"><li>third</li><li>fourth</li></ol></td></tr></tbody></table>');
  187. setTimeout(function () {
  188. range.selectNode(editor.body.firstChild).select();
  189. editor.execCommand('formatmatch');
  190. editor.addListener('mouseup', function () {
  191. setTimeout(function () {
  192. equal(editor.body.lastChild.getElementsByTagName('ol')[0].style.listStyleType, 'lower-alpha', '查看列表是否仍然是字母的');
  193. start();
  194. }, 250);
  195. });
  196. range.selectNode(editor.body.lastChild).select();
  197. ua.mouseup(editor.body);
  198. }, 50);
  199. stop();
  200. });
  201. /*trace 938*/
  202. test('用格式刷刷整个表格', function () {
  203. var editor = te.obj[0];
  204. var range = te.obj[1];
  205. editor.setContent('<p><span style="background: yellow">hello</span></p><table><tbody><tr><td></td></tr></tbody></table>');
  206. setTimeout(function () {
  207. range.selectNode(editor.body.firstChild).select();
  208. editor.addListener('mouseup', function () {
  209. /*整个校验方法不好,没有解决根源的问题,
  210. 校验的目的应当是不会多出不应当出现的内容,除了match还可能会有其他多出来的内容
  211. 但是style之类的东西比较难校验*/
  212. equal(editor.body.innerHTML.indexOf('match'), -1, '没有插入match占位符');
  213. start();
  214. });
  215. editor.execCommand('formatmatch');
  216. range.selectNode(editor.body.lastChild).select();
  217. editor.currentSelectedArr = [editor.body.lastChild.getElementsByTagName('td')[0]];
  218. ua.mouseup(editor.body);
  219. }, 50);
  220. stop();
  221. });
  222. test('表格刷文本', function () {
  223. var editor = te.obj[0];
  224. var range = te.obj[1];
  225. editor.setContent('<p><span style="background: yellow">hello</span></p><table><tbody><tr><td>hello2</td></tr></tbody></table>');
  226. setTimeout(function () {
  227. range.selectNode(editor.body.lastChild).select();
  228. // editor.currentSelectedArr = [editor.body.lastChild.getElementsByTagName('td')[0]];
  229. editor.addListener('mouseup', function () {
  230. equal(editor.body.firstChild.innerHTML, 'hello', ' 去掉hello的格式');
  231. start();
  232. });
  233. editor.execCommand('formatmatch');
  234. setTimeout(function () {
  235. range.selectNode(editor.body.firstChild).select();
  236. ua.mouseup(editor.body);
  237. }, 50);
  238. }, 50);
  239. stop();
  240. });
  241. /*trace 1096*/
  242. test('trace 1096,1761:表格刷表格', function () {
  243. var editor = te.obj[0];
  244. var range = te.obj[1];
  245. editor.setContent('<p><span style="background: yellow">hello</span></p><table><tbody><tr><td>hello2</td><td></td></tr><tr><td></td><td>hello3</td></tr></tbody></table>');
  246. setTimeout(function () {
  247. var trs = editor.body.lastChild.getElementsByTagName('tr');
  248. var ut = editor.getUETable(editor.body.lastChild);
  249. var cellsRange = ut.getCellsRange(trs[0].cells[0], trs[1].cells[0]);
  250. ut.setSelected(cellsRange);
  251. range.setStart(trs[0].cells[0], 0).collapse(true).select();
  252. var tds = editor.body.lastChild.getElementsByTagName('td');
  253. editor.addListener('mouseup', function () {
  254. ok(ua.isEqualArray(ut.selectedTds, [trs[0].cells[0], trs[1].cells[0]]), '比较选择的区域');
  255. // equal( editor.body.getElementsByTagName('table')[0].getAttribute( 'border' ), '1', '表格边框宽度相同' ); /*如果没有指定border,那么不主动设置border*/
  256. // equal( tds[index].style['borderWidth'], '1px', '表格边框宽度相同' );
  257. // equal( tds[index].style['borderStyle'], 'solid', '表格边框样式相同' );
  258. for (var index = 0; index < tds.length; index++) {
  259. equal(tds[index].style['borderColor'], tds[0].style['borderColor'], '表格边框颜色相同');
  260. }
  261. start();
  262. });
  263. editor.execCommand('formatmatch');
  264. // editor.currentSelectedArr = [tds[1], tds[3]];
  265. range.setStart(tds[1], 0).setEnd(tds[3], 1).select();
  266. ua.mouseup(editor.body);
  267. // }, 50);
  268. }, 50);
  269. stop();
  270. });
  271. /*trace 1092, 991*/
  272. test('文本刷a标签(闭合)', function () {
  273. var editor = te.obj[0];
  274. var range = te.obj[1];
  275. editor.setContent('hello<a href="http://www.baidu.com/">baidu</a>');
  276. setTimeout(function () {
  277. var p = editor.body.firstChild;
  278. var a = p.lastChild;
  279. range.selectNode(p.firstChild).select();
  280. /*给文本刷上前景色*/
  281. editor.execCommand('forecolor', 'rgb(255,0,0)');
  282. editor.addListener('mouseup', function () {
  283. var a = p.lastChild;
  284. ua.clearWhiteNode(a);
  285. equal(a.childNodes.length, 3, '3子节点');
  286. //1.2版本中空的span里有删不掉的不可见字符,已经从浏览器复制过来了
  287. ua.checkHTMLSameStyle('ba<span style=\"color: rgb(255,0,0)\"></span>idu', editor.document, a, 'check style');
  288. start();
  289. });
  290. range.selectNode(p.firstChild).select();
  291. editor.execCommand('formatmatch');
  292. range.setStart(p.lastChild.firstChild, 2).collapse(true).select();
  293. ua.mouseup(editor.body);
  294. }, 50);
  295. stop();
  296. });
  297. test('点了格式刷后不刷文本再点一次格式刷', function () {
  298. var editor = te.obj[0];
  299. var range = te.obj[1];
  300. editor.setContent('<p><strong>first</strong></p><p><em>second</em></p><p>third</p>');
  301. setTimeout(function () {
  302. var body = editor.body;
  303. range.setStart(body.firstChild.firstChild.firstChild, 2).collapse(true).select();
  304. editor.addListener('mouseup', function () {
  305. equal(editor.__allListeners['mouseup'].length, num - 1, 'mouseup的侦听器被删除');
  306. equal(editor.queryCommandState('formatmatch'), 0, '刷后状态为0');
  307. equal(body.lastChild.innerHTML.toLowerCase(), 'third');
  308. start();
  309. });
  310. editor.execCommand('formatmatch');
  311. var num = editor.__allListeners['mouseup'].length;
  312. /*删除mouseup侦听器后直接返回*/
  313. editor.execCommand('formatmatch');
  314. equal(editor.__allListeners['mouseup'].length, num - 1, '如果第一次格式刷没执行,下一次格式刷会先去掉上一个mouseup的侦听器然后直接退出');
  315. ua.mouseup(body);
  316. }, 50);
  317. stop();
  318. });
  319. test('a标签刷文本', function () {
  320. var editor = te.obj[0];
  321. var range = te.obj[1];
  322. setTimeout(function () {
  323. editor.setContent('hello<a href="http://www.baidu.com/"><span style="color: red; ">baidu</span></a>');
  324. setTimeout(function () {
  325. var p = editor.body.firstChild;
  326. var a = p.lastChild;
  327. range.setStart(a.firstChild.firstChild, 1).collapse(true).select();
  328. editor.addListener('mouseup', function () {
  329. /*firefox不支持outerHTML*/
  330. equal(p.firstChild.innerHTML, 'hello', 'span包含文本');
  331. ok(p.firstChild.style['color'], 'red', '查看文本是否添加了样式');
  332. start();
  333. });
  334. editor.execCommand('formatmatch');
  335. range.selectNode(p.firstChild).select();
  336. ua.mouseup(editor.body);
  337. }, 50);
  338. },50);
  339. stop();
  340. });