Skip to content

Commit

Permalink
fix(platform-server): generate correct stylings for camel case names (#…
Browse files Browse the repository at this point in the history
…22263)

* Add correct mapping from camel case to kebab case for CSS style
names
* Remove internal CSS methods in favor of native Domino APIs

Fixes #19235

PR Close #22263
  • Loading branch information
CaerusKaru authored and alexeagle committed Feb 27, 2018
1 parent d3827a0 commit 40ba009
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 38 deletions.
6 changes: 6 additions & 0 deletions packages/core/test/dom/dom_adapter_spec.ts
Expand Up @@ -74,6 +74,12 @@ import {el, stringifyElement} from '@angular/platform-browser/testing/src/browse
expect(getDOM().getStyle(d, 'background-url')).toBe('url(http://test.com/bg.jpg)');
});

it('should parse camel-case styles correctly', () => {
const d = getDOM().createElement('div');
getDOM().setStyle(d, 'marginRight', '10px');
expect(getDOM().getStyle(d, 'margin-right')).toBe('10px');
});

if (getDOM().supportsDOMEvents()) {
describe('getBaseHref', () => {
beforeEach(() => getDOM().resetBaseElement());
Expand Down
48 changes: 10 additions & 38 deletions packages/platform-server/src/domino_adapter.ts
Expand Up @@ -135,48 +135,20 @@ export class DominoAdapter extends BrowserDomAdapter {
return href;
}

/** @internal */
_readStyleAttribute(element: any) {
const styleMap = {};
const styleAttribute = element.getAttribute('style');
if (styleAttribute) {
const styleList = styleAttribute.split(/;+/g);
for (let i = 0; i < styleList.length; i++) {
if (styleList[i].length > 0) {
const style = styleList[i] as string;
const colon = style.indexOf(':');
if (colon === -1) {
throw new Error(`Invalid CSS style: ${style}`);
}
(styleMap as any)[style.substr(0, colon).trim()] = style.substr(colon + 1).trim();
}
}
}
return styleMap;
}
/** @internal */
_writeStyleAttribute(element: any, styleMap: any) {
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) {
const styleMap = this._readStyleAttribute(element);
(styleMap as any)[styleName] = styleValue;
this._writeStyleAttribute(element, styleMap);
styleName = styleName.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
element.style[styleName] = styleValue;
}
removeStyle(element: any, styleName: string) {
// IE requires '' instead of null
// see https://github.com/angular/angular/issues/7916
element.style[styleName] = '';
}
removeStyle(element: any, styleName: string) { this.setStyle(element, styleName, null); }
getStyle(element: any, styleName: string): string {
const styleMap = this._readStyleAttribute(element);
return styleMap.hasOwnProperty(styleName) ? (styleMap as any)[styleName] : '';
return element.style[styleName] || element.style.getPropertyValue(styleName);
}
hasStyle(element: any, styleName: string, styleValue?: string): boolean {
const value = this.getStyle(element, styleName) || '';
const value = this.getStyle(element, styleName);
return styleValue ? value == styleValue : value.length > 0;
}

Expand Down Expand Up @@ -206,4 +178,4 @@ export class DominoAdapter extends BrowserDomAdapter {
supportsCookies(): boolean { return false; }
getCookie(name: string): string { throw _notImplemented('getCookie'); }
setCookie(name: string, value: string) { throw _notImplemented('setCookie'); }
}
}

0 comments on commit 40ba009

Please sign in to comment.