Skip to content

Commit 46d96ec

Browse files
author
Nir Maoz
authored
Fix/dont rename non cloudinary props (#181)
1 parent 57084e1 commit 46d96ec

File tree

4 files changed

+59
-32
lines changed

4 files changed

+59
-32
lines changed

src/components/Image/Image.js

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -55,28 +55,25 @@ class Image extends CloudinaryComponent {
5555
const options = {...this.getOptions(), ...additionalOptions};
5656
const {nonCloudinaryProps} = extractCloudinaryProps(options);
5757

58-
let attributes = {...getImageTag(options).attributes(), ...nonCloudinaryProps};
58+
// React requires camelCase instead of snake_case attributes
59+
const attributes = ({...Util.withCamelCaseKeys(getImageTag(options).attributes()), ...nonCloudinaryProps});
5960

60-
//React requires camelCase instead of snake_case attributes
61-
attributes = Util.withCamelCaseKeys(attributes);
61+
// We want to keep 'data-src' if it exists
62+
if (attributes.dataSrc) {
63+
attributes['data-src'] = attributes.dataSrc;
64+
}
6265

6366
// Set placeholder Id
6467
if (placeholder && attributes.id) {
6568
attributes.id = attributes.id + '-cld-placeholder';
6669
}
6770

68-
// Set dataSrc if lazy loading and not in view
71+
// Set data-src if lazy loading and not in view
6972
if (!isInView && this.shouldLazyLoad(options)) {
70-
attributes.dataSrc = attributes.dataSrc || attributes.src;
73+
attributes['data-src'] = attributes.dataSrc || attributes.src;
7174
delete attributes.src;
7275
}
7376

74-
// The data-src attribute was turned into dataSrc by the camelCase function,
75-
// But it's needed by cloudinary-core's responsive() function. Notice that it's not snake_case.
76-
if (attributes.dataSrc) {
77-
attributes['data-src'] = attributes.dataSrc;
78-
}
79-
8077
// Remove unneeded attributes,
8178
['dataSrc', 'accessibility', 'placeholder', 'breakpoints'].forEach(attr => {
8279
delete attributes[attr];
@@ -155,31 +152,38 @@ class Image extends CloudinaryComponent {
155152
});
156153
};
157154

155+
renderPlaceholder = (placeholder, attributes) => {
156+
attributes.style = {...(attributes.style || {}), opacity: 0, position: 'absolute'}
157+
attributes.onLoad = this.handleImageLoaded;
158+
const placeholderWrapperStyle = {display: 'inline'}
159+
const placeholderAttributes = this.getAttributes({placeholder: placeholder.props.type});
160+
161+
return (
162+
<Fragment>
163+
{this.renderImage(attributes)}
164+
<div style={placeholderWrapperStyle}>
165+
<img {...placeholderAttributes}/>
166+
</div>
167+
</Fragment>
168+
);
169+
};
170+
171+
renderImage = (attributes) => {
172+
return <img ref={this.attachRef} {...attributes}/>
173+
}
174+
158175
render() {
159-
const {attachRef, getAttributes, handleImageLoaded} = this;
176+
const {isLoaded} = this.state;
160177
const {children} = this.getExtendedProps();
161178
const placeholder = this.getChildPlaceholder(children);
162-
const {isLoaded} = this.state;
163-
164-
const attributes = getAttributes();
179+
const attributes = this.getAttributes();
165180

166-
//If image wasn't loaded and there's a placeholder then we render it alongside the image.
181+
//If image wasn't loaded and there's a child placeholder then we render it.
167182
if (!isLoaded && placeholder) {
168-
const placeholderStyle = {display: isLoaded ? 'none' : 'inline'}
169-
attributes.style = {...(attributes.style || {}), opacity: 0, position: 'absolute'}
170-
attributes.onLoad = handleImageLoaded;
171-
const placeholderAttributes = getAttributes({placeholder: placeholder.props.type});
172-
173-
return (
174-
<Fragment>
175-
<img ref={attachRef} {...attributes} />
176-
<div style={placeholderStyle}>
177-
<img {...placeholderAttributes}/>
178-
</div>
179-
</Fragment>
180-
);
183+
return this.renderPlaceholder(placeholder, attributes);
181184
}
182-
return <img ref={attachRef} {...attributes}/>
185+
186+
return this.renderImage(attributes);
183187
}
184188
}
185189

src/components/Video/Video.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22
import PropTypes from 'prop-types';
33
import {Cloudinary, Util} from 'cloudinary-core';
44
import CloudinaryComponent from '../CloudinaryComponent';
5+
import extractCloudinaryProps from "../../Util/extractCloudinaryProps";
56

67
/**
78
* A component representing a Cloudinary served video
@@ -70,11 +71,16 @@ class Video extends CloudinaryComponent {
7071
} = this.getMergedProps();
7172

7273
options = CloudinaryComponent.normalizeOptions(options, {});
74+
const {cloudinaryProps, cloudinaryReactProps, nonCloudinaryProps} = extractCloudinaryProps(options);
75+
options = {...cloudinaryProps, ...cloudinaryReactProps};
76+
77+
//const snakeCaseOptions = toSnakeCaseKeys(options);
7378
const snakeCaseOptions = Util.withSnakeCaseKeys(options);
7479
const cld = Cloudinary.new(snakeCaseOptions);
7580

76-
// Let cloudinary-core generate this video tag attributes
77-
const tagAttributes = Util.withCamelCaseKeys(cld.videoTag(publicId, options).attributes());
81+
// Use cloudinary-core to generate this video tag's attributes
82+
let tagAttributes = cld.videoTag(publicId, options).attributes();
83+
tagAttributes = {...Util.withCamelCaseKeys(tagAttributes), ...nonCloudinaryProps};
7884

7985
// Aggregate child transformations, used for generating <source> tags for this video element
8086
const childTransformations = this.getTransformation({...options, children});

test/ImageTest.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ describe('Image', () => {
9494

9595
expect(tag.find('img').prop('src')).to.match(/fn_wasm:blur.wasm\/sample/);
9696
});
97+
it('should not change kebab-case param names', () => {
98+
let tag = mount(
99+
<Image publicId="sample" cloudName="demo" data-testid="testing"/>
100+
);
101+
102+
expect(tag.find('img').prop('data-testid')).to.equal('testing');
103+
expect(tag.find('img').prop('datatestid')).to.equal(undefined);
104+
});
97105
it('should update on prop change', () => {
98106
let tag = mount(<Image publicId="sample" cloudName="demo"/>);
99107
expect(tag.find('img').prop('src')).to.equal('http://res.cloudinary.com/demo/image/upload/sample');

test/VideoTest.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,13 @@ describe('Video', () => {
190190
expect(video.prop('plays_inline')).to.equal(undefined);
191191
expect(video.prop('auto_play')).to.equal(undefined);
192192
});
193+
it('should not change kebab-case param names', () => {
194+
let tag = mount(
195+
<Video publicId="dog" cloudName="demo" data-testid="testing"/>
196+
);
197+
198+
const video = tag.find('video');
199+
expect(video.prop('data-testid')).to.equal("testing");
200+
expect(video.prop('datatestid')).to.equal(undefined);
201+
});
193202
});

0 commit comments

Comments
 (0)