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
This commit is contained in:
parent
c0670ef52d
commit
fc6dfc2e08
@ -74,6 +74,15 @@ import {el, stringifyElement} from '@angular/platform-browser/testing/src/browse
|
|||||||
expect(getDOM().getStyle(d, 'background-url')).toBe('url(http://test.com/bg.jpg)');
|
expect(getDOM().getStyle(d, 'background-url')).toBe('url(http://test.com/bg.jpg)');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Test for regression caused by angular/angular#22536
|
||||||
|
it('should parse styles correctly following the spec', () => {
|
||||||
|
const d = getDOM().createElement('div');
|
||||||
|
getDOM().setStyle(d, 'background-image', 'url("paper.gif")');
|
||||||
|
expect(d.style.backgroundImage).toBe('url("paper.gif")');
|
||||||
|
expect(d.style.getPropertyValue('background-image')).toBe('url("paper.gif")');
|
||||||
|
expect(getDOM().getStyle(d, 'background-image')).toBe('url("paper.gif")');
|
||||||
|
});
|
||||||
|
|
||||||
it('should parse camel-case styles correctly', () => {
|
it('should parse camel-case styles correctly', () => {
|
||||||
const d = getDOM().createElement('div');
|
const d = getDOM().createElement('div');
|
||||||
getDOM().setStyle(d, 'marginRight', '10px');
|
getDOM().setStyle(d, 'marginRight', '10px');
|
||||||
|
@ -135,17 +135,51 @@ export class DominoAdapter extends BrowserDomAdapter {
|
|||||||
return href;
|
return href;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @internal */
|
||||||
|
_readStyleAttribute(element: any): {[name: string]: string} {
|
||||||
|
const styleMap: {[name: string]: string} = {};
|
||||||
|
const styleAttribute = element.getAttribute('style');
|
||||||
|
if (styleAttribute) {
|
||||||
|
const styleList = styleAttribute.split(/;+/g);
|
||||||
|
for (let i = 0; i < styleList.length; i++) {
|
||||||
|
const style = styleList[i].trim();
|
||||||
|
if (style.length > 0) {
|
||||||
|
const colonIndex = style.indexOf(':');
|
||||||
|
if (colonIndex === -1) {
|
||||||
|
throw new Error(`Invalid CSS style: ${style}`);
|
||||||
|
}
|
||||||
|
const name = style.substr(0, colonIndex).trim();
|
||||||
|
styleMap[name] = style.substr(colonIndex + 1).trim();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return styleMap;
|
||||||
|
}
|
||||||
|
/** @internal */
|
||||||
|
_writeStyleAttribute(element: any, styleMap: {[name: string]: string}) {
|
||||||
|
let styleAttrValue = '';
|
||||||
|
for (const key in styleMap) {
|
||||||
|
const newValue = styleMap[key];
|
||||||
|
if (newValue) {
|
||||||
|
styleAttrValue += key + ':' + styleMap[key] + ';';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
element.setAttribute('style', styleAttrValue);
|
||||||
|
}
|
||||||
setStyle(element: any, styleName: string, styleValue?: string|null) {
|
setStyle(element: any, styleName: string, styleValue?: string|null) {
|
||||||
styleName = styleName.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
|
styleName = styleName.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
|
||||||
element.style[styleName] = styleValue;
|
const styleMap = this._readStyleAttribute(element);
|
||||||
|
styleMap[styleName] = styleValue || '';
|
||||||
|
this._writeStyleAttribute(element, styleMap);
|
||||||
}
|
}
|
||||||
removeStyle(element: any, styleName: string) {
|
removeStyle(element: any, styleName: string) {
|
||||||
// IE requires '' instead of null
|
// IE requires '' instead of null
|
||||||
// see https://github.com/angular/angular/issues/7916
|
// see https://github.com/angular/angular/issues/7916
|
||||||
element.style[styleName] = '';
|
this.setStyle(element, styleName, '');
|
||||||
}
|
}
|
||||||
getStyle(element: any, styleName: string): string {
|
getStyle(element: any, styleName: string): string {
|
||||||
return element.style[styleName] || element.style.getPropertyValue(styleName);
|
const styleMap = this._readStyleAttribute(element);
|
||||||
|
return styleMap[styleName] || '';
|
||||||
}
|
}
|
||||||
hasStyle(element: any, styleName: string, styleValue?: string): boolean {
|
hasStyle(element: any, styleName: string, styleValue?: string): boolean {
|
||||||
const value = this.getStyle(element, styleName);
|
const value = this.getStyle(element, styleName);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user