Skip to content

Commit 22a3833

Browse files
SimeonCSimeonC
authored andcommitted
merge(sanitizer) Update sanitizer and test from main
1 parent fccf1fc commit 22a3833

File tree

5 files changed

+145
-69
lines changed

5 files changed

+145
-69
lines changed

dist/textAngular-sanitize.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/textAngular-sanitize.js

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @license AngularJS v1.3.0-build.2711+sha.facd904
2+
* @license AngularJS v1.2.26
33
* (c) 2010-2014 Google, Inc. http://angularjs.org
44
* License: MIT
55
*/
@@ -42,7 +42,7 @@ var $sanitizeMinErr = angular.$$minErr('$sanitize');
4242
/**
4343
* @ngdoc service
4444
* @name $sanitize
45-
* @function
45+
* @kind function
4646
*
4747
* @description
4848
* The input is sanitized by parsing the html into tokens. All safe tokens (from a whitelist) are
@@ -57,20 +57,21 @@ var $sanitizeMinErr = angular.$$minErr('$sanitize');
5757
* @returns {string} Sanitized html.
5858
*
5959
* @example
60-
<example module="ngSanitize" deps="angular-sanitize.js">
60+
<example module="sanitizeExample" deps="angular-sanitize.js">
6161
<file name="index.html">
6262
<script>
63-
function Ctrl($scope, $sce) {
64-
$scope.snippet =
65-
'<p style="color:blue">an html\n' +
66-
'<em onmouseover="this.textContent=\'PWN3D!\'">click here</em>\n' +
67-
'snippet</p>';
68-
$scope.deliberatelyTrustDangerousSnippet = function() {
69-
return $sce.trustAsHtml($scope.snippet);
70-
};
71-
}
63+
angular.module('sanitizeExample', ['ngSanitize'])
64+
.controller('ExampleController', ['$scope', '$sce', function($scope, $sce) {
65+
$scope.snippet =
66+
'<p style="color:blue">an html\n' +
67+
'<em onmouseover="this.textContent=\'PWN3D!\'">click here</em>\n' +
68+
'snippet</p>';
69+
$scope.deliberatelyTrustDangerousSnippet = function() {
70+
return $sce.trustAsHtml($scope.snippet);
71+
};
72+
}]);
7273
</script>
73-
<div ng-controller="Ctrl">
74+
<div ng-controller="ExampleController">
7475
Snippet: <textarea ng-model="snippet" cols="60" rows="3"></textarea>
7576
<table>
7677
<tr>
@@ -158,11 +159,11 @@ function sanitizeText(chars) {
158159

159160
// Regular Expressions for parsing tags and attributes
160161
var START_TAG_REGEXP =
161-
/^<\s*([\w:-]+)((?:\s+[\w:-]+(?:\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*)\s*(\/?)\s*>/,
162-
END_TAG_REGEXP = /^<\s*\/\s*([\w:-]+)[^>]*>/,
162+
/^<((?:[a-zA-Z])[\w:-]*)((?:\s+[\w:-]+(?:\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*)\s*(\/?)\s*(>?)/,
163+
END_TAG_REGEXP = /^<\/\s*([\w:-]+)[^>]*>/,
163164
ATTR_REGEXP = /([\w:-]+)(?:\s*=\s*(?:(?:"((?:[^"])*)")|(?:'((?:[^'])*)')|([^>\s]+)))?/g,
164165
BEGIN_TAG_REGEXP = /^</,
165-
BEGING_END_TAGE_REGEXP = /^<\s*\//,
166+
BEGING_END_TAGE_REGEXP = /^<\//,
166167
COMMENT_REGEXP = /<!--(.*?)-->/g,
167168
DOCTYPE_REGEXP = /<!DOCTYPE([^>]*?)>/i,
168169
CDATA_REGEXP = /<!\[CDATA\[(.*?)]]>/g,
@@ -236,10 +237,18 @@ function makeMap(str) {
236237
* @param {object} handler
237238
*/
238239
function htmlParser( html, handler ) {
239-
var index, chars, match, stack = [], last = html;
240+
if (typeof html !== 'string') {
241+
if (html === null || typeof html === 'undefined') {
242+
html = '';
243+
} else {
244+
html = '' + html;
245+
}
246+
}
247+
var index, chars, match, stack = [], last = html, text;
240248
stack.last = function() { return stack[ stack.length - 1 ]; };
241249

242250
while ( html ) {
251+
text = '';
243252
chars = true;
244253

245254
// Make sure we're not in a script or style element
@@ -278,16 +287,23 @@ function htmlParser( html, handler ) {
278287
match = html.match( START_TAG_REGEXP );
279288

280289
if ( match ) {
281-
html = html.substring( match[0].length );
282-
match[0].replace( START_TAG_REGEXP, parseStartTag );
290+
// We only have a valid start-tag if there is a '>'.
291+
if ( match[4] ) {
292+
html = html.substring( match[0].length );
293+
match[0].replace( START_TAG_REGEXP, parseStartTag );
294+
}
283295
chars = false;
296+
} else {
297+
// no ending tag found --- this piece should be encoded as an entity.
298+
text += '<';
299+
html = html.substring(1);
284300
}
285301
}
286302

287303
if ( chars ) {
288304
index = html.indexOf("<");
289305

290-
var text = index < 0 ? html : html.substring( 0, index );
306+
text += index < 0 ? html : html.substring( 0, index );
291307
html = index < 0 ? "" : html.substring( index );
292308

293309
if (handler.chars) handler.chars( decodeEntities(text) );
@@ -563,7 +579,7 @@ angular.module('ngSanitize', []).provider('$sanitize', $SanitizeProvider);
563579
/**
564580
* @ngdoc filter
565581
* @name linky
566-
* @function
582+
* @kind function
567583
*
568584
* @description
569585
* Finds links in text input and turns them into html links. Supports http/https/ftp/mailto and
@@ -579,20 +595,21 @@ angular.module('ngSanitize', []).provider('$sanitize', $SanitizeProvider);
579595
<span ng-bind-html="linky_expression | linky"></span>
580596
*
581597
* @example
582-
<example module="ngSanitize" deps="angular-sanitize.js">
598+
<example module="linkyExample" deps="angular-sanitize.js">
583599
<file name="index.html">
584600
<script>
585-
function Ctrl($scope) {
586-
$scope.snippet =
587-
'Pretty text with some links:\n'+
588-
'http://angularjs.org/,\n'+
589-
'mailto:us@somewhere.org,\n'+
590-
'another@somewhere.org,\n'+
591-
'and one more: ftp://127.0.0.1/.';
592-
$scope.snippetWithTarget = 'http://angularjs.org/';
593-
}
601+
angular.module('linkyExample', ['ngSanitize'])
602+
.controller('ExampleController', ['$scope', function($scope) {
603+
$scope.snippet =
604+
'Pretty text with some links:\n'+
605+
'http://angularjs.org/,\n'+
606+
'mailto:us@somewhere.org,\n'+
607+
'another@somewhere.org,\n'+
608+
'and one more: ftp://127.0.0.1/.';
609+
$scope.snippetWithTarget = 'http://angularjs.org/';
610+
}]);
594611
</script>
595-
<div ng-controller="Ctrl">
612+
<div ng-controller="ExampleController">
596613
Snippet: <textarea ng-model="snippet" cols="60" rows="3"></textarea>
597614
<table>
598615
<tr>
@@ -661,7 +678,7 @@ angular.module('ngSanitize', []).provider('$sanitize', $SanitizeProvider);
661678
*/
662679
angular.module('ngSanitize').filter('linky', ['$sanitize', function($sanitize) {
663680
var LINKY_URL_REGEXP =
664-
/((ftp|https?):\/\/|(mailto:)?[A-Za-z0-9._%+-]+@)\S*[^\s.;,(){}<>]/,
681+
/((ftp|https?):\/\/|(mailto:)?[A-Za-z0-9._%+-]+@)\S*[^\s.;,(){}<>"]/,
665682
MAILTO_REGEXP = /^mailto:/;
666683

667684
return function(text, target) {

test/textAngularSanitize/linky.spec.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
'use strict';
2+
13
describe('linky', function() {
2-
'use strict';
3-
44
var linky;
55

66
beforeEach(module('ngSanitize'));
@@ -25,6 +25,8 @@ describe('linky', function() {
2525
toEqual('<a href="mailto:me@example.com">me@example.com</a>');
2626
expect(linky("send email to me@example.com, but")).
2727
toEqual('send email to <a href="mailto:me@example.com">me@example.com</a>, but');
28+
expect(linky("my email is \"me@example.com\"")).
29+
toEqual('my email is &#34;<a href="mailto:me@example.com">me@example.com</a>&#34;');
2830
});
2931

3032
it('should handle target:', function() {
@@ -33,4 +35,4 @@ describe('linky', function() {
3335
expect(linky("http://example.com", "someNamedIFrame")).
3436
toEqual('<a target="someNamedIFrame" href="http://example.com">http://example.com</a>');
3537
});
36-
});
38+
});

test/textAngularSanitize/ngBindHtml.spec.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
'use strict';
2+
3+
14
describe('ngBindHtml', function() {
2-
'use strict';
35
beforeEach(module('ngSanitize'));
46

57
it('should set html', inject(function($rootScope, $compile) {
@@ -23,4 +25,4 @@ describe('ngBindHtml', function() {
2325
expect(angular.lowercase(element.html())).toEqual('');
2426
});
2527
}));
26-
});
28+
});

0 commit comments

Comments
 (0)