Skip to content

Commit

Permalink
fix(platform-server): handle styles with extra ':'s correctly (#15189)
Browse files Browse the repository at this point in the history
Previously, style values were parsed with a regex that split on /:+/.

This causes errors for CSS such as

div {
  background-url: url(http://server.com/img.png);
}

since the regex would split the background-url line into 3 values instead of 2.

Now, the : character is detected with indexOf, avoiding this error.

A test was added to verify the behavior is correct.
  • Loading branch information
alxhub authored and chuckjaz committed Mar 16, 2017
1 parent 6e98757 commit 013d806
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
6 changes: 6 additions & 0 deletions packages/core/test/dom/dom_adapter_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ export function main() {
expect(() => getDOM().remove(d)).not.toThrow();
});

it('should parse styles with urls correctly', () => {
const d = getDOM().createElement('div');
getDOM().setStyle(d, 'background-url', 'url(http://test.com/bg.jpg)');
expect(getDOM().getStyle(d, 'background-url')).toBe('url(http://test.com/bg.jpg)');
});

if (getDOM().supportsDOMEvents()) {
describe('getBaseHref', () => {
beforeEach(() => getDOM().resetBaseElement());
Expand Down
8 changes: 6 additions & 2 deletions packages/platform-server/src/parse5_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,12 @@ export class Parse5DomAdapter extends DomAdapter {
const styleList = styleAttrValue.split(/;+/g);
for (let i = 0; i < styleList.length; i++) {
if (styleList[i].length > 0) {
const elems = styleList[i].split(/:+/g);
(styleMap as any)[elems[0].trim()] = elems[1].trim();
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();
}
}
}
Expand Down

0 comments on commit 013d806

Please sign in to comment.