Skip to content

Commit ec39784

Browse files
authored
Merge pull request #34 from kevinzwhuang/master
Fix empty inline style attribute bug from breaking React renders
2 parents 997b433 + bf0ff06 commit ec39784

6 files changed

Lines changed: 46 additions & 3 deletions

File tree

karma.conf.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ module.exports = function(karma) {
1515
singleRun: false,
1616
frameworks: [
1717
'jasmine',
18-
'phantomjs-shim'
18+
'phantomjs-shim',
19+
'es6-shim'
1920
],
2021
preprocessors: {
2122
'tests.webpack.js': 'webpack'

package-lock.json

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"json-loader": "^0.5.4",
5959
"karma": "^0.13.22",
6060
"karma-coverage": "^1.0.0",
61+
"karma-es6-shim": "^1.0.0",
6162
"karma-jasmine": "^1.0.2",
6263
"karma-phantomjs-launcher": "^1.0.0",
6364
"karma-phantomjs-shim": "^1.4.0",

src/utils/generatePropsFromAttributes.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ export default function generatePropsFromAttributes(attributes, key) {
1212
// generate props
1313
const props = Object.assign({}, htmlAttributesToReact(attributes), { key });
1414

15-
// if there is a style prop then convert it to a React style object
16-
if (props.style) {
15+
// if there is an inline/string style prop then convert it to a React style object
16+
// otherwise, it is invalid and omitted
17+
if (typeof props.style === 'string' || props.style instanceof String) {
1718
props.style = inlineStyleToObject(props.style);
19+
} else {
20+
delete props.style;
1821
}
1922

2023
return props;

test/integration/integration.spec.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ describe('Integration tests: ', () => {
6868
test(`<div style="border-radius:1px;background:red${trailingSemiComma}">test</div>`);
6969
});
7070

71+
it('should ignore inline styles that are empty strings', () => {
72+
test('<div style="">test</div>', '<div>test</div>');
73+
});
74+
7175
it('should not allow nesting of void elements', () => {
7276
test('<img><p>test</p></img>', '<img/><p>test</p>');
7377
});

test/unit/utils/generatePropsFromAttributes.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,16 @@ describe('Testing `utils/generatePropsFromAttributes`', () => {
4545

4646
});
4747

48+
it('should return an object containing the converted style prop when the style html attribute is an empty string', () => {
49+
const attributes = {
50+
style: ''
51+
};
52+
53+
expect(generatePropsFromAttributes(attributes, 'style-key')).toEqual({
54+
style: 'converted-style',
55+
key: 'style-key'
56+
});
57+
expect(inlineStyleToObject).toHaveBeenCalledWith('');
58+
});
59+
4860
});

0 commit comments

Comments
 (0)