react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / browser / ui / dom / DOMProperty.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 DOMProperty9* @typechecks static-only10*/1112/*jslint bitwise: true */1314"use strict";1516var invariant = require('invariant');1718function checkMask(value, bitmask) {19return (value & bitmask) === bitmask;20}2122var DOMPropertyInjection = {23/**24* Mapping from normalized, camelcased property names to a configuration that25* specifies how the associated DOM property should be accessed or rendered.26*/27MUST_USE_ATTRIBUTE: 0x1,28MUST_USE_PROPERTY: 0x2,29HAS_SIDE_EFFECTS: 0x4,30HAS_BOOLEAN_VALUE: 0x8,31HAS_NUMERIC_VALUE: 0x10,32HAS_POSITIVE_NUMERIC_VALUE: 0x20 | 0x10,33HAS_OVERLOADED_BOOLEAN_VALUE: 0x40,3435/**36* Inject some specialized knowledge about the DOM. This takes a config object37* with the following properties:38*39* isCustomAttribute: function that given an attribute name will return true40* if it can be inserted into the DOM verbatim. Useful for data-* or aria-*41* attributes where it's impossible to enumerate all of the possible42* attribute names,43*44* Properties: object mapping DOM property name to one of the45* DOMPropertyInjection constants or null. If your attribute isn't in here,46* it won't get written to the DOM.47*48* DOMAttributeNames: object mapping React attribute name to the DOM49* attribute name. Attribute names not specified use the **lowercase**50* normalized name.51*52* DOMPropertyNames: similar to DOMAttributeNames but for DOM properties.53* Property names not specified use the normalized name.54*55* DOMMutationMethods: Properties that require special mutation methods. If56* `value` is undefined, the mutation method should unset the property.57*58* @param {object} domPropertyConfig the config as described above.59*/60injectDOMPropertyConfig: function(domPropertyConfig) {61var Properties = domPropertyConfig.Properties || {};62var DOMAttributeNames = domPropertyConfig.DOMAttributeNames || {};63var DOMPropertyNames = domPropertyConfig.DOMPropertyNames || {};64var DOMMutationMethods = domPropertyConfig.DOMMutationMethods || {};6566if (domPropertyConfig.isCustomAttribute) {67DOMProperty._isCustomAttributeFunctions.push(68domPropertyConfig.isCustomAttribute69);70}7172for (var propName in Properties) {73invariant(74!DOMProperty.isStandardName.hasOwnProperty(propName),75'injectDOMPropertyConfig(...): You\'re trying to inject DOM property ' +76'\'%s\' which has already been injected. You may be accidentally ' +77'injecting the same DOM property config twice, or you may be ' +78'injecting two configs that have conflicting property names.',79propName80);8182DOMProperty.isStandardName[propName] = true;8384var lowerCased = propName.toLowerCase();85DOMProperty.getPossibleStandardName[lowerCased] = propName;8687if (DOMAttributeNames.hasOwnProperty(propName)) {88var attributeName = DOMAttributeNames[propName];89DOMProperty.getPossibleStandardName[attributeName] = propName;90DOMProperty.getAttributeName[propName] = attributeName;91} else {92DOMProperty.getAttributeName[propName] = lowerCased;93}9495DOMProperty.getPropertyName[propName] =96DOMPropertyNames.hasOwnProperty(propName) ?97DOMPropertyNames[propName] :98propName;99100if (DOMMutationMethods.hasOwnProperty(propName)) {101DOMProperty.getMutationMethod[propName] = DOMMutationMethods[propName];102} else {103DOMProperty.getMutationMethod[propName] = null;104}105106var propConfig = Properties[propName];107DOMProperty.mustUseAttribute[propName] =108checkMask(propConfig, DOMPropertyInjection.MUST_USE_ATTRIBUTE);109DOMProperty.mustUseProperty[propName] =110checkMask(propConfig, DOMPropertyInjection.MUST_USE_PROPERTY);111DOMProperty.hasSideEffects[propName] =112checkMask(propConfig, DOMPropertyInjection.HAS_SIDE_EFFECTS);113DOMProperty.hasBooleanValue[propName] =114checkMask(propConfig, DOMPropertyInjection.HAS_BOOLEAN_VALUE);115DOMProperty.hasNumericValue[propName] =116checkMask(propConfig, DOMPropertyInjection.HAS_NUMERIC_VALUE);117DOMProperty.hasPositiveNumericValue[propName] =118checkMask(propConfig, DOMPropertyInjection.HAS_POSITIVE_NUMERIC_VALUE);119DOMProperty.hasOverloadedBooleanValue[propName] =120checkMask(propConfig, DOMPropertyInjection.HAS_OVERLOADED_BOOLEAN_VALUE);121122invariant(123!DOMProperty.mustUseAttribute[propName] ||124!DOMProperty.mustUseProperty[propName],125'DOMProperty: Cannot require using both attribute and property: %s',126propName127);128invariant(129DOMProperty.mustUseProperty[propName] ||130!DOMProperty.hasSideEffects[propName],131'DOMProperty: Properties that have side effects must use property: %s',132propName133);134invariant(135!!DOMProperty.hasBooleanValue[propName] +136!!DOMProperty.hasNumericValue[propName] +137!!DOMProperty.hasOverloadedBooleanValue[propName] <= 1,138'DOMProperty: Value can be one of boolean, overloaded boolean, or ' +139'numeric value, but not a combination: %s',140propName141);142}143}144};145var defaultValueCache = {};146147/**148* DOMProperty exports lookup objects that can be used like functions:149*150* > DOMProperty.isValid['id']151* true152* > DOMProperty.isValid['foobar']153* undefined154*155* Although this may be confusing, it performs better in general.156*157* @see http://jsperf.com/key-exists158* @see http://jsperf.com/key-missing159*/160var DOMProperty = {161162ID_ATTRIBUTE_NAME: 'data-reactid',163164/**165* Checks whether a property name is a standard property.166* @type {Object}167*/168isStandardName: {},169170/**171* Mapping from lowercase property names to the properly cased version, used172* to warn in the case of missing properties.173* @type {Object}174*/175getPossibleStandardName: {},176177/**178* Mapping from normalized names to attribute names that differ. Attribute179* names are used when rendering markup or with `*Attribute()`.180* @type {Object}181*/182getAttributeName: {},183184/**185* Mapping from normalized names to properties on DOM node instances.186* (This includes properties that mutate due to external factors.)187* @type {Object}188*/189getPropertyName: {},190191/**192* Mapping from normalized names to mutation methods. This will only exist if193* mutation cannot be set simply by the property or `setAttribute()`.194* @type {Object}195*/196getMutationMethod: {},197198/**199* Whether the property must be accessed and mutated as an object property.200* @type {Object}201*/202mustUseAttribute: {},203204/**205* Whether the property must be accessed and mutated using `*Attribute()`.206* (This includes anything that fails `<propName> in <element>`.)207* @type {Object}208*/209mustUseProperty: {},210211/**212* Whether or not setting a value causes side effects such as triggering213* resources to be loaded or text selection changes. We must ensure that214* the value is only set if it has changed.215* @type {Object}216*/217hasSideEffects: {},218219/**220* Whether the property should be removed when set to a falsey value.221* @type {Object}222*/223hasBooleanValue: {},224225/**226* Whether the property must be numeric or parse as a227* numeric and should be removed when set to a falsey value.228* @type {Object}229*/230hasNumericValue: {},231232/**233* Whether the property must be positive numeric or parse as a positive234* numeric and should be removed when set to a falsey value.235* @type {Object}236*/237hasPositiveNumericValue: {},238239/**240* Whether the property can be used as a flag as well as with a value. Removed241* when strictly equal to false; present without a value when strictly equal242* to true; present with a value otherwise.243* @type {Object}244*/245hasOverloadedBooleanValue: {},246247/**248* All of the isCustomAttribute() functions that have been injected.249*/250_isCustomAttributeFunctions: [],251252/**253* Checks whether a property name is a custom attribute.254* @method255*/256isCustomAttribute: function(attributeName) {257for (var i = 0; i < DOMProperty._isCustomAttributeFunctions.length; i++) {258var isCustomAttributeFn = DOMProperty._isCustomAttributeFunctions[i];259if (isCustomAttributeFn(attributeName)) {260return true;261}262}263return false;264},265266/**267* Returns the default property value for a DOM property (i.e., not an268* attribute). Most default values are '' or false, but not all. Worse yet,269* some (in particular, `type`) vary depending on the type of element.270*271* TODO: Is it better to grab all the possible properties when creating an272* element to avoid having to create the same element twice?273*/274getDefaultValueForProperty: function(nodeName, prop) {275var nodeDefaults = defaultValueCache[nodeName];276var testElement;277if (!nodeDefaults) {278defaultValueCache[nodeName] = nodeDefaults = {};279}280if (!(prop in nodeDefaults)) {281testElement = document.createElement(nodeName);282nodeDefaults[prop] = testElement[prop];283}284return nodeDefaults[prop];285},286287injection: DOMPropertyInjection288};289290module.exports = DOMProperty;291292293