Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(872)

Side by Side Diff: client/base/scripts/css_code_generator.py

Issue 8360025: Move the individual property definitions from client/base/Css to CSSStyleDeclaration. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 #!/usr/bin/python2.6 1 #!/usr/bin/python2.6
2 # 2 #
3 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 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 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.
Jacob 2011/10/21 17:12:23 Move this script to the client/html/scripts direct
nweiz 2011/10/24 19:48:28 Done.
6 6
7 """Generates Css.dart from css property definitions defined in WebKit.""" 7 """Generates CSSStyleDeclaration from css property definitions defined in WebKit ."""
8 8
9 import tempfile, os 9 import tempfile, os
10 10
11 COMMENT_LINE_PREFIX = ' * ' 11 COMMENT_LINE_PREFIX = ' * '
12 SOURCE_PATH = 'Source/WebCore/css/CSSPropertyNames.in' 12 SOURCE_PATH = 'Source/WebCore/css/CSSPropertyNames.in'
13 INPUT_URL = 'http://trac.webkit.org/export/latest/trunk/%s' % SOURCE_PATH 13 INPUT_URL = 'http://trac.webkit.org/export/latest/trunk/%s' % SOURCE_PATH
14 OUTPUT_FILE = '../Css.dart' 14 INTERFACE_FILE = '../../html/src/CSSStyleDeclaration.dart'
15 CLASS_FILE = '../../html/src/CSSStyleDeclarationWrappingImplementation.dart'
15 16
16 def main(): 17 def main():
17 _, css_names_file = tempfile.mkstemp('.CSSPropertyNames.in') 18 _, css_names_file = tempfile.mkstemp('.CSSPropertyNames.in')
18 try: 19 try:
19 if os.system('wget %s -O %s' % (INPUT_URL, css_names_file)): 20 if os.system('wget %s -O %s' % (INPUT_URL, css_names_file)):
20 return 1 21 return 1
21 generate_code(css_names_file) 22 generate_code(css_names_file)
22 print 'Successfully generated ' + OUTPUT_FILE 23 print 'Successfully generated %s and %s' % (INTERFACE_FILE, CLASS_FILE)
23 finally: 24 finally:
24 os.remove(css_names_file) 25 os.remove(css_names_file)
25 26
26 def camelCaseName(name): 27 def camelCaseName(name):
27 """Convert a CSS property name to a lowerCamelCase name.""" 28 """Convert a CSS property name to a lowerCamelCase name."""
28 name = name.replace('-webkit-', '') 29 name = name.replace('-webkit-', '')
29 words = [] 30 words = []
30 for word in name.split('-'): 31 for word in name.split('-'):
31 if words: 32 if words:
32 words.append(word.title()) 33 words.append(word.title())
33 else: 34 else:
34 words.append(word) 35 words.append(word)
35 return ''.join(words) 36 return ''.join(words)
36 37
37 def generate_code(input_path): 38 def generate_code(input_path):
38 data = open(input_path).readlines() 39 data = open(input_path).readlines()
39 40
40 # filter CSSPropertyNames.in to only the properties 41 # filter CSSPropertyNames.in to only the properties
41 data = [d[:-1] for d in data 42 data = [d[:-1] for d in data
42 if len(d) > 1 43 if len(d) > 1
43 and not d.startswith('#') 44 and not d.startswith('#')
44 and not d.startswith('//') 45 and not d.startswith('//')
45 and not '=' in d] 46 and not '=' in d]
46 47
47 output_file = open(OUTPUT_FILE, 'w') 48 interface_file = open(INTERFACE_FILE, 'w')
49 class_file = open(CLASS_FILE, 'w')
48 50
49 output_file.write(""" 51 interface_file.write("""
50 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 52 // 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 53 // 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. 54 // BSD-style license that can be found in the LICENSE file.
53 55
54 // WARNING: Do not edit. 56 // WARNING: Do not edit.
55 // This file was generated by base/scripts/css_code_generator.py 57 // This file was generated by base/scripts/css_code_generator.py
56 58
57 // Source of CSS properties: 59 // Source of CSS properties:
58 // %s 60 // %s
59 61
60 // TODO(jacobr): add versions that take numeric values in px, miliseconds, etc. 62 // TODO(jacobr): add versions that take numeric values in px, miliseconds, etc.
61 63
62 /** 64 interface CSSStyleDeclaration {
63 * Browser neutral and typesafe class for setting CSS styles from Dart. 65
64 * This class smoothes over browser differences. 66 String get cssText();
65 */ 67
66 class Css { 68 void set cssText(String value);
69
70 int get length();
71
72 CSSRule get parentRule();
73
74 CSSValue getPropertyCSSValue(String propertyName);
75
76 String getPropertyPriority(String propertyName);
77
78 String getPropertyShorthand(String propertyName);
79
80 String getPropertyValue(String propertyName);
81
82 bool isPropertyImplicit(String propertyName);
83
84 String item(int index);
85
86 String removeProperty(String propertyName);
87
88 void setProperty(String propertyName, String value, [String priority]);
89
90 """.lstrip() % SOURCE_PATH)
91
92
93 class_file.write("""
94 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
95 // for details. All rights reserved. Use of this source code is governed by a
96 // BSD-style license that can be found in the LICENSE file.
97
98 // WARNING: Do not edit.
99 // This file was generated by base/scripts/css_code_generator.py
Jacob 2011/10/21 17:12:23 update this directory as part of the move
nweiz 2011/10/24 19:48:28 Done.
100
101 // Source of CSS properties:
102 // %s
103
104 // TODO(jacobr): add versions that take numeric values in px, miliseconds, etc.
105
106 class CSSStyleDeclarationWrappingImplementation extends DOMWrapperBase implement s CSSStyleDeclaration {
67 static String _cachedBrowserPrefix; 107 static String _cachedBrowserPrefix;
68 108
69 final CSSStyleDeclaration raw; 109 CSSStyleDeclarationWrappingImplementation._wrap(ptr) : super._wrap(ptr) {}
70 Css(CSSStyleDeclaration this.raw) { }
71 110
72 static String get _browserPrefix() { 111 static String get _browserPrefix() {
73 if (_cachedBrowserPrefix === null) { 112 if (_cachedBrowserPrefix === null) {
74 if (Device.isFirefox) { 113 if (Device.isFirefox) {
75 _cachedBrowserPrefix = '-moz-'; 114 _cachedBrowserPrefix = '-moz-';
76 } else { 115 } else {
77 _cachedBrowserPrefix = '-webkit-'; 116 _cachedBrowserPrefix = '-webkit-';
78 } 117 }
79 // TODO(jacobr): support IE 9.0 and Opera as well. 118 // TODO(jacobr): support IE 9.0 and Opera as well.
80 } 119 }
81 return _cachedBrowserPrefix; 120 return _cachedBrowserPrefix;
82 } 121 }
83 """.lstrip() % SOURCE_PATH);
84 122
85 static_method_lines = []; 123 String get cssText() { return _ptr.cssText; }
86 property_lines = []; 124
125 void set cssText(String value) { _ptr.cssText = value; }
126
127 int get length() { return _ptr.length; }
128
129 CSSRule get parentRule() { return LevelDom.wrapCSSRule(_ptr.parentRule); }
130
131 CSSValue getPropertyCSSValue(String propertyName) {
132 return LevelDom.wrapCSSValue(_ptr.getPropertyCSSValue(propertyName));
133 }
134
135 String getPropertyPriority(String propertyName) {
136 return _ptr.getPropertyPriority(propertyName);
137 }
138
139 String getPropertyShorthand(String propertyName) {
140 return _ptr.getPropertyShorthand(propertyName);
141 }
142
143 String getPropertyValue(String propertyName) {
144 return _ptr.getPropertyValue(propertyName);
145 }
146
147 bool isPropertyImplicit(String propertyName) {
148 return _ptr.isPropertyImplicit(propertyName);
149 }
150
151 String item(int index) {
152 return _ptr.item(index);
153 }
154
155 String removeProperty(String propertyName) {
156 return _ptr.removeProperty(propertyName);
157 }
158
159 void setProperty(String propertyName, String value, [String priority = '']) {
160 _ptr.setProperty(propertyName, value, priority);
161 }
162
163 String get typeName() { return "CSSStyleDeclaration"; }
164
165 """.lstrip() % SOURCE_PATH)
166
167 interface_lines = [];
168 class_lines = [];
87 169
88 seen = set() 170 seen = set()
89 for prop in sorted(data, key=lambda p: camelCaseName(p)): 171 for prop in sorted(data, key=lambda p: camelCaseName(p)):
90 camel_case_name = camelCaseName(prop) 172 camel_case_name = camelCaseName(prop)
91 upper_camel_case_name = camel_case_name[0].upper() + camel_case_name[1:]; 173 upper_camel_case_name = camel_case_name[0].upper() + camel_case_name[1:];
92 css_name = prop.replace('-webkit-', '${_browserPrefix}') 174 css_name = prop.replace('-webkit-', '${CSSStyleDeclarationWrappingImplementa tion._browserPrefix}')
93 base_css_name = prop.replace('-webkit-', '') 175 base_css_name = prop.replace('-webkit-', '')
94 176
95 if base_css_name in seen: 177 if base_css_name in seen:
96 continue 178 continue
97 seen.add(base_css_name) 179 seen.add(base_css_name)
98 180
99 comment = ' /** %s the value of "' + base_css_name + '" */' 181 comment = ' /** %s the value of "' + base_css_name + '" */'
100 182
101 static_method_lines.append('\n'); 183 interface_lines.append(comment % 'Gets')
102 static_method_lines.append(comment % 'Gets') 184 interface_lines.append("""
103 static_method_lines.append(""" 185 String get %s();
104 static String get%s(CSSStyleDeclaration style) { 186
105 return style.getPropertyValue('%s'); 187 """ % camel_case_name)
188
189 interface_lines.append(comment % 'Sets')
190 interface_lines.append("""
191 void set %s(String value);
192
193 """ % camel_case_name)
194
195 class_lines.append('\n');
196 class_lines.append(comment % 'Gets')
197 class_lines.append("""
198 String get %s() =>
199 getPropertyValue('%s');
200
201 """ % (camel_case_name, css_name))
202
203 class_lines.append(comment % 'Sets')
204 class_lines.append("""
205 void set %s(String value) {
206 setProperty('%s', value, '');
106 } 207 }
208 """ % (camel_case_name, css_name))
107 209
108 """ % (upper_camel_case_name, css_name)) 210 interface_file.write(''.join(interface_lines));
211 interface_file.write('}\n')
212 interface_file.close()
109 213
110 static_method_lines.append(comment % 'Sets') 214 class_file.write(''.join(class_lines));
111 static_method_lines.append(""" 215 class_file.write('}\n')
112 static void set%s(CSSStyleDeclaration style, String value) { 216 class_file.close()
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 217
138 if __name__ == '__main__': 218 if __name__ == '__main__':
139 main() 219 main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698