Skip to content

Commit 5dae6e4

Browse files
Ensure that custom attributes (data and aria) are kept as props
Update `attributesToProps` helper to set the custom attributes if found. Use React's `HTMLDOMPropertyConfig.isCustomAttribute` method to validate the custom attribute. Currently, only `data-` and `aria-` are valid custom attributes in React. https://github.com/facebook/react/blob/master/src/renderers/dom/shared/HTMLDOMPropertyConfig.js#L25 Update tests to verify the helper is not stripping out the custom attributes. Also, add test that makes sure weird capitalization in attribute names does not affect the final output.
1 parent b2ba851 commit 5dae6e4

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

lib/attributes-to-props.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/**
44
* Module dependencies.
55
*/
6+
var HTMLDOMPropertyConfig = require('react/lib/HTMLDOMPropertyConfig');
67
var utilities = require('./utilities');
78
var propertyConfig = require('./property-config');
89

@@ -16,14 +17,22 @@ function attributesToProps(attributes) {
1617
attributes = attributes || {};
1718
var props = {};
1819
var propertyName;
20+
var propertyValue;
1921
var reactProperty;
2022

21-
// convert DOM attribute/property to React attribute/property
2223
for (propertyName in attributes) {
23-
propertyName = propertyName.toLowerCase();
24-
reactProperty = propertyConfig[propertyName]
24+
propertyValue = attributes[propertyName];
25+
26+
// custom attributes (`data-` and `aria-`)
27+
if (HTMLDOMPropertyConfig.isCustomAttribute(propertyName)) {
28+
props[propertyName] = propertyValue;
29+
continue;
30+
}
31+
32+
// make DOM attribute/property consistent with React attribute/property
33+
reactProperty = propertyConfig[propertyName.toLowerCase()]
2534
if (reactProperty) {
26-
props[reactProperty] = attributes[propertyName];
35+
props[reactProperty] = propertyValue;
2736
}
2837
}
2938

test/attributes-to-props.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,36 @@ describe('attributes to props helper', function() {
6767
);
6868
});
6969

70+
it('keeps data- and aria- attributes as is', function() {
71+
assert.deepEqual(
72+
attributesToProps({
73+
'data-foo': 'bar',
74+
'aria-live': 'polite'
75+
}),
76+
{
77+
'data-foo': 'bar',
78+
'aria-live': 'polite'
79+
}
80+
);
81+
});
82+
83+
it('converts DOM attributes with weird capitalization', function() {
84+
assert.deepEqual(
85+
attributesToProps({
86+
'ACCEPT-CHARSET': 'ISO-8859-1',
87+
formNOvalidate: true,
88+
sEcUrItY: 'restricted',
89+
'data-FOO': 'bar'
90+
}),
91+
{
92+
acceptCharset: 'ISO-8859-1',
93+
formNoValidate: true,
94+
security: 'restricted',
95+
'data-FOO': 'bar'
96+
}
97+
);
98+
});
99+
70100
it('converts CSS style string to JS style object', function() {
71101
// proper css
72102
assert.deepEqual(

0 commit comments

Comments
 (0)