filterRuleDemo.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <title>过滤规则定制化</title>
  5. <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
  6. <script type="text/javascript" charset="utf-8" src="../ueditor.config.js"></script>
  7. <script type="text/javascript" charset="utf-8" src="editor_api.js"></script>
  8. <style type="text/css">
  9. .clear {
  10. clear: both;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <p>尝试粘贴内容近来,这里边不能粘贴任何inline的样式,不能有iframe,style,script,embed等标签,表格不能嵌套</p>
  16. <div>
  17. <script id="editor" type="text/plain" style="width:500px;height:500px"></script>
  18. </div>
  19. </body>
  20. <script type="text/javascript">
  21. UE.getEditor('editor', {
  22. filterRules: function () {
  23. return{
  24. span:function(node){
  25. if(/Wingdings|Symbol/.test(node.getStyle('font-family'))){
  26. return true;
  27. }else{
  28. node.parentNode.removeChild(node,true)
  29. }
  30. },
  31. p: function(node){
  32. var listTag;
  33. if(node.getAttr('class') == 'MsoListParagraph'){
  34. listTag = 'MsoListParagraph'
  35. }
  36. node.setAttr();
  37. if(listTag){
  38. node.setAttr('class','MsoListParagraph')
  39. }
  40. if(!node.firstChild()){
  41. node.innerHTML(UE.browser.ie ? '&nbsp;' : '<br>')
  42. }
  43. },
  44. div: function (node) {
  45. var tmpNode, p = UE.uNode.createElement('p');
  46. while (tmpNode = node.firstChild()) {
  47. if (tmpNode.type == 'text' || !UE.dom.dtd.$block[tmpNode.tagName]) {
  48. p.appendChild(tmpNode);
  49. } else {
  50. if (p.firstChild()) {
  51. node.parentNode.insertBefore(p, node);
  52. p = UE.uNode.createElement('p');
  53. } else {
  54. node.parentNode.insertBefore(tmpNode, node);
  55. }
  56. }
  57. }
  58. if (p.firstChild()) {
  59. node.parentNode.insertBefore(p, node);
  60. }
  61. node.parentNode.removeChild(node);
  62. },
  63. //$:{}表示不保留任何属性
  64. br: {$: {}},
  65. // a: function (node) {
  66. // if(!node.firstChild()){
  67. // node.parentNode.removeChild(node);
  68. // return;
  69. // }
  70. // node.setAttr();
  71. // node.setAttr('href', '#')
  72. // },
  73. // strong: {$: {}},
  74. // b:function(node){
  75. // node.tagName = 'strong'
  76. // },
  77. // i:function(node){
  78. // node.tagName = 'em'
  79. // },
  80. // em: {$: {}},
  81. // img: function (node) {
  82. // var src = node.getAttr('src');
  83. // node.setAttr();
  84. // node.setAttr({'src':src})
  85. // },
  86. ol:{$: {}},
  87. ul: {$: {}},
  88. dl:function(node){
  89. node.tagName = 'ul';
  90. node.setAttr()
  91. },
  92. dt:function(node){
  93. node.tagName = 'li';
  94. node.setAttr()
  95. },
  96. dd:function(node){
  97. node.tagName = 'li';
  98. node.setAttr()
  99. },
  100. li: function (node) {
  101. var className = node.getAttr('class');
  102. if (!className || !/list\-/.test(className)) {
  103. node.setAttr()
  104. }
  105. var tmpNodes = node.getNodesByTagName('ol ul');
  106. UE.utils.each(tmpNodes,function(n){
  107. node.parentNode.insertAfter(n,node);
  108. })
  109. },
  110. table: function (node) {
  111. UE.utils.each(node.getNodesByTagName('table'), function (t) {
  112. UE.utils.each(t.getNodesByTagName('tr'), function (tr) {
  113. var p = UE.uNode.createElement('p'), child, html = [];
  114. while (child = tr.firstChild()) {
  115. html.push(child.innerHTML());
  116. tr.removeChild(child);
  117. }
  118. p.innerHTML(html.join('&nbsp;&nbsp;'));
  119. t.parentNode.insertBefore(p, t);
  120. })
  121. t.parentNode.removeChild(t)
  122. });
  123. var val = node.getAttr('width');
  124. node.setAttr();
  125. if (val) {
  126. node.setAttr('width', val);
  127. }
  128. },
  129. tbody: {$: {}},
  130. caption: {$: {}},
  131. th: {$: {}},
  132. td: {$: {valign: 1, align: 1,rowspan:1,colspan:1,width:1,height:1}},
  133. tr: {$: {}},
  134. h3: {$: {}},
  135. h2: {$: {}},
  136. //黑名单,以下标签及其子节点都会被过滤掉
  137. '-': 'script style meta iframe embed object'
  138. }
  139. }()
  140. });
  141. </script>
  142. </html>