Skip to content

Commit

Permalink
fix(ngStyle): do not truncate URLs (#938)
Browse files Browse the repository at this point in the history
Fixes #935
  • Loading branch information
CaerusKaru committed Dec 18, 2018
1 parent 23592ee commit 1548727
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
13 changes: 13 additions & 0 deletions src/lib/extended/style/style-transforms.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
import {customMatchers, expect} from '../../utils/testing/custom-matchers';
import {
NgStyleRawList,
NgStyleKeyValue,
NgStyleMap,
buildRawList,
buildMapFromList,
buildMapFromSet,
stringToKeyValue,
} from './style-transforms';

describe('ngStyleUtils', () => {
Expand Down Expand Up @@ -74,4 +76,15 @@ describe('ngStyleUtils', () => {
});
});

it('should convert string correctly to key value with URLs', () => {
const backgroundUrl = `background-url: url(${URL})`;
const keyValue: NgStyleKeyValue = stringToKeyValue(backgroundUrl);
expect(keyValue.key).toBe('background-url');
expect(keyValue.value).toBe(`url(${URL})`);
});

});


const URL = 'https://cloud.githubusercontent.com/assets/210413/' +
'21288118/917e3faa-c440-11e6-9b08-28aff590c7ae.png';
4 changes: 2 additions & 2 deletions src/lib/extended/style/style-transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ export function buildMapFromSet(source: NgStyleType, sanitize?: NgStyleSanitizer

/** Convert 'key:value' -> [key, value] */
export function stringToKeyValue(it: string): NgStyleKeyValue {
let [key, val] = it.split(':');
return new NgStyleKeyValue(key, val);
const [key, ...vals] = it.split(':');
return new NgStyleKeyValue(key, vals.join(':'));
}

/** Convert [ [key,value] ] -> { key : value } */
Expand Down
17 changes: 15 additions & 2 deletions src/lib/extended/style/style.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,18 @@ describe('style directive', () => {
expectNativeEl(fixture, {fontSize: 19}).toHaveStyle({'font-size': '19px'}, styler);
});

it('should work with URLs', () => {
createTestComponent(`
<div [ngStyle]="{'background-image': 'url(' + testUrl + ')', 'height': '300px'}">
</div>
`);
fixture.detectChanges();
const url = styler.lookupStyle(fixture.debugElement.children[0].nativeElement,
'background-image');
const isUrl = url === `url("${URL}")` || url === `url(${URL})`;
expect(isUrl).toBeTruthy();
});

it('should work with just ngStyle and preexisting styles', () => {
createTestComponent(`
<div style="background-color: red; height: 100px; width: 100px;" [ngStyle]="divStyle">
Expand All @@ -168,8 +180,9 @@ describe('style directive', () => {
})
class TestStyleComponent {
fontSize: number = 0;
testUrl = URL;
divStyle = {'border': '2px solid green'};
}



const URL = 'https://cloud.githubusercontent.com/assets/210413/' +
'21288118/917e3faa-c440-11e6-9b08-28aff590c7ae.png';

0 comments on commit 1548727

Please sign in to comment.