]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/yui3/build/escape/escape.js
Release 6.5.0
[Github/sugarcrm.git] / jssource / src_files / include / javascript / yui3 / build / escape / escape.js
1 /*
2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
5 version: 3.3.0
6 build: 3167
7 */
8 YUI.add('escape', function(Y) {
9
10 /**
11  * Provides utility methods for escaping strings.
12  *
13  * @module escape
14  * @class Escape
15  * @static
16  * @since 3.3.0
17  */
18
19 var HTML_CHARS = {
20         '&': '&',
21         '<': '&lt;',
22         '>': '&gt;',
23         '"': '&quot;',
24         "'": '&#x27;',
25         '/': '&#x2F;',
26         '`': '&#x60;'
27     },
28
29 Escape = {
30     // -- Public Static Methods ------------------------------------------------
31
32     /**
33      * <p>
34      * Returns a copy of the specified string with special HTML characters
35      * escaped. The following characters will be converted to their
36      * corresponding character entities:
37      * <code>&amp; &lt; &gt; &quot; &#x27; &#x2F; &#x60;</code>
38      * </p>
39      *
40      * <p>
41      * This implementation is based on the
42      * <a href="http://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet">OWASP
43      * HTML escaping recommendations</a>. In addition to the characters
44      * in the OWASP recommendation, we also escape the <code>&#x60;</code>
45      * character, since IE interprets it as an attribute delimiter when used in
46      * innerHTML.
47      * </p>
48      *
49      * @method html
50      * @param {String} string String to escape.
51      * @return {String} Escaped string.
52      * @static
53      */
54     html: function (string) {
55         return string.replace(/[&<>"'\/`]/g, Escape._htmlReplacer);
56     },
57
58     /**
59      * Returns a copy of the specified string with special regular expression
60      * characters escaped, allowing the string to be used safely inside a regex.
61      * The following characters, and all whitespace characters, are escaped:
62      * <code>- # $ ^ * ( ) + [ ] { } | \ , . ?</code>
63      *
64      * @method regex
65      * @param {String} string String to escape.
66      * @return {String} Escaped string.
67      * @static
68      */
69     regex: function (string) {
70         return string.replace(/[\-#$\^*()+\[\]{}|\\,.?\s]/g, '\\$&');
71     },
72
73     // -- Protected Static Methods ---------------------------------------------
74
75     /**
76      * Regex replacer for HTML escaping.
77      *
78      * @method _htmlReplacer
79      * @param {String} match Matched character (must exist in HTML_CHARS).
80      * @returns {String} HTML entity.
81      * @static
82      * @protected
83      */
84     _htmlReplacer: function (match) {
85         return HTML_CHARS[match];
86     }
87 };
88
89 Escape.regexp = Escape.regex;
90
91 Y.Escape = Escape;
92
93
94 }, '3.3.0' );