Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80517 views
1
/**
2
* Copyright 2013-2015, 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 ("production" !== process.env.NODE_ENV) {
37
// 'msTransform' is correct, but the other prefixes should be capitalized
38
var badVendoredStyleNamePattern = /^(?:webkit|moz|o)[A-Z]/;
39
40
// style values shouldn't contain a semicolon
41
var badStyleValueWithSemicolonPattern = /;\s*$/;
42
43
var warnedStyleNames = {};
44
var warnedStyleValues = {};
45
46
var warnHyphenatedStyleName = function(name) {
47
if (warnedStyleNames.hasOwnProperty(name) && warnedStyleNames[name]) {
48
return;
49
}
50
51
warnedStyleNames[name] = true;
52
("production" !== process.env.NODE_ENV ? warning(
53
false,
54
'Unsupported style property %s. Did you mean %s?',
55
name,
56
camelizeStyleName(name)
57
) : null);
58
};
59
60
var warnBadVendoredStyleName = function(name) {
61
if (warnedStyleNames.hasOwnProperty(name) && warnedStyleNames[name]) {
62
return;
63
}
64
65
warnedStyleNames[name] = true;
66
("production" !== process.env.NODE_ENV ? warning(
67
false,
68
'Unsupported vendor-prefixed style property %s. Did you mean %s?',
69
name,
70
name.charAt(0).toUpperCase() + name.slice(1)
71
) : null);
72
};
73
74
var warnStyleValueWithSemicolon = function(name, value) {
75
if (warnedStyleValues.hasOwnProperty(value) && warnedStyleValues[value]) {
76
return;
77
}
78
79
warnedStyleValues[value] = true;
80
("production" !== process.env.NODE_ENV ? warning(
81
false,
82
'Style property values shouldn\'t contain a semicolon. ' +
83
'Try "%s: %s" instead.',
84
name,
85
value.replace(badStyleValueWithSemicolonPattern, '')
86
) : null);
87
};
88
89
/**
90
* @param {string} name
91
* @param {*} value
92
*/
93
var warnValidStyle = function(name, value) {
94
if (name.indexOf('-') > -1) {
95
warnHyphenatedStyleName(name);
96
} else if (badVendoredStyleNamePattern.test(name)) {
97
warnBadVendoredStyleName(name);
98
} else if (badStyleValueWithSemicolonPattern.test(value)) {
99
warnStyleValueWithSemicolon(name, value);
100
}
101
};
102
}
103
104
/**
105
* Operations for dealing with CSS properties.
106
*/
107
var CSSPropertyOperations = {
108
109
/**
110
* Serializes a mapping of style properties for use as inline styles:
111
*
112
* > createMarkupForStyles({width: '200px', height: 0})
113
* "width:200px;height:0;"
114
*
115
* Undefined values are ignored so that declarative programming is easier.
116
* The result should be HTML-escaped before insertion into the DOM.
117
*
118
* @param {object} styles
119
* @return {?string}
120
*/
121
createMarkupForStyles: function(styles) {
122
var serialized = '';
123
for (var styleName in styles) {
124
if (!styles.hasOwnProperty(styleName)) {
125
continue;
126
}
127
var styleValue = styles[styleName];
128
if ("production" !== process.env.NODE_ENV) {
129
warnValidStyle(styleName, styleValue);
130
}
131
if (styleValue != null) {
132
serialized += processStyleName(styleName) + ':';
133
serialized += dangerousStyleValue(styleName, styleValue) + ';';
134
}
135
}
136
return serialized || null;
137
},
138
139
/**
140
* Sets the value for multiple styles on a node. If a value is specified as
141
* '' (empty string), the corresponding style property will be unset.
142
*
143
* @param {DOMElement} node
144
* @param {object} styles
145
*/
146
setValueForStyles: function(node, styles) {
147
var style = node.style;
148
for (var styleName in styles) {
149
if (!styles.hasOwnProperty(styleName)) {
150
continue;
151
}
152
if ("production" !== process.env.NODE_ENV) {
153
warnValidStyle(styleName, styles[styleName]);
154
}
155
var styleValue = dangerousStyleValue(styleName, styles[styleName]);
156
if (styleName === 'float') {
157
styleName = styleFloatAccessor;
158
}
159
if (styleValue) {
160
style[styleName] = styleValue;
161
} else {
162
var expansion = CSSProperty.shorthandPropertyExpansions[styleName];
163
if (expansion) {
164
// Shorthand property that IE8 won't like unsetting, so unset each
165
// component to placate it
166
for (var individualStyleName in expansion) {
167
style[individualStyleName] = '';
168
}
169
} else {
170
style[styleName] = '';
171
}
172
}
173
}
174
}
175
176
};
177
178
module.exports = CSSPropertyOperations;
179
180