UNPKG

50 kBJavaScriptView Raw
1(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
2/**
3 * default settings
4 *
5 * @author Zongmin Lei<leizongmin@gmail.com>
6 */
7
8var FilterCSS = require("cssfilter").FilterCSS;
9var getDefaultCSSWhiteList = require("cssfilter").getDefaultWhiteList;
10var _ = require("./util");
11
12function getDefaultWhiteList() {
13 return {
14 a: ["target", "href", "title"],
15 abbr: ["title"],
16 address: [],
17 area: ["shape", "coords", "href", "alt"],
18 article: [],
19 aside: [],
20 audio: ["autoplay", "controls", "loop", "preload", "src"],
21 b: [],
22 bdi: ["dir"],
23 bdo: ["dir"],
24 big: [],
25 blockquote: ["cite"],
26 br: [],
27 caption: [],
28 center: [],
29 cite: [],
30 code: [],
31 col: ["align", "valign", "span", "width"],
32 colgroup: ["align", "valign", "span", "width"],
33 dd: [],
34 del: ["datetime"],
35 details: ["open"],
36 div: [],
37 dl: [],
38 dt: [],
39 em: [],
40 font: ["color", "size", "face"],
41 footer: [],
42 h1: [],
43 h2: [],
44 h3: [],
45 h4: [],
46 h5: [],
47 h6: [],
48 header: [],
49 hr: [],
50 i: [],
51 img: ["src", "alt", "title", "width", "height"],
52 ins: ["datetime"],
53 li: [],
54 mark: [],
55 nav: [],
56 ol: [],
57 p: [],
58 pre: [],
59 s: [],
60 section: [],
61 small: [],
62 span: [],
63 sub: [],
64 sup: [],
65 strong: [],
66 table: ["width", "border", "align", "valign"],
67 tbody: ["align", "valign"],
68 td: ["width", "rowspan", "colspan", "align", "valign"],
69 tfoot: ["align", "valign"],
70 th: ["width", "rowspan", "colspan", "align", "valign"],
71 thead: ["align", "valign"],
72 tr: ["rowspan", "align", "valign"],
73 tt: [],
74 u: [],
75 ul: [],
76 video: ["autoplay", "controls", "loop", "preload", "src", "height", "width"]
77 };
78}
79
80var defaultCSSFilter = new FilterCSS();
81
82/**
83 * default onTag function
84 *
85 * @param {String} tag
86 * @param {String} html
87 * @param {Object} options
88 * @return {String}
89 */
90function onTag(tag, html, options) {
91 // do nothing
92}
93
94/**
95 * default onIgnoreTag function
96 *
97 * @param {String} tag
98 * @param {String} html
99 * @param {Object} options
100 * @return {String}
101 */
102function onIgnoreTag(tag, html, options) {
103 // do nothing
104}
105
106/**
107 * default onTagAttr function
108 *
109 * @param {String} tag
110 * @param {String} name
111 * @param {String} value
112 * @return {String}
113 */
114function onTagAttr(tag, name, value) {
115 // do nothing
116}
117
118/**
119 * default onIgnoreTagAttr function
120 *
121 * @param {String} tag
122 * @param {String} name
123 * @param {String} value
124 * @return {String}
125 */
126function onIgnoreTagAttr(tag, name, value) {
127 // do nothing
128}
129
130/**
131 * default escapeHtml function
132 *
133 * @param {String} html
134 */
135function escapeHtml(html) {
136 return html.replace(REGEXP_LT, "&lt;").replace(REGEXP_GT, "&gt;");
137}
138
139/**
140 * default safeAttrValue function
141 *
142 * @param {String} tag
143 * @param {String} name
144 * @param {String} value
145 * @param {Object} cssFilter
146 * @return {String}
147 */
148function safeAttrValue(tag, name, value, cssFilter) {
149 // unescape attribute value firstly
150 value = friendlyAttrValue(value);
151
152 if (name === "href" || name === "src") {
153 // filter `href` and `src` attribute
154 // only allow the value that starts with `http://` | `https://` | `mailto:` | `/` | `#`
155 value = _.trim(value);
156 if (value === "#") return "#";
157 if (
158 !(
159 value.substr(0, 7) === "http://" ||
160 value.substr(0, 8) === "https://" ||
161 value.substr(0, 7) === "mailto:" ||
162 value.substr(0, 4) === "tel:" ||
163 value[0] === "#" ||
164 value[0] === "/"
165 )
166 ) {
167 return "";
168 }
169 } else if (name === "background") {
170 // filter `background` attribute (maybe no use)
171 // `javascript:`
172 REGEXP_DEFAULT_ON_TAG_ATTR_4.lastIndex = 0;
173 if (REGEXP_DEFAULT_ON_TAG_ATTR_4.test(value)) {
174 return "";
175 }
176 } else if (name === "style") {
177 // `expression()`
178 REGEXP_DEFAULT_ON_TAG_ATTR_7.lastIndex = 0;
179 if (REGEXP_DEFAULT_ON_TAG_ATTR_7.test(value)) {
180 return "";
181 }
182 // `url()`
183 REGEXP_DEFAULT_ON_TAG_ATTR_8.lastIndex = 0;
184 if (REGEXP_DEFAULT_ON_TAG_ATTR_8.test(value)) {
185 REGEXP_DEFAULT_ON_TAG_ATTR_4.lastIndex = 0;
186 if (REGEXP_DEFAULT_ON_TAG_ATTR_4.test(value)) {
187 return "";
188 }
189 }
190 if (cssFilter !== false) {
191 cssFilter = cssFilter || defaultCSSFilter;
192 value = cssFilter.process(value);
193 }
194 }
195
196 // escape `<>"` before returns
197 value = escapeAttrValue(value);
198 return value;
199}
200
201// RegExp list
202var REGEXP_LT = /</g;
203var REGEXP_GT = />/g;
204var REGEXP_QUOTE = /"/g;
205var REGEXP_QUOTE_2 = /&quot;/g;
206var REGEXP_ATTR_VALUE_1 = /&#([a-zA-Z0-9]*);?/gim;
207var REGEXP_ATTR_VALUE_COLON = /&colon;?/gim;
208var REGEXP_ATTR_VALUE_NEWLINE = /&newline;?/gim;
209var REGEXP_DEFAULT_ON_TAG_ATTR_3 = /\/\*|\*\//gm;
210var REGEXP_DEFAULT_ON_TAG_ATTR_4 = /((j\s*a\s*v\s*a|v\s*b|l\s*i\s*v\s*e)\s*s\s*c\s*r\s*i\s*p\s*t\s*|m\s*o\s*c\s*h\s*a)\:/gi;
211var REGEXP_DEFAULT_ON_TAG_ATTR_5 = /^[\s"'`]*(d\s*a\s*t\s*a\s*)\:/gi;
212var REGEXP_DEFAULT_ON_TAG_ATTR_6 = /^[\s"'`]*(d\s*a\s*t\s*a\s*)\:\s*image\//gi;
213var 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*\(.*/gi;
214var REGEXP_DEFAULT_ON_TAG_ATTR_8 = /u\s*r\s*l\s*\(.*/gi;
215
216/**
217 * escape doube quote
218 *
219 * @param {String} str
220 * @return {String} str
221 */
222function escapeQuote(str) {
223 return str.replace(REGEXP_QUOTE, "&quot;");
224}
225
226/**
227 * unescape double quote
228 *
229 * @param {String} str
230 * @return {String} str
231 */
232function unescapeQuote(str) {
233 return str.replace(REGEXP_QUOTE_2, '"');
234}
235
236/**
237 * escape html entities
238 *
239 * @param {String} str
240 * @return {String}
241 */
242function escapeHtmlEntities(str) {
243 return str.replace(REGEXP_ATTR_VALUE_1, function replaceUnicode(str, code) {
244 return code[0] === "x" || code[0] === "X"
245 ? String.fromCharCode(parseInt(code.substr(1), 16))
246 : String.fromCharCode(parseInt(code, 10));
247 });
248}
249
250/**
251 * escape html5 new danger entities
252 *
253 * @param {String} str
254 * @return {String}
255 */
256function escapeDangerHtml5Entities(str) {
257 return str
258 .replace(REGEXP_ATTR_VALUE_COLON, ":")
259 .replace(REGEXP_ATTR_VALUE_NEWLINE, " ");
260}
261
262/**
263 * clear nonprintable characters
264 *
265 * @param {String} str
266 * @return {String}
267 */
268function clearNonPrintableCharacter(str) {
269 var str2 = "";
270 for (var i = 0, len = str.length; i < len; i++) {
271 str2 += str.charCodeAt(i) < 32 ? " " : str.charAt(i);
272 }
273 return _.trim(str2);
274}
275
276/**
277 * get friendly attribute value
278 *
279 * @param {String} str
280 * @return {String}
281 */
282function friendlyAttrValue(str) {
283 str = unescapeQuote(str);
284 str = escapeHtmlEntities(str);
285 str = escapeDangerHtml5Entities(str);
286 str = clearNonPrintableCharacter(str);
287 return str;
288}
289
290/**
291 * unescape attribute value
292 *
293 * @param {String} str
294 * @return {String}
295 */
296function escapeAttrValue(str) {
297 str = escapeQuote(str);
298 str = escapeHtml(str);
299 return str;
300}
301
302/**
303 * `onIgnoreTag` function for removing all the tags that are not in whitelist
304 */
305function onIgnoreTagStripAll() {
306 return "";
307}
308
309/**
310 * remove tag body
311 * specify a `tags` list, if the tag is not in the `tags` list then process by the specify function (optional)
312 *
313 * @param {array} tags
314 * @param {function} next
315 */
316function StripTagBody(tags, next) {
317 if (typeof next !== "function") {
318 next = function() {};
319 }
320
321 var isRemoveAllTag = !Array.isArray(tags);
322 function isRemoveTag(tag) {
323 if (isRemoveAllTag) return true;
324 return _.indexOf(tags, tag) !== -1;
325 }
326
327 var removeList = [];
328 var posStart = false;
329
330 return {
331 onIgnoreTag: function(tag, html, options) {
332 if (isRemoveTag(tag)) {
333 if (options.isClosing) {
334 var ret = "[/removed]";
335 var end = options.position + ret.length;
336 removeList.push([
337 posStart !== false ? posStart : options.position,
338 end
339 ]);
340 posStart = false;
341 return ret;
342 } else {
343 if (!posStart) {
344 posStart = options.position;
345 }
346 return "[removed]";
347 }
348 } else {
349 return next(tag, html, options);
350 }
351 },
352 remove: function(html) {
353 var rethtml = "";
354 var lastPos = 0;
355 _.forEach(removeList, function(pos) {
356 rethtml += html.slice(lastPos, pos[0]);
357 lastPos = pos[1];
358 });
359 rethtml += html.slice(lastPos);
360 return rethtml;
361 }
362 };
363}
364
365/**
366 * remove html comments
367 *
368 * @param {String} html
369 * @return {String}
370 */
371function stripCommentTag(html) {
372 return html.replace(STRIP_COMMENT_TAG_REGEXP, "");
373}
374var STRIP_COMMENT_TAG_REGEXP = /<!--[\s\S]*?-->/g;
375
376/**
377 * remove invisible characters
378 *
379 * @param {String} html
380 * @return {String}
381 */
382function stripBlankChar(html) {
383 var chars = html.split("");
384 chars = chars.filter(function(char) {
385 var c = char.charCodeAt(0);
386 if (c === 127) return false;
387 if (c <= 31) {
388 if (c === 10 || c === 13) return true;
389 return false;
390 }
391 return true;
392 });
393 return chars.join("");
394}
395
396exports.whiteList = getDefaultWhiteList();
397exports.getDefaultWhiteList = getDefaultWhiteList;
398exports.onTag = onTag;
399exports.onIgnoreTag = onIgnoreTag;
400exports.onTagAttr = onTagAttr;
401exports.onIgnoreTagAttr = onIgnoreTagAttr;
402exports.safeAttrValue = safeAttrValue;
403exports.escapeHtml = escapeHtml;
404exports.escapeQuote = escapeQuote;
405exports.unescapeQuote = unescapeQuote;
406exports.escapeHtmlEntities = escapeHtmlEntities;
407exports.escapeDangerHtml5Entities = escapeDangerHtml5Entities;
408exports.clearNonPrintableCharacter = clearNonPrintableCharacter;
409exports.friendlyAttrValue = friendlyAttrValue;
410exports.escapeAttrValue = escapeAttrValue;
411exports.onIgnoreTagStripAll = onIgnoreTagStripAll;
412exports.StripTagBody = StripTagBody;
413exports.stripCommentTag = stripCommentTag;
414exports.stripBlankChar = stripBlankChar;
415exports.cssFilter = defaultCSSFilter;
416exports.getDefaultCSSWhiteList = getDefaultCSSWhiteList;
417
418},{"./util":4,"cssfilter":8}],2:[function(require,module,exports){
419/**
420 * xss
421 *
422 * @author Zongmin Lei<leizongmin@gmail.com>
423 */
424
425var DEFAULT = require("./default");
426var parser = require("./parser");
427var FilterXSS = require("./xss");
428
429/**
430 * filter xss function
431 *
432 * @param {String} html
433 * @param {Object} options { whiteList, onTag, onTagAttr, onIgnoreTag, onIgnoreTagAttr, safeAttrValue, escapeHtml }
434 * @return {String}
435 */
436function filterXSS(html, options) {
437 var xss = new FilterXSS(options);
438 return xss.process(html);
439}
440
441exports = module.exports = filterXSS;
442exports.FilterXSS = FilterXSS;
443for (var i in DEFAULT) exports[i] = DEFAULT[i];
444for (var i in parser) exports[i] = parser[i];
445
446// using `xss` on the browser, output `filterXSS` to the globals
447if (typeof window !== "undefined") {
448 window.filterXSS = module.exports;
449}
450
451// using `xss` on the WebWorker, output `filterXSS` to the globals
452function isWorkerEnv() {
453 return typeof self !== 'undefined' && typeof DedicatedWorkerGlobalScope !== 'undefined' && self instanceof DedicatedWorkerGlobalScope;
454}
455if (isWorkerEnv()) {
456 self.filterXSS = module.exports;
457}
458
459},{"./default":1,"./parser":3,"./xss":5}],3:[function(require,module,exports){
460/**
461 * Simple HTML Parser
462 *
463 * @author Zongmin Lei<leizongmin@gmail.com>
464 */
465
466var _ = require("./util");
467
468/**
469 * get tag name
470 *
471 * @param {String} html e.g. '<a hef="#">'
472 * @return {String}
473 */
474function getTagName(html) {
475 var i = _.spaceIndex(html);
476 if (i === -1) {
477 var tagName = html.slice(1, -1);
478 } else {
479 var tagName = html.slice(1, i + 1);
480 }
481 tagName = _.trim(tagName).toLowerCase();
482 if (tagName.slice(0, 1) === "/") tagName = tagName.slice(1);
483 if (tagName.slice(-1) === "/") tagName = tagName.slice(0, -1);
484 return tagName;
485}
486
487/**
488 * is close tag?
489 *
490 * @param {String} html 如:'<a hef="#">'
491 * @return {Boolean}
492 */
493function isClosing(html) {
494 return html.slice(0, 2) === "</";
495}
496
497/**
498 * parse input html and returns processed html
499 *
500 * @param {String} html
501 * @param {Function} onTag e.g. function (sourcePosition, position, tag, html, isClosing)
502 * @param {Function} escapeHtml
503 * @return {String}
504 */
505function parseTag(html, onTag, escapeHtml) {
506 "user strict";
507
508 var rethtml = "";
509 var lastPos = 0;
510 var tagStart = false;
511 var quoteStart = false;
512 var currentPos = 0;
513 var len = html.length;
514 var currentTagName = "";
515 var currentHtml = "";
516
517 for (currentPos = 0; currentPos < len; currentPos++) {
518 var c = html.charAt(currentPos);
519 if (tagStart === false) {
520 if (c === "<") {
521 tagStart = currentPos;
522 continue;
523 }
524 } else {
525 if (quoteStart === false) {
526 if (c === "<") {
527 rethtml += escapeHtml(html.slice(lastPos, currentPos));
528 tagStart = currentPos;
529 lastPos = currentPos;
530 continue;
531 }
532 if (c === ">") {
533 rethtml += escapeHtml(html.slice(lastPos, tagStart));
534 currentHtml = html.slice(tagStart, currentPos + 1);
535 currentTagName = getTagName(currentHtml);
536 rethtml += onTag(
537 tagStart,
538 rethtml.length,
539 currentTagName,
540 currentHtml,
541 isClosing(currentHtml)
542 );
543 lastPos = currentPos + 1;
544 tagStart = false;
545 continue;
546 }
547 if ((c === '"' || c === "'") && html.charAt(currentPos - 1) === "=") {
548 quoteStart = c;
549 continue;
550 }
551 } else {
552 if (c === quoteStart) {
553 quoteStart = false;
554 continue;
555 }
556 }
557 }
558 }
559 if (lastPos < html.length) {
560 rethtml += escapeHtml(html.substr(lastPos));
561 }
562
563 return rethtml;
564}
565
566var REGEXP_ILLEGAL_ATTR_NAME = /[^a-zA-Z0-9_:\.\-]/gim;
567
568/**
569 * parse input attributes and returns processed attributes
570 *
571 * @param {String} html e.g. `href="#" target="_blank"`
572 * @param {Function} onAttr e.g. `function (name, value)`
573 * @return {String}
574 */
575function parseAttr(html, onAttr) {
576 "user strict";
577
578 var lastPos = 0;
579 var retAttrs = [];
580 var tmpName = false;
581 var len = html.length;
582
583 function addAttr(name, value) {
584 name = _.trim(name);
585 name = name.replace(REGEXP_ILLEGAL_ATTR_NAME, "").toLowerCase();
586 if (name.length < 1) return;
587 var ret = onAttr(name, value || "");
588 if (ret) retAttrs.push(ret);
589 }
590
591 // 逐个分析字符
592 for (var i = 0; i < len; i++) {
593 var c = html.charAt(i);
594 var v, j;
595 if (tmpName === false && c === "=") {
596 tmpName = html.slice(lastPos, i);
597 lastPos = i + 1;
598 continue;
599 }
600 if (tmpName !== false) {
601 if (
602 i === lastPos &&
603 (c === '"' || c === "'") &&
604 html.charAt(i - 1) === "="
605 ) {
606 j = html.indexOf(c, i + 1);
607 if (j === -1) {
608 break;
609 } else {
610 v = _.trim(html.slice(lastPos + 1, j));
611 addAttr(tmpName, v);
612 tmpName = false;
613 i = j;
614 lastPos = i + 1;
615 continue;
616 }
617 }
618 }
619 if (/\s|\n|\t/.test(c)) {
620 html = html.replace(/\s|\n|\t/g, " ");
621 if (tmpName === false) {
622 j = findNextEqual(html, i);
623 if (j === -1) {
624 v = _.trim(html.slice(lastPos, i));
625 addAttr(v);
626 tmpName = false;
627 lastPos = i + 1;
628 continue;
629 } else {
630 i = j - 1;
631 continue;
632 }
633 } else {
634 j = findBeforeEqual(html, i - 1);
635 if (j === -1) {
636 v = _.trim(html.slice(lastPos, i));
637 v = stripQuoteWrap(v);
638 addAttr(tmpName, v);
639 tmpName = false;
640 lastPos = i + 1;
641 continue;
642 } else {
643 continue;
644 }
645 }
646 }
647 }
648
649 if (lastPos < html.length) {
650 if (tmpName === false) {
651 addAttr(html.slice(lastPos));
652 } else {
653 addAttr(tmpName, stripQuoteWrap(_.trim(html.slice(lastPos))));
654 }
655 }
656
657 return _.trim(retAttrs.join(" "));
658}
659
660function findNextEqual(str, i) {
661 for (; i < str.length; i++) {
662 var c = str[i];
663 if (c === " ") continue;
664 if (c === "=") return i;
665 return -1;
666 }
667}
668
669function findBeforeEqual(str, i) {
670 for (; i > 0; i--) {
671 var c = str[i];
672 if (c === " ") continue;
673 if (c === "=") return i;
674 return -1;
675 }
676}
677
678function isQuoteWrapString(text) {
679 if (
680 (text[0] === '"' && text[text.length - 1] === '"') ||
681 (text[0] === "'" && text[text.length - 1] === "'")
682 ) {
683 return true;
684 } else {
685 return false;
686 }
687}
688
689function stripQuoteWrap(text) {
690 if (isQuoteWrapString(text)) {
691 return text.substr(1, text.length - 2);
692 } else {
693 return text;
694 }
695}
696
697exports.parseTag = parseTag;
698exports.parseAttr = parseAttr;
699
700},{"./util":4}],4:[function(require,module,exports){
701module.exports = {
702 indexOf: function(arr, item) {
703 var i, j;
704 if (Array.prototype.indexOf) {
705 return arr.indexOf(item);
706 }
707 for (i = 0, j = arr.length; i < j; i++) {
708 if (arr[i] === item) {
709 return i;
710 }
711 }
712 return -1;
713 },
714 forEach: function(arr, fn, scope) {
715 var i, j;
716 if (Array.prototype.forEach) {
717 return arr.forEach(fn, scope);
718 }
719 for (i = 0, j = arr.length; i < j; i++) {
720 fn.call(scope, arr[i], i, arr);
721 }
722 },
723 trim: function(str) {
724 if (String.prototype.trim) {
725 return str.trim();
726 }
727 return str.replace(/(^\s*)|(\s*$)/g, "");
728 },
729 spaceIndex: function(str) {
730 var reg = /\s|\n|\t/;
731 var match = reg.exec(str);
732 return match ? match.index : -1;
733 }
734};
735
736},{}],5:[function(require,module,exports){
737/**
738 * filter xss
739 *
740 * @author Zongmin Lei<leizongmin@gmail.com>
741 */
742
743var FilterCSS = require("cssfilter").FilterCSS;
744var DEFAULT = require("./default");
745var parser = require("./parser");
746var parseTag = parser.parseTag;
747var parseAttr = parser.parseAttr;
748var _ = require("./util");
749
750/**
751 * returns `true` if the input value is `undefined` or `null`
752 *
753 * @param {Object} obj
754 * @return {Boolean}
755 */
756function isNull(obj) {
757 return obj === undefined || obj === null;
758}
759
760/**
761 * get attributes for a tag
762 *
763 * @param {String} html
764 * @return {Object}
765 * - {String} html
766 * - {Boolean} closing
767 */
768function getAttrs(html) {
769 var i = _.spaceIndex(html);
770 if (i === -1) {
771 return {
772 html: "",
773 closing: html[html.length - 2] === "/"
774 };
775 }
776 html = _.trim(html.slice(i + 1, -1));
777 var isClosing = html[html.length - 1] === "/";
778 if (isClosing) html = _.trim(html.slice(0, -1));
779 return {
780 html: html,
781 closing: isClosing
782 };
783}
784
785/**
786 * shallow copy
787 *
788 * @param {Object} obj
789 * @return {Object}
790 */
791function shallowCopyObject(obj) {
792 var ret = {};
793 for (var i in obj) {
794 ret[i] = obj[i];
795 }
796 return ret;
797}
798
799/**
800 * FilterXSS class
801 *
802 * @param {Object} options
803 * whiteList, onTag, onTagAttr, onIgnoreTag,
804 * onIgnoreTagAttr, safeAttrValue, escapeHtml
805 * stripIgnoreTagBody, allowCommentTag, stripBlankChar
806 * css{whiteList, onAttr, onIgnoreAttr} `css=false` means don't use `cssfilter`
807 */
808function FilterXSS(options) {
809 options = shallowCopyObject(options || {});
810
811 if (options.stripIgnoreTag) {
812 if (options.onIgnoreTag) {
813 console.error(
814 'Notes: cannot use these two options "stripIgnoreTag" and "onIgnoreTag" at the same time'
815 );
816 }
817 options.onIgnoreTag = DEFAULT.onIgnoreTagStripAll;
818 }
819
820 options.whiteList = options.whiteList || DEFAULT.whiteList;
821 options.onTag = options.onTag || DEFAULT.onTag;
822 options.onTagAttr = options.onTagAttr || DEFAULT.onTagAttr;
823 options.onIgnoreTag = options.onIgnoreTag || DEFAULT.onIgnoreTag;
824 options.onIgnoreTagAttr = options.onIgnoreTagAttr || DEFAULT.onIgnoreTagAttr;
825 options.safeAttrValue = options.safeAttrValue || DEFAULT.safeAttrValue;
826 options.escapeHtml = options.escapeHtml || DEFAULT.escapeHtml;
827 this.options = options;
828
829 if (options.css === false) {
830 this.cssFilter = false;
831 } else {
832 options.css = options.css || {};
833 this.cssFilter = new FilterCSS(options.css);
834 }
835}
836
837/**
838 * start process and returns result
839 *
840 * @param {String} html
841 * @return {String}
842 */
843FilterXSS.prototype.process = function(html) {
844 // compatible with the input
845 html = html || "";
846 html = html.toString();
847 if (!html) return "";
848
849 var me = this;
850 var options = me.options;
851 var whiteList = options.whiteList;
852 var onTag = options.onTag;
853 var onIgnoreTag = options.onIgnoreTag;
854 var onTagAttr = options.onTagAttr;
855 var onIgnoreTagAttr = options.onIgnoreTagAttr;
856 var safeAttrValue = options.safeAttrValue;
857 var escapeHtml = options.escapeHtml;
858 var cssFilter = me.cssFilter;
859
860 // remove invisible characters
861 if (options.stripBlankChar) {
862 html = DEFAULT.stripBlankChar(html);
863 }
864
865 // remove html comments
866 if (!options.allowCommentTag) {
867 html = DEFAULT.stripCommentTag(html);
868 }
869
870 // if enable stripIgnoreTagBody
871 var stripIgnoreTagBody = false;
872 if (options.stripIgnoreTagBody) {
873 var stripIgnoreTagBody = DEFAULT.StripTagBody(
874 options.stripIgnoreTagBody,
875 onIgnoreTag
876 );
877 onIgnoreTag = stripIgnoreTagBody.onIgnoreTag;
878 }
879
880 var retHtml = parseTag(
881 html,
882 function(sourcePosition, position, tag, html, isClosing) {
883 var info = {
884 sourcePosition: sourcePosition,
885 position: position,
886 isClosing: isClosing,
887 isWhite: whiteList.hasOwnProperty(tag)
888 };
889
890 // call `onTag()`
891 var ret = onTag(tag, html, info);
892 if (!isNull(ret)) return ret;
893
894 if (info.isWhite) {
895 if (info.isClosing) {
896 return "</" + tag + ">";
897 }
898
899 var attrs = getAttrs(html);
900 var whiteAttrList = whiteList[tag];
901 var attrsHtml = parseAttr(attrs.html, function(name, value) {
902 // call `onTagAttr()`
903 var isWhiteAttr = _.indexOf(whiteAttrList, name) !== -1;
904 var ret = onTagAttr(tag, name, value, isWhiteAttr);
905 if (!isNull(ret)) return ret;
906
907 if (isWhiteAttr) {
908 // call `safeAttrValue()`
909 value = safeAttrValue(tag, name, value, cssFilter);
910 if (value) {
911 return name + '="' + value + '"';
912 } else {
913 return name;
914 }
915 } else {
916 // call `onIgnoreTagAttr()`
917 var ret = onIgnoreTagAttr(tag, name, value, isWhiteAttr);
918 if (!isNull(ret)) return ret;
919 return;
920 }
921 });
922
923 // build new tag html
924 var html = "<" + tag;
925 if (attrsHtml) html += " " + attrsHtml;
926 if (attrs.closing) html += " /";
927 html += ">";
928 return html;
929 } else {
930 // call `onIgnoreTag()`
931 var ret = onIgnoreTag(tag, html, info);
932 if (!isNull(ret)) return ret;
933 return escapeHtml(html);
934 }
935 },
936 escapeHtml
937 );
938
939 // if enable stripIgnoreTagBody
940 if (stripIgnoreTagBody) {
941 retHtml = stripIgnoreTagBody.remove(retHtml);
942 }
943
944 return retHtml;
945};
946
947module.exports = FilterXSS;
948
949},{"./default":1,"./parser":3,"./util":4,"cssfilter":8}],6:[function(require,module,exports){
950/**
951 * cssfilter
952 *
953 * @author 老雷<leizongmin@gmail.com>
954 */
955
956var DEFAULT = require('./default');
957var parseStyle = require('./parser');
958var _ = require('./util');
959
960
961/**
962 * 返回值是否为空
963 *
964 * @param {Object} obj
965 * @return {Boolean}
966 */
967function isNull (obj) {
968 return (obj === undefined || obj === null);
969}
970
971/**
972 * 浅拷贝对象
973 *
974 * @param {Object} obj
975 * @return {Object}
976 */
977function shallowCopyObject (obj) {
978 var ret = {};
979 for (var i in obj) {
980 ret[i] = obj[i];
981 }
982 return ret;
983}
984
985/**
986 * 创建CSS过滤器
987 *
988 * @param {Object} options
989 * - {Object} whiteList
990 * - {Function} onAttr
991 * - {Function} onIgnoreAttr
992 * - {Function} safeAttrValue
993 */
994function FilterCSS (options) {
995 options = shallowCopyObject(options || {});
996 options.whiteList = options.whiteList || DEFAULT.whiteList;
997 options.onAttr = options.onAttr || DEFAULT.onAttr;
998 options.onIgnoreAttr = options.onIgnoreAttr || DEFAULT.onIgnoreAttr;
999 options.safeAttrValue = options.safeAttrValue || DEFAULT.safeAttrValue;
1000 this.options = options;
1001}
1002
1003FilterCSS.prototype.process = function (css) {
1004 // 兼容各种奇葩输入
1005 css = css || '';
1006 css = css.toString();
1007 if (!css) return '';
1008
1009 var me = this;
1010 var options = me.options;
1011 var whiteList = options.whiteList;
1012 var onAttr = options.onAttr;
1013 var onIgnoreAttr = options.onIgnoreAttr;
1014 var safeAttrValue = options.safeAttrValue;
1015
1016 var retCSS = parseStyle(css, function (sourcePosition, position, name, value, source) {
1017
1018 var check = whiteList[name];
1019 var isWhite = false;
1020 if (check === true) isWhite = check;
1021 else if (typeof check === 'function') isWhite = check(value);
1022 else if (check instanceof RegExp) isWhite = check.test(value);
1023 if (isWhite !== true) isWhite = false;
1024
1025 // 如果过滤后 value 为空则直接忽略
1026 value = safeAttrValue(name, value);
1027 if (!value) return;
1028
1029 var opts = {
1030 position: position,
1031 sourcePosition: sourcePosition,
1032 source: source,
1033 isWhite: isWhite
1034 };
1035
1036 if (isWhite) {
1037
1038 var ret = onAttr(name, value, opts);
1039 if (isNull(ret)) {
1040 return name + ':' + value;
1041 } else {
1042 return ret;
1043 }
1044
1045 } else {
1046
1047 var ret = onIgnoreAttr(name, value, opts);
1048 if (!isNull(ret)) {
1049 return ret;
1050 }
1051
1052 }
1053 });
1054
1055 return retCSS;
1056};
1057
1058
1059module.exports = FilterCSS;
1060
1061},{"./default":7,"./parser":9,"./util":10}],7:[function(require,module,exports){
1062/**
1063 * cssfilter
1064 *
1065 * @author 老雷<leizongmin@gmail.com>
1066 */
1067
1068function getDefaultWhiteList () {
1069 // 白名单值说明:
1070 // true: 允许该属性
1071 // Function: function (val) { } 返回true表示允许该属性,其他值均表示不允许
1072 // RegExp: regexp.test(val) 返回true表示允许该属性,其他值均表示不允许
1073 // 除上面列出的值外均表示不允许
1074 var whiteList = {};
1075
1076 whiteList['align-content'] = false; // default: auto
1077 whiteList['align-items'] = false; // default: auto
1078 whiteList['align-self'] = false; // default: auto
1079 whiteList['alignment-adjust'] = false; // default: auto
1080 whiteList['alignment-baseline'] = false; // default: baseline
1081 whiteList['all'] = false; // default: depending on individual properties
1082 whiteList['anchor-point'] = false; // default: none
1083 whiteList['animation'] = false; // default: depending on individual properties
1084 whiteList['animation-delay'] = false; // default: 0
1085 whiteList['animation-direction'] = false; // default: normal
1086 whiteList['animation-duration'] = false; // default: 0
1087 whiteList['animation-fill-mode'] = false; // default: none
1088 whiteList['animation-iteration-count'] = false; // default: 1
1089 whiteList['animation-name'] = false; // default: none
1090 whiteList['animation-play-state'] = false; // default: running
1091 whiteList['animation-timing-function'] = false; // default: ease
1092 whiteList['azimuth'] = false; // default: center
1093 whiteList['backface-visibility'] = false; // default: visible
1094 whiteList['background'] = true; // default: depending on individual properties
1095 whiteList['background-attachment'] = true; // default: scroll
1096 whiteList['background-clip'] = true; // default: border-box
1097 whiteList['background-color'] = true; // default: transparent
1098 whiteList['background-image'] = true; // default: none
1099 whiteList['background-origin'] = true; // default: padding-box
1100 whiteList['background-position'] = true; // default: 0% 0%
1101 whiteList['background-repeat'] = true; // default: repeat
1102 whiteList['background-size'] = true; // default: auto
1103 whiteList['baseline-shift'] = false; // default: baseline
1104 whiteList['binding'] = false; // default: none
1105 whiteList['bleed'] = false; // default: 6pt
1106 whiteList['bookmark-label'] = false; // default: content()
1107 whiteList['bookmark-level'] = false; // default: none
1108 whiteList['bookmark-state'] = false; // default: open
1109 whiteList['border'] = true; // default: depending on individual properties
1110 whiteList['border-bottom'] = true; // default: depending on individual properties
1111 whiteList['border-bottom-color'] = true; // default: current color
1112 whiteList['border-bottom-left-radius'] = true; // default: 0
1113 whiteList['border-bottom-right-radius'] = true; // default: 0
1114 whiteList['border-bottom-style'] = true; // default: none
1115 whiteList['border-bottom-width'] = true; // default: medium
1116 whiteList['border-collapse'] = true; // default: separate
1117 whiteList['border-color'] = true; // default: depending on individual properties
1118 whiteList['border-image'] = true; // default: none
1119 whiteList['border-image-outset'] = true; // default: 0
1120 whiteList['border-image-repeat'] = true; // default: stretch
1121 whiteList['border-image-slice'] = true; // default: 100%
1122 whiteList['border-image-source'] = true; // default: none
1123 whiteList['border-image-width'] = true; // default: 1
1124 whiteList['border-left'] = true; // default: depending on individual properties
1125 whiteList['border-left-color'] = true; // default: current color
1126 whiteList['border-left-style'] = true; // default: none
1127 whiteList['border-left-width'] = true; // default: medium
1128 whiteList['border-radius'] = true; // default: 0
1129 whiteList['border-right'] = true; // default: depending on individual properties
1130 whiteList['border-right-color'] = true; // default: current color
1131 whiteList['border-right-style'] = true; // default: none
1132 whiteList['border-right-width'] = true; // default: medium
1133 whiteList['border-spacing'] = true; // default: 0
1134 whiteList['border-style'] = true; // default: depending on individual properties
1135 whiteList['border-top'] = true; // default: depending on individual properties
1136 whiteList['border-top-color'] = true; // default: current color
1137 whiteList['border-top-left-radius'] = true; // default: 0
1138 whiteList['border-top-right-radius'] = true; // default: 0
1139 whiteList['border-top-style'] = true; // default: none
1140 whiteList['border-top-width'] = true; // default: medium
1141 whiteList['border-width'] = true; // default: depending on individual properties
1142 whiteList['bottom'] = false; // default: auto
1143 whiteList['box-decoration-break'] = true; // default: slice
1144 whiteList['box-shadow'] = true; // default: none
1145 whiteList['box-sizing'] = true; // default: content-box
1146 whiteList['box-snap'] = true; // default: none
1147 whiteList['box-suppress'] = true; // default: show
1148 whiteList['break-after'] = true; // default: auto
1149 whiteList['break-before'] = true; // default: auto
1150 whiteList['break-inside'] = true; // default: auto
1151 whiteList['caption-side'] = false; // default: top
1152 whiteList['chains'] = false; // default: none
1153 whiteList['clear'] = true; // default: none
1154 whiteList['clip'] = false; // default: auto
1155 whiteList['clip-path'] = false; // default: none
1156 whiteList['clip-rule'] = false; // default: nonzero
1157 whiteList['color'] = true; // default: implementation dependent
1158 whiteList['color-interpolation-filters'] = true; // default: auto
1159 whiteList['column-count'] = false; // default: auto
1160 whiteList['column-fill'] = false; // default: balance
1161 whiteList['column-gap'] = false; // default: normal
1162 whiteList['column-rule'] = false; // default: depending on individual properties
1163 whiteList['column-rule-color'] = false; // default: current color
1164 whiteList['column-rule-style'] = false; // default: medium
1165 whiteList['column-rule-width'] = false; // default: medium
1166 whiteList['column-span'] = false; // default: none
1167 whiteList['column-width'] = false; // default: auto
1168 whiteList['columns'] = false; // default: depending on individual properties
1169 whiteList['contain'] = false; // default: none
1170 whiteList['content'] = false; // default: normal
1171 whiteList['counter-increment'] = false; // default: none
1172 whiteList['counter-reset'] = false; // default: none
1173 whiteList['counter-set'] = false; // default: none
1174 whiteList['crop'] = false; // default: auto
1175 whiteList['cue'] = false; // default: depending on individual properties
1176 whiteList['cue-after'] = false; // default: none
1177 whiteList['cue-before'] = false; // default: none
1178 whiteList['cursor'] = false; // default: auto
1179 whiteList['direction'] = false; // default: ltr
1180 whiteList['display'] = true; // default: depending on individual properties
1181 whiteList['display-inside'] = true; // default: auto
1182 whiteList['display-list'] = true; // default: none
1183 whiteList['display-outside'] = true; // default: inline-level
1184 whiteList['dominant-baseline'] = false; // default: auto
1185 whiteList['elevation'] = false; // default: level
1186 whiteList['empty-cells'] = false; // default: show
1187 whiteList['filter'] = false; // default: none
1188 whiteList['flex'] = false; // default: depending on individual properties
1189 whiteList['flex-basis'] = false; // default: auto
1190 whiteList['flex-direction'] = false; // default: row
1191 whiteList['flex-flow'] = false; // default: depending on individual properties
1192 whiteList['flex-grow'] = false; // default: 0
1193 whiteList['flex-shrink'] = false; // default: 1
1194 whiteList['flex-wrap'] = false; // default: nowrap
1195 whiteList['float'] = false; // default: none
1196 whiteList['float-offset'] = false; // default: 0 0
1197 whiteList['flood-color'] = false; // default: black
1198 whiteList['flood-opacity'] = false; // default: 1
1199 whiteList['flow-from'] = false; // default: none
1200 whiteList['flow-into'] = false; // default: none
1201 whiteList['font'] = true; // default: depending on individual properties
1202 whiteList['font-family'] = true; // default: implementation dependent
1203 whiteList['font-feature-settings'] = true; // default: normal
1204 whiteList['font-kerning'] = true; // default: auto
1205 whiteList['font-language-override'] = true; // default: normal
1206 whiteList['font-size'] = true; // default: medium
1207 whiteList['font-size-adjust'] = true; // default: none
1208 whiteList['font-stretch'] = true; // default: normal
1209 whiteList['font-style'] = true; // default: normal
1210 whiteList['font-synthesis'] = true; // default: weight style
1211 whiteList['font-variant'] = true; // default: normal
1212 whiteList['font-variant-alternates'] = true; // default: normal
1213 whiteList['font-variant-caps'] = true; // default: normal
1214 whiteList['font-variant-east-asian'] = true; // default: normal
1215 whiteList['font-variant-ligatures'] = true; // default: normal
1216 whiteList['font-variant-numeric'] = true; // default: normal
1217 whiteList['font-variant-position'] = true; // default: normal
1218 whiteList['font-weight'] = true; // default: normal
1219 whiteList['grid'] = false; // default: depending on individual properties
1220 whiteList['grid-area'] = false; // default: depending on individual properties
1221 whiteList['grid-auto-columns'] = false; // default: auto
1222 whiteList['grid-auto-flow'] = false; // default: none
1223 whiteList['grid-auto-rows'] = false; // default: auto
1224 whiteList['grid-column'] = false; // default: depending on individual properties
1225 whiteList['grid-column-end'] = false; // default: auto
1226 whiteList['grid-column-start'] = false; // default: auto
1227 whiteList['grid-row'] = false; // default: depending on individual properties
1228 whiteList['grid-row-end'] = false; // default: auto
1229 whiteList['grid-row-start'] = false; // default: auto
1230 whiteList['grid-template'] = false; // default: depending on individual properties
1231 whiteList['grid-template-areas'] = false; // default: none
1232 whiteList['grid-template-columns'] = false; // default: none
1233 whiteList['grid-template-rows'] = false; // default: none
1234 whiteList['hanging-punctuation'] = false; // default: none
1235 whiteList['height'] = true; // default: auto
1236 whiteList['hyphens'] = false; // default: manual
1237 whiteList['icon'] = false; // default: auto
1238 whiteList['image-orientation'] = false; // default: auto
1239 whiteList['image-resolution'] = false; // default: normal
1240 whiteList['ime-mode'] = false; // default: auto
1241 whiteList['initial-letters'] = false; // default: normal
1242 whiteList['inline-box-align'] = false; // default: last
1243 whiteList['justify-content'] = false; // default: auto
1244 whiteList['justify-items'] = false; // default: auto
1245 whiteList['justify-self'] = false; // default: auto
1246 whiteList['left'] = false; // default: auto
1247 whiteList['letter-spacing'] = true; // default: normal
1248 whiteList['lighting-color'] = true; // default: white
1249 whiteList['line-box-contain'] = false; // default: block inline replaced
1250 whiteList['line-break'] = false; // default: auto
1251 whiteList['line-grid'] = false; // default: match-parent
1252 whiteList['line-height'] = false; // default: normal
1253 whiteList['line-snap'] = false; // default: none
1254 whiteList['line-stacking'] = false; // default: depending on individual properties
1255 whiteList['line-stacking-ruby'] = false; // default: exclude-ruby
1256 whiteList['line-stacking-shift'] = false; // default: consider-shifts
1257 whiteList['line-stacking-strategy'] = false; // default: inline-line-height
1258 whiteList['list-style'] = true; // default: depending on individual properties
1259 whiteList['list-style-image'] = true; // default: none
1260 whiteList['list-style-position'] = true; // default: outside
1261 whiteList['list-style-type'] = true; // default: disc
1262 whiteList['margin'] = true; // default: depending on individual properties
1263 whiteList['margin-bottom'] = true; // default: 0
1264 whiteList['margin-left'] = true; // default: 0
1265 whiteList['margin-right'] = true; // default: 0
1266 whiteList['margin-top'] = true; // default: 0
1267 whiteList['marker-offset'] = false; // default: auto
1268 whiteList['marker-side'] = false; // default: list-item
1269 whiteList['marks'] = false; // default: none
1270 whiteList['mask'] = false; // default: border-box
1271 whiteList['mask-box'] = false; // default: see individual properties
1272 whiteList['mask-box-outset'] = false; // default: 0
1273 whiteList['mask-box-repeat'] = false; // default: stretch
1274 whiteList['mask-box-slice'] = false; // default: 0 fill
1275 whiteList['mask-box-source'] = false; // default: none
1276 whiteList['mask-box-width'] = false; // default: auto
1277 whiteList['mask-clip'] = false; // default: border-box
1278 whiteList['mask-image'] = false; // default: none
1279 whiteList['mask-origin'] = false; // default: border-box
1280 whiteList['mask-position'] = false; // default: center
1281 whiteList['mask-repeat'] = false; // default: no-repeat
1282 whiteList['mask-size'] = false; // default: border-box
1283 whiteList['mask-source-type'] = false; // default: auto
1284 whiteList['mask-type'] = false; // default: luminance
1285 whiteList['max-height'] = true; // default: none
1286 whiteList['max-lines'] = false; // default: none
1287 whiteList['max-width'] = true; // default: none
1288 whiteList['min-height'] = true; // default: 0
1289 whiteList['min-width'] = true; // default: 0
1290 whiteList['move-to'] = false; // default: normal
1291 whiteList['nav-down'] = false; // default: auto
1292 whiteList['nav-index'] = false; // default: auto
1293 whiteList['nav-left'] = false; // default: auto
1294 whiteList['nav-right'] = false; // default: auto
1295 whiteList['nav-up'] = false; // default: auto
1296 whiteList['object-fit'] = false; // default: fill
1297 whiteList['object-position'] = false; // default: 50% 50%
1298 whiteList['opacity'] = false; // default: 1
1299 whiteList['order'] = false; // default: 0
1300 whiteList['orphans'] = false; // default: 2
1301 whiteList['outline'] = false; // default: depending on individual properties
1302 whiteList['outline-color'] = false; // default: invert
1303 whiteList['outline-offset'] = false; // default: 0
1304 whiteList['outline-style'] = false; // default: none
1305 whiteList['outline-width'] = false; // default: medium
1306 whiteList['overflow'] = false; // default: depending on individual properties
1307 whiteList['overflow-wrap'] = false; // default: normal
1308 whiteList['overflow-x'] = false; // default: visible
1309 whiteList['overflow-y'] = false; // default: visible
1310 whiteList['padding'] = true; // default: depending on individual properties
1311 whiteList['padding-bottom'] = true; // default: 0
1312 whiteList['padding-left'] = true; // default: 0
1313 whiteList['padding-right'] = true; // default: 0
1314 whiteList['padding-top'] = true; // default: 0
1315 whiteList['page'] = false; // default: auto
1316 whiteList['page-break-after'] = false; // default: auto
1317 whiteList['page-break-before'] = false; // default: auto
1318 whiteList['page-break-inside'] = false; // default: auto
1319 whiteList['page-policy'] = false; // default: start
1320 whiteList['pause'] = false; // default: implementation dependent
1321 whiteList['pause-after'] = false; // default: implementation dependent
1322 whiteList['pause-before'] = false; // default: implementation dependent
1323 whiteList['perspective'] = false; // default: none
1324 whiteList['perspective-origin'] = false; // default: 50% 50%
1325 whiteList['pitch'] = false; // default: medium
1326 whiteList['pitch-range'] = false; // default: 50
1327 whiteList['play-during'] = false; // default: auto
1328 whiteList['position'] = false; // default: static
1329 whiteList['presentation-level'] = false; // default: 0
1330 whiteList['quotes'] = false; // default: text
1331 whiteList['region-fragment'] = false; // default: auto
1332 whiteList['resize'] = false; // default: none
1333 whiteList['rest'] = false; // default: depending on individual properties
1334 whiteList['rest-after'] = false; // default: none
1335 whiteList['rest-before'] = false; // default: none
1336 whiteList['richness'] = false; // default: 50
1337 whiteList['right'] = false; // default: auto
1338 whiteList['rotation'] = false; // default: 0
1339 whiteList['rotation-point'] = false; // default: 50% 50%
1340 whiteList['ruby-align'] = false; // default: auto
1341 whiteList['ruby-merge'] = false; // default: separate
1342 whiteList['ruby-position'] = false; // default: before
1343 whiteList['shape-image-threshold'] = false; // default: 0.0
1344 whiteList['shape-outside'] = false; // default: none
1345 whiteList['shape-margin'] = false; // default: 0
1346 whiteList['size'] = false; // default: auto
1347 whiteList['speak'] = false; // default: auto
1348 whiteList['speak-as'] = false; // default: normal
1349 whiteList['speak-header'] = false; // default: once
1350 whiteList['speak-numeral'] = false; // default: continuous
1351 whiteList['speak-punctuation'] = false; // default: none
1352 whiteList['speech-rate'] = false; // default: medium
1353 whiteList['stress'] = false; // default: 50
1354 whiteList['string-set'] = false; // default: none
1355 whiteList['tab-size'] = false; // default: 8
1356 whiteList['table-layout'] = false; // default: auto
1357 whiteList['text-align'] = true; // default: start
1358 whiteList['text-align-last'] = true; // default: auto
1359 whiteList['text-combine-upright'] = true; // default: none
1360 whiteList['text-decoration'] = true; // default: none
1361 whiteList['text-decoration-color'] = true; // default: currentColor
1362 whiteList['text-decoration-line'] = true; // default: none
1363 whiteList['text-decoration-skip'] = true; // default: objects
1364 whiteList['text-decoration-style'] = true; // default: solid
1365 whiteList['text-emphasis'] = true; // default: depending on individual properties
1366 whiteList['text-emphasis-color'] = true; // default: currentColor
1367 whiteList['text-emphasis-position'] = true; // default: over right
1368 whiteList['text-emphasis-style'] = true; // default: none
1369 whiteList['text-height'] = true; // default: auto
1370 whiteList['text-indent'] = true; // default: 0
1371 whiteList['text-justify'] = true; // default: auto
1372 whiteList['text-orientation'] = true; // default: mixed
1373 whiteList['text-overflow'] = true; // default: clip
1374 whiteList['text-shadow'] = true; // default: none
1375 whiteList['text-space-collapse'] = true; // default: collapse
1376 whiteList['text-transform'] = true; // default: none
1377 whiteList['text-underline-position'] = true; // default: auto
1378 whiteList['text-wrap'] = true; // default: normal
1379 whiteList['top'] = false; // default: auto
1380 whiteList['transform'] = false; // default: none
1381 whiteList['transform-origin'] = false; // default: 50% 50% 0
1382 whiteList['transform-style'] = false; // default: flat
1383 whiteList['transition'] = false; // default: depending on individual properties
1384 whiteList['transition-delay'] = false; // default: 0s
1385 whiteList['transition-duration'] = false; // default: 0s
1386 whiteList['transition-property'] = false; // default: all
1387 whiteList['transition-timing-function'] = false; // default: ease
1388 whiteList['unicode-bidi'] = false; // default: normal
1389 whiteList['vertical-align'] = false; // default: baseline
1390 whiteList['visibility'] = false; // default: visible
1391 whiteList['voice-balance'] = false; // default: center
1392 whiteList['voice-duration'] = false; // default: auto
1393 whiteList['voice-family'] = false; // default: implementation dependent
1394 whiteList['voice-pitch'] = false; // default: medium
1395 whiteList['voice-range'] = false; // default: medium
1396 whiteList['voice-rate'] = false; // default: normal
1397 whiteList['voice-stress'] = false; // default: normal
1398 whiteList['voice-volume'] = false; // default: medium
1399 whiteList['volume'] = false; // default: medium
1400 whiteList['white-space'] = false; // default: normal
1401 whiteList['widows'] = false; // default: 2
1402 whiteList['width'] = true; // default: auto
1403 whiteList['will-change'] = false; // default: auto
1404 whiteList['word-break'] = true; // default: normal
1405 whiteList['word-spacing'] = true; // default: normal
1406 whiteList['word-wrap'] = true; // default: normal
1407 whiteList['wrap-flow'] = false; // default: auto
1408 whiteList['wrap-through'] = false; // default: wrap
1409 whiteList['writing-mode'] = false; // default: horizontal-tb
1410 whiteList['z-index'] = false; // default: auto
1411
1412 return whiteList;
1413}
1414
1415
1416/**
1417 * 匹配到白名单上的一个属性时
1418 *
1419 * @param {String} name
1420 * @param {String} value
1421 * @param {Object} options
1422 * @return {String}
1423 */
1424function onAttr (name, value, options) {
1425 // do nothing
1426}
1427
1428/**
1429 * 匹配到不在白名单上的一个属性时
1430 *
1431 * @param {String} name
1432 * @param {String} value
1433 * @param {Object} options
1434 * @return {String}
1435 */
1436function onIgnoreAttr (name, value, options) {
1437 // do nothing
1438}
1439
1440var REGEXP_URL_JAVASCRIPT = /javascript\s*\:/img;
1441
1442/**
1443 * 过滤属性值
1444 *
1445 * @param {String} name
1446 * @param {String} value
1447 * @return {String}
1448 */
1449function safeAttrValue(name, value) {
1450 if (REGEXP_URL_JAVASCRIPT.test(value)) return '';
1451 return value;
1452}
1453
1454
1455exports.whiteList = getDefaultWhiteList();
1456exports.getDefaultWhiteList = getDefaultWhiteList;
1457exports.onAttr = onAttr;
1458exports.onIgnoreAttr = onIgnoreAttr;
1459exports.safeAttrValue = safeAttrValue;
1460
1461},{}],8:[function(require,module,exports){
1462/**
1463 * cssfilter
1464 *
1465 * @author 老雷<leizongmin@gmail.com>
1466 */
1467
1468var DEFAULT = require('./default');
1469var FilterCSS = require('./css');
1470
1471
1472/**
1473 * XSS过滤
1474 *
1475 * @param {String} css 要过滤的CSS代码
1476 * @param {Object} options 选项:whiteList, onAttr, onIgnoreAttr
1477 * @return {String}
1478 */
1479function filterCSS (html, options) {
1480 var xss = new FilterCSS(options);
1481 return xss.process(html);
1482}
1483
1484
1485// 输出
1486exports = module.exports = filterCSS;
1487exports.FilterCSS = FilterCSS;
1488for (var i in DEFAULT) exports[i] = DEFAULT[i];
1489
1490// 在浏览器端使用
1491if (typeof window !== 'undefined') {
1492 window.filterCSS = module.exports;
1493}
1494
1495},{"./css":6,"./default":7}],9:[function(require,module,exports){
1496/**
1497 * cssfilter
1498 *
1499 * @author 老雷<leizongmin@gmail.com>
1500 */
1501
1502var _ = require('./util');
1503
1504
1505/**
1506 * 解析style
1507 *
1508 * @param {String} css
1509 * @param {Function} onAttr 处理属性的函数
1510 * 参数格式: function (sourcePosition, position, name, value, source)
1511 * @return {String}
1512 */
1513function parseStyle (css, onAttr) {
1514 css = _.trimRight(css);
1515 if (css[css.length - 1] !== ';') css += ';';
1516 var cssLength = css.length;
1517 var isParenthesisOpen = false;
1518 var lastPos = 0;
1519 var i = 0;
1520 var retCSS = '';
1521
1522 function addNewAttr () {
1523 // 如果没有正常的闭合圆括号,则直接忽略当前属性
1524 if (!isParenthesisOpen) {
1525 var source = _.trim(css.slice(lastPos, i));
1526 var j = source.indexOf(':');
1527 if (j !== -1) {
1528 var name = _.trim(source.slice(0, j));
1529 var value = _.trim(source.slice(j + 1));
1530 // 必须有属性名称
1531 if (name) {
1532 var ret = onAttr(lastPos, retCSS.length, name, value, source);
1533 if (ret) retCSS += ret + '; ';
1534 }
1535 }
1536 }
1537 lastPos = i + 1;
1538 }
1539
1540 for (; i < cssLength; i++) {
1541 var c = css[i];
1542 if (c === '/' && css[i + 1] === '*') {
1543 // 备注开始
1544 var j = css.indexOf('*/', i + 2);
1545 // 如果没有正常的备注结束,则后面的部分全部跳过
1546 if (j === -1) break;
1547 // 直接将当前位置调到备注结尾,并且初始化状态
1548 i = j + 1;
1549 lastPos = i + 1;
1550 isParenthesisOpen = false;
1551 } else if (c === '(') {
1552 isParenthesisOpen = true;
1553 } else if (c === ')') {
1554 isParenthesisOpen = false;
1555 } else if (c === ';') {
1556 if (isParenthesisOpen) {
1557 // 在圆括号里面,忽略
1558 } else {
1559 addNewAttr();
1560 }
1561 } else if (c === '\n') {
1562 addNewAttr();
1563 }
1564 }
1565
1566 return _.trim(retCSS);
1567}
1568
1569module.exports = parseStyle;
1570
1571},{"./util":10}],10:[function(require,module,exports){
1572module.exports = {
1573 indexOf: function (arr, item) {
1574 var i, j;
1575 if (Array.prototype.indexOf) {
1576 return arr.indexOf(item);
1577 }
1578 for (i = 0, j = arr.length; i < j; i++) {
1579 if (arr[i] === item) {
1580 return i;
1581 }
1582 }
1583 return -1;
1584 },
1585 forEach: function (arr, fn, scope) {
1586 var i, j;
1587 if (Array.prototype.forEach) {
1588 return arr.forEach(fn, scope);
1589 }
1590 for (i = 0, j = arr.length; i < j; i++) {
1591 fn.call(scope, arr[i], i, arr);
1592 }
1593 },
1594 trim: function (str) {
1595 if (String.prototype.trim) {
1596 return str.trim();
1597 }
1598 return str.replace(/(^\s*)|(\s*$)/g, '');
1599 },
1600 trimRight: function (str) {
1601 if (String.prototype.trimRight) {
1602 return str.trimRight();
1603 }
1604 return str.replace(/(\s*$)/g, '');
1605 }
1606};
1607
1608},{}]},{},[2]);