plugin.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1.  CKEDITOR.plugins.add('ecleanalltext',
  2. {
  3. init: function(editor)
  4. {
  5. //plugin code goes here
  6. var pluginName = 'ecleanalltext';
  7. //CKEDITOR.dialog.add(pluginName, this.path + 'dialogs/flvPlayer.js');
  8. editor.addCommand(pluginName,{exec:function(editor){
  9. var htmlContent=editor.getData();
  10. //允许的标签项
  11. allowTags = ['p', 'a', 'img', 'br'];
  12. //验证的正则
  13. tagPatrn = /<\s*([\/]?)\s*([\w]+)[^>]*>/ig;
  14. //删除允许范围之外的标签
  15. htmlContent = htmlContent.replace(tagPatrn, function(withTag, isClose, htmlTag){
  16. var htmlReturn = '';
  17. //alert('isClose:'+isClose+'#### tag:'+htmlTag);
  18. htmlTag = htmlTag.toLowerCase();
  19. for (i = 0; i < allowTags.length; i++){
  20. if(allowTags[i] != htmlTag){
  21. continue;
  22. }
  23. if(isClose == ''){
  24. switch(htmlTag){
  25. case 'p':
  26. htmlReturn = '<p>';
  27. break;
  28. case 'a':
  29. htmlReturn = withTag;
  30. break;
  31. case 'br':
  32. htmlReturn = '</p><p>';
  33. break;
  34. default:
  35. htmlReturn = withTag;
  36. break;
  37. }
  38. }else
  39. htmlReturn = withTag;
  40. break;
  41. }
  42. return htmlReturn;
  43. });
  44. htmlContent = htmlContent.replace(/<a\s[^>]*>([^<]*)<\/a>/img,'$1');// remove link
  45. htmlContent = htmlContent.replace(/<p>(\s|&nbsp;|\u20)*(.*)<\/p>/img,function(a, b, c){
  46. if(c =='') return '';
  47. else return '<p>'+c+'</p>';
  48. });
  49. editor.setData(htmlContent);
  50. }});
  51. editor.ui.addButton('ecleanalltext',
  52. {
  53. label: '一键清理代码',
  54. command: pluginName,
  55. icon: this.path + 'images/cleanalltext.gif'
  56. });
  57. }
  58. });