Skip to content

Commit 03793d0

Browse files
vicbmhevery
authored andcommitted
feat(CssProcessor): add support for CssTransformers
Closes angular#860
1 parent 9982520 commit 03793d0

File tree

15 files changed

+71
-20
lines changed

15 files changed

+71
-20
lines changed

modules/angular2/src/core/application.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ function _injectorBindings(appComponentType): List<Binding> {
9999
UrlResolver,
100100
StyleUrlResolver,
101101
StyleInliner,
102-
CssProcessor,
102+
bind(CssProcessor).toFactory(() => new CssProcessor(null), []),
103103
];
104104
}
105105

modules/angular2/src/core/compiler/css_processor.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {DOM} from 'angular2/src/dom/dom_adapter';
22

33
import {isPresent} from 'angular2/src/facade/lang';
4+
import {List} from 'angular2/src/facade/collection';
45

56
import {CompileStep} from './pipeline/compile_step';
67
import {CompileElement} from './pipeline/compile_element';
@@ -10,9 +11,16 @@ import {ShadowDomStrategy} from './shadow_dom_strategy';
1011
import {DirectiveMetadata} from './directive_metadata';
1112

1213
/**
13-
* Processes the <style> tags during the compilation.
14+
* Processes the <style> tags during the compilation:
15+
* - Apply any given transformers,
16+
* - Apply the shadow DOM strategy style step.
1417
*/
1518
export class CssProcessor {
19+
_transformers: List<CssTransformer>;
20+
21+
constructor(transformers: List<CssTransformer>) {
22+
this._transformers = transformers;
23+
}
1624

1725
/**
1826
* Returns a compile step to be added to the compiler pipeline.
@@ -24,22 +32,35 @@ export class CssProcessor {
2432
getCompileStep(cmpMetadata: DirectiveMetadata, shadowDomStrategy: ShadowDomStrategy,
2533
templateUrl: string) {
2634
var strategyStep = shadowDomStrategy.getStyleCompileStep(cmpMetadata, templateUrl);
27-
return new _CssProcessorStep(strategyStep);
35+
return new _CssProcessorStep(strategyStep, this._transformers);
2836
}
2937
}
3038

39+
export class CssTransformer {
40+
transform(styleElement) {};
41+
}
42+
3143
class _CssProcessorStep extends CompileStep {
3244
_strategyStep: CompileStep;
45+
_transformers: List<CssTransformer>;
3346

34-
constructor(strategyStep: CompileStep) {
47+
constructor(strategyStep: CompileStep, transformers: List<CssTransformer>) {
3548
super();
3649
this._strategyStep = strategyStep;
50+
this._transformers = transformers;
3751
}
3852

3953
process(parent:CompileElement, current:CompileElement, control:CompileControl) {
4054
if (DOM.tagName(current.element) == 'STYLE') {
4155
current.ignoreBindings = true;
4256

57+
if (isPresent(this._transformers)) {
58+
var styleEl = current.element;
59+
for (var i = 0; i < this._transformers.length; i++) {
60+
this._transformers[i].transform(styleEl);
61+
}
62+
}
63+
4364
if (isPresent(this._strategyStep)) {
4465
this._strategyStep.process(parent, current, control);
4566
}

modules/angular2/test/core/compiler/compiler_common_tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ class TestableCompiler extends Compiler {
302302
templateResolver,
303303
cmpUrlMapper,
304304
urlResolver,
305-
new CssProcessor());
305+
new CssProcessor(null));
306306

307307
this.steps = steps;
308308
}

modules/angular2/test/core/compiler/css_processor_spec.js

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {describe, it, expect, beforeEach, ddescribe, iit, xit, el} from 'angular2/test_lib';
2-
import {CssProcessor} from 'angular2/src/core/compiler/css_processor';
2+
import {CssProcessor, CssTransformer} from 'angular2/src/core/compiler/css_processor';
33

44
import {ShadowDomStrategy} from 'angular2/src/core/compiler/shadow_dom_strategy';
55

@@ -8,6 +8,8 @@ import {CompileElement} from 'angular2/src/core/compiler/pipeline/compile_elemen
88
import {CompileStep} from 'angular2/src/core/compiler/pipeline/compile_step';
99
import {CompileControl} from 'angular2/src/core/compiler/pipeline/compile_control';
1010

11+
import {DOM} from 'angular2/src/dom/dom_adapter';
12+
1113
import {Component} from 'angular2/src/core/annotations/annotations';
1214

1315
import {DirectiveMetadata} from 'angular2/src/core/compiler/directive_metadata';
@@ -28,7 +30,7 @@ export function main() {
2830

2931
it('it should set ignoreBindings to true for style elements', () => {
3032
var strategy = new FakeShadowDomStrategy(null);
31-
var cssProcessor = new CssProcessor();
33+
var cssProcessor = new CssProcessor(null);
3234

3335
var pipeline = createPipeline(cssProcessor, strategy, 'http://base');
3436
var results = pipeline.process(el('<div><style></style></div>'));
@@ -44,13 +46,26 @@ export function main() {
4446
});
4547
var strategy = new FakeShadowDomStrategy(compileStep);
4648

47-
var cssProcessor = new CssProcessor();
49+
var cssProcessor = new CssProcessor(null);
4850
var pipeline = createPipeline(cssProcessor, strategy, 'http://base');
4951
var results = pipeline.process(el('<div><style></style></div>'));
5052

5153
expect(processedEls.length).toEqual(1);
5254
expect(processedEls[0]).toBe(results[1].element);
5355
});
56+
57+
it('should apply the given transformers', () => {
58+
var strategy = new FakeShadowDomStrategy(null);
59+
var cssProcessor = new CssProcessor([
60+
new FakeCssTransformer('/*transformer1 */'),
61+
new FakeCssTransformer('/*transformer2 */'),
62+
]);
63+
64+
var pipeline = createPipeline(cssProcessor, strategy, 'http://base');
65+
var results = pipeline.process(el('<div><style></style></div>'));
66+
67+
expect(results[1].element).toHaveText('/*transformer1 *//*transformer2 */');
68+
});
5469
});
5570
});
5671
}
@@ -79,4 +94,19 @@ class MockStep extends CompileStep {
7994
}
8095
}
8196

