Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn about unknown property values #267

Merged
merged 1 commit into from
Sep 7, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/addons/transitions/ReactTransitionGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,12 @@ var ReactTransitionGroup = React.createClass({
render: function() {
return this.transferPropsTo(
this.props.component(
null,
{
transitionName: null,
transitionEnter: null,
transitionLeave: null,
component: null
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@petehunt might actually complain about this and opt for setting each individually to avoid allocating an object here (though it gets allocated in the constructor anyway so it's probably not the end of the world.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I figured that the constructor will allocate the same object so it doesn't make a difference.

this.renderTransitionableChildren(this.props.children)
)
);
Expand Down
12 changes: 11 additions & 1 deletion src/dom/DOMProperty.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,11 @@ var DOMPropertyInjection = {

DOMProperty.isStandardName[propName] = true;

var lowerCased = propName.toLowerCase();
DOMProperty.getPossibleStandardName[lowerCased] = propName;

DOMProperty.getAttributeName[propName] =
DOMAttributeNames[propName] || propName.toLowerCase();
DOMAttributeNames[propName] || lowerCased;

DOMProperty.getPropertyName[propName] =
DOMPropertyNames[propName] || propName;
Expand Down Expand Up @@ -141,6 +144,13 @@ var DOMProperty = {
*/
isStandardName: {},

/**
* Mapping from lowercase property names to the properly cased version, used
* to warn in the case of missing properties.
* @type {Object}
*/
getPossibleStandardName: {},

/**
* Mapping from normalized names to attribute names that differ. Attribute
* names are used when rendering markup or with `*Attribute()`.
Expand Down
39 changes: 39 additions & 0 deletions src/dom/DOMPropertyOperations.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,36 @@ var processAttributeNameAndPrefix = memoizeStringOnly(function(name) {
return escapeTextForBrowser(name) + '="';
});

if (__DEV__) {
var reactProps = {
'{owner}': true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should maybe just export this from ReactComponent, but then requiring it might lead to some circular dependencies :/

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The proper fix for this is probably to move {owner} out of props -- its presence is sometimes confusing already, especially when implementing shouldComponentUpdate.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. This needs to get out of props at some point. Definitely. I think it's fine to do that separately.

children: true,
dangerouslySetInnerHTML: true,
key: true,
ref: true
};
var warnedProperties = {};

var warnUnknownProperty = function(name) {
if (reactProps[name] || warnedProperties[name]) {
return;
}

warnedProperties[name] = true;
var message = 'Unknown DOM property ' + name + '.';
var lowerCasedName = name.toLowerCase();

// data-* attributes should be lowercase; suggest the lowercase version
var standardName = DOMProperty.isCustomAttribute(lowerCasedName) ?
lowerCasedName : DOMProperty.getPossibleStandardName[lowerCasedName];
if (standardName != null) {
message += ' Did you mean ' + standardName + '?';
}

console.warn(message);
};
}

/**
* Operations for dealing with DOM properties.
*/
Expand Down Expand Up @@ -55,6 +85,9 @@ var DOMPropertyOperations = {
return processAttributeNameAndPrefix(name) +
escapeTextForBrowser(value) + '"';
} else {
if (__DEV__) {
warnUnknownProperty(name);
}
return null;
}
},
Expand Down Expand Up @@ -85,6 +118,10 @@ var DOMPropertyOperations = {
}
} else if (DOMProperty.isCustomAttribute(name)) {
node.setAttribute(name, value);
} else {
if (__DEV__) {
warnUnknownProperty(name);
}
}
},

Expand All @@ -110,6 +147,8 @@ var DOMPropertyOperations = {
}
} else if (DOMProperty.isCustomAttribute(name)) {
node.removeAttribute(name);
} else if (__DEV__) {
warnUnknownProperty(name);
}
}

Expand Down
16 changes: 16 additions & 0 deletions src/dom/__tests__/DOMPropertyOperations-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ describe('DOMPropertyOperations', function() {
)).toBe('id="simple"');
});

it('should warn about incorrect casing', function() {
spyOn(console, 'warn');
expect(DOMPropertyOperations.createMarkupForProperty(
'tabindex',
'1'
)).toBe(null);
expect(console.warn.argsForCall.length).toBe(1);
expect(console.warn.argsForCall[0][0]).toContain('tabIndex');
});

it('should create markup for boolean properties', function() {
expect(DOMPropertyOperations.createMarkupForProperty(
'checked',
Expand Down Expand Up @@ -141,18 +151,24 @@ describe('DOMPropertyOperations', function() {

describe('injectDOMPropertyConfig', function() {
it('should support custom attributes', function() {
spyOn(console, 'warn');

// foobar does not exist yet
expect(DOMPropertyOperations.createMarkupForProperty(
'foobar',
'simple'
)).toBe(null);

expect(console.warn.argsForCall.length).toBe(1);

// foo-* does not exist yet
expect(DOMPropertyOperations.createMarkupForProperty(
'foo-xyz',
'simple'
)).toBe(null);

expect(console.warn.argsForCall.length).toBe(2);

// inject foobar DOM property
DOMProperty.injection.injectDOMPropertyConfig({
isCustomAttribute: function(name) {
Expand Down
2 changes: 2 additions & 0 deletions src/dom/components/ReactDOMInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ var ReactDOMInput = ReactCompositeComponent.createClass({
// Clone `this.props` so we don't mutate the input.
var props = merge(this.props);

props.defaultChecked = null;
props.defaultValue = null;
props.checked =
this.props.checked != null ? this.props.checked : this.state.checked;
// Cast `this.props.value` to a string so equality checks pass.
Expand Down
1 change: 1 addition & 0 deletions src/dom/components/ReactDOMTextarea.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ var ReactDOMTextarea = ReactCompositeComponent.createClass({
'`dangerouslySetInnerHTML` does not make sense on <textarea>.'
);

props.defaultValue = null;
props.value =
this.props.value != null ? this.props.value : this.state.value;
props.onChange = this._handleChange;
Expand Down