Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit 1548727

Browse files
authored
fix(ngStyle): do not truncate URLs (#938)
Fixes #935
1 parent 23592ee commit 1548727

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

src/lib/extended/style/style-transforms.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
import {customMatchers, expect} from '../../utils/testing/custom-matchers';
99
import {
1010
NgStyleRawList,
11+
NgStyleKeyValue,
1112
NgStyleMap,
1213
buildRawList,
1314
buildMapFromList,
1415
buildMapFromSet,
16+
stringToKeyValue,
1517
} from './style-transforms';
1618

1719
describe('ngStyleUtils', () => {
@@ -74,4 +76,15 @@ describe('ngStyleUtils', () => {
7476
});
7577
});
7678

79+
it('should convert string correctly to key value with URLs', () => {
80+
const backgroundUrl = `background-url: url(${URL})`;
81+
const keyValue: NgStyleKeyValue = stringToKeyValue(backgroundUrl);
82+
expect(keyValue.key).toBe('background-url');
83+
expect(keyValue.value).toBe(`url(${URL})`);
84+
});
85+
7786
});
87+
88+
89+
const URL = 'https://cloud.githubusercontent.com/assets/210413/' +
90+
'21288118/917e3faa-c440-11e6-9b08-28aff590c7ae.png';

src/lib/extended/style/style-transforms.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ export function buildMapFromSet(source: NgStyleType, sanitize?: NgStyleSanitizer
7979

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

8686
/** Convert [ [key,value] ] -> { key : value } */

src/lib/extended/style/style.spec.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,18 @@ describe('style directive', () => {
145145
expectNativeEl(fixture, {fontSize: 19}).toHaveStyle({'font-size': '19px'}, styler);
146146
});
147147

148+
it('should work with URLs', () => {
149+
createTestComponent(`
150+
<div [ngStyle]="{'background-image': 'url(' + testUrl + ')', 'height': '300px'}">
151+
</div>
152+
`);
153+
fixture.detectChanges();
154+
const url = styler.lookupStyle(fixture.debugElement.children[0].nativeElement,
155+
'background-image');
156+
const isUrl = url === `url("${URL}")` || url === `url(${URL})`;
157+
expect(isUrl).toBeTruthy();
158+
});
159+
148160
it('should work with just ngStyle and preexisting styles', () => {
149161
createTestComponent(`
150162
<div style="background-color: red; height: 100px; width: 100px;" [ngStyle]="divStyle">
@@ -168,8 +180,9 @@ describe('style directive', () => {
168180
})
169181
class TestStyleComponent {
170182
fontSize: number = 0;
183+
testUrl = URL;
171184
divStyle = {'border': '2px solid green'};
172185
}
173186

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

0 commit comments

Comments
 (0)