From 85594379e2696f1f636fb1c782b425a54b92d8d2 Mon Sep 17 00:00:00 2001 From: Zongmin Lei Date: Fri, 16 Jan 2015 20:27:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81AMD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 109 +++++++++++++++++++++++++++------------------ README.zh.md | 56 +++++++++++++++-------- bower.json | 5 ++- dist/test.html | 4 +- dist/test_amd.html | 20 +++++++++ dist/xss.js | 50 ++++++++++++--------- lib/index.js | 50 ++++++++++++--------- package.json | 2 +- 8 files changed, 189 insertions(+), 107 deletions(-) create mode 100644 dist/test_amd.html diff --git a/README.md b/README.md index b4a0ba9..a44956d 100644 --- a/README.md +++ b/README.md @@ -9,10 +9,6 @@ Sanitize untrusted HTML (to prevent XSS) with a configuration specified by a Whi -------------- -**NOTE: The format of custom configuration (except Whitelist) from version -0.0.X was changed a lot since version 0.1.X. To use a newer version, it's -suggested to read the following guidelines carefully.** - **[中文版文档](https://github.com/leizongmin/js-xss/blob/master/README.zh.md)** `xss` is a module used to filter input from users to prevent XSS attacks. @@ -61,17 +57,69 @@ For test code please refer to `benchmark` directory. Run `npm test` command in the source directary. -## Active Test +## Install -Run the following command, them you can type HTML -code in the command-line, and check the filtered output: +### NPM ```bash -$ xss -t +$ npm install xss ``` +### Bower + +```bash +$ bower install xss +``` + +Or + +```bash +$ bower install https://github.com/leizongmin/js-xss.git +``` + + +## Usages + +### On Node.js + +```JavaScript +var xss = require('xss'); +var html = xss(''); +console.log(html); +``` + +### On Browser + +Shim mode (reference file `test/test.html`): + +```HTML + + +``` + +AMD mode (reference file `test/test_amd.html`): + +```HTML + +``` + + ## Command Line Tool +### Process file + You can use the xss command line tool to process a file. Usage: ```bash @@ -84,45 +132,18 @@ Example: $ xss -i origin.html -o target.html ``` +### Active Test + +Run the following command, them you can type HTML +code in the command-line, and check the filtered output: + +```bash +$ xss -t +``` + For more details, please run `$ xss -h` to see it. -## Usages - -### In Node.js - -To install: - -```bash -$ npm install xss -``` - -Simple usage: - -```JavaScript -var xss = require('xss'); -var html = xss(''); -console.log(html); -``` - -### In browsers - -```HTML - - -``` - -### Bower - -```bash -$ bower install xss -``` - - ## Custom filter rules When using the `xss()` function, the second parameter could be used to specify diff --git a/README.zh.md b/README.zh.md index 4054a89..13f61a7 100644 --- a/README.zh.md +++ b/README.zh.md @@ -9,10 +9,6 @@ -------------- -**注意:0.1.x版本与0.0.x版本在自定义配置(除白名单配置外)格式上有较大改动,如果 -要使用新版本,请详细阅读下文的使用说明** - - `xss`是一个用于对用户输入的内容进行过滤,以避免遭受XSS攻击的模块 ([什么是XSS攻击?](http://baike.baidu.com/view/2161269.htm))。主要用于论坛、博客、网上商店等等一些可允许用户录入页面排版、 格式控制相关的HTML的场景,`xss`模块通过白名单来控制允许的标签及相关的标签属性, @@ -54,12 +50,24 @@ 在源码目录执行命令: `npm test` -## 在线测试 +## 安装 -执行以下命令,可在命令行中输入HTML代码,并看到过滤后的代码: +### NPM ```bash -$ xss -t +$ npm install xss +``` + +### Bower + +```bash +$ bower install xss +``` + +或者 + +```bash +$ bower install https://github.com/leizongmin/js-xss.git ``` @@ -67,14 +75,6 @@ $ xss -t ### 在Node.js中使用 -安装: - -```bash -$ npm install xss -``` - -简单使用方法: - ```JavaScript var xss = require('xss'); var html = xss(''); @@ -83,6 +83,8 @@ console.log(html); ### 在浏览器端使用 +Shim模式(参考文件 `test/test.html`): + ```HTML ``` -### Bower +AMD模式(参考文件 `test/test_amd.html`): -```bash -$ bower install xss +```HTML + ``` ### 使用命令行工具来对文件进行XSS处理 +### 处理文件 + 可通过内置的 `xss` 命令来对输入的文件进行XSS处理。使用方法: ```bash @@ -113,6 +125,14 @@ xss -i <源文件> -o <目标文件> $ xss -i origin.html -o target.html ``` +### 在线测试 + +执行以下命令,可在命令行中输入HTML代码,并看到过滤后的代码: + +```bash +$ xss -t +``` + 详细命令行参数说明,请输入 `$ xss -h` 来查看。 diff --git a/bower.json b/bower.json index 28d66c2..bec3d74 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "xss", - "version": "0.1.13", + "version": "0.1.17", "homepage": "https://github.com/leizongmin/js-xss", "authors": [ "Zongmin Lei " @@ -9,7 +9,8 @@ "main": "dist/xss.js", "moduleType": [ "globals", - "node" + "node", + "amd" ], "keywords": [ "sanitization", diff --git a/dist/test.html b/dist/test.html index 825b7ed..cae361e 100644 --- a/dist/test.html +++ b/dist/test.html @@ -5,11 +5,11 @@ - +

 
 
 
 
\ No newline at end of file
diff --git a/dist/test_amd.html b/dist/test_amd.html
new file mode 100644
index 0000000..29fe705
--- /dev/null
+++ b/dist/test_amd.html
@@ -0,0 +1,20 @@
+
+
+
+  测试
+  
+
+
+  

+
+
+
+
diff --git a/dist/xss.js b/dist/xss.js
index 6a7a788..24df8e8 100644
--- a/dist/xss.js
+++ b/dist/xss.js
@@ -407,28 +407,38 @@ for (var i in DEFAULT) exports[i] = DEFAULT[i];
 for (var i in parser) exports[i] = parser[i];
 
 
+// 低版本浏览器支持
+if (!Array.prototype.indexOf) {
+  Array.prototype.indexOf = function (item) {
+    for (var i = 0; i < this.length; i++) {
+      if (this[i] === item) return i;
+    }
+    return -1;
+  };
+}
+if (!Array.prototype.forEach) {
+  Array.prototype.forEach = function (fn, scope) {
+    for (var i = 0; i < this.length; i++) {
+      fn.call(scope, this[i], i, this);
+    }
+  };
+}
+if (!String.prototype.trim) {
+  String.prototype.trim = function () {
+    return this.replace(/(^\s*)|(\s*$)/g, '');
+  };
+}
+
+
+// 在AMD下使用
+if (typeof define === 'function' && define.amd) {
+  define(function () {
+    return module.exports;
+  });
+}
+
 // 在浏览器端使用
 if (typeof window !== 'undefined') {
-  // 低版本浏览器支持
-  if (!Array.prototype.indexOf) {
-    Array.prototype.indexOf = function (item) {
-      for(var i=0;i (http://ucdok.com)",
   "contributors": [