|
16 | 16 | *
|
17 | 17 | */
|
18 | 18 |
|
| 19 | + |
| 20 | + |
| 21 | +/** |
| 22 | + * @ngdoc service |
| 23 | + * @name angular.module.ng.$sanitize |
| 24 | + * @function |
| 25 | + * |
| 26 | + * @description |
| 27 | + * The input is sanitized by parsing the html into tokens. All safe tokens (from a whitelist) are |
| 28 | + * then serialized back to properly escaped html string. This means that no unsafe input can make |
| 29 | + * it into the returned string, however, since our parser is more strict than a typical browser |
| 30 | + * parser, it's possible that some obscure input, which would be recognized as valid HTML by a |
| 31 | + * browser, won't make it through the sanitizer. |
| 32 | + * |
| 33 | + * @param {string} html Html input. |
| 34 | + * @returns {string} Sanitized html. |
| 35 | + * |
| 36 | + * @example |
| 37 | + <doc:example> |
| 38 | + <doc:source> |
| 39 | + <script> |
| 40 | + function Ctrl() { |
| 41 | + this.snippet = |
| 42 | + '<p style="color:blue">an html\n' + |
| 43 | + '<em onmouseover="this.textContent=\'PWN3D!\'">click here</em>\n' + |
| 44 | + 'snippet</p>'; |
| 45 | + } |
| 46 | + </script> |
| 47 | + <div ng:controller="Ctrl"> |
| 48 | + Snippet: <textarea ng:model="snippet" cols="60" rows="3"></textarea> |
| 49 | + <table> |
| 50 | + <tr> |
| 51 | + <td>Filter</td> |
| 52 | + <td>Source</td> |
| 53 | + <td>Rendered</td> |
| 54 | + </tr> |
| 55 | + <tr id="html-filter"> |
| 56 | + <td>html filter</td> |
| 57 | + <td> |
| 58 | + <pre><div ng:bind-html="snippet"><br/></div></pre> |
| 59 | + </td> |
| 60 | + <td> |
| 61 | + <div ng:bind-html="snippet"></div> |
| 62 | + </td> |
| 63 | + </tr> |
| 64 | + <tr id="escaped-html"> |
| 65 | + <td>no filter</td> |
| 66 | + <td><pre><div ng:bind-="snippet"><br/></div></pre></td> |
| 67 | + <td><div ng:bind="snippet"></div></td> |
| 68 | + </tr> |
| 69 | + <tr id="html-unsafe-filter"> |
| 70 | + <td>unsafe html filter</td> |
| 71 | + <td><pre><div ng:bind-html-unsafe="snippet"><br/></div></pre></td> |
| 72 | + <td><div ng:bind-html-unsafe="snippet"></div></td> |
| 73 | + </tr> |
| 74 | + </table> |
| 75 | + </div> |
| 76 | + </doc:source> |
| 77 | + <doc:scenario> |
| 78 | + it('should sanitize the html snippet ', function() { |
| 79 | + expect(using('#html-filter').element('div').html()). |
| 80 | + toBe('<p>an html\n<em>click here</em>\nsnippet</p>'); |
| 81 | + }); |
| 82 | +
|
| 83 | + it('should escape snippet without any filter', function() { |
| 84 | + expect(using('#escaped-html').element('div').html()). |
| 85 | + toBe("<p style=\"color:blue\">an html\n" + |
| 86 | + "<em onmouseover=\"this.textContent='PWN3D!'\">click here</em>\n" + |
| 87 | + "snippet</p>"); |
| 88 | + }); |
| 89 | +
|
| 90 | + it('should inline raw snippet if filtered as unsafe', function() { |
| 91 | + expect(using('#html-unsafe-filter').element("div").html()). |
| 92 | + toBe("<p style=\"color:blue\">an html\n" + |
| 93 | + "<em onmouseover=\"this.textContent='PWN3D!'\">click here</em>\n" + |
| 94 | + "snippet</p>"); |
| 95 | + }); |
| 96 | +
|
| 97 | + it('should update', function() { |
| 98 | + input('snippet').enter('new <b>text</b>'); |
| 99 | + expect(using('#html-filter').binding('snippet')).toBe('new <b>text</b>'); |
| 100 | + expect(using('#escaped-html').element('div').html()).toBe("new <b>text</b>"); |
| 101 | + expect(using('#html-unsafe-filter').binding("snippet")).toBe('new <b>text</b>'); |
| 102 | + }); |
| 103 | + </doc:scenario> |
| 104 | + </doc:example> |
| 105 | + */ |
| 106 | + |
| 107 | +function $SanitizeProvider() { |
| 108 | + this.$get = valueFn(function(html) { |
| 109 | + var buf = []; |
| 110 | + htmlParser(html, htmlSanitizeWriter(buf)); |
| 111 | + return buf.join(''); |
| 112 | + }); |
| 113 | +}; |
| 114 | + |
19 | 115 | // Regular Expressions for parsing tags and attributes
|
20 | 116 | var START_TAG_REGEXP = /^<\s*([\w:-]+)((?:\s+[\w:-]+(?:\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*)\s*(\/?)\s*>/,
|
21 | 117 | END_TAG_REGEXP = /^<\s*\/\s*([\w:-]+)[^>]*>/,
|
|
0 commit comments