首页 > 语言 > JavaScript > 正文

kindeditor修复会替换script内容的问题

2024-05-06 16:18:17
字体:大 中 小
来源:转载
供稿:网友

这里给大家分享的是个人修改的kindeditor的代码,主要是修复了一些BUG,添加了些常用功能,推荐给大家,有需要的小伙伴可以参考下。

kindeditor一些个人修改

1.替换script里面的内容的问题

2.颜色选择器扩展,复制的fck编辑器选颜色

3.swfupload.swf上传前图片预览功能

kindeditor.js

 

 
  1. function _formatHtml(html, htmlTags, urlType, wellFormatted, indentChar) { 
  2. if (html == null) { 
  3. html = ''; 
  4. } 
  5. //2015-03-25 
  6. html = html.replace(/textarea__/ig, "textarea"); 
  7. urlType = urlType || ''; 
  8. wellFormatted = _undef(wellFormatted, false); 
  9. indentChar = _undef(indentChar, '/t'); 
  10. var fontSizeList = 'xx-small,x-small,small,medium,large,x-large,xx-large'.split(','); 
  11.  
  12. //2015-04-02将script里面的数据读取处理 
  13. var obj = {}; 
  14. var index = 0; 
  15. html = html.replace(/(<script/s[^>]*>)([/s/S]*?)(<//script>)/ig, function ($0, $1, $2, $3) { 
  16. var value = $2 + ''; 
  17. if (value) { 
  18. var key = "{$script__" + index + "$}"; 
  19. obj[key] = value; 
  20. index++; 
  21. return $1 + key + $3; 
  22. } else { 
  23. return $0; 
  24. } 
  25. }); 
  26. html = html.replace(/(<(?:pre|pre/s[^>]*)>)([/s/S]*?)(<//pre>)/ig, function ($0, $1, $2, $3) { 
  27. return $1 + $2.replace(/<(?:br|br/s[^>]*)>/ig, '/n') + $3; 
  28. }); 
  29. html = html.replace(/<(?:br|br/s[^>]*)/s*//?>/s*<//p>/ig, '</p>'); 
  30. html = html.replace(/(<(?:p|p/s[^>]*)>)/s*(<//p>)/ig, '$1<br />$2'); 
  31. html = html.replace(//u200B/g, ''); 
  32. html = html.replace(//u00A9/g, '©'); 
  33. html = html.replace(//u00AE/g, '®'); 
  34. html = html.replace(/<[^>]+/g, function ($0) { 
  35. return $0.replace(//s+/g, ' '); 
  36. }); 
  37. var htmlTagMap = {}; 
  38. if (htmlTags) { 
  39. _each(htmlTags, function (key, val) { 
  40. var arr = key.split(','); 
  41. for (var i = 0, len = arr.length; i < len; i++) { 
  42. htmlTagMap[arr[i]] = _toMap(val); 
  43. } 
  44. }); 
  45. if (!htmlTagMap.script) { 
  46. html = html.replace(/(<(?:script|script/s[^>]*)>)([/s/S]*?)(<//script>)/ig, ''); 
  47. } 
  48. if (!htmlTagMap.style) { 
  49. html = html.replace(/(<(?:style|style/s[^>]*)>)([/s/S]*?)(<//style>)/ig, ''); 
  50. } 
  51. } 
  52. var re = /(/s*)<(//)?([/w/-:]+)((?:/s+|(?:/s+[/w/-:]+)|(?:/s+[/w/-:]+=[^/s"'<>]+)|(?:/s+[/w/-:"]+="[^"]*")|(?:/s+[/w/-:"]+='[^']*'))*)(//)?>(/s*)/g; 
  53. var tagStack = []; 
  54. html = html.replace(re, function ($0, $1, $2, $3, $4, $5, $6) { 
  55. var full = $0, 
  56. startNewline = $1 || '', 
  57. startSlash = $2 || '', 
  58. tagName = $3.toLowerCase(), 
  59. attr = $4 || '', 
  60. endSlash = $5 ? ' ' + $5 : '', 
  61. endNewline = $6 || ''; 
  62. if (htmlTags && !htmlTagMap[tagName]) { 
  63. return ''; 
  64. } 
  65. if (endSlash === '' && _SINGLE_TAG_MAP[tagName]) { 
  66. endSlash = ' /'; 
  67. } 
  68. if (_INLINE_TAG_MAP[tagName]) { 
  69. if (startNewline) { 
  70. startNewline = ' '; 
  71. } 
  72. if (endNewline) { 
  73. endNewline = ' '; 
  74. } 
  75. } 
  76. if (_PRE_TAG_MAP[tagName]) { 
  77. if (startSlash) { 
  78. endNewline = '/n'; 
  79. } else { 
  80. startNewline = '/n'; 
  81. } 
  82. } 
  83. if (wellFormatted && tagName == 'br') { 
  84. endNewline = '/n'; 
  85. } 
  86. if (_BLOCK_TAG_MAP[tagName] && !_PRE_TAG_MAP[tagName]) { 
  87. if (wellFormatted) { 
  88. if (startSlash && tagStack.length > 0 && tagStack[tagStack.length - 1] === tagName) { 
  89. tagStack.pop(); 
  90. } else { 
  91. tagStack.push(tagName); 
  92. } 
  93. startNewline = '/n'; 
  94. endNewline = '/n'; 
  95. for (var i = 0, len = startSlash ? tagStack.length : tagStack.length - 1; i < len; i++) { 
  96. startNewline += indentChar; 
  97. if (!startSlash) { 
  98. endNewline += indentChar; 
  99. } 
  100. } 
  101. if (endSlash) { 
  102. tagStack.pop(); 
  103. } else if (!startSlash) { 
  104. endNewline += indentChar; 
  105. } 
  106. } else { 
  107. startNewline = endNewline = ''; 
  108. } 
  109. } 
  110. if (attr !== '') { 
  111. var attrMap = _getAttrList(full); 
  112. if (tagName === 'font') { 
  113. var fontStyleMap = {}, fontStyle = ''; 
  114. _each(attrMap, function (key, val) { 
  115. if (key === 'color') { 
  116. fontStyleMap.color = val; 
  117. delete attrMap[key]; 
  118. } 
  119. if (key === 'size') { 
  120. fontStyleMap['font-size'] = fontSizeList[parseInt(val, 10) - 1] || ''; 
  121. delete attrMap[key]; 
  122. } 
  123. if (key === 'face') { 
  124. fontStyleMap['font-family'] = val; 
  125. delete attrMap[key]; 
  126. } 
  127. if (key === 'style') { 
  128. fontStyle = val; 
  129. } 
  130. }); 
  131. if (fontStyle && !/;$/.test(fontStyle)) { 
  132. fontStyle += ';'; 
  133. } 
  134. _each(fontStyleMap, function (key, val) { 
  135. if (val === '') { 
  136. return; 
  137. } 
  138. if (//s/.test(val)) { 
  139. val = "'" + val + "'"; 
  140. } 
  141. fontStyle += key + ':' + val + ';'; 
  142. }); 
  143. attrMap.style = fontStyle; 
  144. } 
  145. _each(attrMap, function (key, val) { 
  146. if (_FILL_ATTR_MAP[key]) { 
  147. attrMap[key] = key; 
  148. } 
  149. if (_inArray(key, ['src', 'href']) >= 0) { 
  150. attrMap[key] = _formatUrl(val, urlType); 
  151. } 
  152. if (htmlTags && key !== 'style' && !htmlTagMap[tagName]['*'] && !htmlTagMap[tagName][key] || 
  153. tagName === 'body' && key === 'contenteditable' || 
  154. /^kindeditor_/d+$/.test(key)) { 
  155. delete attrMap[key]; 
  156. } 
  157. if (key === 'style' && val !== '') { 
  158. var styleMap = _getCssList(val); 
  159. _each(styleMap, function (k, v) { 
  160. if (htmlTags && !htmlTagMap[tagName].style && !htmlTagMap[tagName]['.' + k]) { 
  161. delete styleMap[k]; 
  162. } 
  163. }); 
  164. var style = ''; 
  165. _each(styleMap, function (k, v) { 
  166. style += k + ':' + v + ';'; 
  167. }); 
  168. attrMap.style = style; 
  169. } 
  170. }); 
  171. attr = ''; 
  172. _each(attrMap, function (key, val) { 
  173. if (key === 'style' && val === '') { 
  174. return; 
  175. } 
  176. val = val.replace(/"/g, '"'); 
  177. attr += ' ' + key + '="' + val + '"'; 
  178. }); 
  179. } 
  180. if (tagName === 'font') { 
  181. tagName = 'span'; 
  182. } 
  183. return startNewline + '<' + startSlash + tagName + attr + endSlash + '>' + endNewline; 
  184. }); 
  185. html = html.replace(/(<(?:pre|pre/s[^>]*)>)([/s/S]*?)(<//pre>)/ig, function ($0, $1, $2, $3) { 
  186. return $1 + $2.replace(//n/g, '<span id="__kindeditor_pre_newline__">/n') + $3; 
  187. }); 
  188. html = html.replace(//n/s*/n/g, '/n'); 
  189. html = html.replace(/<span id="__kindeditor_pre_newline__">/n/g, '/n'); 
  190. for (var key in obj) { 
  191. html = html.replace(key, obj[key]); 
  192. } 
  193. //alert(html); 
  194. return _trim(html); 
  195. } 

以上所述就是修改的kindeditor的全部代码了,希望大家能够喜欢。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选