react / react-0.13.3 / examples / basic-commonjs / node_modules / react / lib / DOMPropertyOperations.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 DOMPropertyOperations9* @typechecks static-only10*/1112'use strict';1314var DOMProperty = require("./DOMProperty");1516var quoteAttributeValueForBrowser = require("./quoteAttributeValueForBrowser");17var warning = require("./warning");1819function shouldIgnoreValue(name, value) {20return value == null ||21(DOMProperty.hasBooleanValue[name] && !value) ||22(DOMProperty.hasNumericValue[name] && isNaN(value)) ||23(DOMProperty.hasPositiveNumericValue[name] && (value < 1)) ||24(DOMProperty.hasOverloadedBooleanValue[name] && value === false);25}2627if ("production" !== process.env.NODE_ENV) {28var reactProps = {29children: true,30dangerouslySetInnerHTML: true,31key: true,32ref: true33};34var warnedProperties = {};3536var warnUnknownProperty = function(name) {37if (reactProps.hasOwnProperty(name) && reactProps[name] ||38warnedProperties.hasOwnProperty(name) && warnedProperties[name]) {39return;40}4142warnedProperties[name] = true;43var lowerCasedName = name.toLowerCase();4445// data-* attributes should be lowercase; suggest the lowercase version46var standardName = (47DOMProperty.isCustomAttribute(lowerCasedName) ?48lowerCasedName :49DOMProperty.getPossibleStandardName.hasOwnProperty(lowerCasedName) ?50DOMProperty.getPossibleStandardName[lowerCasedName] :51null52);5354// For now, only warn when we have a suggested correction. This prevents55// logging too much when using transferPropsTo.56("production" !== process.env.NODE_ENV ? warning(57standardName == null,58'Unknown DOM property %s. Did you mean %s?',59name,60standardName61) : null);6263};64}6566/**67* Operations for dealing with DOM properties.68*/69var DOMPropertyOperations = {7071/**72* Creates markup for the ID property.73*74* @param {string} id Unescaped ID.75* @return {string} Markup string.76*/77createMarkupForID: function(id) {78return DOMProperty.ID_ATTRIBUTE_NAME + '=' +79quoteAttributeValueForBrowser(id);80},8182/**83* Creates markup for a property.84*85* @param {string} name86* @param {*} value87* @return {?string} Markup string, or null if the property was invalid.88*/89createMarkupForProperty: function(name, value) {90if (DOMProperty.isStandardName.hasOwnProperty(name) &&91DOMProperty.isStandardName[name]) {92if (shouldIgnoreValue(name, value)) {93return '';94}95var attributeName = DOMProperty.getAttributeName[name];96if (DOMProperty.hasBooleanValue[name] ||97(DOMProperty.hasOverloadedBooleanValue[name] && value === true)) {98return attributeName;99}100return attributeName + '=' + quoteAttributeValueForBrowser(value);101} else if (DOMProperty.isCustomAttribute(name)) {102if (value == null) {103return '';104}105return name + '=' + quoteAttributeValueForBrowser(value);106} else if ("production" !== process.env.NODE_ENV) {107warnUnknownProperty(name);108}109return null;110},111112/**113* Sets the value for a property on a node.114*115* @param {DOMElement} node116* @param {string} name117* @param {*} value118*/119setValueForProperty: function(node, name, value) {120if (DOMProperty.isStandardName.hasOwnProperty(name) &&121DOMProperty.isStandardName[name]) {122var mutationMethod = DOMProperty.getMutationMethod[name];123if (mutationMethod) {124mutationMethod(node, value);125} else if (shouldIgnoreValue(name, value)) {126this.deleteValueForProperty(node, name);127} else if (DOMProperty.mustUseAttribute[name]) {128// `setAttribute` with objects becomes only `[object]` in IE8/9,129// ('' + value) makes it output the correct toString()-value.130node.setAttribute(DOMProperty.getAttributeName[name], '' + value);131} else {132var propName = DOMProperty.getPropertyName[name];133// Must explicitly cast values for HAS_SIDE_EFFECTS-properties to the134// property type before comparing; only `value` does and is string.135if (!DOMProperty.hasSideEffects[name] ||136('' + node[propName]) !== ('' + value)) {137// Contrary to `setAttribute`, object properties are properly138// `toString`ed by IE8/9.139node[propName] = value;140}141}142} else if (DOMProperty.isCustomAttribute(name)) {143if (value == null) {144node.removeAttribute(name);145} else {146node.setAttribute(name, '' + value);147}148} else if ("production" !== process.env.NODE_ENV) {149warnUnknownProperty(name);150}151},152153/**154* Deletes the value for a property on a node.155*156* @param {DOMElement} node157* @param {string} name158*/159deleteValueForProperty: function(node, name) {160if (DOMProperty.isStandardName.hasOwnProperty(name) &&161DOMProperty.isStandardName[name]) {162var mutationMethod = DOMProperty.getMutationMethod[name];163if (mutationMethod) {164mutationMethod(node, undefined);165} else if (DOMProperty.mustUseAttribute[name]) {166node.removeAttribute(DOMProperty.getAttributeName[name]);167} else {168var propName = DOMProperty.getPropertyName[name];169var defaultValue = DOMProperty.getDefaultValueForProperty(170node.nodeName,171propName172);173if (!DOMProperty.hasSideEffects[name] ||174('' + node[propName]) !== defaultValue) {175node[propName] = defaultValue;176}177}178} else if (DOMProperty.isCustomAttribute(name)) {179node.removeAttribute(name);180} else if ("production" !== process.env.NODE_ENV) {181warnUnknownProperty(name);182}183}184185};186187module.exports = DOMPropertyOperations;188189190