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

Commit a86cf20

Browse files
tboschIgorMinar
authored andcommittedOct 22, 2013
fix: don't inline css in csp mode.
Also add `angular-csp.css` to the resulting build.
1 parent b9557b0 commit a86cf20

File tree

5 files changed

+41
-10
lines changed

5 files changed

+41
-10
lines changed
 

‎Gruntfile.js

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ module.exports = function(grunt) {
121121
src: util.wrap([files['angularSrc']], 'angular'),
122122
styles: {
123123
css: ['css/angular.css'],
124+
generateCspCssFile: true,
124125
minify: true
125126
}
126127
},

‎lib/grunt/utils.js

+32-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var shell = require('shelljs');
33
var grunt = require('grunt');
44
var spawn = require('child_process').spawn;
55
var version;
6+
var CSP_CSS_HEADER = '/* Include this file in your html if you are using the CSP mode. */\n\n';
67

78
module.exports = {
89

@@ -70,12 +71,20 @@ module.exports = {
7071

7172

7273
addStyle: function(src, styles, minify){
73-
styles = styles.map(processCSS.bind(this)).join('\n');
74-
src += styles;
75-
return src;
74+
styles = styles.reduce(processCSS.bind(this), {
75+
js: [src],
76+
css: []
77+
});
78+
return {
79+
js: styles.js.join('\n'),
80+
css: styles.css.join('\n')
81+
};
82+
83+
function processCSS(state, file) {
84+
var css = fs.readFileSync(file).toString(),
85+
js;
86+
state.css.push(css);
7687

77-
function processCSS(file){
78-
var css = fs.readFileSync(file).toString();
7988
if(minify){
8089
css = css
8190
.replace(/\r?\n/g, '')
@@ -91,7 +100,10 @@ module.exports = {
91100
.replace(/\\/g, '\\\\')
92101
.replace(/'/g, "\\'")
93102
.replace(/\r?\n/g, '\\n');
94-
return "!angular.$$csp() && angular.element(document).find('head').prepend('<style type=\"text/css\">" + css + "</style>');";
103+
js = "!angular.$$csp() && angular.element(document).find('head').prepend('<style type=\"text/css\">" + css + "</style>');";
104+
state.js.push(js);
105+
106+
return state;
95107
}
96108
},
97109

@@ -100,7 +112,7 @@ module.exports = {
100112
var processed = src
101113
.replace(/"NG_VERSION_FULL"/g, NG_VERSION.full)
102114
.replace(/"NG_VERSION_MAJOR"/, NG_VERSION.major)
103-
.replace(/"NG_VERSION_MINOR"/, NG_VERSION.minor)
115+
.replace(/"NG_VERSION_ MINOR"/, NG_VERSION.minor)
Has conversations. Original line has conversations.
104116
.replace(/"NG_VERSION_DOT"/, NG_VERSION.dot)
105117
.replace(/"NG_VERSION_CDN"/, NG_VERSION.cdn)
106118
.replace(/"NG_VERSION_CODENAME"/, NG_VERSION.codename);
@@ -112,17 +124,28 @@ module.exports = {
112124
build: function(config, fn){
113125
var files = grunt.file.expand(config.src);
114126
var styles = config.styles;
127+
var processedStyles;
115128
//concat
116-
var src = files.map(function(filepath){
129+
var src = files.map(function(filepath) {
117130
return grunt.file.read(filepath);
118131
}).join(grunt.util.normalizelf('\n'));
119132
//process
120133
var processed = this.process(src, grunt.config('NG_VERSION'), config.strict);
121-
if (styles) processed = this.addStyle(processed, styles.css, styles.minify);
134+
if (styles) {
135+
processedStyles = this.addStyle(processed, styles.css, styles.minify);
136+
processed = processedStyles.js;
137+
if (config.styles.generateCspCssFile) {
138+
grunt.file.write(removeSuffix(config.dest) + '-csp.css', CSP_CSS_HEADER + processedStyles.css);
139+
}
140+
}
122141
//write
123142
grunt.file.write(config.dest, processed);
124143
grunt.log.ok('File ' + config.dest + ' created.');
125144
fn();
145+
146+
function removeSuffix(fileName) {
147+
return fileName.replace(/\.js$/, '');
148+
}
126149
},
127150

128151

‎src/ng/directive/ngCloak.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
* of the browser view.
1616
*
1717
* `ngCloak` works in cooperation with the following css rule embedded within `angular.js` and
18-
* `angular.min.js`:
18+
* `angular.min.js`.
19+
* For CSP mode please add `angular-csp.css` to your html file (see {@link ng.directive:ngCsp ngCsp}).
1920
*
2021
* <pre>
2122
* [ng\:cloak], [ng-cloak], [data-ng-cloak], [x-ng-cloak], .ng-cloak, .x-ng-cloak {

‎src/ng/directive/ngCsp.js

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
* evaluate all expressions up to 30% slower than in non-CSP mode, but no security violations will
2020
* be raised.
2121
*
22+
* CSP forbids JavaScript to inline stylesheet rules. In non CSP mode Angular automatically
23+
* includes some CSS rules (e.g. {@link ng.directive:ngCloak ngCloak}).
24+
* To make those directives work in CSP mode, include the `angular-csp.css` manually.
25+
*
2226
* In order to use this feature put the `ngCsp` directive on the root element of the application.
2327
*
2428
* *Note: This directive is only available in the `ng-csp` and `data-ng-csp` attribute form.*

‎src/ng/directive/ngShowHide.js

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* provided to the ngShow attribute. The element is shown or hidden by removing or adding
1010
* the `ng-hide` CSS class onto the element. The `.ng-hide` CSS class is predefined
1111
* in AngularJS and sets the display style to none (using an !important flag).
12+
* For CSP mode please add `angular-csp.css` to your html file (see {@link ng.directive:ngCsp ngCsp}).
1213
*
1314
* <pre>
1415
* <!-- when $scope.myValue is truthy (element is visible) -->
@@ -161,6 +162,7 @@ var ngShowDirective = ['$animate', function($animate) {
161162
* provided to the ngHide attribute. The element is shown or hidden by removing or adding
162163
* the `ng-hide` CSS class onto the element. The `.ng-hide` CSS class is predefined
163164
* in AngularJS and sets the display style to none (using an !important flag).
165+
* For CSP mode please add `angular-csp.css` to your html file (see {@link ng.directive:ngCsp ngCsp}).
164166
*
165167
* <pre>
166168
* <!-- when $scope.myValue is truthy (element is hidden) -->

0 commit comments

Comments
 (0)
This repository has been archived.