react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / browser / ui / dom / dangerousStyleValue.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 dangerousStyleValue9* @typechecks static-only10*/1112"use strict";1314var CSSProperty = require('CSSProperty');1516var isUnitlessNumber = CSSProperty.isUnitlessNumber;1718/**19* Convert a value into the proper css writable value. The style name `name`20* should be logical (no hyphens), as specified21* in `CSSProperty.isUnitlessNumber`.22*23* @param {string} name CSS property name such as `topMargin`.24* @param {*} value CSS property value such as `10px`.25* @return {string} Normalized style value with dimensions applied.26*/27function dangerousStyleValue(name, value) {28// Note that we've removed escapeTextForBrowser() calls here since the29// whole string will be escaped when the attribute is injected into30// the markup. If you provide unsafe user data here they can inject31// arbitrary CSS which may be problematic (I couldn't repro this):32// https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet33// http://www.thespanner.co.uk/2007/11/26/ultimate-xss-css-injection/34// This is not an XSS hole but instead a potential CSS injection issue35// which has lead to a greater discussion about how we're going to36// trust URLs moving forward. See #21159013738var isEmpty = value == null || typeof value === 'boolean' || value === '';39if (isEmpty) {40return '';41}4243var isNonNumeric = isNaN(value);44if (isNonNumeric || value === 0 ||45isUnitlessNumber.hasOwnProperty(name) && isUnitlessNumber[name]) {46return '' + value; // cast to string47}4849if (typeof value === 'string') {50value = value.trim();51}52return value + 'px';53}5455module.exports = dangerousStyleValue;565758