Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 5bcd719

Browse files
committed
chore(ngSanitize): extract $sanitize, ngBindHtml, linkyFilter into a module
Create build for other modules as well (ngResource, ngCookies): - wrap into a function - add license - add version Breaks `$sanitize` service, `ngBindHtml` directive and `linky` filter were moved to the `ngSanitize` module. Apps that depend on any of these will need to load `angular-sanitize.js` and include `ngSanitize` in their dependency list: `var myApp = angular.module('myApp', ['ngSanitize']);`
1 parent e1743cc commit 5bcd719

File tree

20 files changed

+301
-244
lines changed

20 files changed

+301
-244
lines changed

Rakefile

+20-4
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,23 @@ task :compile => [:init, :compile_scenario, :compile_jstd_scenario_adapter] do
8282
'src/loader.js',
8383
'src/loader.suffix'])
8484

85-
FileUtils.cp 'src/ngMock/angular-mocks.js', path_to('angular-mocks.js')
86-
FileUtils.cp 'src/ngResource/resource.js', path_to('angular-resource.js')
87-
FileUtils.cp 'src/ngCookies/cookies.js', path_to('angular-cookies.js')
8885

86+
concat_module('sanitize', [
87+
'src/ngSanitize/sanitize.js',
88+
'src/ngSanitize/directive/ngBindHtml.js',
89+
'src/ngSanitize/filter/linky.js'])
90+
91+
concat_module('resource', ['src/ngResource/resource.js'])
92+
concat_module('cookies', ['src/ngCookies/cookies.js'])
93+
94+
95+
FileUtils.cp 'src/ngMock/angular-mocks.js', path_to('angular-mocks.js')
8996

9097
closure_compile('angular.js')
9198
closure_compile('angular-cookies.js')
9299
closure_compile('angular-loader.js')
93100
closure_compile('angular-resource.js')
101+
closure_compile('angular-sanitize.js')
94102

95103
end
96104

@@ -121,6 +129,8 @@ task :package => [:clean, :compile, :docs] do
121129
path_to('angular-cookies.min.js'),
122130
path_to('angular-resource.js'),
123131
path_to('angular-resource.min.js'),
132+
path_to('angular-sanitize.js'),
133+
path_to('angular-sanitize.min.js'),
124134
path_to('angular-scenario.js'),
125135
path_to('jstd-scenario-adapter.js'),
126136
path_to('jstd-scenario-adapter-config.js'),
@@ -147,7 +157,8 @@ task :package => [:clean, :compile, :docs] do
147157
rewrite_file(src) do |content|
148158
content.sub!('angular.js', "angular-#{NG_VERSION.full}.js").
149159
sub!('angular-resource.js', "angular-resource-#{NG_VERSION.full}.js").
150-
sub!('angular-cookies.js', "angular-cookies-#{NG_VERSION.full}.js")
160+
sub!('angular-cookies.js', "angular-cookies-#{NG_VERSION.full}.js").
161+
sub!('angular-sanitize.js', "angular-sanitize-#{NG_VERSION.full}.js")
151162
end
152163
end
153164

@@ -290,6 +301,11 @@ def concat_file(filename, deps, footer='')
290301
end
291302

292303

304+
def concat_module(name, files)
305+
concat_file('angular-' + name + '.js', ['src/module.prefix'] + files + ['src/module.suffix'])
306+
end
307+
308+
293309
def rewrite_file(filename)
294310
File.open(filename, File::RDWR) do |f|
295311
content = f.read

