paste.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. var txtContent,htmlContent,address;
  66. function filter(div){
  67. var html;
  68. if ( div.firstChild ) {
  69. //去掉cut中添加的边界值
  70. var nodes = domUtils.getElementsByTagName(div,'span');
  71. for(var i=0,ni;ni=nodes[i++];){
  72. if(ni.id == '_baidu_cut_start' || ni.id == '_baidu_cut_end'){
  73. domUtils.remove(ni);
  74. }
  75. }
  76. if(browser.webkit){
  77. var brs = div.querySelectorAll('div br');
  78. for(var i=0,bi;bi=brs[i++];){
  79. var pN = bi.parentNode;
  80. if(pN.tagName == 'DIV' && pN.childNodes.length ==1){
  81. pN.innerHTML = '<p><br/></p>';
  82. domUtils.remove(pN);
  83. }
  84. }
  85. var divs = div.querySelectorAll('#baidu_pastebin');
  86. for(var i=0,di;di=divs[i++];){
  87. var tmpP = me.document.createElement('p');
  88. di.parentNode.insertBefore(tmpP,di);
  89. while(di.firstChild){
  90. tmpP.appendChild(di.firstChild);
  91. }
  92. domUtils.remove(di);
  93. }
  94. var metas = div.querySelectorAll('meta');
  95. for(var i=0,ci;ci=metas[i++];){
  96. domUtils.remove(ci);
  97. }
  98. var brs = div.querySelectorAll('br');
  99. for(i=0;ci=brs[i++];){
  100. if(/^apple-/.test(ci)){
  101. domUtils.remove(ci);
  102. }
  103. }
  104. utils.each(domUtils.getElementsByTagName(div,'span',function(node){
  105. if(node.style.cssText){
  106. node.style.cssText = node.style.cssText.replace(/white-space[^;]+;/g,'');
  107. if(!node.style.cssText){
  108. domUtils.removeAttributes(node,'style');
  109. if(domUtils.hasNoAttributes(node)){
  110. return 1
  111. }
  112. }
  113. }
  114. return 0
  115. }),function(si){
  116. domUtils.remove(si,true)
  117. })
  118. }
  119. if(browser.gecko){
  120. var dirtyNodes = div.querySelectorAll('[_moz_dirty]');
  121. for(i=0;ci=dirtyNodes[i++];){
  122. ci.removeAttribute( '_moz_dirty' );
  123. }
  124. }
  125. if(!browser.ie ){
  126. var spans = div.querySelectorAll('span.Apple-style-span');
  127. for(var i=0,ci;ci=spans[i++];){
  128. domUtils.remove(ci,true);
  129. }
  130. }
  131. //ie下使用innerHTML会产生多余的\r\n字符,也会产生&nbsp;这里过滤掉
  132. html = div.innerHTML.replace(/>(?:(\s|&nbsp;)*?)</g,'><');
  133. var f = me.serialize;
  134. if(f){
  135. //如果过滤出现问题,捕获它,直接插入内容,避免出现错误导致粘贴整个失败
  136. try{
  137. html = UE.filterWord(html);
  138. var node = f.transformInput(
  139. f.parseHTML(
  140. //todo: 暂时不走dtd的过滤
  141. html//, true
  142. ),word_img_flag
  143. );
  144. //trace:924
  145. //纯文本模式也要保留段落
  146. node = f.filter(node,pasteplain ? {
  147. whiteList: {
  148. 'p': {'br':1,'BR':1,$:{}},
  149. 'br':{'$':{}},
  150. 'div':{'br':1,'BR':1,'$':{}},
  151. 'li':{'$':{}},
  152. 'tr':{'td':1,'$':{}},
  153. 'td':{'$':{}}
  154. },
  155. blackList: {
  156. 'style':1,
  157. 'script':1,
  158. 'object':1
  159. }
  160. } : null, !pasteplain ? modify_num : null);
  161. if(browser.webkit){
  162. var length = node.children.length,
  163. child;
  164. while((child = node.children[length-1]) && child.tag == 'br'){
  165. node.children.splice(length-1,1);
  166. length = node.children.length;
  167. }
  168. }
  169. html = f.toHTML(node,pasteplain);
  170. txtContent = f.filter(node,{
  171. whiteList: {
  172. 'p': {'br':1,'BR':1,$:{}},
  173. 'br':{'$':{}},
  174. 'div':{'br':1,'BR':1,'$':{},'table':1,'ul':1,'ol':1},
  175. 'li':{'$':{}},
  176. 'ul':{'li':1,'$':{}},
  177. 'ol':{'li':1,'$':{}},
  178. 'tr':{'td':1,'$':{}},
  179. 'td':{'$':{}},
  180. 'table': {'tr':1,'tbody':1,'td':1,'$':{}},
  181. 'tbody': {'tr':1,'td':1,'$':{}},
  182. h1:{'$':{}},h2:{'$':{}},h3:{'$':{}},h4:{'$':{}},h5:{'$':{}},h6:{'$':{}}
  183. },
  184. blackList: {
  185. 'style':1,
  186. 'script':1,
  187. 'object':1
  188. }
  189. });
  190. txtContent = f.toHTML(txtContent,true)
  191. }catch(e){}
  192. }
  193. //自定义的处理
  194. html = {'html':html,'txtContent':txtContent};
  195. me.fireEvent('beforepaste',html);
  196. //不用在走过滤了
  197. if(html.html){
  198. htmlContent = html.html;
  199. address = me.selection.getRange().createAddress(true);
  200. me.execCommand( 'insertHtml',htmlContent,true);
  201. me.fireEvent("afterpaste");
  202. }
  203. }
  204. }
  205. me.addListener('pasteTransfer',function(cmd,plainType){
  206. if(address && txtContent && htmlContent && txtContent != htmlContent){
  207. var range = me.selection.getRange();
  208. range.moveToAddress(address,true).deleteContents();
  209. range.select(true);
  210. me.__hasEnterExecCommand = true;
  211. var html = htmlContent;
  212. if(plainType === 2){
  213. html = html.replace(/<(\/?)([\w\-]+)([^>]*)>/gi,function(a,b,tagName,attrs){
  214. tagName = tagName.toLowerCase();
  215. if({img:1}[tagName]){
  216. return a;
  217. }
  218. attrs = attrs.replace(/([\w\-]*?)\s*=\s*(("([^"]*)")|('([^']*)')|([^\s>]+))/gi,function(str,atr,val){
  219. if({
  220. 'src':1,
  221. 'href':1,
  222. 'name':1
  223. }[atr.toLowerCase()]){
  224. return atr + '=' + val + ' '
  225. }
  226. return ''
  227. });
  228. if({
  229. 'span':1,
  230. 'div':1
  231. }[tagName]){
  232. return ''
  233. }else{
  234. return '<' + b + tagName + ' ' + utils.trim(attrs) + '>'
  235. }
  236. });
  237. }else if(plainType){
  238. html = txtContent;
  239. }
  240. me.execCommand('inserthtml',html,true);
  241. me.__hasEnterExecCommand = false;
  242. var tmpAddress = me.selection.getRange().createAddress(true);
  243. address.endAddress = tmpAddress.startAddress;
  244. }
  245. });
  246. me.addListener('ready',function(){
  247. domUtils.on(me.body,'cut',function(){
  248. var range = me.selection.getRange();
  249. if(!range.collapsed && me.undoManger){
  250. me.undoManger.save();
  251. }
  252. });
  253. //ie下beforepaste在点击右键时也会触发,所以用监控键盘才处理
  254. domUtils.on(me.body, browser.ie || browser.opera ? 'keydown' : 'paste',function(e){
  255. if((browser.ie || browser.opera) && ((!e.ctrlKey && !e.metaKey) || e.keyCode != '86')){
  256. return;
  257. }
  258. getClipboardData.call( me, function( div ) {
  259. filter(div);
  260. } );
  261. });
  262. });
  263. };
  264. })();