react / react-0.13.3 / examples / basic-commonjs / node_modules / react / lib / CSSPropertyOperations.js
81143 views/**1* Copyright 2013-2015, Facebook, Inc.2* All rights reserved.3*4* This source code is licensed under the BSD-style license found in the5* LICENSE file in the root directory of this source tree. An additional grant6* of patent rights can be found in the PATENTS file in the same directory.7*8* @providesModule CSSPropertyOperations9* @typechecks static-only10*/1112'use strict';1314var CSSProperty = require("./CSSProperty");15var ExecutionEnvironment = require("./ExecutionEnvironment");1617var camelizeStyleName = require("./camelizeStyleName");18var dangerousStyleValue = require("./dangerousStyleValue");19var hyphenateStyleName = require("./hyphenateStyleName");20var memoizeStringOnly = require("./memoizeStringOnly");21var warning = require("./warning");2223var processStyleName = memoizeStringOnly(function(styleName) {24return hyphenateStyleName(styleName);25});2627var styleFloatAccessor = 'cssFloat';28if (ExecutionEnvironment.canUseDOM) {29// IE8 only supports accessing cssFloat (standard) as styleFloat30if (document.documentElement.style.cssFloat === undefined) {31styleFloatAccessor = 'styleFloat';32}33}3435if ("production" !== process.env.NODE_ENV) {36// 'msTransform' is correct, but the other prefixes should be capitalized37var badVendoredStyleNamePattern = /^(?:webkit|moz|o)[A-Z]/;3839// style values shouldn't contain a semicolon40var badStyleValueWithSemicolonPattern = /;\s*$/;4142var warnedStyleNames = {};43var warnedStyleValues = {};4445var warnHyphenatedStyleName = function(name) {46if (warnedStyleNames.hasOwnProperty(name) && warnedStyleNames[name]) {47return;48}4950warnedStyleNames[name] = true;51("production" !== process.env.NODE_ENV ? warning(52false,53'Unsupported style property %s. Did you mean %s?',54name,55camelizeStyleName(name)56) : null);57};5859var warnBadVendoredStyleName = function(name) {60if (warnedStyleNames.hasOwnProperty(name) && warnedStyleNames[name]) {61return;62}6364warnedStyleNames[name] = true;65("production" !== process.env.NODE_ENV ? warning(66false,67'Unsupported vendor-prefixed style property %s. Did you mean %s?',68name,69name.charAt(0).toUpperCase() + name.slice(1)70) : null);71};7273var warnStyleValueWithSemicolon = function(name, value) {74if (warnedStyleValues.hasOwnProperty(value) && warnedStyleValues[value]) {75return;76}7778warnedStyleValues[value] = true;79("production" !== process.env.NODE_ENV ? warning(80false,81'Style property values shouldn\'t contain a semicolon. ' +82'Try "%s: %s" instead.',83name,84value.replace(badStyleValueWithSemicolonPattern, '')85) : null);86};8788/**89* @param {string} name90* @param {*} value91*/92var warnValidStyle = function(name, value) {93if (name.indexOf('-') > -1) {94warnHyphenatedStyleName(name);95} else if (badVendoredStyleNamePattern.test(name)) {96warnBadVendoredStyleName(name);97} else if (badStyleValueWithSemicolonPattern.test(value)) {98warnStyleValueWithSemicolon(name, value);99}100};101}102103/**104* Operations for dealing with CSS properties.105*/106var CSSPropertyOperations = {107108/**109* Serializes a mapping of style properties for use as inline styles:110*111* > createMarkupForStyles({width: '200px', height: 0})112* "width:200px;height:0;"113*114* Undefined values are ignored so that declarative programming is easier.115* The result should be HTML-escaped before insertion into the DOM.116*117* @param {object} styles118* @return {?string}119*/120createMarkupForStyles: function(styles) {121var serialized = '';122for (var styleName in styles) {123if (!styles.hasOwnProperty(styleName)) {124continue;125}126var styleValue = styles[styleName];127if ("production" !== process.env.NODE_ENV) {128warnValidStyle(styleName, styleValue);129}130if (styleValue != null) {131serialized += processStyleName(styleName) + ':';132serialized += dangerousStyleValue(styleName, styleValue) + ';';133}134}135return serialized || null;136},137138/**139* Sets the value for multiple styles on a node. If a value is specified as140* '' (empty string), the corresponding style property will be unset.141*142* @param {DOMElement} node143* @param {object} styles144*/145setValueForStyles: function(node, styles) {146var style = node.style;147for (var styleName in styles) {148if (!styles.hasOwnProperty(styleName)) {149continue;150}151if ("production" !== process.env.NODE_ENV) {152warnValidStyle(styleName, styles[styleName]);153}154var styleValue = dangerousStyleValue(styleName, styles[styleName]);155if (styleName === 'float') {156styleName = styleFloatAccessor;157}158if (styleValue) {159style[styleName] = styleValue;160} else {161var expansion = CSSProperty.shorthandPropertyExpansions[styleName];162if (expansion) {163// Shorthand property that IE8 won't like unsetting, so unset each164// component to placate it165for (var individualStyleName in expansion) {166style[individualStyleName] = '';167}168} else {169style[styleName] = '';170}171}172}173}174175};176177module.exports = CSSPropertyOperations;178179180