Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81159 views
1
/**
2
* Copyright 2013-2014, Facebook, Inc.
3
* All rights reserved.
4
*
5
* This source code is licensed under the BSD-style license found in the
6
* LICENSE file in the root directory of this source tree. An additional grant
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* @providesModule CSSPropertyOperations
10
* @typechecks static-only
11
*/
12
13
"use strict";
14
15
var CSSProperty = require('CSSProperty');
16
var ExecutionEnvironment = require('ExecutionEnvironment');
17
18
var camelizeStyleName = require('camelizeStyleName');
19
var dangerousStyleValue = require('dangerousStyleValue');
20
var hyphenateStyleName = require('hyphenateStyleName');
21
var memoizeStringOnly = require('memoizeStringOnly');
22
var warning = require('warning');
23
24
var processStyleName = memoizeStringOnly(function(styleName) {
25
return hyphenateStyleName(styleName);
26
});
27
28
var styleFloatAccessor = 'cssFloat';
29
if (ExecutionEnvironment.canUseDOM) {
30
// IE8 only supports accessing cssFloat (standard) as styleFloat
31
if (document.documentElement.style.cssFloat === undefined) {
32
styleFloatAccessor = 'styleFloat';
33
}
34
}
35
36
if (__DEV__) {
37
var warnedStyleNames = {};
38
39
var warnHyphenatedStyleName = function(name) {
40
if (warnedStyleNames.hasOwnProperty(name) && warnedStyleNames[name]) {
41
return;
42
}
43
44
warnedStyleNames[name] = true;
45
warning(
46
false,
47
'Unsupported style property ' + name + '. Did you mean ' +
48
camelizeStyleName(name) + '?'
49
);
50
};
51
}
52
53
/**
54
* Operations for dealing with CSS properties.
55
*/
56
var CSSPropertyOperations = {
57
58
/**
59
* Serializes a mapping of style properties for use as inline styles:
60
*
61
* > createMarkupForStyles({width: '200px', height: 0})
62
* "width:200px;height:0;"
63
*
64
* Undefined values are ignored so that declarative programming is easier.
65
* The result should be HTML-escaped before insertion into the DOM.
66
*
67
* @param {object} styles
68
* @return {?string}
69
*/
70
createMarkupForStyles: function(styles) {
71
var serialized = '';
72
for (var styleName in styles) {
73
if (!styles.hasOwnProperty(styleName)) {
74
continue;
75
}
76
if (__DEV__) {
77
if (styleName.indexOf('-') > -1) {
78
warnHyphenatedStyleName(styleName);
79
}
80
}
81
var styleValue = styles[styleName];
82
if (styleValue != null) {
83
serialized += processStyleName(styleName) + ':';
84
serialized += dangerousStyleValue(styleName, styleValue) + ';';
85
}
86
}
87
return serialized || null;
88
},
89
90
/**
91
* Sets the value for multiple styles on a node. If a value is specified as
92
* '' (empty string), the corresponding style property will be unset.
93
*
94
* @param {DOMElement} node
95
* @param {object} styles
96
*/
97
setValueForStyles: function(node, styles) {
98
var style = node.style;
99
for (var styleName in styles) {
100
if (!styles.hasOwnProperty(styleName)) {
101
continue;
102
}
103
if (__DEV__) {
104
if (styleName.indexOf('-') > -1) {
105
warnHyphenatedStyleName(styleName);
106
}
107
}
108
var styleValue = dangerousStyleValue(styleName, styles[styleName]);
109
if (styleName === 'float') {
110
styleName = styleFloatAccessor;
111
}
112
if (styleValue) {
113
style[styleName] = styleValue;
114
} else {
115
var expansion = CSSProperty.shorthandPropertyExpansions[styleName];
116
if (expansion) {
117
// Shorthand property that IE8 won't like unsetting, so unset each
118
// component to placate it
119
for (var individualStyleName in expansion) {
120
style[individualStyleName] = '';
121
}
122
} else {
123
style[styleName] = '';
124
}
125
}
126
}
127
}
128
129
};
130
131
module.exports = CSSPropertyOperations;
132
133