97+
class FakeCssTransformer extends CssTransformer {
98+
_css: string;
99+
100+
constructor(css: string) {
101+
super();
102+
this._css = css;
103+
}
104+
105+
transform(styleEl) {
106+
var cssText = DOM.getText(styleEl);
107+
cssText += this._css;
108+
DOM.setText(styleEl, cssText);
109+
}
110+
}
111+
82112
class SomeComponent {}

modules/angular2/test/core/compiler/integration_spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export function main() {
4242
tplResolver,
4343
new ComponentUrlMapper(),
4444
urlResolver,
45-
new CssProcessor()
45+
new CssProcessor(null)
4646
);
4747
}
4848

modules/angular2/test/core/compiler/shadow_dom/shadow_dom_emulation_integration_spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export function main() {
6060
tplResolver,
6161
new ComponentUrlMapper(),
6262
urlResolver,
63-
new CssProcessor()
63+
new CssProcessor(null)
6464
);
6565
});
6666

modules/angular2/test/directives/foreach_spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function main() {
3838
tplResolver,
3939
new ComponentUrlMapper(),
4040
urlResolver,
41-
new CssProcessor()
41+
new CssProcessor(null)
4242
);
4343
});
4444

modules/angular2/test/directives/if_spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function main() {
3838
tplResolver,
3939
new ComponentUrlMapper(),
4040
urlResolver,
41-
new CssProcessor()
41+
new CssProcessor(null)
4242
);
4343
});
4444

modules/angular2/test/directives/non_bindable_spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function main() {
3636
tplResolver,
3737
new ComponentUrlMapper(),
3838
urlResolver,
39-
new CssProcessor()
39+
new CssProcessor(null)
4040
);
4141
});
4242

modules/angular2/test/directives/switch_spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function main() {
3333
tplResolver,
3434
new ComponentUrlMapper(),
3535
urlResolver,
36-
new CssProcessor()
36+
new CssProcessor(null)
3737
);
3838
});
3939

0 commit comments

Comments
 (0)