2 * @license AngularJS v1.2.16
3 * (c) 2010-2014 Google, Inc. http://angularjs.org
6 (function(window, angular, undefined) {'use strict';
8 var $sanitizeMinErr = angular.$$minErr('$sanitize');
17 * The `ngSanitize` module provides functionality to sanitize HTML.
20 * <div doc-module-components="ngSanitize"></div>
22 * See {@link ngSanitize.$sanitize `$sanitize`} for usage.
26 * HTML Parser By Misko Hevery (misko@hevery.com)
27 * based on: HTML Parser By John Resig (ejohn.org)
28 * Original code by Erik Arvidsson, Mozilla Public License
29 * http://erik.eae.net/simplehtmlparser/simplehtmlparser.js
32 * htmlParser(htmlString, {
33 * start: function(tag, attrs, unary) {},
34 * end: function(tag) {},
35 * chars: function(text) {},
36 * comment: function(text) {}
48 * The input is sanitized by parsing the html into tokens. All safe tokens (from a whitelist) are
49 * then serialized back to properly escaped html string. This means that no unsafe input can make
50 * it into the returned string, however, since our parser is more strict than a typical browser
51 * parser, it's possible that some obscure input, which would be recognized as valid HTML by a
52 * browser, won't make it through the sanitizer.
53 * The whitelist is configured using the functions `aHrefSanitizationWhitelist` and
54 * `imgSrcSanitizationWhitelist` of {@link ng.$compileProvider `$compileProvider`}.
56 * @param {string} html Html input.
57 * @returns {string} Sanitized html.
60 <example module="ngSanitize" deps="angular-sanitize.js">
61 <file name="index.html">
63 function Ctrl($scope, $sce) {
65 '<p style="color:blue">an html\n' +
66 '<em onmouseover="this.textContent=\'PWN3D!\'">click here</em>\n' +
68 $scope.deliberatelyTrustDangerousSnippet = function() {
69 return $sce.trustAsHtml($scope.snippet);
73 <div ng-controller="Ctrl">
74 Snippet: <textarea ng-model="snippet" cols="60" rows="3"></textarea>
82 <tr id="bind-html-with-sanitize">
84 <td>Automatically uses $sanitize</td>
85 <td><pre><div ng-bind-html="snippet"><br/></div></pre></td>
86 <td><div ng-bind-html="snippet"></div></td>
88 <tr id="bind-html-with-trust">
90 <td>Bypass $sanitize by explicitly trusting the dangerous value</td>
92 <pre><div ng-bind-html="deliberatelyTrustDangerousSnippet()">
95 <td><div ng-bind-html="deliberatelyTrustDangerousSnippet()"></div></td>
97 <tr id="bind-default">
99 <td>Automatically escapes</td>
100 <td><pre><div ng-bind="snippet"><br/></div></pre></td>
101 <td><div ng-bind="snippet"></div></td>
106 <file name="protractor.js" type="protractor">
107 it('should sanitize the html snippet by default', function() {
108 expect(element(by.css('#bind-html-with-sanitize div')).getInnerHtml()).
109 toBe('<p>an html\n<em>click here</em>\nsnippet</p>');
112 it('should inline raw snippet if bound to a trusted value', function() {
113 expect(element(by.css('#bind-html-with-trust div')).getInnerHtml()).
114 toBe("<p style=\"color:blue\">an html\n" +
115 "<em onmouseover=\"this.textContent='PWN3D!'\">click here</em>\n" +
119 it('should escape snippet without any filter', function() {
120 expect(element(by.css('#bind-default div')).getInnerHtml()).
121 toBe("<p style=\"color:blue\">an html\n" +
122 "<em onmouseover=\"this.textContent='PWN3D!'\">click here</em>\n" +
123 "snippet</p>");
126 it('should update', function() {
127 element(by.model('snippet')).clear();
128 element(by.model('snippet')).sendKeys('new <b onclick="alert(1)">text</b>');
129 expect(element(by.css('#bind-html-with-sanitize div')).getInnerHtml()).
130 toBe('new <b>text</b>');
131 expect(element(by.css('#bind-html-with-trust div')).getInnerHtml()).toBe(
132 'new <b onclick="alert(1)">text</b>');
133 expect(element(by.css('#bind-default div')).getInnerHtml()).toBe(
134 "new <b onclick=\"alert(1)\">text</b>");
139 function $SanitizeProvider() {
140 this.$get = ['$$sanitizeUri', function($$sanitizeUri) {
141 return function(html) {
143 htmlParser(html, htmlSanitizeWriter(buf, function(uri, isImage) {
144 return !/^unsafe/.test($$sanitizeUri(uri, isImage));
151 function sanitizeText(chars) {
153 var writer = htmlSanitizeWriter(buf, angular.noop);
159 // Regular Expressions for parsing tags and attributes
160 var START_TAG_REGEXP =
161 /^<\s*([\w:-]+)((?:\s+[\w:-]+(?:\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*)\s*(\/?)\s*>/,
162 END_TAG_REGEXP = /^<\s*\/\s*([\w:-]+)[^>]*>/,
163 ATTR_REGEXP = /([\w:-]+)(?:\s*=\s*(?:(?:"((?:[^"])*)")|(?:'((?:[^'])*)')|([^>\s]+)))?/g,
164 BEGIN_TAG_REGEXP = /^</,
165 BEGING_END_TAGE_REGEXP = /^<\s*\//,
166 COMMENT_REGEXP = /<!--(.*?)-->/g,
167 DOCTYPE_REGEXP = /<!DOCTYPE([^>]*?)>/i,
168 CDATA_REGEXP = /<!\[CDATA\[(.*?)]]>/g,
169 // Match everything outside of normal chars and " (quote character)
170 NON_ALPHANUMERIC_REGEXP = /([^\#-~| |!])/g;
173 // Good source of info about elements and attributes
174 // http://dev.w3.org/html5/spec/Overview.html#semantics
175 // http://simon.html5.org/html-elements
177 // Safe Void Elements - HTML5
178 // http://dev.w3.org/html5/spec/Overview.html#void-elements
179 var voidElements = makeMap("area,br,col,hr,img,wbr");
181 // Elements that you can, intentionally, leave open (and which close themselves)
182 // http://dev.w3.org/html5/spec/Overview.html#optional-tags
183 var optionalEndTagBlockElements = makeMap("colgroup,dd,dt,li,p,tbody,td,tfoot,th,thead,tr"),
184 optionalEndTagInlineElements = makeMap("rp,rt"),
185 optionalEndTagElements = angular.extend({},
186 optionalEndTagInlineElements,
187 optionalEndTagBlockElements);
189 // Safe Block Elements - HTML5
190 var blockElements = angular.extend({}, optionalEndTagBlockElements, makeMap("address,article," +
191 "aside,blockquote,caption,center,del,dir,div,dl,figure,figcaption,footer,h1,h2,h3,h4,h5," +
192 "h6,header,hgroup,hr,ins,map,menu,nav,ol,pre,script,section,table,ul"));
194 // Inline Elements - HTML5
195 var inlineElements = angular.extend({}, optionalEndTagInlineElements, makeMap("a,abbr,acronym,b," +
196 "bdi,bdo,big,br,cite,code,del,dfn,em,font,i,img,ins,kbd,label,map,mark,q,ruby,rp,rt,s," +
197 "samp,small,span,strike,strong,sub,sup,time,tt,u,var"));
200 // Special Elements (can contain anything)
201 var specialElements = makeMap("script,style");
203 var validElements = angular.extend({},
207 optionalEndTagElements);
209 //Attributes that have href and hence need to be sanitized
210 var uriAttrs = makeMap("background,cite,href,longdesc,src,usemap");
211 var validAttrs = angular.extend({}, uriAttrs, makeMap(
212 'abbr,align,alt,axis,bgcolor,border,cellpadding,cellspacing,class,clear,'+
213 'color,cols,colspan,compact,coords,dir,face,headers,height,hreflang,hspace,'+
214 'ismap,lang,language,nohref,nowrap,rel,rev,rows,rowspan,rules,'+
215 'scope,scrolling,shape,size,span,start,summary,target,title,type,'+
216 'valign,value,vspace,width'));
218 function makeMap(str) {
219 var obj = {}, items = str.split(','), i;
220 for (i = 0; i < items.length; i++) obj[items[i]] = true;
227 * htmlParser(htmlString, {
228 * start: function(tag, attrs, unary) {},
229 * end: function(tag) {},
230 * chars: function(text) {},
231 * comment: function(text) {}
234 * @param {string} html string
235 * @param {object} handler
237 function htmlParser( html, handler ) {
238 var index, chars, match, stack = [], last = html;
239 stack.last = function() { return stack[ stack.length - 1 ]; };
244 // Make sure we're not in a script or style element
245 if ( !stack.last() || !specialElements[ stack.last() ] ) {
248 if ( html.indexOf("<!--") === 0 ) {
249 // comments containing -- are not allowed unless they terminate the comment
250 index = html.indexOf("--", 4);
252 if ( index >= 0 && html.lastIndexOf("-->", index) === index) {
253 if (handler.comment) handler.comment( html.substring( 4, index ) );
254 html = html.substring( index + 3 );
258 } else if ( DOCTYPE_REGEXP.test(html) ) {
259 match = html.match( DOCTYPE_REGEXP );
262 html = html.replace( match[0], '');
266 } else if ( BEGING_END_TAGE_REGEXP.test(html) ) {
267 match = html.match( END_TAG_REGEXP );
270 html = html.substring( match[0].length );
271 match[0].replace( END_TAG_REGEXP, parseEndTag );
276 } else if ( BEGIN_TAG_REGEXP.test(html) ) {
277 match = html.match( START_TAG_REGEXP );
280 html = html.substring( match[0].length );
281 match[0].replace( START_TAG_REGEXP, parseStartTag );
287 index = html.indexOf("<");
289 var text = index < 0 ? html : html.substring( 0, index );
290 html = index < 0 ? "" : html.substring( index );
292 if (handler.chars) handler.chars( decodeEntities(text) );
296 html = html.replace(new RegExp("(.*)<\\s*\\/\\s*" + stack.last() + "[^>]*>", 'i'),
298 text = text.replace(COMMENT_REGEXP, "$1").replace(CDATA_REGEXP, "$1");
300 if (handler.chars) handler.chars( decodeEntities(text) );
305 parseEndTag( "", stack.last() );
308 if ( html == last ) {
309 throw $sanitizeMinErr('badparse', "The sanitizer was unable to parse the following block " +
310 "of html: {0}", html);
315 // Clean up any remaining tags
318 function parseStartTag( tag, tagName, rest, unary ) {
319 tagName = angular.lowercase(tagName);
320 if ( blockElements[ tagName ] ) {
321 while ( stack.last() && inlineElements[ stack.last() ] ) {
322 parseEndTag( "", stack.last() );
326 if ( optionalEndTagElements[ tagName ] && stack.last() == tagName ) {
327 parseEndTag( "", tagName );
330 unary = voidElements[ tagName ] || !!unary;
333 stack.push( tagName );
337 rest.replace(ATTR_REGEXP,
338 function(match, name, doubleQuotedValue, singleQuotedValue, unquotedValue) {
339 var value = doubleQuotedValue
344 attrs[name] = decodeEntities(value);
346 if (handler.start) handler.start( tagName, attrs, unary );
349 function parseEndTag( tag, tagName ) {
351 tagName = angular.lowercase(tagName);
353 // Find the closest opened tag of the same type
354 for ( pos = stack.length - 1; pos >= 0; pos-- )
355 if ( stack[ pos ] == tagName )
359 // Close all the open elements, up the stack
360 for ( i = stack.length - 1; i >= pos; i-- )
361 if (handler.end) handler.end( stack[ i ] );
363 // Remove the open elements from the stack
369 var hiddenPre=document.createElement("pre");
370 var spaceRe = /^(\s*)([\s\S]*?)(\s*)$/;
372 * decodes all entities into regular string
374 * @returns {string} A string with decoded entities.
376 function decodeEntities(value) {
377 if (!value) { return ''; }
379 // Note: IE8 does not preserve spaces at the start/end of innerHTML
380 // so we must capture them and reattach them afterward
381 var parts = spaceRe.exec(value);
382 var spaceBefore = parts[1];
383 var spaceAfter = parts[3];
384 var content = parts[2];
386 hiddenPre.innerHTML=content.replace(/</g,"<");
387 // innerText depends on styling as it doesn't display hidden elements.
388 // Therefore, it's better to use textContent not to cause unnecessary
389 // reflows. However, IE<9 don't support textContent so the innerText
390 // fallback is necessary.
391 content = 'textContent' in hiddenPre ?
392 hiddenPre.textContent : hiddenPre.innerText;
394 return spaceBefore + content + spaceAfter;
398 * Escapes all potentially dangerous characters, so that the
399 * resulting string can be safely inserted into attribute or
402 * @returns {string} escaped text
404 function encodeEntities(value) {
406 replace(/&/g, '&').
407 replace(NON_ALPHANUMERIC_REGEXP, function(value){
408 return '&#' + value.charCodeAt(0) + ';';
410 replace(/</g, '<').
411 replace(/>/g, '>');
415 * create an HTML/XML writer which writes to buffer
416 * @param {Array} buf use buf.jain('') to get out sanitized html string
417 * @returns {object} in the form of {
418 * start: function(tag, attrs, unary) {},
419 * end: function(tag) {},
420 * chars: function(text) {},
421 * comment: function(text) {}
424 function htmlSanitizeWriter(buf, uriValidator){
426 var out = angular.bind(buf, buf.push);
428 start: function(tag, attrs, unary){
429 tag = angular.lowercase(tag);
430 if (!ignore && specialElements[tag]) {
433 if (!ignore && validElements[tag] === true) {
436 angular.forEach(attrs, function(value, key){
437 var lkey=angular.lowercase(key);
438 var isImage = (tag === 'img' && lkey === 'src') || (lkey === 'background');
439 if (validAttrs[lkey] === true &&
440 (uriAttrs[lkey] !== true || uriValidator(value, isImage))) {
444 out(encodeEntities(value));
448 out(unary ? '/>' : '>');
452 tag = angular.lowercase(tag);
453 if (!ignore && validElements[tag] === true) {
462 chars: function(chars){
464 out(encodeEntities(chars));
471 // define ngSanitize module and register $sanitize service
472 angular.module('ngSanitize', []).provider('$sanitize', $SanitizeProvider);
474 /* global sanitizeText: false */
482 * Finds links in text input and turns them into html links. Supports http/https/ftp/mailto and
483 * plain email address links.
485 * Requires the {@link ngSanitize `ngSanitize`} module to be installed.
487 * @param {string} text Input text.
488 * @param {string} target Window (_blank|_self|_parent|_top) or named frame to open links in.
489 * @returns {string} Html-linkified text.
492 <span ng-bind-html="linky_expression | linky"></span>
495 <example module="ngSanitize" deps="angular-sanitize.js">
496 <file name="index.html">
498 function Ctrl($scope) {
500 'Pretty text with some links:\n'+
501 'http://angularjs.org/,\n'+
502 'mailto:us@somewhere.org,\n'+
503 'another@somewhere.org,\n'+
504 'and one more: ftp://127.0.0.1/.';
505 $scope.snippetWithTarget = 'http://angularjs.org/';
508 <div ng-controller="Ctrl">
509 Snippet: <textarea ng-model="snippet" cols="60" rows="3"></textarea>
516 <tr id="linky-filter">
517 <td>linky filter</td>
519 <pre><div ng-bind-html="snippet | linky"><br></div></pre>
522 <div ng-bind-html="snippet | linky"></div>
525 <tr id="linky-target">
526 <td>linky target</td>
528 <pre><div ng-bind-html="snippetWithTarget | linky:'_blank'"><br></div></pre>
531 <div ng-bind-html="snippetWithTarget | linky:'_blank'"></div>
534 <tr id="escaped-html">
536 <td><pre><div ng-bind="snippet"><br></div></pre></td>
537 <td><div ng-bind="snippet"></div></td>
541 <file name="protractor.js" type="protractor">
542 it('should linkify the snippet with urls', function() {
543 expect(element(by.id('linky-filter')).element(by.binding('snippet | linky')).getText()).
544 toBe('Pretty text with some links: http://angularjs.org/, us@somewhere.org, ' +
545 'another@somewhere.org, and one more: ftp://127.0.0.1/.');
546 expect(element.all(by.css('#linky-filter a')).count()).toEqual(4);
549 it('should not linkify snippet without the linky filter', function() {
550 expect(element(by.id('escaped-html')).element(by.binding('snippet')).getText()).
551 toBe('Pretty text with some links: http://angularjs.org/, mailto:us@somewhere.org, ' +
552 'another@somewhere.org, and one more: ftp://127.0.0.1/.');
553 expect(element.all(by.css('#escaped-html a')).count()).toEqual(0);
556 it('should update', function() {
557 element(by.model('snippet')).clear();
558 element(by.model('snippet')).sendKeys('new http://link.');
559 expect(element(by.id('linky-filter')).element(by.binding('snippet | linky')).getText()).
560 toBe('new http://link.');
561 expect(element.all(by.css('#linky-filter a')).count()).toEqual(1);
562 expect(element(by.id('escaped-html')).element(by.binding('snippet')).getText())
563 .toBe('new http://link.');
566 it('should work with the target property', function() {
567 expect(element(by.id('linky-target')).
568 element(by.binding("snippetWithTarget | linky:'_blank'")).getText()).
569 toBe('http://angularjs.org/');
570 expect(element(by.css('#linky-target a')).getAttribute('target')).toEqual('_blank');
575 angular.module('ngSanitize').filter('linky', ['$sanitize', function($sanitize) {
576 var LINKY_URL_REGEXP =
577 /((ftp|https?):\/\/|(mailto:)?[A-Za-z0-9._%+-]+@)\S*[^\s.;,(){}<>]/,
578 MAILTO_REGEXP = /^mailto:/;
580 return function(text, target) {
581 if (!text) return text;
587 while ((match = raw.match(LINKY_URL_REGEXP))) {
588 // We can not end in these as they are sometimes found at the end of the sentence
590 // if we did not match ftp/http/mailto then assume mailto
591 if (match[2] == match[3]) url = 'mailto:' + url;
593 addText(raw.substr(0, i));
594 addLink(url, match[0].replace(MAILTO_REGEXP, ''));
595 raw = raw.substring(i + match[0].length);
598 return $sanitize(html.join(''));
600 function addText(text) {
604 html.push(sanitizeText(text));
607 function addLink(url, text) {
609 if (angular.isDefined(target)) {
610 html.push('target="');
624 })(window, window.angular);