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 相关文章推荐
xtree.js 代码
Mar 13 Javascript
jquery 插件 人性化的消息显示
Jan 21 Javascript
javascript getElementsByName()的用法说明
Jul 31 Javascript
jQuery对象和Javascript对象之间转换的实例代码
Mar 20 Javascript
$(document).ready(function() {})不执行初始化脚本
Jun 19 Javascript
jquery实现带缩略图的全屏图片画廊效果实例
Jun 25 Javascript
jQuery+css3实现Ajax点击后动态删除功能的方法
Aug 10 Javascript
JavaScript中Number对象的toFixed() 方法详解
Sep 02 Javascript
Vue2.x中的父子组件相互通信的实现方法
May 02 Javascript
Vue.js中轻松解决v-for执行出错的三个方案
Jun 09 Javascript
ES6学习之变量的两种命名方法示例
Jul 18 Javascript
Vue axios 跨域请求无法带上cookie的解决
Sep 08 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
使用MaxMind 根据IP地址对访问者定位
2006/10/09 PHP
PHP mb_convert_encoding 获取字符串编码类型实现代码
2009/04/26 PHP
php图片加中文水印实现代码分享
2012/10/31 PHP
基于php上传图片重命名的6种解决方法的详细介绍
2013/04/28 PHP
PHP之APC缓存详细介绍 apc模块安装
2014/01/13 PHP
浅谈使用PHP开发微信支付的流程
2015/10/04 PHP
Laravel下生成验证码的类
2017/11/15 PHP
解析JavaScript中点号“.”的多义性
2013/12/02 Javascript
jquery中filter方法用法实例分析
2015/02/06 Javascript
jQuery+AJAX实现网页无刷新上传
2015/02/22 Javascript
自定义jQuery插件方式实现强制对象重绘的方法
2015/03/23 Javascript
jQuery基于$.ajax设置移动端click超时处理方法
2016/05/14 Javascript
JS实现列表的响应式排版(推荐)
2016/09/01 Javascript
js实现String.Fomat的实例代码
2016/09/02 Javascript
Bootstrap select下拉联动(jQuery cxselect)
2017/01/04 Javascript
微信小程序 列表的上拉加载和下拉刷新的实现
2017/04/01 Javascript
Node.js 异步异常的处理与domain模块解析
2017/05/10 Javascript
Electron autoUpdater实现Windows安装包自动更新的方法
2018/12/24 Javascript
React降级配置及Ant Design配置详解
2018/12/27 Javascript
详解Next.js页面渲染的优化方案
2019/01/27 Javascript
基于Echarts图表在div动态切换时不显示的解决方式
2020/07/20 Javascript
Vue-cli assets SubDirectory及PublicPath区别详解
2020/08/18 Javascript
Python如何爬取实时变化的WebSocket数据的方法
2019/03/09 Python
使用gunicorn部署django项目的问题
2020/12/30 Python
10个很棒的 CSS3 开发工具 推荐
2011/05/16 HTML / CSS
2013年入党人员的自我鉴定
2013/10/25 职场文书
半年思想汇报
2013/12/30 职场文书
团队经理竞聘书
2014/03/31 职场文书
教师个人自我评价范文
2014/04/13 职场文书
汽车运用工程专业求职信
2014/06/18 职场文书
公证委托书格式
2014/09/13 职场文书
单位单身证明样本
2014/10/11 职场文书
七年级写作指导之游记作文
2019/10/07 职场文书
解决Mysql报错 Table 'mysql.user' doesn't exist
2022/05/06 MySQL
Django框架中视图的用法
2022/06/10 Python
keepalived + nginx 实现高可用方案
2022/12/24 Servers