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 相关文章推荐
jquery高效反选具体实现
May 05 Javascript
js函数参数设置默认值的一种变通实现方法
May 26 Javascript
DOM基础教程之使用DOM + Css
Jan 20 Javascript
js实现拖拽效果
Feb 12 Javascript
JavaScript中常见获取元素的方法汇总
Mar 04 Javascript
Bootstrap与KnockoutJs相结合实现分页效果实例详解
May 03 Javascript
基于BootStrap Metronic开发框架经验小结【七】数据的导入、导出及附件的查看处理
May 12 Javascript
Bootstrap实现渐变顶部固定自适应导航栏
Aug 27 Javascript
angular ng-repeat数组中的数组实例
Feb 18 Javascript
AngularJS自定义指令之复制指令实现方法
May 18 Javascript
vue-cli开发环境实现跨域请求的方法
Apr 07 Javascript
Vue左滑组件slider使用详解
Aug 21 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
PHP下载远程文件到本地存储的方法
2015/03/24 PHP
Javascript 中的 &amp;&amp; 和 || 使用小结
2010/04/25 Javascript
JavaScript实现的日期控件具体代码
2013/11/18 Javascript
JavaScript闭包实例讲解
2014/04/22 Javascript
jQuery大于号(&gt;)选择器的作用解释
2015/01/13 Javascript
JS+CSS实现模仿浏览器网页字符查找功能的方法
2015/02/26 Javascript
JS获取网页图片name属性的方法
2015/04/01 Javascript
jQuery实现气球弹出框式的侧边导航菜单效果
2015/09/22 Javascript
jQuery为动态生成的select元素添加事件的方法
2016/08/29 Javascript
微信公众平台开发教程(五)详解自定义菜单
2016/12/02 Javascript
详解jQuery中基本的动画方法
2016/12/14 Javascript
js仿搜狐视频记录片列表展示效果
2020/05/30 Javascript
vue中父子组件注意事项,传值及slot应用技巧
2018/05/09 Javascript
js中时间格式化的几种方法
2018/07/22 Javascript
高效jQuery选择器的5个技巧实例分析
2019/11/26 jQuery
JavaScript ECMA-262-3 深入解析(二):变量对象实例详解
2020/04/25 Javascript
Python实现简单登录验证
2016/04/13 Python
python 截取 取出一部分的字符串方法
2017/03/01 Python
python面向对象_详谈类的继承与方法的重载
2017/06/07 Python
Python Socket使用实例
2017/12/18 Python
Python实现将数据框数据写入mongodb及mysql数据库的方法
2018/04/02 Python
python实现五子棋游戏
2019/06/18 Python
通过PHP与Python代码对比的语法差异详解
2019/07/10 Python
Jupyter Notebook安装及使用方法解析
2020/11/12 Python
CSS3动画和HTML5新特性详解
2020/08/31 HTML / CSS
Html5移动端div固定到底部实现底部导航条的几种方式
2021/03/09 HTML / CSS
八年级音乐教学反思
2014/01/09 职场文书
课前一分钟演讲稿
2014/08/26 职场文书
个人债务授权委托书范本
2014/10/05 职场文书
2014年党员个人剖析材料
2014/10/08 职场文书
2015年幼儿园保育工作总结
2015/05/12 职场文书
土木工程毕业答辩开场白
2015/05/29 职场文书
歌剧魅影观后感
2015/06/05 职场文书
大学学生会主席竞选稿
2015/11/19 职场文书
Python装饰器的练习题
2021/11/23 Python
解决Oracle数据库用户密码过期
2022/05/11 Oracle