paste.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. ///import core
  2. ///import plugins/inserthtml.js
  3. ///import plugins/undo.js
  4. ///import plugins/serialize.js
  5. ///commands 粘贴
  6. ///commandsName PastePlain
  7. ///commandsTitle 纯文本粘贴模式
  8. /*
  9. ** @description 粘贴
  10. * @author zhanyi
  11. */
  12. (function() {
  13. function getClipboardData( callback ) {
  14. var doc = this.document;
  15. if ( doc.getElementById( 'baidu_pastebin' ) ) {
  16. return;
  17. }
  18. var range = this.selection.getRange(),
  19. bk = range.createBookmark(),
  20. //创建剪贴的容器div
  21. pastebin = doc.createElement( 'div' );
  22. pastebin.id = 'baidu_pastebin';
  23. // Safari 要求div必须有内容,才能粘贴内容进来
  24. browser.webkit && pastebin.appendChild( doc.createTextNode( domUtils.fillChar + domUtils.fillChar ) );
  25. doc.body.appendChild( pastebin );
  26. //trace:717 隐藏的span不能得到top
  27. //bk.start.innerHTML = ' ';
  28. bk.start.style.display = '';
  29. pastebin.style.cssText = "position:absolute;width:1px;height:1px;overflow:hidden;left:-1000px;white-space:nowrap;top:" +
  30. //要在现在光标平行的位置加入,否则会出现跳动的问题
  31. domUtils.getXY( bk.start ).y + 'px';
  32. range.selectNodeContents( pastebin ).select( true );
  33. setTimeout( function() {
  34. if (browser.webkit) {
  35. for(var i=0,pastebins = doc.querySelectorAll('#baidu_pastebin'),pi;pi=pastebins[i++];){
  36. if(domUtils.isEmptyNode(pi)){
  37. domUtils.remove(pi)
  38. }else{
  39. pastebin = pi;
  40. break;
  41. }
  42. }
  43. }
  44. try{
  45. pastebin.parentNode.removeChild(pastebin);
  46. }catch(e){}
  47. range.moveToBookmark( bk ).select(true);
  48. callback( pastebin );
  49. }, 0 );
  50. }
  51. UE.plugins['paste'] = function() {
  52. var me = this;
  53. var word_img_flag = {flag:""};
  54. var pasteplain = me.options.pasteplain === true;
  55. var modify_num = {flag:""};
  56. me.commands['pasteplain'] = {
  57. queryCommandState: function (){
  58. return pasteplain;
  59. },
  60. execCommand: function (){
  61. pasteplain = !pasteplain|0;
  62. },
  63. notNeedUndo : 1
  64. };
  65. function filter(div){
  66. var html;
  67. if ( div.firstChild ) {
  68. //去掉cut中添加的边界值
  69. var nodes = domUtils.getElementsByTagName(div,'span');
  70. for(var i=0,ni;ni=nodes[i++];){
  71. if(ni.id == '_baidu_cut_start' || ni.id == '_baidu_cut_end'){
  72. domUtils.remove(ni)
  73. }
  74. }
  75. if(browser.webkit){
  76. var brs = div.querySelectorAll('div br');
  77. for(var i=0,bi;bi=brs[i++];){
  78. var pN = bi.parentNode;
  79. if(pN.tagName == 'DIV' && pN.childNodes.length ==1){
  80. pN.innerHTML = '<p><br/></p>';
  81. domUtils.remove(pN)
  82. }
  83. }
  84. var divs = div.querySelectorAll('#baidu_pastebin');
  85. for(var i=0,di;di=divs[i++];){
  86. var tmpP = me.document.createElement('p');
  87. di.parentNode.insertBefore(tmpP,di);
  88. while(di.firstChild){
  89. tmpP.appendChild(di.firstChild)
  90. }
  91. domUtils.remove(di)
  92. }
  93. var metas = div.querySelectorAll('meta');
  94. for(var i=0,ci;ci=metas[i++];){
  95. domUtils.remove(ci);
  96. }
  97. var brs = div.querySelectorAll('br');
  98. for(i=0;ci=brs[i++];){
  99. if(/^apple-/.test(ci)){
  100. domUtils.remove(ci)
  101. }
  102. }
  103. }
  104. if(browser.gecko){
  105. var dirtyNodes = div.querySelectorAll('[_moz_dirty]')
  106. for(i=0;ci=dirtyNodes[i++];){
  107. ci.removeAttribute( '_moz_dirty' )
  108. }
  109. }
  110. if(!browser.ie ){
  111. var spans = div.querySelectorAll('span.apple-style-span');
  112. for(var i=0,ci;ci=spans[i++];){
  113. domUtils.remove(ci,true);
  114. }
  115. }
  116. html = div.innerHTML;
  117. var f = me.serialize;
  118. if(f){
  119. //如果过滤出现问题,捕获它,直接插入内容,避免出现错误导致粘贴整个失败
  120. try{
  121. var node = f.transformInput(
  122. f.parseHTML(
  123. //todo: 暂时不走dtd的过滤
  124. f.word(html)//, true
  125. ),word_img_flag
  126. );
  127. //trace:924
  128. //纯文本模式也要保留段落
  129. node = f.filter(node,pasteplain ? {
  130. whiteList: {
  131. 'p': {'br':1,'BR':1},
  132. 'br':{'$':{}},
  133. 'div':{'br':1,'BR':1,'$':{}},
  134. 'li':{'$':{}},
  135. 'tr':{'td':1,'$':{}},
  136. 'td':{'$':{}}
  137. },
  138. blackList: {
  139. 'style':1,
  140. 'script':1,
  141. 'object':1
  142. }
  143. } : null, !pasteplain ? modify_num : null);
  144. if(browser.webkit){
  145. var length = node.children.length,
  146. child;
  147. while((child = node.children[length-1]) && child.tag == 'br'){
  148. node.children.splice(length-1,1);
  149. length = node.children.length;
  150. }
  151. }
  152. html = f.toHTML(node,pasteplain)
  153. }catch(e){}
  154. }
  155. //自定义的处理
  156. html = {'html':html};
  157. me.fireEvent('beforepaste',html);
  158. //不用在走过滤了
  159. me.execCommand( 'insertHtml',html.html,true);
  160. me.fireEvent("afterpaste");
  161. }
  162. }
  163. me.addListener('ready',function(){
  164. domUtils.on(me.body,'cut',function(){
  165. var range = me.selection.getRange();
  166. if(!range.collapsed && me.undoManger){
  167. me.undoManger.save()
  168. }
  169. });
  170. //ie下beforepaste在点击右键时也会触发,所以用监控键盘才处理
  171. domUtils.on(me.body, browser.ie ? 'keydown' : 'paste',function(e){
  172. if(browser.ie && (!e.ctrlKey || e.keyCode != '86'))
  173. return;
  174. getClipboardData.call( me, function( div ) {
  175. filter(div);
  176. } );
  177. })
  178. });
  179. }
  180. })();