react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / browser / ui / dom / DOMPropertyOperations.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 DOMPropertyOperations9* @typechecks static-only10*/1112"use strict";1314var DOMProperty = require('DOMProperty');1516var escapeTextForBrowser = require('escapeTextForBrowser');17var memoizeStringOnly = require('memoizeStringOnly');18var warning = require('warning');1920function shouldIgnoreValue(name, value) {21return value == null ||22(DOMProperty.hasBooleanValue[name] && !value) ||23(DOMProperty.hasNumericValue[name] && isNaN(value)) ||24(DOMProperty.hasPositiveNumericValue[name] && (value < 1)) ||25(DOMProperty.hasOverloadedBooleanValue[name] && value === false);26}2728var processAttributeNameAndPrefix = memoizeStringOnly(function(name) {29return escapeTextForBrowser(name) + '="';30});3132if (__DEV__) {33var reactProps = {34children: true,35dangerouslySetInnerHTML: true,36key: true,37ref: true38};39var warnedProperties = {};4041var warnUnknownProperty = function(name) {42if (reactProps.hasOwnProperty(name) && reactProps[name] ||43warnedProperties.hasOwnProperty(name) && warnedProperties[name]) {44return;45}4647warnedProperties[name] = true;48var lowerCasedName = name.toLowerCase();4950// data-* attributes should be lowercase; suggest the lowercase version51var standardName = (52DOMProperty.isCustomAttribute(lowerCasedName) ?53lowerCasedName :54DOMProperty.getPossibleStandardName.hasOwnProperty(lowerCasedName) ?55DOMProperty.getPossibleStandardName[lowerCasedName] :56null57);5859// For now, only warn when we have a suggested correction. This prevents60// logging too much when using transferPropsTo.61warning(62standardName == null,63'Unknown DOM property ' + name + '. Did you mean ' + standardName + '?'64);6566};67}6869/**70* Operations for dealing with DOM properties.71*/72var DOMPropertyOperations = {7374/**75* Creates markup for the ID property.76*77* @param {string} id Unescaped ID.78* @return {string} Markup string.79*/80createMarkupForID: function(id) {81return processAttributeNameAndPrefix(DOMProperty.ID_ATTRIBUTE_NAME) +82escapeTextForBrowser(id) + '"';83},8485/**86* Creates markup for a property.87*88* @param {string} name89* @param {*} value90* @return {?string} Markup string, or null if the property was invalid.91*/92createMarkupForProperty: function(name, value) {93if (DOMProperty.isStandardName.hasOwnProperty(name) &&94DOMProperty.isStandardName[name]) {95if (shouldIgnoreValue(name, value)) {96return '';97}98var attributeName = DOMProperty.getAttributeName[name];99if (DOMProperty.hasBooleanValue[name] ||100(DOMProperty.hasOverloadedBooleanValue[name] && value === true)) {101return escapeTextForBrowser(attributeName);102}103return processAttributeNameAndPrefix(attributeName) +104escapeTextForBrowser(value) + '"';105} else if (DOMProperty.isCustomAttribute(name)) {106if (value == null) {107return '';108}109return processAttributeNameAndPrefix(name) +110escapeTextForBrowser(value) + '"';111} else if (__DEV__) {112warnUnknownProperty(name);113}114return null;115},116117/**118* Sets the value for a property on a node.119*120* @param {DOMElement} node121* @param {string} name122* @param {*} value123*/124setValueForProperty: function(node, name, value) {125if (DOMProperty.isStandardName.hasOwnProperty(name) &&126DOMProperty.isStandardName[name]) {127var mutationMethod = DOMProperty.getMutationMethod[name];128if (mutationMethod) {129mutationMethod(node, value);130} else if (shouldIgnoreValue(name, value)) {131this.deleteValueForProperty(node, name);132} else if (DOMProperty.mustUseAttribute[name]) {133// `setAttribute` with objects becomes only `[object]` in IE8/9,134// ('' + value) makes it output the correct toString()-value.135node.setAttribute(DOMProperty.getAttributeName[name], '' + value);136} else {137var propName = DOMProperty.getPropertyName[name];138// Must explicitly cast values for HAS_SIDE_EFFECTS-properties to the139// property type before comparing; only `value` does and is string.140if (!DOMProperty.hasSideEffects[name] ||141('' + node[propName]) !== ('' + value)) {142// Contrary to `setAttribute`, object properties are properly143// `toString`ed by IE8/9.144node[propName] = value;145}146}147} else if (DOMProperty.isCustomAttribute(name)) {148if (value == null) {149node.removeAttribute(name);150} else {151node.setAttribute(name, '' + value);152}153} else if (__DEV__) {154warnUnknownProperty(name);155}156},157158/**159* Deletes the value for a property on a node.160*161* @param {DOMElement} node162* @param {string} name163*/164deleteValueForProperty: function(node, name) {165if (DOMProperty.isStandardName.hasOwnProperty(name) &&166DOMProperty.isStandardName[name]) {167var mutationMethod = DOMProperty.getMutationMethod[name];168if (mutationMethod) {169mutationMethod(node, undefined);170} else if (DOMProperty.mustUseAttribute[name]) {171node.removeAttribute(DOMProperty.getAttributeName[name]);172} else {173var propName = DOMProperty.getPropertyName[name];174var defaultValue = DOMProperty.getDefaultValueForProperty(175node.nodeName,176propName177);178if (!DOMProperty.hasSideEffects[name] ||179('' + node[propName]) !== defaultValue) {180node[propName] = defaultValue;181}182}183} else if (DOMProperty.isCustomAttribute(name)) {184node.removeAttribute(name);185} else if (__DEV__) {186warnUnknownProperty(name);187}188}189190};191192module.exports = DOMPropertyOperations;193194195