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 | 10 import tempfile, os, re |
11 | 11 |
12 COMMENT_LINE_PREFIX = ' * ' | 12 COMMENT_LINE_PREFIX = ' * ' |
13 # TODO(efortuna): Pull from DEPS so that we have latest css *in sync* with our | 13 # TODO(efortuna): Pull from DEPS so that we have latest css *in sync* with our |
14 # Dartium. Then remove the checked in CSSPropertyNames.in. | 14 # Dartium. Then remove the checked in CSSPropertyNames.in. |
15 SOURCE_PATH = 'CSSPropertyNames.in' | 15 SOURCE_PATH = 'CSSPropertyNames.in' |
16 #SOURCE_PATH = 'Source/WebCore/css/CSSPropertyNames.in' | 16 #SOURCE_PATH = 'Source/WebCore/css/CSSPropertyNames.in' |
17 TEMPLATE_FILE = '../templates/html/impl/impl_CSSStyleDeclaration.darttemplate' | 17 TEMPLATE_FILE = '../templates/html/impl/impl_CSSStyleDeclaration.darttemplate' |
18 | 18 |
19 # These are the properties that are supported on all Dart project supported | |
20 # browsers as camelCased names on the CssStyleDeclaration. | |
21 BROWSER_PATHS = [ | |
22 'cssProperties.ie9.txt', | |
Alan Knight
2015/03/04 17:49:32
Shouldn't we be doing ie10 and 11 rather than 9 an
sra1
2015/03/25 05:23:35
Yes, but to not include ie9 would break ie9 which
Alan Knight
2015/03/25 17:51:05
OK. But shouldn't there be IE11?
| |
23 'cssProperties.ie10.txt', | |
24 'cssProperties.ff36.txt', | |
25 'cssProperties.chrome40.txt', | |
26 'cssProperties.safari-7.1.3.txt', | |
27 ] | |
28 | |
19 # Supported annotations for any specific CSS properties. | 29 # Supported annotations for any specific CSS properties. |
20 annotated = { | 30 annotated = { |
21 'transition': '''@SupportedBrowser(SupportedBrowser.CHROME) | 31 'transition': '''@SupportedBrowser(SupportedBrowser.CHROME) |
22 @SupportedBrowser(SupportedBrowser.FIREFOX) | 32 @SupportedBrowser(SupportedBrowser.FIREFOX) |
23 @SupportedBrowser(SupportedBrowser.IE, '10') | 33 @SupportedBrowser(SupportedBrowser.IE, '10') |
24 @SupportedBrowser(SupportedBrowser.SAFARI)''' | 34 @SupportedBrowser(SupportedBrowser.SAFARI)''' |
25 } | 35 } |
26 | 36 |
37 class Error: | |
38 def __init__(self, message): | |
39 self.message = message | |
40 def __repr__(self): | |
41 return self.message | |
42 | |
27 def camelCaseName(name): | 43 def camelCaseName(name): |
28 """Convert a CSS property name to a lowerCamelCase name.""" | 44 """Convert a CSS property name to a lowerCamelCase name.""" |
29 name = name.replace('-webkit-', '') | 45 name = name.replace('-webkit-', '') |
30 words = [] | 46 words = [] |
31 for word in name.split('-'): | 47 for word in name.split('-'): |
32 if words: | 48 if words: |
33 words.append(word.title()) | 49 words.append(word.title()) |
34 else: | 50 else: |
35 words.append(word) | 51 words.append(word) |
36 return ''.join(words) | 52 return ''.join(words) |
37 | 53 |
54 def dashifyName(camelName): | |
55 def fix(match): | |
56 return '-' + match.group(0).lower() | |
57 return re.sub(r'[A-Z]', fix, camelName) | |
58 | |
59 def isCommentLine(line): | |
60 return line.strip() == '' or line.startswith('#') or line.startswith('//') | |
61 | |
62 def readCssProperties(filename): | |
63 data = open(filename).readlines() | |
64 data = sorted([d.strip() for d in set(data) if not isCommentLine(d)]) | |
65 return data | |
66 | |
38 def GenerateCssTemplateFile(): | 67 def GenerateCssTemplateFile(): |
39 data = open(SOURCE_PATH).readlines() | 68 data = open(SOURCE_PATH).readlines() |
40 | 69 |
41 # filter CSSPropertyNames.in to only the properties | 70 # filter CSSPropertyNames.in to only the properties |
42 # TODO(efortuna): do we also want CSSPropertyNames.in? | 71 # TODO(efortuna): do we also want CSSPropertyNames.in? |
43 data = [d[:-1] for d in data | 72 data = [d.strip() for d in data |
44 if len(d) > 1 | 73 if not isCommentLine(d) |
45 and not d.startswith('#') | |
46 and not d.startswith('//') | |
47 and not '=' in d] | 74 and not '=' in d] |
48 | 75 |
76 browser_props = [readCssProperties(file) for file in BROWSER_PATHS] | |
77 universal_properties = reduce( | |
78 lambda a, b: set(a).intersection(b), browser_props) | |
79 universal_properties = universal_properties.difference(['cssText']) | |
80 universal_properties = universal_properties.intersection( | |
81 map(camelCaseName, data)) | |
82 | |
49 class_file = open(TEMPLATE_FILE, 'w') | 83 class_file = open(TEMPLATE_FILE, 'w') |
50 | 84 |
51 class_file.write(""" | 85 class_file.write(""" |
52 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 86 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
53 // for details. All rights reserved. Use of this source code is governed by a | 87 // for details. All rights reserved. Use of this source code is governed by a |
54 // BSD-style license that can be found in the LICENSE file. | 88 // BSD-style license that can be found in the LICENSE file. |
55 | 89 |
56 // WARNING: DO NOT EDIT THIS TEMPLATE FILE. | 90 // WARNING: DO NOT EDIT THIS TEMPLATE FILE. |
57 // The template file was generated by scripts/css_code_generator.py | 91 // The template file was generated by scripts/css_code_generator.py |
58 | 92 |
59 // Source of CSS properties: | 93 // Source of CSS properties: |
60 // %s | 94 // %s |
61 | 95 |
62 part of $LIBRARYNAME; | 96 part of $LIBRARYNAME; |
97 """ % SOURCE_PATH) | |
63 | 98 |
64 $(ANNOTATIONS)$(NATIVESPEC)$(CLASS_MODIFIERS) class $CLASSNAME $EXTENDS with | 99 |
100 class_file.write(""" | |
101 $(ANNOTATIONS)$(NATIVESPEC)$(CLASS_MODIFIERS)class $CLASSNAME $EXTENDS with | |
65 $(CLASSNAME)Base $IMPLEMENTS { | 102 $(CLASSNAME)Base $IMPLEMENTS { |
66 factory $CLASSNAME() => new CssStyleDeclaration.css(''); | 103 factory $CLASSNAME() => new CssStyleDeclaration.css(''); |
67 | 104 |
68 factory $CLASSNAME.css(String css) { | 105 factory $CLASSNAME.css(String css) { |
69 final style = new Element.tag('div').style; | 106 final style = new Element.tag('div').style; |
70 style.cssText = css; | 107 style.cssText = css; |
71 return style; | 108 return style; |
72 } | 109 } |
73 | 110 |
74 String getPropertyValue(String propertyName) { | 111 String getPropertyValue(String propertyName) { |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
169 } | 206 } |
170 _setProperty(propertyName, value, priority); | 207 _setProperty(propertyName, value, priority); |
171 } | 208 } |
172 | 209 |
173 /** | 210 /** |
174 * Checks to see if CSS Transitions are supported. | 211 * Checks to see if CSS Transitions are supported. |
175 */ | 212 */ |
176 static bool get supportsTransitions => true; | 213 static bool get supportsTransitions => true; |
177 $endif | 214 $endif |
178 $!MEMBERS | 215 $!MEMBERS |
216 $if DART2JS | |
217 """) | |
218 | |
219 for camelName in sorted(universal_properties): | |
220 property = dashifyName(camelName) | |
221 class_file.write(""" | |
222 /** Gets the value of "%s" */ | |
223 String get %s => JS('String', '#.%s', this); | |
224 | |
225 /** Sets the value of "%s" */ | |
226 void set %s(String value) { | |
227 JS('', '#.%s = #', this, value == null ? '' : value); | |
228 } | |
229 """ % (property, camelName, camelName, property, camelName, camelName)) | |
230 | |
231 class_file.write(""" | |
232 $endif | |
179 } | 233 } |
180 | 234 |
181 class _CssStyleDeclarationSet extends Object with CssStyleDeclarationBase { | 235 class _CssStyleDeclarationSet extends Object with CssStyleDeclarationBase { |
182 final Iterable<Element> _elementIterable; | 236 final Iterable<Element> _elementIterable; |
183 Iterable<CssStyleDeclaration> _elementCssStyleDeclarationSetIterable; | 237 Iterable<CssStyleDeclaration> _elementCssStyleDeclarationSetIterable; |
184 | 238 |
185 _CssStyleDeclarationSet(this._elementIterable) { | 239 _CssStyleDeclarationSet(this._elementIterable) { |
186 _elementCssStyleDeclarationSetIterable = new List.from( | 240 _elementCssStyleDeclarationSetIterable = new List.from( |
187 _elementIterable).map((e) => e.style); | 241 _elementIterable).map((e) => e.style); |
188 } | 242 } |
189 | 243 |
190 String getPropertyValue(String propertyName) => | 244 String getPropertyValue(String propertyName) => |
191 _elementCssStyleDeclarationSetIterable.first.getPropertyValue( | 245 _elementCssStyleDeclarationSetIterable.first.getPropertyValue( |
192 propertyName); | 246 propertyName); |
193 | 247 |
194 void setProperty(String propertyName, String value, [String priority]) { | 248 void setProperty(String propertyName, String value, [String priority]) { |
195 _elementCssStyleDeclarationSetIterable.forEach((e) => | 249 _elementCssStyleDeclarationSetIterable.forEach((e) => |
196 e.setProperty(propertyName, value, priority)); | 250 e.setProperty(propertyName, value, priority)); |
197 } | 251 } |
198 // Important note: CssStyleDeclarationSet does NOT implement every method | 252 // Important note: CssStyleDeclarationSet does NOT implement every method |
199 // available in CssStyleDeclaration. Some of the methods don't make so much | 253 // available in CssStyleDeclaration. Some of the methods don't make so much |
200 // sense in terms of having a resonable value to return when you're | 254 // sense in terms of having a resonable value to return when you're |
201 // considering a list of Elements. You will need to manually add any of the | 255 // considering a list of Elements. You will need to manually add any of the |
202 // items in the MEMBERS set if you want that functionality. | 256 // items in the MEMBERS set if you want that functionality. |
203 } | 257 } |
204 | 258 |
205 abstract class CssStyleDeclarationBase { | 259 abstract class CssStyleDeclarationBase { |
206 String getPropertyValue(String propertyName); | 260 String getPropertyValue(String propertyName); |
207 void setProperty(String propertyName, String value, [String priority]); | 261 void setProperty(String propertyName, String value, [String priority]); |
208 """ % SOURCE_PATH) | 262 """) |
209 | 263 |
210 class_lines = []; | 264 class_lines = []; |
211 | 265 |
212 seen = set() | 266 seen = set() |
213 for prop in sorted(data, key=lambda p: camelCaseName(p)): | 267 for prop in sorted(data, key=camelCaseName): |
214 camel_case_name = camelCaseName(prop) | 268 camel_case_name = camelCaseName(prop) |
215 upper_camel_case_name = camel_case_name[0].upper() + camel_case_name[1:]; | 269 upper_camel_case_name = camel_case_name[0].upper() + camel_case_name[1:]; |
216 css_name = prop.replace('-webkit-', '') | 270 css_name = prop.replace('-webkit-', '') |
217 base_css_name = prop.replace('-webkit-', '') | 271 base_css_name = prop.replace('-webkit-', '') |
218 | 272 |
219 if base_css_name in seen or base_css_name.startswith('-internal'): | 273 if base_css_name in seen or base_css_name.startswith('-internal'): |
220 continue | 274 continue |
221 seen.add(base_css_name) | 275 seen.add(base_css_name) |
222 | 276 |
223 comment = ' /** %s the value of "' + base_css_name + '" */' | 277 comment = ' /** %s the value of "' + base_css_name + '" */' |
(...skipping 12 matching lines...) Expand all Loading... | |
236 class_lines.append(annotated[base_css_name]) | 290 class_lines.append(annotated[base_css_name]) |
237 class_lines.append(""" | 291 class_lines.append(""" |
238 void set %s(String value) { | 292 void set %s(String value) { |
239 setProperty('%s', value, ''); | 293 setProperty('%s', value, ''); |
240 } | 294 } |
241 """ % (camel_case_name, css_name)) | 295 """ % (camel_case_name, css_name)) |
242 | 296 |
243 class_file.write(''.join(class_lines)); | 297 class_file.write(''.join(class_lines)); |
244 class_file.write('}\n') | 298 class_file.write('}\n') |
245 class_file.close() | 299 class_file.close() |
OLD | NEW |