2015-03-17 20:42:44 +00:00
|
|
|
/**
|
2015-03-23 22:07:33 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
2015-03-17 20:42:44 +00:00
|
|
|
*
|
|
|
|
* @providesModule StyleSheetValidation
|
2015-03-25 02:34:12 +00:00
|
|
|
* @flow
|
2015-03-17 20:42:44 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var ImageStylePropTypes = require('ImageStylePropTypes');
|
|
|
|
var TextStylePropTypes = require('TextStylePropTypes');
|
|
|
|
var ViewStylePropTypes = require('ViewStylePropTypes');
|
|
|
|
|
2016-03-02 12:27:13 +00:00
|
|
|
var invariant = require('fbjs/lib/invariant');
|
2015-03-17 20:42:44 +00:00
|
|
|
|
2017-02-08 22:38:37 +00:00
|
|
|
// Hardcoded because this is a legit case but we don't want to load it from
|
|
|
|
// a private API. We might likely want to unify style sheet creation with how it
|
|
|
|
// is done in the DOM so this might move into React. I know what I'm doing so
|
|
|
|
// plz don't fire me.
|
|
|
|
const ReactPropTypesSecret = 'SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED';
|
|
|
|
|
2015-03-17 20:42:44 +00:00
|
|
|
class StyleSheetValidation {
|
2017-11-11 05:03:52 +00:00
|
|
|
/* $FlowFixMe(>=0.59.0 site=react_native_fb) This comment suppresses an error
|
|
|
|
* caught by Flow 0.59 which was not caught before. Most likely, this error
|
|
|
|
* is because an exported function parameter is missing an annotation.
|
|
|
|
* Without an annotation, these parameters are uncovered by Flow. */
|
2015-03-17 20:42:44 +00:00
|
|
|
static validateStyleProp(prop, style, caller) {
|
|
|
|
if (!__DEV__) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (allStylePropTypes[prop] === undefined) {
|
|
|
|
var message1 = '"' + prop + '" is not a valid style property.';
|
|
|
|
var message2 = '\nValid style props: ' +
|
2015-06-15 20:27:20 +00:00
|
|
|
JSON.stringify(Object.keys(allStylePropTypes).sort(), null, ' ');
|
2017-10-03 03:59:10 +00:00
|
|
|
/* $FlowFixMe(>=0.56.0 site=react_native_oss) This comment suppresses an
|
|
|
|
* error found when Flow v0.56 was deployed. To see the error delete this
|
|
|
|
* comment and run Flow. */
|
2015-03-17 20:42:44 +00:00
|
|
|
styleError(message1, style, caller, message2);
|
|
|
|
}
|
|
|
|
var error = allStylePropTypes[prop](
|
|
|
|
style,
|
|
|
|
prop,
|
|
|
|
caller,
|
2017-02-08 22:38:37 +00:00
|
|
|
'prop',
|
2016-07-21 16:01:07 +00:00
|
|
|
null,
|
2017-02-08 22:38:37 +00:00
|
|
|
ReactPropTypesSecret,
|
2015-03-17 20:42:44 +00:00
|
|
|
);
|
|
|
|
if (error) {
|
2017-10-03 03:59:10 +00:00
|
|
|
/* $FlowFixMe(>=0.56.0 site=react_native_oss) This comment suppresses an
|
|
|
|
* error found when Flow v0.56 was deployed. To see the error delete this
|
|
|
|
* comment and run Flow. */
|
|
|
|
/* $FlowFixMe(>=0.56.0 site=react_native_fb,react_native_oss) This
|
|
|
|
* comment suppresses an error found when Flow v0.56 was deployed. To see
|
|
|
|
* the error delete this comment and run Flow. */
|
2015-03-17 20:42:44 +00:00
|
|
|
styleError(error.message, style, caller);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-11 05:03:52 +00:00
|
|
|
/* $FlowFixMe(>=0.59.0 site=react_native_fb) This comment suppresses an error
|
|
|
|
* caught by Flow 0.59 which was not caught before. Most likely, this error
|
|
|
|
* is because an exported function parameter is missing an annotation.
|
|
|
|
* Without an annotation, these parameters are uncovered by Flow. */
|
2015-03-17 20:42:44 +00:00
|
|
|
static validateStyle(name, styles) {
|
|
|
|
if (!__DEV__) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (var prop in styles[name]) {
|
|
|
|
StyleSheetValidation.validateStyleProp(prop, styles[name], 'StyleSheet ' + name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static addValidStylePropTypes(stylePropTypes) {
|
|
|
|
for (var key in stylePropTypes) {
|
|
|
|
allStylePropTypes[key] = stylePropTypes[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 02:34:12 +00:00
|
|
|
var styleError = function(message1, style, caller?, message2?) {
|
2015-03-17 20:42:44 +00:00
|
|
|
invariant(
|
|
|
|
false,
|
|
|
|
message1 + '\n' + (caller || '<<unknown>>') + ': ' +
|
|
|
|
JSON.stringify(style, null, ' ') + (message2 || '')
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
var allStylePropTypes = {};
|
|
|
|
|
|
|
|
StyleSheetValidation.addValidStylePropTypes(ImageStylePropTypes);
|
|
|
|
StyleSheetValidation.addValidStylePropTypes(TextStylePropTypes);
|
|
|
|
StyleSheetValidation.addValidStylePropTypes(ViewStylePropTypes);
|
|
|
|
|
|
|
|
module.exports = StyleSheetValidation;
|