From 1c0e746ff63b90a8b3768804404a4da5df061651 Mon Sep 17 00:00:00 2001 From: gdi2290 Date: Sun, 8 Feb 2015 20:53:51 -0800 Subject: [PATCH 1/2] fix(dom): correctly set css for style --- modules/angular2/src/facade/dom.es6 | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/modules/angular2/src/facade/dom.es6 b/modules/angular2/src/facade/dom.es6 index bd14f254f462..fd565d05241a 100644 --- a/modules/angular2/src/facade/dom.es6 +++ b/modules/angular2/src/facade/dom.es6 @@ -112,7 +112,12 @@ export class DOM { } static createStyleElement(css:string, doc=document):StyleElement { var style = doc.createElement('STYLE'); - style.innerText = css; + style.type = 'text/css'; + if (style.styleSheet){ + style.styleSheet.cssText = css; + } else { + style.appendChild(doc.createTextNode(css)); + } return style; } static clone(node:Node) { From 7af71715f41b7ec624fa3503b89e830a418afdd2 Mon Sep 17 00:00:00 2001 From: gdi2290 Date: Sun, 8 Feb 2015 20:57:48 -0800 Subject: [PATCH 2/2] feat(TemplateLoader): cssUrl MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I noticed this feature but didn’t work so I went ahead and implemented it. The only problem I have atm is the createTemplateWithCss helper method that should be in dom or just in closure scope. I also need some tests after your feedback about where to put the helper createTemplateWithCss. --- .../src/core/annotations/template_config.js | 4 ++ .../src/core/compiler/template_loader.js | 50 ++++++++++++++----- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/modules/angular2/src/core/annotations/template_config.js b/modules/angular2/src/core/annotations/template_config.js index edd6e958783c..d78cff5ac63d 100644 --- a/modules/angular2/src/core/annotations/template_config.js +++ b/modules/angular2/src/core/annotations/template_config.js @@ -3,6 +3,7 @@ import {List} from 'angular2/src/facade/collection'; export class TemplateConfig { url:any; //string; + cssUrl:any; //string inline:any; //string; directives:any; //List; formatters:any; //List; @@ -10,12 +11,14 @@ export class TemplateConfig { @CONST() constructor({ url, + cssUrl, inline, directives, formatters, source }: { url: string, + cssUrl: string, inline: string, directives: List, formatters: List, @@ -23,6 +26,7 @@ export class TemplateConfig { }) { this.url = url; + this.cssUrl = cssUrl; this.inline = inline; this.directives = directives; this.formatters = formatters; diff --git a/modules/angular2/src/core/compiler/template_loader.js b/modules/angular2/src/core/compiler/template_loader.js index ce7538a1f7a7..9c1430a16f05 100644 --- a/modules/angular2/src/core/compiler/template_loader.js +++ b/modules/angular2/src/core/compiler/template_loader.js @@ -31,21 +31,47 @@ export class TemplateLoader { return PromiseWrapper.resolve(template); } + if (isPresent(tplConfig.url) && isPresent(tplConfig.cssUrl)) { + return Promise.all([ + this.buildUrl(tplConfig.url), + this.buildUrl(tplConfig.cssUrl) + ]) + .then((props) => { + // if I had .spread this would be one line + var html = props[0]; + var style = props[1]; + return this.createTemplateWithCss(html, style); + }); + } + if (isPresent(tplConfig.url)) { - var url = tplConfig.url; - var promise = StringMapWrapper.get(this._cache, url); - - if (isBlank(promise)) { - promise = this._xhr.get(url).then(function (html) { - var template = DOM.createTemplate(html); - return template; - }); - StringMapWrapper.set(this._cache, url, promise); - } - - return promise; + return this.buildUrl(tplConfig.url).then(DOM.createTemplate); + } + + if (isPresent(tplConfig.cssUrl)) { + return this.buildUrl(tplConfig.cssUrl).then(DOM.createStyleElement); } throw new BaseException(`No template configured for component ${stringify(cmpMetadata.type)}`); } + buildUrl(url):Promise { + var promise = StringMapWrapper.get(this._cache, url); + + if (isBlank(promise)) { + promise = this._xhr.get(url) + StringMapWrapper.set(this._cache, url, promise); + } + + return promise; + } + createTemplateWithCss(html, css) { + var style = DOM.createStyleElement(css); + + var d = DOM.createElement('div'); + DOM.setInnerHTML(d, html); + DOM.insertBefore(d.firstChild, style); + + return DOM.createTemplate(d.innerHTML); + } + }