| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/python2.6 | |
| 2 # | |
| 3 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
| 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. | |
| 6 | |
| 7 """Generates Css.dart from css property definitions defined in WebKit.""" | |
| 8 | |
| 9 import tempfile, os | |
| 10 | |
| 11 COMMENT_LINE_PREFIX = ' * ' | |
| 12 SOURCE_PATH = 'Source/WebCore/css/CSSPropertyNames.in' | |
| 13 INPUT_URL = 'http://trac.webkit.org/export/latest/trunk/%s' % SOURCE_PATH | |
| 14 OUTPUT_FILE = '../Css.dart' | |
| 15 | |
| 16 def main(): | |
| 17 _, css_names_file = tempfile.mkstemp('.CSSPropertyNames.in') | |
| 18 try: | |
| 19 if os.system('wget %s -O %s' % (INPUT_URL, css_names_file)): | |
| 20 return 1 | |
| 21 generate_code(css_names_file) | |
| 22 print 'Successfully generated ' + OUTPUT_FILE | |
| 23 finally: | |
| 24 os.remove(css_names_file) | |
| 25 | |
| 26 def camelCaseName(name): | |
| 27 """Convert a CSS property name to a lowerCamelCase name.""" | |
| 28 name = name.replace('-webkit-', '') | |
| 29 words = [] | |
| 30 for word in name.split('-'): | |
| 31 if words: | |
| 32 words.append(word.title()) | |
| 33 else: | |
| 34 words.append(word) | |
| 35 return ''.join(words) | |
| 36 | |
| 37 def generate_code(input_path): | |
| 38 data = open(input_path).readlines() | |
| 39 | |
| 40 # filter CSSPropertyNames.in to only the properties | |
| 41 data = [d[:-1] for d in data | |
| 42 if len(d) > 1 | |
| 43 and not d.startswith('#') | |
| 44 and not d.startswith('//') | |
| 45 and not '=' in d] | |
| 46 | |
| 47 output_file = open(OUTPUT_FILE, 'w') | |
| 48 | |
| 49 output_file.write(""" | |
| 50 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
| 51 // for details. All rights reserved. Use of this source code is governed by a | |
| 52 // BSD-style license that can be found in the LICENSE file. | |
| 53 | |
| 54 // WARNING: Do not edit. | |
| 55 // This file was generated by base/scripts/css_code_generator.py | |
| 56 | |
| 57 // Source of CSS properties: | |
| 58 // %s | |
| 59 | |
| 60 // TODO(jacobr): add versions that take numeric values in px, miliseconds, etc. | |
| 61 | |
| 62 /** | |
| 63 * Browser neutral and typesafe class for setting CSS styles from Dart. | |
| 64 * This class smoothes over browser differences. | |
| 65 */ | |
| 66 class Css { | |
| 67 static String _cachedBrowserPrefix; | |
| 68 | |
| 69 final CSSStyleDeclaration raw; | |
| 70 Css(CSSStyleDeclaration this.raw) { } | |
| 71 | |
| 72 static String get _browserPrefix() { | |
| 73 if (_cachedBrowserPrefix === null) { | |
| 74 if (Device.isFirefox) { | |
| 75 _cachedBrowserPrefix = '-moz-'; | |
| 76 } else { | |
| 77 _cachedBrowserPrefix = '-webkit-'; | |
| 78 } | |
| 79 // TODO(jacobr): support IE 9.0 and Opera as well. | |
| 80 } | |
| 81 return _cachedBrowserPrefix; | |
| 82 } | |
| 83 """.lstrip() % SOURCE_PATH); | |
| 84 | |
| 85 static_method_lines = []; | |
| 86 property_lines = []; | |
| 87 | |
| 88 seen = set() | |
| 89 for prop in sorted(data, key=lambda p: camelCaseName(p)): | |
| 90 camel_case_name = camelCaseName(prop) | |
| 91 upper_camel_case_name = camel_case_name[0].upper() + camel_case_name[1:]; | |
| 92 css_name = prop.replace('-webkit-', '${_browserPrefix}') | |
| 93 base_css_name = prop.replace('-webkit-', '') | |
| 94 | |
| 95 if base_css_name in seen: | |
| 96 continue | |
| 97 seen.add(base_css_name) | |
| 98 | |
| 99 comment = ' /** %s the value of "' + base_css_name + '" */' | |
| 100 | |
| 101 static_method_lines.append('\n'); | |
| 102 static_method_lines.append(comment % 'Gets') | |
| 103 static_method_lines.append(""" | |
| 104 static String get%s(CSSStyleDeclaration style) { | |
| 105 return style.getPropertyValue('%s'); | |
| 106 } | |
| 107 | |
| 108 """ % (upper_camel_case_name, css_name)) | |
| 109 | |
| 110 static_method_lines.append(comment % 'Sets') | |
| 111 static_method_lines.append(""" | |
| 112 static void set%s(CSSStyleDeclaration style, String value) { | |
| 113 style.setProperty('%s', value, ''); | |
| 114 } | |
| 115 """ % (upper_camel_case_name, css_name)) | |
| 116 | |
| 117 property_lines.append('\n') | |
| 118 property_lines.append(comment % 'Gets') | |
| 119 property_lines.append(""" | |
| 120 String get %s() { | |
| 121 return get%s(raw); | |
| 122 } | |
| 123 | |
| 124 """ % (camel_case_name, upper_camel_case_name)) | |
| 125 | |
| 126 property_lines.append(comment % 'Sets') | |
| 127 property_lines.append(""" | |
| 128 void set %s(String value) { | |
| 129 set%s(raw, value); | |
| 130 } | |
| 131 """ % (camel_case_name, upper_camel_case_name)) | |
| 132 | |
| 133 output_file.write(''.join(static_method_lines)); | |
| 134 output_file.write(''.join(property_lines)); | |
| 135 output_file.write('}\n') | |
| 136 output_file.close() | |
| 137 | |
| 138 if __name__ == '__main__': | |
| 139 main() | |
| OLD | NEW |