forked from angular/angular
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(compiler): add codegen for stylesheets
Part of angular#3605
- Loading branch information
Showing
8 changed files
with
200 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import {StringWrapper, isJsObject} from 'angular2/src/core/facade/lang'; | ||
import {SourceModule} from './api'; | ||
|
||
var ESCAPE_QUOTES_RE = /'|\\|\n/g; | ||
|
||
export function styleCodeGen(cssText:string, importUrls:string[]):SourceModule { | ||
var imports:string[][] = []; | ||
var sourceParts:string[] = []; | ||
for (var i=0; i<importUrls.length; i++) { | ||
var url = importUrls[i]; | ||
var moduleAlias = `import${i}`; | ||
imports.push([url, moduleAlias]); | ||
sourceParts.push(`${moduleAlias}.STYLES`); | ||
} | ||
var escapedStyle = StringWrapper.replaceAllMapped(cssText, ESCAPE_QUOTES_RE, (match) => { | ||
if (match[0] == "'" || match[0] == '\\') { | ||
return `\\${match[0]}` | ||
} else { | ||
return '\\n'; | ||
} | ||
}); | ||
sourceParts.push(`'${escapedStyle}'`); | ||
var moduleSource = `${isJsObject(true) ? 'export': ''} var STYLES = ${sourceParts.join('+')};`; | ||
return new SourceModule(moduleSource, imports); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { | ||
ddescribe, | ||
describe, | ||
xdescribe, | ||
it, | ||
iit, | ||
xit, | ||
expect, | ||
beforeEach, | ||
afterEach, | ||
AsyncTestCompleter, | ||
inject | ||
} from 'angular2/test_lib'; | ||
import {PromiseWrapper} from 'angular2/src/core/facade/async'; | ||
import {IS_DART} from '../platform'; | ||
|
||
import {evalModule} from './eval_module'; | ||
import {styleCodeGen} from 'angular2/src/compiler/style_codegen'; | ||
import {SourceModule} from 'angular2/src/compiler/api'; | ||
|
||
// used by the test that imports styles from another stylesheet | ||
export var STYLES = 'span {color: blue};'; | ||
|
||
const MODULE_NAME = 'angular2/test/compiler/style_codegen_spec'; | ||
|
||
export function main() { | ||
describe('StyleCodeGen', () => { | ||
it('should convert simple css rules', inject([AsyncTestCompleter], (async) => { | ||
var sourceModule = styleCodeGen('div {color: red};', []); | ||
evalModule(testableModule(sourceModule.source), sourceModule.imports, null).then( (value) => { | ||
expect(value).toEqual('div {color: red};'); | ||
async.done(); | ||
}); | ||
})); | ||
|
||
it('should convert css rules with newlines and quotes', inject([AsyncTestCompleter], (async) => { | ||
var sourceModule = styleCodeGen('div\n{"color": \'red\'};', []); | ||
evalModule(testableModule(sourceModule.source), sourceModule.imports, null).then( (value) => { | ||
expect(value).toEqual('div\n{"color": \'red\'};'); | ||
async.done(); | ||
}); | ||
})); | ||
|
||
it('should allow to import rules', inject([AsyncTestCompleter], (async) => { | ||
var sourceModule = styleCodeGen('div {color: red};', [MODULE_NAME]); | ||
evalModule(testableModule(sourceModule.source), sourceModule.imports, null).then( (value) => { | ||
expect(value).toEqual('span {color: blue};div {color: red};'); | ||
async.done(); | ||
}); | ||
})); | ||
}); | ||
} | ||
|
||
function testableModule(sourceModule:string) { | ||
if (IS_DART) { | ||
return `${sourceModule} | ||
run(_) { return STYLES; } | ||
`; | ||
} else { | ||
return `${sourceModule} | ||
exports.run = function(_) { return STYLES; }; | ||
`; | ||
} | ||
} |
136 changes: 69 additions & 67 deletions
136
modules/angular2/test/compiler/style_url_resolver_spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.