1
0

image.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. module( 'plugins.image' );
  2. /**
  3. * 插入视频
  4. * 插入图像
  5. * 选区闭合和不闭合
  6. * 表格中插入图像
  7. */
  8. /*trace1491 修改动图的宽高*/
  9. test( 'trace1491 修改动图的宽高', function () {
  10. setTimeout(function () {
  11. expect(3);
  12. var editor = te.obj[0];
  13. var range = te.obj[1];
  14. var body = editor.body;
  15. editor.setContent('<p><br></p>');
  16. setTimeout(function () {
  17. range.setStart(body.firstChild, 0).collapse(1).select();
  18. editor.execCommand('insertimage', {src: '../data/test.JPG'});
  19. setTimeout(function () {
  20. ua.manualDeleteFillData(editor.body);
  21. range.selectNode(body.firstChild.firstChild).select();
  22. var img = body.getElementsByTagName('img')[0];
  23. editor.execCommand('insertimage', {src: '../data/test.JPG', width: 50, height: 80});
  24. setTimeout(function () {
  25. equal($(img).attr('width'), '50', '比较width');
  26. equal($(img).attr('height'), '80', '比较width');
  27. ok(/data\/test\.JPG/.test(img.getAttribute('src')), '比较src');
  28. start();
  29. }, 500);
  30. }, 100);
  31. }, 100);
  32. }, 100);
  33. stop();
  34. } );
  35. test( '插入新图像', function () {
  36. var editor = te.obj[0];
  37. var range = te.obj[1];
  38. var body = editor.body;
  39. editor.setContent( '<p><br></p>' );
  40. range.setStart( body.firstChild, 0 ).collapse( 1 ).select();
  41. editor.execCommand( 'insertimage', {src:'http://img.baidu.com/hi/jx2/j_0001.gif', width:50, height:51} );
  42. ua.manualDeleteFillData( editor.body );
  43. var img = body.getElementsByTagName( 'img' )[0];
  44. equal( img.getAttribute( 'src' ), 'http://img.baidu.com/hi/jx2/j_0001.gif', '比较src' );
  45. equal( img.getAttribute( 'width' ), '50', '比较width' );
  46. equal( img.getAttribute( 'height' ), '51', '比较height' );
  47. } );
  48. /*trace 1490 不设宽高,插入图片*/
  49. test( 'trace 1490 不设宽高,插入图片', function () {
  50. var editor = te.obj[0];
  51. var range = te.obj[1];
  52. var body = editor.body;
  53. editor.setContent( '<p><br></p>' );
  54. range.setStart( body.firstChild, 0 ).collapse( 1 ).select();
  55. editor.execCommand( 'insertimage', {src:'http://img.baidu.com/hi/jx2/j_0001.gif'} );
  56. ua.manualDeleteFillData( editor.body );
  57. var img = body.getElementsByTagName( 'img' )[0];
  58. equal( img.getAttribute( 'src' ), 'http://img.baidu.com/hi/jx2/j_0001.gif', '比较src' );
  59. } );
  60. test( '插入对齐方式为居中对齐的图像,新建一个p,在p上设置居中对齐', function () {
  61. var editor = te.obj[0];
  62. var range = te.obj[1];
  63. var body = editor.body;
  64. editor.setContent( '<p>hello</p>' );
  65. range.setStart( body.firstChild, 0 ).collapse( 1 ).select();
  66. editor.execCommand( 'insertimage', {src:'http://img.baidu.com/hi/jx2/j_0001.gif', width:50, height:51, floatStyle:'center'} );
  67. ua.manualDeleteFillData( editor.body );
  68. var img = body.getElementsByTagName( 'img' )[0];
  69. equal( body.childNodes.length, 2, '2个p' );
  70. var p = body.firstChild;
  71. equal( p.style['textAlign'], 'center', '居中对齐' );
  72. ok( p.nextSibling.innerHTML.indexOf( 'hello' ) > -1, '第二个p里面是hello' ); //1.2版本在FF中,hello前有不可见字符
  73. if ( baidu.editor.browser.ie )
  74. equal( img.style['styleFloat'], '', 'float为空' );
  75. else
  76. equal( img.style['cssFloat'], '', 'float为空' );
  77. equal( img.getAttribute( 'src' ), 'http://img.baidu.com/hi/jx2/j_0001.gif', '比较src' );
  78. equal( img.getAttribute( 'width' ), '50', '比较width' );
  79. equal( img.getAttribute( 'height' ), '51', '比较height' );
  80. } );
  81. test( '修改已有图片的属性', function () {
  82. var editor = te.obj[0];
  83. var range = te.obj[1];
  84. var body = editor.body;
  85. editor.setContent( '<p><img src="http://img.baidu.com/hi/jx2/j_0004.gif" >hello<img src="http://img.baidu.com/hi/jx2/j_0053.gif" ></p>' );
  86. range.selectNode( body.firstChild.firstChild ).select();
  87. editor.execCommand( 'insertimage', {src:'http://img.baidu.com/hi/jx2/j_0018.gif'} );
  88. equal( ua.getChildHTML( body.firstChild ), '<img src="http://img.baidu.com/hi/jx2/j_0018.gif" _src=\"http://img.baidu.com/hi/jx2/j_0004.gif\">hello<img src="http://img.baidu.com/hi/jx2/j_0053.gif" _src=\"http://img.baidu.com/hi/jx2/j_0053.gif\">', '检查插入的图像地址' );
  89. equal( body.firstChild.childNodes.length, 3, '2个img孩子' );
  90. } );
  91. test( '选区不闭合插入图像', function () {
  92. var editor = te.obj[0];
  93. var range = te.obj[1];
  94. var body = editor.body;
  95. editor.setContent( '<p>hello1</p><p>hello2<img src="http://img.baidu.com/hi/jx2/j_0004.gif"></p>' );
  96. setTimeout(function(){
  97. range.setStart( body.firstChild.firstChild, 2 ).setEnd( body.lastChild, 2 ).select();
  98. editor.execCommand( 'insertimage', {src:'http://img.baidu.com/hi/jx2/j_0016.gif', width:'100', height:'100'} );
  99. ua.manualDeleteFillData( editor.body );
  100. equal( body.childNodes.length, 1, '只有一个p' );
  101. ua.clearWhiteNode(body.firstChild);
  102. var img = body.firstChild.lastChild;
  103. equal( img.getAttribute( 'src' ), 'http://img.baidu.com/hi/jx2/j_0016.gif', '比较src' );
  104. equal( img.getAttribute( 'width' ), '100', '比较width' );
  105. equal( img.getAttribute( 'height' ), '100', '比较height' );
  106. start();
  107. },50);
  108. stop();
  109. } );
  110. test( '图像设置左右浮动', function () {
  111. var editor = te.obj[0];
  112. var range = te.obj[1];
  113. var body = editor.body;
  114. editor.setContent( '<p>hello1</p><p>hello2<img src="http://img.baidu.com/hi/jx2/j_0004.gif"></p>' );
  115. range.selectNode( body.lastChild.lastChild ).select();
  116. editor.execCommand( 'imagefloat', 'left' );
  117. equal( body.getElementsByTagName( 'img' )[0].style['cssFloat'] || body.getElementsByTagName( 'img' )[0].style['styleFloat'], 'left', '左浮动' );
  118. // equal( body.getElementsByTagName( 'img' )[0].style['float'], 'left', '左浮动' );
  119. equal( editor.queryCommandValue( 'imagefloat' ), 'left' );
  120. editor.execCommand( 'imagefloat', 'right' );
  121. equal( body.getElementsByTagName( 'img' )[0].style['cssFloat'] || body.getElementsByTagName( 'img' )[0].style['styleFloat'], 'right', '右浮动' );
  122. equal( editor.queryCommandValue( 'imagefloat' ), 'right' );
  123. equal( editor.queryCommandState( 'imagefloat' ), 0, '图片被选中,因此图片菜单高亮' );
  124. range.setStart( body.firstChild, 0 ).collapse( 1 ).select();
  125. equal( editor.queryCommandState( 'imagefloat' ), -1, '光标闭合,因此图片菜单高不高亮' );
  126. equal( editor.queryCommandValue( 'justify' ), 'left', '段落的对齐方式为左对齐' );
  127. equal( editor.queryCommandValue( 'imagefloat' ), 'none', '图片对齐方式在闭合情况获取为空' )
  128. range.selectNode( body.firstChild.firstChild ).select();
  129. equal( editor.queryCommandValue( 'imagefloat' ), 'none', '选中文本,因此图片菜单高不高亮' );
  130. } );
  131. test( '左浮动变为默认的样式和居中', function () {
  132. var editor = te.obj[0];
  133. var range = te.obj[1];
  134. var body = editor.body;
  135. editor.setContent( '<p>hello1</p><p>hello2<img src="http://img.baidu.com/hi/jx2/j_0004.gif" style="float:left"></p>' );
  136. range.selectNode( body.lastChild.lastChild ).select();
  137. editor.execCommand( 'imagefloat', 'none' );
  138. equal( ua.getFloatStyle( body.getElementsByTagName( 'img' )[0] ), '', '没有浮动方式' );
  139. equal( editor.queryCommandValue( 'imagefloat' ), 'none' );
  140. $( body.getElementsByTagName( 'img' )[0] ).css( 'float' );
  141. range.selectNode( body.getElementsByTagName( 'img' )[0] ).select();
  142. editor.execCommand( 'imagefloat', 'center' );
  143. equal( editor.queryCommandValue( 'imagefloat' ), 'center' );
  144. equal( body.childNodes.length, 3, '3个p,image被切出一个p出来了' );
  145. var p = body.childNodes[2];
  146. equal( p.tagName.toLowerCase(), 'p', '第2个是p' );
  147. equal( p.firstChild.tagName.toLowerCase(), 'img', 'p的孩子为image' );
  148. equal( ua.getFloatStyle( p.firstChild ), '', 'image对齐方式float为空' );
  149. equal( editor.queryCommandValue( 'justify' ), 'center', '段落的对齐方式为居中' );
  150. } );
  151. test( ' 带有超链接的图片', function () {
  152. var editor = te.obj[0];
  153. var range = te.obj[1];
  154. var body = editor.body;
  155. editor.setContent( '<p>hello1</p><p>hello2<a href="www.baidu.com"><img src="http://img.baidu.com/hi/jx2/j_0004.gif" style="float:left"></a></p>' );
  156. range.selectNode( body.lastChild.lastChild ).select();
  157. editor.execCommand( 'imagefloat', 'center' );
  158. var p = body.childNodes[2];
  159. equal( p.firstChild.tagName.toLowerCase(), 'a', 'p的孩子为a' );
  160. equal( ua.getFloatStyle( p.firstChild ), '', 'image对齐方式float为空' );
  161. equal( editor.queryCommandValue( 'justify' ), 'center', '段落的对齐方式为居中' );
  162. editor.execCommand( 'imagefloat', 'left' );
  163. equal( p.firstChild.tagName.toLowerCase(), 'a', 'p的孩子为a' );
  164. equal( ua.getFloatStyle( p.firstChild.firstChild ), 'left', 'image对齐方式float为left' );
  165. editor.execCommand( 'imagefloat', 'none' );
  166. equal( p.firstChild.tagName.toLowerCase(), 'a', 'p的孩子为a' );
  167. equal( ua.getFloatStyle( p.firstChild.firstChild ), '', 'image对齐方式float为空' );
  168. } );
  169. test( ' 默认样式切换到居中再切换回默认,会把居中导致的3个p合并', function () {
  170. var editor = te.obj[0];
  171. var range = te.obj[1];
  172. var body = editor.body;
  173. editor.setContent( '<p>hello2<a href="www.baidu.com"><img src="http://img.baidu.com/hi/jx2/j_0004.gif" style="float:left"></a>hello3</p>' );
  174. setTimeout( function () {
  175. range.selectNode( body.getElementsByTagName( 'a' )[0] ).select();
  176. editor.execCommand( 'imagefloat', 'center' );
  177. var p = body.childNodes[1];
  178. equal( p.firstChild.tagName.toLowerCase(), 'a', 'p的孩子为a' );
  179. equal( ua.getFloatStyle( p.firstChild ), '', 'image对齐方式float为空' );
  180. equal( editor.queryCommandValue( 'justify' ), 'center', '段落的对齐方式为居中' );
  181. editor.execCommand( 'imagefloat', 'none' );
  182. equal( body.childNodes.length, 1, '3个p合并为1个' );
  183. var a = body.firstChild.firstChild.nextSibling;
  184. equal( a.tagName.toLowerCase(), 'a', 'p的孩子为a' );
  185. equal( a.firstChild.tagName.toLowerCase(), 'img', 'a的孩子是img' );
  186. equal( ua.getFloatStyle( a.firstChild ), '', 'image对齐方式float为空' );
  187. start();
  188. }, 50 );
  189. stop();
  190. } );