Skip to content

Commit fc6dfc2

Browse files
CaerusKarukara
authored andcommitted
fix(platform-server): add styles to elements correctly (#22527)
* Partially reverts #22263 due to lack of total spec compliance on the server * Maintains the camel-case styles fix PR Close #22527
1 parent c0670ef commit fc6dfc2

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

packages/core/test/dom/dom_adapter_spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ import {el, stringifyElement} from '@angular/platform-browser/testing/src/browse
7474
expect(getDOM().getStyle(d, 'background-url')).toBe('url(http://test.com/bg.jpg)');
7575
});
7676

77+
// Test for regression caused by angular/angular#22536
78+
it('should parse styles correctly following the spec', () => {
79+
const d = getDOM().createElement('div');
80+
getDOM().setStyle(d, 'background-image', 'url("paper.gif")');
81+
expect(d.style.backgroundImage).toBe('url("paper.gif")');
82+
expect(d.style.getPropertyValue('background-image')).toBe('url("paper.gif")');
83+
expect(getDOM().getStyle(d, 'background-image')).toBe('url("paper.gif")');
84+
});
85+
7786
it('should parse camel-case styles correctly', () => {
7887
const d = getDOM().createElement('div');
7988
getDOM().setStyle(d, 'marginRight', '10px');

packages/platform-server/src/domino_adapter.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,17 +135,51 @@ export class DominoAdapter extends BrowserDomAdapter {
135135
return href;
136136
}
137137

138+
/** @internal */
139+
_readStyleAttribute(element: any): {[name: string]: string} {
140+
const styleMap: {[name: string]: string} = {};
141+
const styleAttribute = element.getAttribute('style');
142+
if (styleAttribute) {
143+
const styleList = styleAttribute.split(/;+/g);
144+
for (let i = 0; i < styleList.length; i++) {
145+
const style = styleList[i].trim();
146+
if (style.length > 0) {
147+
const colonIndex = style.indexOf(':');
148+
if (colonIndex === -1) {
149+
throw new Error(`Invalid CSS style: ${style}`);
150+
}
151+
const name = style.substr(0, colonIndex).trim();
152+
styleMap[name] = style.substr(colonIndex + 1).trim();
153+
}
154+
}
155+
}
156+
return styleMap;
157+
}
158+
/** @internal */
159+
_writeStyleAttribute(element: any, styleMap: {[name: string]: string}) {
160+
let styleAttrValue = '';
161+
for (const key in styleMap) {
162+
const newValue = styleMap[key];
163+
if (newValue) {
164+
styleAttrValue += key + ':' + styleMap[key] + ';';
165+
}
166+
}
167+
element.setAttribute('style', styleAttrValue);
168+
}
138169
setStyle(element: any, styleName: string, styleValue?: string|null) {
139170
styleName = styleName.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
140-
element.style[styleName] = styleValue;
171+
const styleMap = this._readStyleAttribute(element);
172+
styleMap[styleName] = styleValue || '';
173+
this._writeStyleAttribute(element, styleMap);
141174
}
142175
removeStyle(element: any, styleName: string) {
143176
// IE requires '' instead of null
144177
// see https://github.com/angular/angular/issues/7916
145-
element.style[styleName] = '';
178+
this.setStyle(element, styleName, '');
146179
}
147180
getStyle(element: any, styleName: string): string {
148-
return element.style[styleName] || element.style.getPropertyValue(styleName);
181+
const styleMap = this._readStyleAttribute(element);
182+
return styleMap[styleName] || '';
149183
}
150184
hasStyle(element: any, styleName: string, styleValue?: string): boolean {
151185
const value = this.getStyle(element, styleName);

0 commit comments

Comments
 (0)