translate all comments to English

This commit is contained in:
Zongmin Lei
2017-12-21 14:19:10 +08:00
parent 2deacabc3f
commit 32a4bece31
20 changed files with 232 additions and 278 deletions

View File

@@ -1,14 +1,13 @@
/**
* 默认配置
* default settings
*
* @author 老雷<leizongmin@gmail.com>
* @author Zongmin Lei<leizongmin@gmail.com>
*/
var FilterCSS = require('cssfilter').FilterCSS;
var getDefaultCSSWhiteList = require('cssfilter').getDefaultWhiteList;
var _ = require('./util');
// 默认白名单
function getDefaultWhiteList () {
return {
a: ['target', 'href', 'title'],
@@ -77,11 +76,10 @@ function getDefaultWhiteList () {
};
}
// 默认CSS Filter
var defaultCSSFilter = new FilterCSS();
/**
* 匹配到标签时的处理方法
* default onTag function
*
* @param {String} tag
* @param {String} html
@@ -93,7 +91,7 @@ function onTag (tag, html, options) {
}
/**
* 匹配到不在白名单上的标签时的处理方法
* default onIgnoreTag function
*
* @param {String} tag
* @param {String} html
@@ -105,7 +103,7 @@ function onIgnoreTag (tag, html, options) {
}
/**
* 匹配到标签属性时的处理方法
* default onTagAttr function
*
* @param {String} tag
* @param {String} name
@@ -117,7 +115,7 @@ function onTagAttr (tag, name, value) {
}
/**
* 匹配到不在白名单上的标签属性时的处理方法
* default onIgnoreTagAttr function
*
* @param {String} tag
* @param {String} name
@@ -129,7 +127,7 @@ function onIgnoreTagAttr (tag, name, value) {
}
/**
* HTML转义
* default escapeHtml function
*
* @param {String} html
*/
@@ -138,7 +136,7 @@ function escapeHtml (html) {
}
/**
* 安全的标签属性值
* default safeAttrValue function
*
* @param {String} tag
* @param {String} name
@@ -147,12 +145,12 @@ function escapeHtml (html) {
* @return {String}
*/
function safeAttrValue (tag, name, value, cssFilter) {
// 转换为友好的属性值,再做判断
// unescape attribute value firstly
value = friendlyAttrValue(value);
if (name === 'href' || name === 'src') {
// 过滤 href 和 src 属性
// 仅允许 http:// | https:// | mailto: | / | # 开头的地址
// filter `href` and `src` attribute
// only allow the value that starts with `http://` | `https://` | `mailto:` | `/` | `#`
value = _.trim(value);
if (value === '#') return '#';
if (!(value.substr(0, 7) === 'http://' ||
@@ -164,24 +162,19 @@ function safeAttrValue (tag, name, value, cssFilter) {
return '';
}
} else if (name === 'background') {
// 过滤 background 属性 这个xss漏洞较老了可能已经不适用
// javascript:
// filter `background` attribute (maybe no use)
// `javascript:`
REGEXP_DEFAULT_ON_TAG_ATTR_4.lastIndex = 0;
if (REGEXP_DEFAULT_ON_TAG_ATTR_4.test(value)) {
return '';
}
} else if (name === 'style') {
// /*注释*/
/*REGEXP_DEFAULT_ON_TAG_ATTR_3.lastIndex = 0;
if (REGEXP_DEFAULT_ON_TAG_ATTR_3.test(value)) {
return '';
}*/
// expression()
// `expression()`
REGEXP_DEFAULT_ON_TAG_ATTR_7.lastIndex = 0;
if (REGEXP_DEFAULT_ON_TAG_ATTR_7.test(value)) {
return '';
}
// url()
// `url()`
REGEXP_DEFAULT_ON_TAG_ATTR_8.lastIndex = 0;
if (REGEXP_DEFAULT_ON_TAG_ATTR_8.test(value)) {
REGEXP_DEFAULT_ON_TAG_ATTR_4.lastIndex = 0;
@@ -195,12 +188,12 @@ function safeAttrValue (tag, name, value, cssFilter) {
}
}
// 输出时需要转义<>"
// escape `<>"` before returns
value = escapeAttrValue(value);
return value;
}
// 正则表达式
// RegExp list
var REGEXP_LT = /</g;
var REGEXP_GT = />/g;
var REGEXP_QUOTE = /"/g;
@@ -216,7 +209,7 @@ var REGEXP_DEFAULT_ON_TAG_ATTR_7 = /e\s*x\s*p\s*r\s*e\s*s\s*s\s*i\s*o\s*n\s*\(.*
var REGEXP_DEFAULT_ON_TAG_ATTR_8 = /u\s*r\s*l\s*\(.*/ig;
/**
* 对双引号进行转义
* escape doube quote
*
* @param {String} str
* @return {String} str
@@ -226,7 +219,7 @@ function escapeQuote (str) {
}
/**
* 对双引号进行转义
* unescape double quote
*
* @param {String} str
* @return {String} str
@@ -236,7 +229,7 @@ function unescapeQuote (str) {
}
/**
* 对html实体编码进行转义
* escape html entities
*
* @param {String} str
* @return {String}
@@ -250,7 +243,7 @@ function escapeHtmlEntities (str) {
}
/**
* 对html5新增的危险实体编码进行转义
* escape html5 new danger entities
*
* @param {String} str
* @return {String}
@@ -261,7 +254,7 @@ function escapeDangerHtml5Entities (str) {
}
/**
* 清除不可见字符
* clear nonprintable characters
*
* @param {String} str
* @return {String}
@@ -275,21 +268,21 @@ function clearNonPrintableCharacter (str) {
}
/**
* 将标签的属性值转换成一般字符,便于分析
* get friendly attribute value
*
* @param {String} str
* @return {String}
*/
function friendlyAttrValue (str) {
str = unescapeQuote(str); // 双引号
str = escapeHtmlEntities(str); // 转换HTML实体编码
str = escapeDangerHtml5Entities(str); // 转换危险的HTML5新增实体编码
str = clearNonPrintableCharacter(str); // 清除不可见字符
str = unescapeQuote(str);
str = escapeHtmlEntities(str);
str = escapeDangerHtml5Entities(str);
str = clearNonPrintableCharacter(str);
return str;
}
/**
* 转义用于输出的标签属性值
* unescape attribute value
*
* @param {String} str
* @return {String}
@@ -301,17 +294,18 @@ function escapeAttrValue (str) {
}
/**
* 去掉不在白名单中的标签onIgnoreTag处理方法
* `onIgnoreTag` function for removing all the tags that are not in whitelist
*/
function onIgnoreTagStripAll () {
return '';
}
/**
* 删除标签体
* remove tag body
* specify a `tags` list, if the tag is not in the `tags` list then process by the specify function (optional)
*
* @param {array} tags 要删除的标签列表
* @param {function} next 对不在列表中的标签的处理函数,可选
* @param {array} tags
* @param {function} next
*/
function StripTagBody (tags, next) {
if (typeof(next) !== 'function') {
@@ -324,8 +318,8 @@ function StripTagBody (tags, next) {
return (_.indexOf(tags, tag) !== -1);
}
var removeList = []; // 要删除的位置范围列表
var posStart = false; // 当前标签开始位置
var removeList = [];
var posStart = false;
return {
onIgnoreTag: function (tag, html, options) {
@@ -360,7 +354,7 @@ function StripTagBody (tags, next) {
}
/**
* 去除备注标签
* remove html comments
*
* @param {String} html
* @return {String}
@@ -371,7 +365,7 @@ function stripCommentTag (html) {
var STRIP_COMMENT_TAG_REGEXP = /<!--[\s\S]*?-->/g;
/**
* 去除不可见字符
* remove invisible characters
*
* @param {String} html
* @return {String}