react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / browser / ui / dom / CSSPropertyOperations.js
81159 views/**1* Copyright 2013-2014, 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 (__DEV__) {36var warnedStyleNames = {};3738var warnHyphenatedStyleName = function(name) {39if (warnedStyleNames.hasOwnProperty(name) && warnedStyleNames[name]) {40return;41}4243warnedStyleNames[name] = true;44warning(45false,46'Unsupported style property ' + name + '. Did you mean ' +47camelizeStyleName(name) + '?'48);49};50}5152/**53* Operations for dealing with CSS properties.54*/55var CSSPropertyOperations = {5657/**58* Serializes a mapping of style properties for use as inline styles:59*60* > createMarkupForStyles({width: '200px', height: 0})61* "width:200px;height:0;"62*63* Undefined values are ignored so that declarative programming is easier.64* The result should be HTML-escaped before insertion into the DOM.65*66* @param {object} styles67* @return {?string}68*/69createMarkupForStyles: function(styles) {70var serialized = '';71for (var styleName in styles) {72if (!styles.hasOwnProperty(styleName)) {73continue;74}75if (__DEV__) {76if (styleName.indexOf('-') > -1) {77warnHyphenatedStyleName(styleName);78}79}80var styleValue = styles[styleName];81if (styleValue != null) {82serialized += processStyleName(styleName) + ':';83serialized += dangerousStyleValue(styleName, styleValue) + ';';84}85}86return serialized || null;87},8889/**90* Sets the value for multiple styles on a node. If a value is specified as91* '' (empty string), the corresponding style property will be unset.92*93* @param {DOMElement} node94* @param {object} styles95*/96setValueForStyles: function(node, styles) {97var style = node.style;98for (var styleName in styles) {99if (!styles.hasOwnProperty(styleName)) {100continue;101}102if (__DEV__) {103if (styleName.indexOf('-') > -1) {104warnHyphenatedStyleName(styleName);105}106}107var styleValue = dangerousStyleValue(styleName, styles[styleName]);108if (styleName === 'float') {109styleName = styleFloatAccessor;110}111if (styleValue) {112style[styleName] = styleValue;113} else {114var expansion = CSSProperty.shorthandPropertyExpansions[styleName];115if (expansion) {116// Shorthand property that IE8 won't like unsetting, so unset each117// component to placate it118for (var individualStyleName in expansion) {119style[individualStyleName] = '';120}121} else {122style[styleName] = '';123}124}125}126}127128};129130module.exports = CSSPropertyOperations;131132133