修复ie6,7下不支持trim,forEach,indexOf,string 不支持数组形式调用,改为charAt,format了一下数组格式,避免地版本浏览器兼容错误,修改几处局部重复定义变量语句
This commit is contained in:
48
lib/index.js
48
lib/index.js
@@ -45,7 +45,7 @@ var defaultWhiteList = {
|
||||
footer: [],
|
||||
blockquote: [],
|
||||
audio: ['autoplay', 'controls', 'loop', 'preload', 'src'],
|
||||
video: ['autoplay', 'controls', 'loop', 'preload', 'src', 'height', 'width'],
|
||||
video: ['autoplay', 'controls', 'loop', 'preload', 'src', 'height', 'width']
|
||||
};
|
||||
|
||||
// 正则表达式
|
||||
@@ -121,7 +121,7 @@ function noTag (text) {
|
||||
*
|
||||
*/
|
||||
function replaceUnicode (str, code) {
|
||||
return String.fromCharCode(parseInt(code));
|
||||
return String.fromCharCode(parseInt(code,10));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -161,7 +161,7 @@ FilterXSS.prototype.filterAttributes = function (tagName, attrs) {
|
||||
if (!hasSprit && name === '/') {
|
||||
hasSprit = true;
|
||||
return;
|
||||
};
|
||||
}
|
||||
name = name.replace(REGEXP_ATTR_NAME, '').toLowerCase();
|
||||
if (name.length < 1) return;
|
||||
if (whites.indexOf(name) !== -1) {
|
||||
@@ -171,7 +171,7 @@ FilterXSS.prototype.filterAttributes = function (tagName, attrs) {
|
||||
value = value.replace(REGEXP_ATTR_VALUE, replaceUnicode);
|
||||
var _value = '';
|
||||
for (var i = 0, len = value.length; i < len; i++) {
|
||||
_value += value.charCodeAt(i) < 32 ? ' ' : value[i];
|
||||
_value += value.charCodeAt(i) < 32 ? ' ' : value.charAt(i);
|
||||
}
|
||||
value = _value.trim();
|
||||
var newValue = me.onTagAttr(tagName, name, value);
|
||||
@@ -184,7 +184,7 @@ FilterXSS.prototype.filterAttributes = function (tagName, attrs) {
|
||||
};
|
||||
|
||||
for (var i = 0, len = attrs.length; i < len; i++) {
|
||||
var c = attrs[i];
|
||||
var c = attrs.charAt(i),v;
|
||||
if (tmpName === false && c === '=') {
|
||||
tmpName = attrs.slice(lastPos, i);
|
||||
lastPos = i + 1;
|
||||
@@ -196,7 +196,7 @@ FilterXSS.prototype.filterAttributes = function (tagName, attrs) {
|
||||
if (j === -1) {
|
||||
break;
|
||||
} else {
|
||||
var v = attrs.slice(lastPos + 1, j).trim();
|
||||
v = attrs.slice(lastPos + 1, j).trim();
|
||||
addAttr(tmpName, v);
|
||||
tmpName = false;
|
||||
i = j;
|
||||
@@ -206,7 +206,7 @@ FilterXSS.prototype.filterAttributes = function (tagName, attrs) {
|
||||
}
|
||||
}
|
||||
if (c === ' ') {
|
||||
var v = attrs.slice(lastPos, i).trim();
|
||||
v = attrs.slice(lastPos, i).trim();
|
||||
if (tmpName === false) {
|
||||
addAttr(v);
|
||||
} else {
|
||||
@@ -241,22 +241,24 @@ FilterXSS.prototype.addNewTag = function (tag, currentPos, targetPos) {
|
||||
'use strict';
|
||||
|
||||
var rethtml = '';
|
||||
var tagName;
|
||||
var hasSprit;
|
||||
var spos = tag.slice(0, 2) === '</' ? 2 : 1;
|
||||
|
||||
var i = tag.indexOf(' ');
|
||||
if (i === -1) {
|
||||
var tagName = tag.slice(spos, tag.length - 1).trim();
|
||||
tagName = tag.slice(spos, tag.length - 1).trim();
|
||||
} else {
|
||||
var tagName = tag.slice(spos, i + 1).trim();
|
||||
tagName = tag.slice(spos, i + 1).trim();
|
||||
}
|
||||
tagName = tagName.toLowerCase();
|
||||
|
||||
// 检查标签是否以“/”结尾
|
||||
if (tagName.slice(-1) === '/') {
|
||||
tagName = tagName.slice(0, -1);
|
||||
var hasSprit = true;
|
||||
hasSprit = true;
|
||||
} else {
|
||||
var hasSprit = false;
|
||||
hasSprit = false;
|
||||
}
|
||||
|
||||
if (tagName in this.whiteList) {
|
||||
@@ -300,8 +302,8 @@ FilterXSS.prototype.process = function (html) {
|
||||
var currentPos = 0;
|
||||
|
||||
// 逐个分析字符
|
||||
for (var currentPos = 0, len = html.length; currentPos < len; currentPos++) {
|
||||
var c = html[currentPos];
|
||||
for (currentPos = 0, len = html.length; currentPos < len; currentPos++) {
|
||||
var c = html.charAt(currentPos);
|
||||
if (tagStart === false) {
|
||||
if (c === '<') {
|
||||
tagStart = currentPos;
|
||||
@@ -351,7 +353,7 @@ FilterXSS.prototype.process = function (html) {
|
||||
function filterXSS (html, options) {
|
||||
var xss = new FilterXSS(options);
|
||||
return xss.process(html);
|
||||
};
|
||||
}
|
||||
|
||||
// 默认配置
|
||||
exports = module.exports = filterXSS;
|
||||
@@ -365,5 +367,23 @@ exports.utils = require('./utils');
|
||||
|
||||
// 在浏览器端使用
|
||||
if (typeof window !== 'undefined') {
|
||||
if(!Array.indexOf){
|
||||
Array.prototype.indexOf = function(item){
|
||||
for(var i=0;i<this.length;i++){
|
||||
if(this[i] == item) return i;
|
||||
}
|
||||
return -1;
|
||||
};
|
||||
}
|
||||
if(!Array.forEach){
|
||||
Array.prototype.forEach = function(fn){
|
||||
for(var i=0;i<this.length;i++) fn(this[i],i,this);
|
||||
};
|
||||
}
|
||||
if(!String.trim){
|
||||
String.prototype.trim = function(){
|
||||
return this.replace(/(^\s*)|(\s*$)/g,"");
|
||||
};
|
||||
}
|
||||
window.filterXSS = module.exports;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user