glamor
Version:
inline css for component systems
162 lines (133 loc) • 6.67 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.processStyleName = undefined;
exports.createMarkupForStyles = createMarkupForStyles;
var _camelizeStyleName = require('fbjs/lib/camelizeStyleName');
var _camelizeStyleName2 = _interopRequireDefault(_camelizeStyleName);
var _dangerousStyleValue = require('./dangerousStyleValue');
var _dangerousStyleValue2 = _interopRequireDefault(_dangerousStyleValue);
var _hyphenateStyleName = require('fbjs/lib/hyphenateStyleName');
var _hyphenateStyleName2 = _interopRequireDefault(_hyphenateStyleName);
var _memoizeStringOnly = require('fbjs/lib/memoizeStringOnly');
var _memoizeStringOnly2 = _interopRequireDefault(_memoizeStringOnly);
var _warning = require('fbjs/lib/warning');
var _warning2 = _interopRequireDefault(_warning);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var processStyleName = exports.processStyleName = (0, _memoizeStringOnly2.default)(_hyphenateStyleName2.default); /**
* Copyright 2013-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.
*
* @providesModule CSSPropertyOperations
*/
if (process.env.NODE_ENV !== 'production') {
// 'msTransform' is correct, but the other prefixes should be capitalized
var badVendoredStyleNamePattern = /^(?:webkit|moz|o)[A-Z]/;
// style values shouldn't contain a semicolon
var badStyleValueWithSemicolonPattern = /;\s*$/;
var warnedStyleNames = {};
var warnedStyleValues = {};
var warnedForNaNValue = false;
var warnHyphenatedStyleName = function warnHyphenatedStyleName(name, owner) {
if (warnedStyleNames.hasOwnProperty(name) && warnedStyleNames[name]) {
return;
}
warnedStyleNames[name] = true;
process.env.NODE_ENV !== 'production' ? (0, _warning2.default)(false, 'Unsupported style property %s. Did you mean %s?%s', name, (0, _camelizeStyleName2.default)(name), checkRenderMessage(owner)) : void 0;
};
var warnBadVendoredStyleName = function warnBadVendoredStyleName(name, owner) {
if (warnedStyleNames.hasOwnProperty(name) && warnedStyleNames[name]) {
return;
}
warnedStyleNames[name] = true;
process.env.NODE_ENV !== 'production' ? (0, _warning2.default)(false, 'Unsupported vendor-prefixed style property %s. Did you mean %s?%s', name, name.charAt(0).toUpperCase() + name.slice(1), checkRenderMessage(owner)) : void 0;
};
var warnStyleValueWithSemicolon = function warnStyleValueWithSemicolon(name, value, owner) {
if (warnedStyleValues.hasOwnProperty(value) && warnedStyleValues[value]) {
return;
}
warnedStyleValues[value] = true;
process.env.NODE_ENV !== 'production' ? (0, _warning2.default)(false, 'Style property values shouldn\'t contain a semicolon.%s ' + 'Try "%s: %s" instead.', checkRenderMessage(owner), name, value.replace(badStyleValueWithSemicolonPattern, '')) : void 0;
};
var warnStyleValueIsNaN = function warnStyleValueIsNaN(name, value, owner) {
if (warnedForNaNValue) {
return;
}
warnedForNaNValue = true;
process.env.NODE_ENV !== 'production' ? (0, _warning2.default)(false, '`NaN` is an invalid value for the `%s` css style property.%s', name, checkRenderMessage(owner)) : void 0;
};
var checkRenderMessage = function checkRenderMessage(owner) {
if (owner) {
var name = owner.getName();
if (name) {
return ' Check the render method of `' + name + '`.';
}
}
return '';
};
/**
* @param {string} name
* @param {*} value
* @param {ReactDOMComponent} component
*/
var warnValidStyle = function warnValidStyle(name, value, component) {
//eslint-disable-line no-var
var owner = void 0;
if (component) {
owner = component._currentElement._owner;
}
if (name.indexOf('-') > -1) {
warnHyphenatedStyleName(name, owner);
} else if (badVendoredStyleNamePattern.test(name)) {
warnBadVendoredStyleName(name, owner);
} else if (badStyleValueWithSemicolonPattern.test(value)) {
warnStyleValueWithSemicolon(name, value, owner);
}
if (typeof value === 'number' && isNaN(value)) {
warnStyleValueIsNaN(name, value, owner);
}
};
}
/**
* Serializes a mapping of style properties for use as inline styles:
*
* > createMarkupForStyles({width: '200px', height: 0})
* "width:200px;height:0;"
*
* Undefined values are ignored so that declarative programming is easier.
* The result should be HTML-escaped before insertion into the DOM.
*
* @param {object} styles
* @param {ReactDOMComponent} component
* @return {?string}
*/
function createMarkupForStyles(styles, component) {
var serialized = '';
for (var styleName in styles) {
var isCustomProp = styleName.indexOf('--') === 0;
if (!styles.hasOwnProperty(styleName)) {
continue;
}
if (styleName === 'label') {
continue;
}
var styleValue = styles[styleName];
if (process.env.NODE_ENV !== 'production' && !isCustomProp) {
warnValidStyle(styleName, styleValue, component);
}
if (styleValue != null) {
if (isCustomProp) {
serialized += styleName + ':' + styleValue + ';';
} else {
serialized += processStyleName(styleName) + ':';
serialized += (0, _dangerousStyleValue2.default)(styleName, styleValue, component) + ';';
}
}
}
return serialized || null;
}