angularFiles.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ angularFiles = {
2424
'src/ng/route.js',
2525
'src/ng/routeParams.js',
2626
'src/ng/rootScope.js',
27-
'src/ng/sanitize.js',
2827
'src/ng/sniffer.js',
2928
'src/ng/window.js',
3029
'src/ng/http.js',
@@ -65,6 +64,9 @@ angularFiles = {
6564
'angularSrcModules': [
6665
'src/ngCookies/cookies.js',
6766
'src/ngResource/resource.js',
67+
'src/ngSanitize/sanitize.js',
68+
'src/ngSanitize/directive/ngBindHtml.js',
69+
'src/ngSanitize/filter/linky.js',
6870
'src/ngMock/angular-mocks.js'
6971
],
7072

@@ -98,6 +100,9 @@ angularFiles = {
98100
'test/ng/filter/*.js',
99101
'test/ngCookies/*.js',
100102
'test/ngResource/*.js',
103+
'test/ngSanitize/*.js',
104+
'test/ngSanitize/directive/*.js',
105+
'test/ngSanitize/filter/*.js',
101106
'test/ngMock/*.js'
102107
],
103108

@@ -136,10 +141,16 @@ angularFiles = {
136141
'src/ngMock/angular-mocks.js',
137142
'src/ngCookies/cookies.js',
138143
'src/ngResource/resource.js',
144+
'src/ngSanitize/sanitize.js',
145+
'src/ngSanitize/directive/ngBindHtml.js',
146+
'src/ngSanitize/filter/linky.js',
139147
'test/matchers.js',
140148
'test/ngMock/*.js',
141149
'test/ngCookies/*.js',
142-
'test/ngResource/*.js'
150+
'test/ngResource/*.js',
151+
'test/ngSanitize/*.js',
152+
'test/ngSanitize/directive/*.js',
153+
'test/ngSanitize/filter/*.js'
143154
],
144155

145156
'jstdPerf': [

docs/content/cookbook/deeplinking.ngdoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ The two partials are defined in the following URLs:
3939
<doc:example module="deepLinking">
4040
<doc:source jsfiddle="false">
4141
<script>
42-
angular.module('deepLinking', [])
42+
angular.module('deepLinking', ['ngSanitize'])
4343
.config(function($routeProvider) {
4444
$routeProvider.when("/welcome", {template:'./examples/welcome.html', controller:WelcomeCntl});
4545
$routeProvider.when("/settings", {template:'./examples/settings.html', controller:SettingsCntl});

docs/src/templates/docs.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ function TutorialInstructionsCtrl($scope, $cookieStore) {
146146
};
147147
}
148148

149-
angular.module('ngdocs', ['ngdocs.directives', 'ngResource', 'ngCookies'], function($locationProvider, $filterProvider, $compileProvider) {
149+
angular.module('ngdocs', ['ngdocs.directives', 'ngResource', 'ngCookies', 'ngSanitize'],
150+
function($locationProvider, $filterProvider, $compileProvider) {
150151
$locationProvider.html5Mode(true).hashPrefix('!');
151152

152153
$filterProvider.register('title', function(){

docs/src/templates/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
addTag('script', {src: path('angular.js')}, sync);
3131
addTag('script', {src: path('angular-resource.js') }, sync);
3232
addTag('script', {src: path('angular-cookies.js') }, sync);
33+
addTag('script', {src: path('angular-sanitize.js') }, sync);
3334
addTag('script', {src: 'docs-combined.js'}, sync);
3435
addTag('script', {src: 'docs-keywords.js'}, sync);
3536

src/AngularPublic.js

-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ function publishExternalAPI(angular){
7171
style: styleDirective,
7272
option: optionDirective,
7373
ngBind: ngBindDirective,
74-
ngBindHtml: ngBindHtmlDirective,
7574
ngBindHtmlUnsafe: ngBindHtmlUnsafeDirective,
7675
ngBindTemplate: ngBindTemplateDirective,
7776
ngClass: ngClassDirective,
@@ -123,7 +122,6 @@ function publishExternalAPI(angular){
123122
$routeParams: $RouteParamsProvider,
124123
$rootScope: $RootScopeProvider,
125124
$q: $QProvider,
126-
$sanitize: $SanitizeProvider,
127125
$sniffer: $SnifferProvider,
128126
$templateCache: $TemplateCacheProvider,
129127
$window: $WindowProvider

src/module.prefix

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* @license AngularJS v"NG_VERSION_FULL"
3+
* (c) 2010-2012 AngularJS http://angularjs.org
4+
* License: MIT
5+
*/
6+
(function(angular) {

src/module.suffix

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
})(window.angular);

src/ng/directive/ngBind.js

+25-48
Original file line numberDiff line numberDiff line change
@@ -55,54 +55,6 @@ var ngBindDirective = ngDirective(function(scope, element, attr) {
5555
});
5656

5757

58-
/**
59-
* @ngdoc directive
60-
* @name angular.module.ng.$compileProvider.directive.ngBindHtmlUnsafe
61-
*
62-
* @description
63-
* Creates a binding that will innerHTML the result of evaluating the `expression` into the current
64-
* element. *The innerHTML-ed content will not be sanitized!* You should use this directive only if
65-
* {@link angular.module.ng.$compileProvider.directive.ngBindHtml ngBindHtml} directive is too
66-
* restrictive and when you absolutely trust the source of the content you are binding to.
67-
*
68-
* See {@link angular.module.ng.$sanitize $sanitize} docs for examples.
69-
*
70-
* @element ANY
71-
* @param {expression} ngBindHtmlUnsafe {@link guide/dev_guide.expressions Expression} to evaluate.
72-
*/
73-
var ngBindHtmlUnsafeDirective = ngDirective(function(scope, element, attr) {
74-
element.addClass('ng-binding').data('$binding', attr.ngBindHtmlUnsafe);
75-
scope.$watch(attr.ngBindHtmlUnsafe, function(value) {
76-
element.html(value || '');
77-
});
78-
});
79-
80-
81-
/**
82-
* @ngdoc directive
83-
* @name angular.module.ng.$compileProvider.directive.ngBindHtml
84-
*
85-
* @description
86-
* Creates a binding that will sanitize the result of evaluating the `expression` with the
87-
* {@link angular.module.ng.$sanitize $sanitize} service and innerHTML the result into the current
88-
* element.
89-
*
90-
* See {@link angular.module.ng.$sanitize $sanitize} docs for examples.
91-
*
92-
* @element ANY
93-
* @param {expression} ngBindHtml {@link guide/dev_guide.expressions Expression} to evaluate.
94-
*/
95-
var ngBindHtmlDirective = ['$sanitize', function($sanitize) {
96-
return function(scope, element, attr) {
97-
element.addClass('ng-binding').data('$binding', attr.ngBindHtml);
98-
scope.$watch(attr.ngBindHtml, function(value) {
99-
value = $sanitize(value);
100-
element.html(value || '');
101-
});
102-
}
103-
}];
104-
105-
10658
/**
10759
* @ngdoc directive
10860
* @name angular.module.ng.$compileProvider.directive.ngBindTemplate
@@ -160,3 +112,28 @@ var ngBindTemplateDirective = ['$interpolate', function($interpolate) {
160112
});
161113
}
162114
}];
115+
116+
117+
/**
118+
* @ngdoc directive
119+
* @name angular.module.ng.$compileProvider.directive.ngBindHtmlUnsafe
120+
*
121+
* @description
122+
* Creates a binding that will innerHTML the result of evaluating the `expression` into the current
123+
* element. *The innerHTML-ed content will not be sanitized!* You should use this directive only if
124+
* {@link angular.module.ng.$compileProvider.directive.ngBindHtml ngBindHtml} directive is too
125+
* restrictive and when you absolutely trust the source of the content you are binding to.
126+
*
127+
* See {@link angular.module.ng.$sanitize $sanitize} docs for examples.
128+
*
129+
* @element ANY
130+
* @param {expression} ngBindHtmlUnsafe {@link guide/dev_guide.expressions Expression} to evaluate.
131+
*/
132+
var ngBindHtmlUnsafeDirective = [function() {
133+
return function(scope, element, attr) {
134+
element.addClass('ng-binding').data('$binding', attr.ngBindHtmlUnsafe);
135+
scope.$watch(attr.ngBindHtmlUnsafe, function(value) {
136+
element.html(value || '');
137+
});
138+
};
139+
}];

src/ng/filter.js

-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ function $FilterProvider($provide) {
9696
register('filter', filterFilter);
9797
register('json', jsonFilter);
9898
register('limitTo', limitToFilter);
99-
register('linky', linkyFilter);
10099
register('lowercase', lowercaseFilter);
101100
register('number', numberFilter);
102101
register('orderBy', orderByFilter);

src/ng/filter/filters.js

-107
Original file line numberDiff line numberDiff line change
@@ -439,110 +439,3 @@ var lowercaseFilter = valueFn(lowercase);
439439
* @see angular.uppercase
440440
*/
441441
var uppercaseFilter = valueFn(uppercase);
442-
443-
444-
/**
445-
* @ngdoc filter
446-
* @name angular.module.ng.$filter.linky
447-
* @function
448-
*
449-
* @description
450-
* Finds links in text input and turns them into html links. Supports http/https/ftp/mailto and
451-
* plain email address links.
452-
*
453-
* @param {string} text Input text.
454-
* @returns {string} Html-linkified text.
455-
*
456-
* @example
457-
<doc:example>
458-
<doc:source>
459-
<script>
460-
function Ctrl($scope) {
461-
$scope.snippet =
462-
'Pretty text with some links:\n'+
463-
'http://angularjs.org/,\n'+
464-
'mailto:us@somewhere.org,\n'+
465-
'another@somewhere.org,\n'+
466-
'and one more: ftp://127.0.0.1/.';
467-
}
468-
</script>
469-
<div ng-controller="Ctrl">
470-
Snippet: <textarea ng-model="snippet" cols="60" rows="3"></textarea>
471-
<table>
472-
<tr>
473-
<td>Filter</td>
474-
<td>Source</td>
475-
<td>Rendered</td>
476-
</tr>
477-
<tr id="linky-filter">
478-
<td>linky filter</td>
479-
<td>
480-
<pre>&lt;div ng-bind-html="snippet | linky"&gt;<br>&lt;/div&gt;</pre>
481-
</td>
482-
<td>
483-
<div ng-bind-html="snippet | linky"></div>
484-
</td>
485-
</tr>
486-
<tr id="escaped-html">
487-
<td>no filter</td>
488-
<td><pre>&lt;div ng-bind="snippet"&gt;<br>&lt;/div&gt;</pre></td>
489-
<td><div ng-bind="snippet"></div></td>
490-
</tr>
491-
</table>
492-
</doc:source>
493-
<doc:scenario>
494-
it('should linkify the snippet with urls', function() {
495-
expect(using('#linky-filter').binding('snippet | linky')).
496-
toBe('Pretty text with some links:&#10;' +
497-
'<a href="http://angularjs.org/">http://angularjs.org/</a>,&#10;' +
498-
'<a href="mailto:us@somewhere.org">us@somewhere.org</a>,&#10;' +
499-
'<a href="mailto:another@somewhere.org">another@somewhere.org</a>,&#10;' +
500-
'and one more: <a href="ftp://127.0.0.1/">ftp://127.0.0.1/</a>.');
501-
});
502-
503-
it ('should not linkify snippet without the linky filter', function() {
504-
expect(using('#escaped-html').binding('snippet')).
505-
toBe("Pretty text with some links:\n" +
506-
"http://angularjs.org/,\n" +
507-
"mailto:us@somewhere.org,\n" +
508-
"another@somewhere.org,\n" +
509-
"and one more: ftp://127.0.0.1/.");
510-
});
511-
512-
it('should update', function() {
513-
input('snippet').enter('new http://link.');
514-
expect(using('#linky-filter').binding('snippet | linky')).
515-
toBe('new <a href="http://link">http://link</a>.');
516-
expect(using('#escaped-html').binding('snippet')).toBe('new http://link.');
517-
});
518-
</doc:scenario>
519-
</doc:example>
520-
*/
521-
function linkyFilter() {
522-
var LINKY_URL_REGEXP = /((ftp|https?):\/\/|(mailto:)?[A-Za-z0-9._%+-]+@)\S*[^\s\.\;\,\(\)\{\}\<\>]/,
523-
MAILTO_REGEXP = /^mailto:/;
524-
525-
return function(text) {
526-
if (!text) return text;
527-
var match;
528-
var raw = text;
529-
var html = [];
530-
var writer = htmlSanitizeWriter(html);
531-
var url;
532-
var i;
533-
while ((match = raw.match(LINKY_URL_REGEXP))) {
534-
// We can not end in these as they are sometimes found at the end of the sentence
535-
url = match[0];
536-
// if we did not match ftp/http/mailto then assume mailto
537-
if (match[2] == match[3]) url = 'mailto:' + url;
538-
i = match.index;
539-
writer.chars(raw.substr(0, i));
540-
writer.start('a', {href:url});
541-
writer.chars(match[0].replace(MAILTO_REGEXP, ''));
542-
writer.end('a');
543-
raw = raw.substring(i + match[0].length);
544-
}
545-
writer.chars(raw);
546-
return html.join('');
547-
};
548-
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
3+
4+
/**
5+
* @ngdoc directive
6+
* @name angular.module.ngSanitize.directive.ngBindHtml
7+
*
8+
* @description
9+
* Creates a binding that will sanitize the result of evaluating the `expression` with the
10+
* {@link angular.module.ng.$sanitize $sanitize} service and innerHTML the result into the current
11+
* element.
12+
*
13+
* See {@link angular.module.ng.$sanitize $sanitize} docs for examples.
14+
*
15+
* @element ANY
16+
* @param {expression} ngBindHtml {@link guide/dev_guide.expressions Expression} to evaluate.
17+
*/
18+
angular.module('ngSanitize').directive('ngBindHtml', ['$sanitize', function($sanitize) {
19+
return function(scope, element, attr) {
20+
element.addClass('ng-binding').data('$binding', attr.ngBindHtml);
21+
scope.$watch(attr.ngBindHtml, function(value) {
22+
value = $sanitize(value);
23+
element.html(value || '');
24+
});
25+
};
26+
}]);

0 commit comments

Comments
 (0)