Digital Office Automation System Backend
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

angular-sanitize.js 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. /**
  2. * @license AngularJS v1.2.13
  3. * (c) 2010-2014 Google, Inc. http://angularjs.org
  4. * License: MIT
  5. */
  6. (function(window, angular, undefined) {'use strict';
  7. var $sanitizeMinErr = angular.$$minErr('$sanitize');
  8. /**
  9. * @ngdoc overview
  10. * @name ngSanitize
  11. * @description
  12. *
  13. * # ngSanitize
  14. *
  15. * The `ngSanitize` module provides functionality to sanitize HTML.
  16. *
  17. * {@installModule sanitize}
  18. *
  19. * <div doc-module-components="ngSanitize"></div>
  20. *
  21. * See {@link ngSanitize.$sanitize `$sanitize`} for usage.
  22. */
  23. /*
  24. * HTML Parser By Misko Hevery (misko@hevery.com)
  25. * based on: HTML Parser By John Resig (ejohn.org)
  26. * Original code by Erik Arvidsson, Mozilla Public License
  27. * http://erik.eae.net/simplehtmlparser/simplehtmlparser.js
  28. *
  29. * // Use like so:
  30. * htmlParser(htmlString, {
  31. * start: function(tag, attrs, unary) {},
  32. * end: function(tag) {},
  33. * chars: function(text) {},
  34. * comment: function(text) {}
  35. * });
  36. *
  37. */
  38. /**
  39. * @ngdoc service
  40. * @name ngSanitize.$sanitize
  41. * @function
  42. *
  43. * @description
  44. * The input is sanitized by parsing the html into tokens. All safe tokens (from a whitelist) are
  45. * then serialized back to properly escaped html string. This means that no unsafe input can make
  46. * it into the returned string, however, since our parser is more strict than a typical browser
  47. * parser, it's possible that some obscure input, which would be recognized as valid HTML by a
  48. * browser, won't make it through the sanitizer.
  49. * The whitelist is configured using the functions `aHrefSanitizationWhitelist` and
  50. * `imgSrcSanitizationWhitelist` of {@link ng.$compileProvider `$compileProvider`}.
  51. *
  52. * @param {string} html Html input.
  53. * @returns {string} Sanitized html.
  54. *
  55. * @example
  56. <doc:example module="ngSanitize">
  57. <doc:source>
  58. <script>
  59. function Ctrl($scope, $sce) {
  60. $scope.snippet =
  61. '<p style="color:blue">an html\n' +
  62. '<em onmouseover="this.textContent=\'PWN3D!\'">click here</em>\n' +
  63. 'snippet</p>';
  64. $scope.deliberatelyTrustDangerousSnippet = function() {
  65. return $sce.trustAsHtml($scope.snippet);
  66. };
  67. }
  68. </script>
  69. <div ng-controller="Ctrl">
  70. Snippet: <textarea ng-model="snippet" cols="60" rows="3"></textarea>
  71. <table>
  72. <tr>
  73. <td>Directive</td>
  74. <td>How</td>
  75. <td>Source</td>
  76. <td>Rendered</td>
  77. </tr>
  78. <tr id="bind-html-with-sanitize">
  79. <td>ng-bind-html</td>
  80. <td>Automatically uses $sanitize</td>
  81. <td><pre>&lt;div ng-bind-html="snippet"&gt;<br/>&lt;/div&gt;</pre></td>
  82. <td><div ng-bind-html="snippet"></div></td>
  83. </tr>
  84. <tr id="bind-html-with-trust">
  85. <td>ng-bind-html</td>
  86. <td>Bypass $sanitize by explicitly trusting the dangerous value</td>
  87. <td>
  88. <pre>&lt;div ng-bind-html="deliberatelyTrustDangerousSnippet()"&gt;
  89. &lt;/div&gt;</pre>
  90. </td>
  91. <td><div ng-bind-html="deliberatelyTrustDangerousSnippet()"></div></td>
  92. </tr>
  93. <tr id="bind-default">
  94. <td>ng-bind</td>
  95. <td>Automatically escapes</td>
  96. <td><pre>&lt;div ng-bind="snippet"&gt;<br/>&lt;/div&gt;</pre></td>
  97. <td><div ng-bind="snippet"></div></td>
  98. </tr>
  99. </table>
  100. </div>
  101. </doc:source>
  102. <doc:protractor>
  103. it('should sanitize the html snippet by default', function() {
  104. expect(element(by.css('#bind-html-with-sanitize div')).getInnerHtml()).
  105. toBe('<p>an html\n<em>click here</em>\nsnippet</p>');
  106. });
  107. it('should inline raw snippet if bound to a trusted value', function() {
  108. expect(element(by.css('#bind-html-with-trust div')).getInnerHtml()).
  109. toBe("<p style=\"color:blue\">an html\n" +
  110. "<em onmouseover=\"this.textContent='PWN3D!'\">click here</em>\n" +
  111. "snippet</p>");
  112. });
  113. it('should escape snippet without any filter', function() {
  114. expect(element(by.css('#bind-default div')).getInnerHtml()).
  115. toBe("&lt;p style=\"color:blue\"&gt;an html\n" +
  116. "&lt;em onmouseover=\"this.textContent='PWN3D!'\"&gt;click here&lt;/em&gt;\n" +
  117. "snippet&lt;/p&gt;");
  118. });
  119. it('should update', function() {
  120. element(by.model('snippet')).clear();
  121. element(by.model('snippet')).sendKeys('new <b onclick="alert(1)">text</b>');
  122. expect(element(by.css('#bind-html-with-sanitize div')).getInnerHtml()).
  123. toBe('new <b>text</b>');
  124. expect(element(by.css('#bind-html-with-trust div')).getInnerHtml()).toBe(
  125. 'new <b onclick="alert(1)">text</b>');
  126. expect(element(by.css('#bind-default div')).getInnerHtml()).toBe(
  127. "new &lt;b onclick=\"alert(1)\"&gt;text&lt;/b&gt;");
  128. });
  129. </doc:protractor>
  130. </doc:example>
  131. */
  132. function $SanitizeProvider() {
  133. this.$get = ['$$sanitizeUri', function($$sanitizeUri) {
  134. return function(html) {
  135. var buf = [];
  136. htmlParser(html, htmlSanitizeWriter(buf, function(uri, isImage) {
  137. return !/^unsafe/.test($$sanitizeUri(uri, isImage));
  138. }));
  139. return buf.join('');
  140. };
  141. }];
  142. }
  143. function sanitizeText(chars) {
  144. var buf = [];
  145. var writer = htmlSanitizeWriter(buf, angular.noop);
  146. writer.chars(chars);
  147. return buf.join('');
  148. }
  149. // Regular Expressions for parsing tags and attributes
  150. var START_TAG_REGEXP =
  151. /^<\s*([\w:-]+)((?:\s+[\w:-]+(?:\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*)\s*(\/?)\s*>/,
  152. END_TAG_REGEXP = /^<\s*\/\s*([\w:-]+)[^>]*>/,
  153. ATTR_REGEXP = /([\w:-]+)(?:\s*=\s*(?:(?:"((?:[^"])*)")|(?:'((?:[^'])*)')|([^>\s]+)))?/g,
  154. BEGIN_TAG_REGEXP = /^</,
  155. BEGING_END_TAGE_REGEXP = /^<\s*\//,
  156. COMMENT_REGEXP = /<!--(.*?)-->/g,
  157. DOCTYPE_REGEXP = /<!DOCTYPE([^>]*?)>/i,
  158. CDATA_REGEXP = /<!\[CDATA\[(.*?)]]>/g,
  159. // Match everything outside of normal chars and " (quote character)
  160. NON_ALPHANUMERIC_REGEXP = /([^\#-~| |!])/g;
  161. // Good source of info about elements and attributes
  162. // http://dev.w3.org/html5/spec/Overview.html#semantics
  163. // http://simon.html5.org/html-elements
  164. // Safe Void Elements - HTML5
  165. // http://dev.w3.org/html5/spec/Overview.html#void-elements
  166. var voidElements = makeMap("area,br,col,hr,img,wbr");
  167. // Elements that you can, intentionally, leave open (and which close themselves)
  168. // http://dev.w3.org/html5/spec/Overview.html#optional-tags
  169. var optionalEndTagBlockElements = makeMap("colgroup,dd,dt,li,p,tbody,td,tfoot,th,thead,tr"),
  170. optionalEndTagInlineElements = makeMap("rp,rt"),
  171. optionalEndTagElements = angular.extend({},
  172. optionalEndTagInlineElements,
  173. optionalEndTagBlockElements);
  174. // Safe Block Elements - HTML5
  175. var blockElements = angular.extend({}, optionalEndTagBlockElements, makeMap("address,article," +
  176. "aside,blockquote,caption,center,del,dir,div,dl,figure,figcaption,footer,h1,h2,h3,h4,h5," +
  177. "h6,header,hgroup,hr,ins,map,menu,nav,ol,pre,script,section,table,ul"));
  178. // Inline Elements - HTML5
  179. var inlineElements = angular.extend({}, optionalEndTagInlineElements, makeMap("a,abbr,acronym,b," +
  180. "bdi,bdo,big,br,cite,code,del,dfn,em,font,i,img,ins,kbd,label,map,mark,q,ruby,rp,rt,s," +
  181. "samp,small,span,strike,strong,sub,sup,time,tt,u,var"));
  182. // Special Elements (can contain anything)
  183. var specialElements = makeMap("script,style");
  184. var validElements = angular.extend({},
  185. voidElements,
  186. blockElements,
  187. inlineElements,
  188. optionalEndTagElements);
  189. //Attributes that have href and hence need to be sanitized
  190. var uriAttrs = makeMap("background,cite,href,longdesc,src,usemap");
  191. var validAttrs = angular.extend({}, uriAttrs, makeMap(
  192. 'abbr,align,alt,axis,bgcolor,border,cellpadding,cellspacing,class,clear,'+
  193. 'color,cols,colspan,compact,coords,dir,face,headers,height,hreflang,hspace,'+
  194. 'ismap,lang,language,nohref,nowrap,rel,rev,rows,rowspan,rules,'+
  195. 'scope,scrolling,shape,size,span,start,summary,target,title,type,'+
  196. 'valign,value,vspace,width'));
  197. function makeMap(str) {
  198. var obj = {}, items = str.split(','), i;
  199. for (i = 0; i < items.length; i++) obj[items[i]] = true;
  200. return obj;
  201. }
  202. /**
  203. * @example
  204. * htmlParser(htmlString, {
  205. * start: function(tag, attrs, unary) {},
  206. * end: function(tag) {},
  207. * chars: function(text) {},
  208. * comment: function(text) {}
  209. * });
  210. *
  211. * @param {string} html string
  212. * @param {object} handler
  213. */
  214. function htmlParser( html, handler ) {
  215. var index, chars, match, stack = [], last = html;
  216. stack.last = function() { return stack[ stack.length - 1 ]; };
  217. while ( html ) {
  218. chars = true;
  219. // Make sure we're not in a script or style element
  220. if ( !stack.last() || !specialElements[ stack.last() ] ) {
  221. // Comment
  222. if ( html.indexOf("<!--") === 0 ) {
  223. // comments containing -- are not allowed unless they terminate the comment
  224. index = html.indexOf("--", 4);
  225. if ( index >= 0 && html.lastIndexOf("-->", index) === index) {
  226. if (handler.comment) handler.comment( html.substring( 4, index ) );
  227. html = html.substring( index + 3 );
  228. chars = false;
  229. }
  230. // DOCTYPE
  231. } else if ( DOCTYPE_REGEXP.test(html) ) {
  232. match = html.match( DOCTYPE_REGEXP );
  233. if ( match ) {
  234. html = html.replace( match[0] , '');
  235. chars = false;
  236. }
  237. // end tag
  238. } else if ( BEGING_END_TAGE_REGEXP.test(html) ) {
  239. match = html.match( END_TAG_REGEXP );
  240. if ( match ) {
  241. html = html.substring( match[0].length );
  242. match[0].replace( END_TAG_REGEXP, parseEndTag );
  243. chars = false;
  244. }
  245. // start tag
  246. } else if ( BEGIN_TAG_REGEXP.test(html) ) {
  247. match = html.match( START_TAG_REGEXP );
  248. if ( match ) {
  249. html = html.substring( match[0].length );
  250. match[0].replace( START_TAG_REGEXP, parseStartTag );
  251. chars = false;
  252. }
  253. }
  254. if ( chars ) {
  255. index = html.indexOf("<");
  256. var text = index < 0 ? html : html.substring( 0, index );
  257. html = index < 0 ? "" : html.substring( index );
  258. if (handler.chars) handler.chars( decodeEntities(text) );
  259. }
  260. } else {
  261. html = html.replace(new RegExp("(.*)<\\s*\\/\\s*" + stack.last() + "[^>]*>", 'i'),
  262. function(all, text){
  263. text = text.replace(COMMENT_REGEXP, "$1").replace(CDATA_REGEXP, "$1");
  264. if (handler.chars) handler.chars( decodeEntities(text) );
  265. return "";
  266. });
  267. parseEndTag( "", stack.last() );
  268. }
  269. if ( html == last ) {
  270. throw $sanitizeMinErr('badparse', "The sanitizer was unable to parse the following block " +
  271. "of html: {0}", html);
  272. }
  273. last = html;
  274. }
  275. // Clean up any remaining tags
  276. parseEndTag();
  277. function parseStartTag( tag, tagName, rest, unary ) {
  278. tagName = angular.lowercase(tagName);
  279. if ( blockElements[ tagName ] ) {
  280. while ( stack.last() && inlineElements[ stack.last() ] ) {
  281. parseEndTag( "", stack.last() );
  282. }
  283. }
  284. if ( optionalEndTagElements[ tagName ] && stack.last() == tagName ) {
  285. parseEndTag( "", tagName );
  286. }
  287. unary = voidElements[ tagName ] || !!unary;
  288. if ( !unary )
  289. stack.push( tagName );
  290. var attrs = {};
  291. rest.replace(ATTR_REGEXP,
  292. function(match, name, doubleQuotedValue, singleQuotedValue, unquotedValue) {
  293. var value = doubleQuotedValue
  294. || singleQuotedValue
  295. || unquotedValue
  296. || '';
  297. attrs[name] = decodeEntities(value);
  298. });
  299. if (handler.start) handler.start( tagName, attrs, unary );
  300. }
  301. function parseEndTag( tag, tagName ) {
  302. var pos = 0, i;
  303. tagName = angular.lowercase(tagName);
  304. if ( tagName )
  305. // Find the closest opened tag of the same type
  306. for ( pos = stack.length - 1; pos >= 0; pos-- )
  307. if ( stack[ pos ] == tagName )
  308. break;
  309. if ( pos >= 0 ) {
  310. // Close all the open elements, up the stack
  311. for ( i = stack.length - 1; i >= pos; i-- )
  312. if (handler.end) handler.end( stack[ i ] );
  313. // Remove the open elements from the stack
  314. stack.length = pos;
  315. }
  316. }
  317. }
  318. var hiddenPre=document.createElement("pre");
  319. var spaceRe = /^(\s*)([\s\S]*?)(\s*)$/;
  320. /**
  321. * decodes all entities into regular string
  322. * @param value
  323. * @returns {string} A string with decoded entities.
  324. */
  325. function decodeEntities(value) {
  326. if (!value) { return ''; }
  327. // Note: IE8 does not preserve spaces at the start/end of innerHTML
  328. // so we must capture them and reattach them afterward
  329. var parts = spaceRe.exec(value);
  330. var spaceBefore = parts[1];
  331. var spaceAfter = parts[3];
  332. var content = parts[2];
  333. if (content) {
  334. hiddenPre.innerHTML=content.replace(/</g,"&lt;");
  335. // innerText depends on styling as it doesn't display hidden elements.
  336. // Therefore, it's better to use textContent not to cause unnecessary
  337. // reflows. However, IE<9 don't support textContent so the innerText
  338. // fallback is necessary.
  339. content = 'textContent' in hiddenPre ?
  340. hiddenPre.textContent : hiddenPre.innerText;
  341. }
  342. return spaceBefore + content + spaceAfter;
  343. }
  344. /**
  345. * Escapes all potentially dangerous characters, so that the
  346. * resulting string can be safely inserted into attribute or
  347. * element text.
  348. * @param value
  349. * @returns escaped text
  350. */
  351. function encodeEntities(value) {
  352. return value.
  353. replace(/&/g, '&amp;').
  354. replace(NON_ALPHANUMERIC_REGEXP, function(value){
  355. return '&#' + value.charCodeAt(0) + ';';
  356. }).
  357. replace(/</g, '&lt;').
  358. replace(/>/g, '&gt;');
  359. }
  360. /**
  361. * create an HTML/XML writer which writes to buffer
  362. * @param {Array} buf use buf.jain('') to get out sanitized html string
  363. * @returns {object} in the form of {
  364. * start: function(tag, attrs, unary) {},
  365. * end: function(tag) {},
  366. * chars: function(text) {},
  367. * comment: function(text) {}
  368. * }
  369. */
  370. function htmlSanitizeWriter(buf, uriValidator){
  371. var ignore = false;
  372. var out = angular.bind(buf, buf.push);
  373. return {
  374. start: function(tag, attrs, unary){
  375. tag = angular.lowercase(tag);
  376. if (!ignore && specialElements[tag]) {
  377. ignore = tag;
  378. }
  379. if (!ignore && validElements[tag] === true) {
  380. out('<');
  381. out(tag);
  382. angular.forEach(attrs, function(value, key){
  383. var lkey=angular.lowercase(key);
  384. var isImage = (tag === 'img' && lkey === 'src') || (lkey === 'background');
  385. if (validAttrs[lkey] === true &&
  386. (uriAttrs[lkey] !== true || uriValidator(value, isImage))) {
  387. out(' ');
  388. out(key);
  389. out('="');
  390. out(encodeEntities(value));
  391. out('"');
  392. }
  393. });
  394. out(unary ? '/>' : '>');
  395. }
  396. },
  397. end: function(tag){
  398. tag = angular.lowercase(tag);
  399. if (!ignore && validElements[tag] === true) {
  400. out('</');
  401. out(tag);
  402. out('>');
  403. }
  404. if (tag == ignore) {
  405. ignore = false;
  406. }
  407. },
  408. chars: function(chars){
  409. if (!ignore) {
  410. out(encodeEntities(chars));
  411. }
  412. }
  413. };
  414. }
  415. // define ngSanitize module and register $sanitize service
  416. angular.module('ngSanitize', []).provider('$sanitize', $SanitizeProvider);
  417. /* global sanitizeText: false */
  418. /**
  419. * @ngdoc filter
  420. * @name ngSanitize.filter:linky
  421. * @function
  422. *
  423. * @description
  424. * Finds links in text input and turns them into html links. Supports http/https/ftp/mailto and
  425. * plain email address links.
  426. *
  427. * Requires the {@link ngSanitize `ngSanitize`} module to be installed.
  428. *
  429. * @param {string} text Input text.
  430. * @param {string} target Window (_blank|_self|_parent|_top) or named frame to open links in.
  431. * @returns {string} Html-linkified text.
  432. *
  433. * @usage
  434. <span ng-bind-html="linky_expression | linky"></span>
  435. *
  436. * @example
  437. <doc:example module="ngSanitize">
  438. <doc:source>
  439. <script>
  440. function Ctrl($scope) {
  441. $scope.snippet =
  442. 'Pretty text with some links:\n'+
  443. 'http://angularjs.org/,\n'+
  444. 'mailto:us@somewhere.org,\n'+
  445. 'another@somewhere.org,\n'+
  446. 'and one more: ftp://127.0.0.1/.';
  447. $scope.snippetWithTarget = 'http://angularjs.org/';
  448. }
  449. </script>
  450. <div ng-controller="Ctrl">
  451. Snippet: <textarea ng-model="snippet" cols="60" rows="3"></textarea>
  452. <table>
  453. <tr>
  454. <td>Filter</td>
  455. <td>Source</td>
  456. <td>Rendered</td>
  457. </tr>
  458. <tr id="linky-filter">
  459. <td>linky filter</td>
  460. <td>
  461. <pre>&lt;div ng-bind-html="snippet | linky"&gt;<br>&lt;/div&gt;</pre>
  462. </td>
  463. <td>
  464. <div ng-bind-html="snippet | linky"></div>
  465. </td>
  466. </tr>
  467. <tr id="linky-target">
  468. <td>linky target</td>
  469. <td>
  470. <pre>&lt;div ng-bind-html="snippetWithTarget | linky:'_blank'"&gt;<br>&lt;/div&gt;</pre>
  471. </td>
  472. <td>
  473. <div ng-bind-html="snippetWithTarget | linky:'_blank'"></div>
  474. </td>
  475. </tr>
  476. <tr id="escaped-html">
  477. <td>no filter</td>
  478. <td><pre>&lt;div ng-bind="snippet"&gt;<br>&lt;/div&gt;</pre></td>
  479. <td><div ng-bind="snippet"></div></td>
  480. </tr>
  481. </table>
  482. </doc:source>
  483. <doc:protractor>
  484. it('should linkify the snippet with urls', function() {
  485. expect(element(by.id('linky-filter')).element(by.binding('snippet | linky')).getText()).
  486. toBe('Pretty text with some links: http://angularjs.org/, us@somewhere.org, ' +
  487. 'another@somewhere.org, and one more: ftp://127.0.0.1/.');
  488. expect(element.all(by.css('#linky-filter a')).count()).toEqual(4);
  489. });
  490. it('should not linkify snippet without the linky filter', function() {
  491. expect(element(by.id('escaped-html')).element(by.binding('snippet')).getText()).
  492. toBe('Pretty text with some links: http://angularjs.org/, mailto:us@somewhere.org, ' +
  493. 'another@somewhere.org, and one more: ftp://127.0.0.1/.');
  494. expect(element.all(by.css('#escaped-html a')).count()).toEqual(0);
  495. });
  496. it('should update', function() {
  497. element(by.model('snippet')).clear();
  498. element(by.model('snippet')).sendKeys('new http://link.');
  499. expect(element(by.id('linky-filter')).element(by.binding('snippet | linky')).getText()).
  500. toBe('new http://link.');
  501. expect(element.all(by.css('#linky-filter a')).count()).toEqual(1);
  502. expect(element(by.id('escaped-html')).element(by.binding('snippet')).getText())
  503. .toBe('new http://link.');
  504. });
  505. it('should work with the target property', function() {
  506. expect(element(by.id('linky-target')).
  507. element(by.binding("snippetWithTarget | linky:'_blank'")).getText()).
  508. toBe('http://angularjs.org/');
  509. expect(element(by.css('#linky-target a')).getAttribute('target')).toEqual('_blank');
  510. });
  511. </doc:protractor>
  512. </doc:example>
  513. */
  514. angular.module('ngSanitize').filter('linky', ['$sanitize', function($sanitize) {
  515. var LINKY_URL_REGEXP =
  516. /((ftp|https?):\/\/|(mailto:)?[A-Za-z0-9._%+-]+@)\S*[^\s.;,(){}<>]/,
  517. MAILTO_REGEXP = /^mailto:/;
  518. return function(text, target) {
  519. if (!text) return text;
  520. var match;
  521. var raw = text;
  522. var html = [];
  523. var url;
  524. var i;
  525. while ((match = raw.match(LINKY_URL_REGEXP))) {
  526. // We can not end in these as they are sometimes found at the end of the sentence
  527. url = match[0];
  528. // if we did not match ftp/http/mailto then assume mailto
  529. if (match[2] == match[3]) url = 'mailto:' + url;
  530. i = match.index;
  531. addText(raw.substr(0, i));
  532. addLink(url, match[0].replace(MAILTO_REGEXP, ''));
  533. raw = raw.substring(i + match[0].length);
  534. }
  535. addText(raw);
  536. return $sanitize(html.join(''));
  537. function addText(text) {
  538. if (!text) {
  539. return;
  540. }
  541. html.push(sanitizeText(text));
  542. }
  543. function addLink(url, text) {
  544. html.push('<a ');
  545. if (angular.isDefined(target)) {
  546. html.push('target="');
  547. html.push(target);
  548. html.push('" ');
  549. }
  550. html.push('href="');
  551. html.push(url);
  552. html.push('">');
  553. addText(text);
  554. html.push('</a>');
  555. }
  556. };
  557. }]);
  558. })(window, window.angular);