OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # | 2 # |
3 # Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 3 # Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
4 # for details. All rights reserved. Use of this source code is governed by a | 4 # for details. All rights reserved. Use of this source code is governed by a |
5 # BSD-style license that can be found in the LICENSE file. | 5 # BSD-style license that can be found in the LICENSE file. |
6 | 6 |
7 """Generates CSSStyleDeclaration template file from css property definitions | 7 """Generates CSSStyleDeclaration template file from css property definitions |
8 defined in WebKit.""" | 8 defined in WebKit.""" |
9 | 9 |
10 import tempfile, os, re | 10 import tempfile, os, re |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 return style; | 112 return style; |
113 } | 113 } |
114 | 114 |
115 /// Returns the value of the property if the provided *CSS* property | 115 /// Returns the value of the property if the provided *CSS* property |
116 /// name is supported on this element and if the value is set. Otherwise | 116 /// name is supported on this element and if the value is set. Otherwise |
117 /// returns an empty string. | 117 /// returns an empty string. |
118 /// | 118 /// |
119 /// Please note the property name uses camelCase, not-hyphens. | 119 /// Please note the property name uses camelCase, not-hyphens. |
120 String getPropertyValue(String propertyName) { | 120 String getPropertyValue(String propertyName) { |
121 var propValue = _getPropertyValueHelper(propertyName); | 121 var propValue = _getPropertyValueHelper(propertyName); |
122 return propValue != null ? propValue : ''; | 122 return propValue ?? ''; |
123 } | 123 } |
124 | 124 |
125 String _getPropertyValueHelper(String propertyName) { | 125 String _getPropertyValueHelper(String propertyName) { |
126 if (_supportsProperty(_camelCase(propertyName))) { | 126 return _getPropertyValue(_browserPropertyName(propertyName)); |
127 return _getPropertyValue(propertyName); | |
128 } else { | |
129 return _getPropertyValue(Device.cssPrefix + propertyName); | |
130 } | |
131 } | 127 } |
132 | 128 |
133 /** | 129 /** |
134 * Returns true if the provided *CSS* property name is supported on this | 130 * Returns true if the provided *CSS* property name is supported on this |
135 * element. | 131 * element. |
136 * | 132 * |
137 * Please note the property name camelCase, not-hyphens. This | 133 * Please note the property name camelCase, not-hyphens. This |
138 * method returns true if the property is accessible via an unprefixed _or_ | 134 * method returns true if the property is accessible via an unprefixed _or_ |
139 * prefixed property. | 135 * prefixed property. |
140 */ | 136 */ |
141 bool supportsProperty(String propertyName) { | 137 bool supportsProperty(String propertyName) { |
142 return _supportsProperty(propertyName) || | 138 return _supportsProperty(propertyName) || |
143 _supportsProperty(_camelCase(Device.cssPrefix + propertyName)); | 139 _supportsProperty(_camelCase("${Device.cssPrefix}$propertyName")); |
144 } | 140 } |
145 | 141 |
146 bool _supportsProperty(String propertyName) { | 142 bool _supportsProperty(String propertyName) { |
147 $if DART2JS | 143 $if DART2JS |
148 return JS('bool', '# in #', propertyName, this); | 144 return JS('bool', '# in #', propertyName, this); |
149 $else | 145 $else |
150 // You can't just check the value of a property, because there is no way | 146 // You can't just check the value of a property, because there is no way |
151 // to distinguish between property not being present in the browser and | 147 // to distinguish between property not being present in the browser and |
152 // not having a value at all. (Ultimately we'll want the native method to | 148 // not having a value at all. (Ultimately we'll want the native method to |
153 // return null if the property doesn't exist and empty string if it's | 149 // return null if the property doesn't exist and empty string if it's |
154 // defined but just doesn't have a value. | 150 // defined but just doesn't have a value. |
155 return _hasProperty(propertyName); | 151 return _hasProperty(propertyName); |
156 $endif | 152 $endif |
157 } | 153 } |
158 | 154 |
159 $if DARTIUM | 155 $if DARTIUM |
160 bool _hasProperty(String propertyName) => | 156 bool _hasProperty(String propertyName) => |
161 _blink.BlinkCSSStyleDeclaration.instance.$__get___propertyIsEnumerable_Cal
lback_1_(this, propertyName); | 157 _blink.BlinkCSSStyleDeclaration.instance.$__get___propertyIsEnumerable_Cal
lback_1_(this, propertyName); |
162 $endif | 158 $endif |
163 | 159 |
164 @DomName('CSSStyleDeclaration.setProperty') | 160 @DomName('CSSStyleDeclaration.setProperty') |
165 void setProperty(String propertyName, String value, [String priority]) { | 161 void setProperty(String propertyName, String value, [String priority]) { |
166 return _setPropertyHelper(_browserPropertyName(propertyName), | 162 return _setPropertyHelper(_browserPropertyName(propertyName), |
167 value, priority); | 163 value, priority); |
168 } | 164 } |
169 | 165 |
170 String _browserPropertyName(String propertyName) { | 166 String _browserPropertyName(String propertyName) { |
171 String name = _readCache(propertyName); | 167 String name = _readCache(propertyName); |
172 if (name is String) return name; | 168 if (name is String) return name; |
173 if (_supportsProperty(_camelCase(propertyName))) { | 169 name = _supportedBrowserPropertyName(propertyName); |
174 name = propertyName; | |
175 } else { | |
176 name = Device.cssPrefix + propertyName; | |
177 } | |
178 _writeCache(propertyName, name); | 170 _writeCache(propertyName, name); |
179 return name; | 171 return name; |
180 } | 172 } |
181 | 173 |
| 174 String _supportedBrowserPropertyName(String propertyName) { |
| 175 if (_supportsProperty(_camelCase(propertyName))) { |
| 176 return propertyName; |
| 177 } |
| 178 var prefixed = "${Device.cssPrefix}$propertyName"; |
| 179 if (_supportsProperty(prefixed)) { |
| 180 return prefixed; |
| 181 } |
| 182 // May be a CSS variable, just use it as provided. |
| 183 return propertyName; |
| 184 } |
| 185 |
182 $if DART2JS | 186 $if DART2JS |
183 static final _propertyCache = JS('', '{}'); | 187 static final _propertyCache = JS('', '{}'); |
184 static String _readCache(String key) => | 188 static String _readCache(String key) => |
185 JS('String|Null', '#[#]', _propertyCache, key); | 189 JS('String|Null', '#[#]', _propertyCache, key); |
186 static void _writeCache(String key, String value) { | 190 static void _writeCache(String key, String value) { |
187 JS('void', '#[#] = #', _propertyCache, key, value); | 191 JS('void', '#[#] = #', _propertyCache, key, value); |
188 } | 192 } |
189 $else | 193 $else |
190 static String _readCache(String key) => null; | 194 static String _readCache(String key) => null; |
191 static void _writeCache(String key, value) {} | 195 static void _writeCache(String key, value) {} |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
356 class_lines.append(annotated[base_css_name]) | 360 class_lines.append(annotated[base_css_name]) |
357 class_lines.append(""" | 361 class_lines.append(""" |
358 set %s(String value) { | 362 set %s(String value) { |
359 setProperty('%s', value, ''); | 363 setProperty('%s', value, ''); |
360 } | 364 } |
361 """ % (camel_case_name, css_name)) | 365 """ % (camel_case_name, css_name)) |
362 | 366 |
363 class_file.write(''.join(class_lines)); | 367 class_file.write(''.join(class_lines)); |
364 class_file.write('}\n') | 368 class_file.write('}\n') |
365 class_file.close() | 369 class_file.close() |
OLD | NEW |