2 YUI 3.13.0 (build 508226d)
3 Copyright 2013 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
8 YUI.add('escape', function (Y, NAME) {
11 Provides utility methods for escaping strings.
30 // -- Public Static Methods ------------------------------------------------
33 Returns a copy of the specified string with special HTML characters
34 escaped. The following characters will be converted to their
35 corresponding character entities:
39 This implementation is based on the [OWASP HTML escaping
40 recommendations][1]. In addition to the characters in the OWASP
41 recommendations, we also escape the <code>`</code> character, since IE
42 interprets it as an attribute delimiter.
44 If _string_ is not already a string, it will be coerced to a string.
46 [1]: http://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet
49 @param {String} string String to escape.
50 @return {String} Escaped string.
53 html: function (string) {
54 return (string + '').replace(/[&<>"'\/`]/g, Escape._htmlReplacer);
58 Returns a copy of the specified string with special regular expression
59 characters escaped, allowing the string to be used safely inside a regex.
60 The following characters, and all whitespace characters, are escaped:
62 - $ ^ * ( ) + [ ] { } | \ , . ?
64 If _string_ is not already a string, it will be coerced to a string.
67 @param {String} string String to escape.
68 @return {String} Escaped string.
71 regex: function (string) {
72 // There's no need to escape !, =, and : since they only have meaning
73 // when they follow a parenthesized ?, as in (?:...), and we already
74 // escape parens and question marks.
75 return (string + '').replace(/[\-$\^*()+\[\]{}|\\,.?\s]/g, '\\$&');
78 // -- Protected Static Methods ---------------------------------------------
81 * Regex replacer for HTML escaping.
83 * @method _htmlReplacer
84 * @param {String} match Matched character (must exist in HTML_CHARS).
85 * @return {String} HTML entity.
89 _htmlReplacer: function (match) {
90 return HTML_CHARS[match];
94 Escape.regexp = Escape.regex;
99 }, '3.13.0', {"requires": ["yui-base"]});