Skip to content

Commit 9250cd6

Browse files
committed
fix(ShimShadowCss): preserve attribute on style elements
1 parent edb797e commit 9250cd6

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

modules/angular2/src/core/compiler/pipeline/shim_shadow_css.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {CompileControl} from './compile_control';
55
import {DirectiveMetadata} from 'angular2/src/core/compiler/directive_metadata';
66
import {ShadowDomStrategy} from 'angular2/src/core/compiler/shadow_dom_strategy';
77

8-
import {DOM, Element} from 'angular2/src/facade/dom';
8+
import {DOM, Element, StyleElement} from 'angular2/src/facade/dom';
99
import {isPresent, isBlank, Type} from 'angular2/src/facade/lang';
1010

1111
export class ShimShadowCss extends CompileStep {
@@ -27,23 +27,24 @@ export class ShimShadowCss extends CompileStep {
2727
if (DOM.tagName(current.element) == 'STYLE') {
2828
current.ignoreBindings = true;
2929
if (this._strategy.extractStyles()) {
30-
DOM.remove(current.element);
31-
var css = DOM.getText(current.element);
30+
var styleEl = current.element;
31+
DOM.remove(styleEl);
32+
var css = DOM.getText(styleEl);
3233
var shimComponent = this._strategy.getShimComponent(this._component);
3334
css = shimComponent.shimCssText(css);
34-
this._insertStyle(this._styleHost, css);
35+
DOM.setText(styleEl, css);
36+
this._insertStyle(this._styleHost, styleEl);
3537
}
3638
}
3739
}
3840

39-
_insertStyle(el: Element, css: string) {
40-
var style = DOM.createStyleElement(css);
41+
_insertStyle(host: Element, style: StyleElement) {
4142
if (isBlank(this._lastInsertedStyle)) {
42-
var firstChild = DOM.firstChild(el);
43+
var firstChild = DOM.firstChild(host);
4344
if (isPresent(firstChild)) {
4445
DOM.insertBefore(firstChild, style);
4546
} else {
46-
DOM.appendChild(el, style);
47+
DOM.appendChild(host, style);
4748
}
4849
} else {
4950
DOM.insertAfter(this._lastInsertedStyle, style);

modules/angular2/test/core/compiler/pipeline/shim_shadow_css_spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {DirectiveMetadata} from 'angular2/src/core/compiler/directive_metadata';
99
import {ShadowDomStrategy} from 'angular2/src/core/compiler/shadow_dom_strategy';
1010

1111
import {Type} from 'angular2/src/facade/lang';
12+
import {DOM} from 'angular2/src/facade/dom';
1213

1314
export function main() {
1415
describe('ShimShadowCss', () => {
@@ -53,13 +54,23 @@ export function main() {
5354
expect(host).toHaveText('/* shim */.s{}original content');
5455
});
5556

57+
it('should preserve attributes on moved style', () => {
58+
var host = el('<div></div>');
59+
var pipeline = createPipeline(new FakeStrategy(true), host);
60+
var template = el('<div><style media="print">.s{}</style></div>');
61+
pipeline.process(template);
62+
var styleEl = DOM.firstChild(host);
63+
expect(DOM.getAttribute(styleEl, 'media')).toEqual('print');
64+
});
65+
5666
it('should move the styles to the host in the original order', () => {
5767
var host = el('<div></div>');
5868
var pipeline = createPipeline(new FakeStrategy(true), host);
5969
var template = el('<div><style>.s1{}</style><style>.s2{}</style></div>');
6070
pipeline.process(template);
6171
expect(host).toHaveText('/* shim */.s1{}/* shim */.s2{}');
6272
});
73+
6374
});
6475
}
6576

0 commit comments

Comments
 (0)