Skip to content

Commit b7829b4

Browse files
authored
Fix user variables (#276)
1 parent f487e8f commit b7829b4

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

projects/angular-cld/src/lib/cloudinary-image.component.spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,39 @@ describe('CloudinaryImage', () => {
308308
});
309309
});
310310

311+
describe('should support user variables', () => {
312+
@Component({
313+
template:
314+
`
315+
<cl-image public-id="sample">
316+
<cl-transformation variables="[['$imgWidth','150']]"></cl-transformation>
317+
<cl-transformation width="$imgWidth" crop="crop" ></cl-transformation>
318+
</cl-image>
319+
`
320+
})
321+
class TestComponent { }
322+
323+
let fixture: ComponentFixture<TestComponent>;
324+
let des: DebugElement; // the elements w/ the directive
325+
326+
beforeEach(() => {
327+
fixture = TestBed.configureTestingModule({
328+
declarations: [CloudinaryTransformationDirective, CloudinaryImage, TestComponent],
329+
providers: [{ provide: Cloudinary, useValue: localCloudinary }]
330+
}).createComponent(TestComponent);
331+
332+
fixture.detectChanges(); // initial binding
333+
334+
// Our element under test, which is attached to CloudinaryImage
335+
des = fixture.debugElement.query(By.directive(CloudinaryImage));
336+
});
337+
338+
it('creates an img element with user variables', () => {
339+
const img = des.children[0].nativeElement as HTMLImageElement;
340+
expect(img.attributes.getNamedItem('src').value).toEqual('http://res.cloudinary.com/@@fake_angular2_sdk@@/image/upload/$imgWidth_150/c_crop,w_$imgWidth/sample');
341+
});
342+
});
343+
311344
describe('Sample code presented in README', () => {
312345
@Component({
313346
template:

projects/angular-cld/src/lib/cloudinary.service.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ const isJsonLikeString = function (str: any): boolean {
1515
return str && typeof str === 'string' && (str.trim().match(/^{[\s\S]*?}$/) !== null);
1616
};
1717

18+
const isArrayLikeString = function (str: any): boolean {
19+
return str && typeof str === 'string' && (str.trim().match(/^\[[\s\S]*?]$/) !== null);
20+
};
21+
1822
const isNamedNodeMap = function (obj: any): boolean {
1923
return obj && (obj.constructor.name === 'NamedNodeMap' || obj instanceof NamedNodeMap);
2024
};
@@ -31,9 +35,13 @@ const namedNodeMapToObject = function (source: NamedNodeMap): any {
3135

3236
const transformKeyNames = function (obj: any): any {
3337
let _obj = obj;
34-
if (isJsonLikeString(obj)) {
38+
if (isJsonLikeString(obj) || isArrayLikeString(obj)) {
3539
// Given attribute value is in the form of a JSON object -
36-
// Transforms the string into an object, as the Javascript API expects
40+
// Transforms the string into an object or array, as the Javascript API expects
41+
42+
if (isArrayLikeString(obj)) {
43+
obj = obj.replace(/'/g, '"');
44+
}
3745
_obj = JSON.parse(obj);
3846
} else if (isNamedNodeMap(obj)) {
3947
_obj = namedNodeMapToObject(obj);

0 commit comments

Comments
 (0)