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


Posted in Javascript onApril 03, 2015

kindeditor一些个人修改

1.替换script里面的内容的问题
2.颜色选择器扩展,复制的fck编辑器选颜色
3.swfupload.swf上传前图片预览功能

kindeditor.js

function _formatHtml(html, htmlTags, urlType, wellFormatted, indentChar) {
    if (html == null) {
      html = '';
    }
    //2015-03-25
    html = html.replace(/textarea__/ig, "textarea");
    urlType = urlType || '';
    wellFormatted = _undef(wellFormatted, false);
    indentChar = _undef(indentChar, '\t');
    var fontSizeList = 'xx-small,x-small,small,medium,large,x-large,xx-large'.split(',');
 
    //2015-04-02将script里面的数据读取处理
    var obj = {};
    var index = 0;
    html = html.replace(/(<script\s[^>]*>)([\s\S]*?)(<\/script>)/ig, function ($0, $1, $2, $3) {
      var value = $2 + '';
      if (value) {
        var key = "{$script__" + index + "$}";
        obj[key] = value;
        index++;
        return $1 + key + $3;
      } else {
        return $0;
      }
    });
    html = html.replace(/(<(?:pre|pre\s[^>]*)>)([\s\S]*?)(<\/pre>)/ig, function ($0, $1, $2, $3) {
      return $1 + $2.replace(/<(?:br|br\s[^>]*)>/ig, '\n') + $3;
    });
    html = html.replace(/<(?:br|br\s[^>]*)\s*\/?>\s*<\/p>/ig, '</p>');
    html = html.replace(/(<(?:p|p\s[^>]*)>)\s*(<\/p>)/ig, '$1<br />$2');
    html = html.replace(/\u200B/g, '');
    html = html.replace(/\u00A9/g, '©');
    html = html.replace(/\u00AE/g, '®');
    html = html.replace(/<[^>]+/g, function ($0) {
      return $0.replace(/\s+/g, ' ');
    });
    var htmlTagMap = {};
    if (htmlTags) {
      _each(htmlTags, function (key, val) {
        var arr = key.split(',');
        for (var i = 0, len = arr.length; i < len; i++) {
          htmlTagMap[arr[i]] = _toMap(val);
        }
      });
      if (!htmlTagMap.script) {
        html = html.replace(/(<(?:script|script\s[^>]*)>)([\s\S]*?)(<\/script>)/ig, '');
      }
      if (!htmlTagMap.style) {
        html = html.replace(/(<(?:style|style\s[^>]*)>)([\s\S]*?)(<\/style>)/ig, '');
      }
    }
    var re = /(\s*)<(\/)?([\w\-:]+)((?:\s+|(?:\s+[\w\-:]+)|(?:\s+[\w\-:]+=[^\s"'<>]+)|(?:\s+[\w\-:"]+="[^"]*")|(?:\s+[\w\-:"]+='[^']*'))*)(\/)?>(\s*)/g;
    var tagStack = [];
    html = html.replace(re, function ($0, $1, $2, $3, $4, $5, $6) {
      var full = $0,
      startNewline = $1 || '',
      startSlash = $2 || '',
      tagName = $3.toLowerCase(),
      attr = $4 || '',
      endSlash = $5 ? ' ' + $5 : '',
      endNewline = $6 || '';
      if (htmlTags && !htmlTagMap[tagName]) {
        return '';
      }
      if (endSlash === '' && _SINGLE_TAG_MAP[tagName]) {
        endSlash = ' /';
      }
      if (_INLINE_TAG_MAP[tagName]) {
        if (startNewline) {
          startNewline = ' ';
        }
        if (endNewline) {
          endNewline = ' ';
        }
      }
      if (_PRE_TAG_MAP[tagName]) {
        if (startSlash) {
          endNewline = '\n';
        } else {
          startNewline = '\n';
        }
      }
      if (wellFormatted && tagName == 'br') {
        endNewline = '\n';
      }
      if (_BLOCK_TAG_MAP[tagName] && !_PRE_TAG_MAP[tagName]) {
        if (wellFormatted) {
          if (startSlash && tagStack.length > 0 && tagStack[tagStack.length - 1] === tagName) {
            tagStack.pop();
          } else {
            tagStack.push(tagName);
          }
          startNewline = '\n';
          endNewline = '\n';
          for (var i = 0, len = startSlash ? tagStack.length : tagStack.length - 1; i < len; i++) {
            startNewline += indentChar;
            if (!startSlash) {
              endNewline += indentChar;
            }
          }
          if (endSlash) {
            tagStack.pop();
          } else if (!startSlash) {
            endNewline += indentChar;
          }
        } else {
          startNewline = endNewline = '';
        }
      }
      if (attr !== '') {
        var attrMap = _getAttrList(full);
        if (tagName === 'font') {
          var fontStyleMap = {}, fontStyle = '';
          _each(attrMap, function (key, val) {
            if (key === 'color') {
              fontStyleMap.color = val;
              delete attrMap[key];
            }
            if (key === 'size') {
              fontStyleMap['font-size'] = fontSizeList[parseInt(val, 10) - 1] || '';
              delete attrMap[key];
            }
            if (key === 'face') {
              fontStyleMap['font-family'] = val;
              delete attrMap[key];
            }
            if (key === 'style') {
              fontStyle = val;
            }
          });
          if (fontStyle && !/;$/.test(fontStyle)) {
            fontStyle += ';';
          }
          _each(fontStyleMap, function (key, val) {
            if (val === '') {
              return;
            }
            if (/\s/.test(val)) {
              val = "'" + val + "'";
            }
            fontStyle += key + ':' + val + ';';
          });
          attrMap.style = fontStyle;
        }
        _each(attrMap, function (key, val) {
          if (_FILL_ATTR_MAP[key]) {
            attrMap[key] = key;
          }
          if (_inArray(key, ['src', 'href']) >= 0) {
            attrMap[key] = _formatUrl(val, urlType);
          }
          if (htmlTags && key !== 'style' && !htmlTagMap[tagName]['*'] && !htmlTagMap[tagName][key] ||
          tagName === 'body' && key === 'contenteditable' ||
          /^kindeditor_\d+$/.test(key)) {
            delete attrMap[key];
          }
          if (key === 'style' && val !== '') {
            var styleMap = _getCssList(val);
            _each(styleMap, function (k, v) {
              if (htmlTags && !htmlTagMap[tagName].style && !htmlTagMap[tagName]['.' + k]) {
                delete styleMap[k];
              }
            });
            var style = '';
            _each(styleMap, function (k, v) {
              style += k + ':' + v + ';';
            });
            attrMap.style = style;
          }
        });
        attr = '';
        _each(attrMap, function (key, val) {
          if (key === 'style' && val === '') {
            return;
          }
          val = val.replace(/"/g, '"');
          attr += ' ' + key + '="' + val + '"';
        });
      }
      if (tagName === 'font') {
        tagName = 'span';
      }
      return startNewline + '<' + startSlash + tagName + attr + endSlash + '>' + endNewline;
    });
    html = html.replace(/(<(?:pre|pre\s[^>]*)>)([\s\S]*?)(<\/pre>)/ig, function ($0, $1, $2, $3) {
      return $1 + $2.replace(/\n/g, '<span id="__kindeditor_pre_newline__">\n') + $3;
    });
    html = html.replace(/\n\s*\n/g, '\n');
    html = html.replace(/<span id="__kindeditor_pre_newline__">\n/g, '\n');
    for (var key in obj) {
      html = html.replace(key, obj[key]);
    }
    //alert(html);
    return _trim(html);
  }

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

Javascript 相关文章推荐
Array 重排序方法和操作方法的简单实例
Jan 24 Javascript
js监控IE火狐浏览器关闭、刷新、回退、前进事件
Jul 23 Javascript
获取阴历(农历)和当前日期的js代码
Feb 15 Javascript
JavaScript记录光标在编辑器中位置的实现方法
Apr 22 Javascript
浅析js绑定事件的常用方法
May 15 Javascript
vue.js树形组件之删除双击增加分支实例代码
Feb 28 Javascript
JS仿QQ好友列表展开、收缩功能(第一篇)
Jul 07 Javascript
Three.js利用orbit controls插件(轨道控制)控制模型交互动作详解
Sep 25 Javascript
教你用Cordova打包Vue项目的方法
Oct 17 Javascript
使用Vue组件实现一个简单弹窗效果
Apr 23 Javascript
JavaScript中concat复制数组方法浅析
Jan 20 Javascript
使用 vue 实例更好的监听事件及vue实例的方法
Apr 22 Javascript
jquery 插件实现瀑布流图片展示实例
Apr 03 #Javascript
javascript实现了照片拖拽点击置顶的照片墙代码
Apr 03 #Javascript
Javascript代码实现仿实例化类
Apr 03 #Javascript
使用Jquery实现每日签到功能
Apr 03 #Javascript
JavaScript获取按钮所在form表单id的方法
Apr 02 #Javascript
JavaScript获取表单内所有元素值的方法
Apr 02 #Javascript
jQuery简单实现QQ空间点赞已经取消点赞
Apr 02 #Javascript
You might like
使用GROUP BY的时候如何统计记录条数 COUNT(*) DISTINCT
2011/04/23 PHP
php数组保存文本与文本反编成数组实例
2014/11/13 PHP
老生常谈文本文件和二进制文件的区别
2017/02/27 PHP
php对象工厂类完整示例
2018/08/09 PHP
JS应用之禁止抓屏、复制、打印
2008/02/21 Javascript
javascript模拟select,jselect的方法实现
2012/11/08 Javascript
jQuery+JSON+jPlayer实现QQ空间音乐查询功能示例
2013/06/17 Javascript
js生成的验证码的实现与技术分析
2014/09/17 Javascript
jQueryMobile之Helloworld与页面切换的方法
2015/02/04 Javascript
WebGL利用FBO完成立方体贴图效果完整实例(附demo源码下载)
2016/01/26 Javascript
JavaScript必看小技巧(必看)
2016/06/07 Javascript
jquery自动补齐功能插件flexselect用法示例
2016/08/06 Javascript
利用jQuery的动画函数animate实现豌豆发射效果
2016/08/28 Javascript
vue中的$emit 与$on父子组件与兄弟组件的之间通信方式
2018/05/13 Javascript
简单了解微信小程序 e.target与e.currentTarget的不同
2019/09/27 Javascript
谈一谈vue请求数据放在created好还是mounted里好
2020/07/27 Javascript
python转换字符串为摩尔斯电码的方法
2015/07/06 Python
Python中列表和元组的使用方法和区别详解
2020/12/30 Python
python使用tkinter实现简单计算器
2018/01/30 Python
python DataFrame获取行数、列数、索引及第几行第几列的值方法
2018/04/08 Python
对python csv模块配置分隔符和引用符详解
2018/12/12 Python
PyQt QCombobox设置行高的方法
2019/06/20 Python
Python抓包程序mitmproxy安装和使用过程图解
2020/03/02 Python
python实现从ftp服务器下载文件
2020/03/03 Python
python中scipy.stats产生随机数实例讲解
2021/02/19 Python
html5开发三八女王节表白神器
2018/03/07 HTML / CSS
娇韵诗法国官网:Clarins法国
2019/01/29 全球购物
美国汽车零部件和配件网站:CarParts
2019/03/13 全球购物
介绍一下木马病毒的种类
2015/07/26 面试题
幼师自荐信范文
2013/10/06 职场文书
十八大感想感言
2014/02/10 职场文书
2015年体检中心工作总结
2015/05/27 职场文书
红与黑读书笔记
2015/06/29 职场文书
小学班主任教育随笔
2015/08/15 职场文书
优秀党员主要事迹范文
2015/11/05 职场文书
Mysql案例刨析事务隔离级别
2021/09/25 MySQL