Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2016 The Chromium Authors. All rights reserved. | 2 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 import math | 6 import math |
| 7 import sys | 7 import sys |
| 8 | 8 |
| 9 import json5_generator | 9 import json5_generator |
| 10 import template_expander | 10 import template_expander |
| 11 import make_style_builder | 11 import make_style_builder |
| 12 | 12 |
| 13 from name_utilities import camel_case, lower_first, upper_first_letter, enum_for _css_keyword | 13 from name_utilities import camel_case, lower_first, upper_first_letter, enum_for _css_keyword |
| 14 | 14 |
| 15 | 15 |
| 16 # Temporary hard-coded list of fields that are not CSS properties. | 16 # Temporary hard-coded list of fields that are not CSS properties. |
| 17 # Ideally these would be specified in a .in or .json5 file. | 17 # Ideally these would be specified in a .json5 file. |
| 18 NONPROPERTY_FIELDS = set([ | 18 NONPROPERTY_FIELDS = [ |
| 19 # Style can not be shared. | 19 # Style can not be shared. |
| 20 'unique', | 20 {'name': 'unique', 'field_template': 'monotonic_flag'}, |
|
meade_UTC10
2017/03/15 06:24:54
Can you please add something to the CL description
shend
2017/03/16 02:08:01
Done.
| |
| 21 # Whether this style is affected by these pseudo-classes. | 21 # Whether this style is affected by these pseudo-classes. |
| 22 'affectedByFocus', | 22 {'name': 'affectedByFocus', 'field_template': 'monotonic_flag'}, |
| 23 'affectedByHover', | 23 {'name': 'affectedByHover', 'field_template': 'monotonic_flag'}, |
| 24 'affectedByActive', | 24 {'name': 'affectedByActive', 'field_template': 'monotonic_flag'}, |
| 25 'affectedByDrag', | 25 {'name': 'affectedByDrag', 'field_template': 'monotonic_flag'}, |
| 26 # A non-inherited property references a variable or @apply is used | 26 # A non-inherited property references a variable or @apply is used |
| 27 'hasVariableReferenceFromNonInheritedProperty', | 27 {'name': 'hasVariableReferenceFromNonInheritedProperty', 'field_template': ' monotonic_flag'}, |
| 28 # Explicitly inherits a non-inherited property | 28 # Explicitly inherits a non-inherited property |
| 29 'hasExplicitlyInheritedProperties', | 29 {'name': 'hasExplicitlyInheritedProperties', 'field_template': 'monotonic_fl ag'} |
| 30 ]) | 30 ] |
| 31 | 31 |
| 32 | 32 |
| 33 class Field(object): | 33 class Field(object): |
| 34 """ | 34 """ |
| 35 The generated ComputedStyle object is made up of a series of Fields. | 35 The generated ComputedStyle object is made up of a series of Fields. |
| 36 Each Field has a name, size, type, etc, and a bunch of attributes to | 36 Each Field has a name, size, type, etc, and a bunch of attributes to |
| 37 determine which methods it will be used in. | 37 determine which methods it will be used in. |
| 38 | 38 |
| 39 A Field also has enough information to use any storage type in C++, such as | 39 A Field also has enough information to use any storage type in C++, such as |
| 40 regular member variables, or more complex storage like vectors or hashmaps. | 40 regular member variables, or more complex storage like vectors or hashmaps. |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 193 field_template='flag', | 193 field_template='flag', |
| 194 size=1, | 194 size=1, |
| 195 default_value='true', | 195 default_value='true', |
| 196 getter_method_name=field_name_suffix_lower, | 196 getter_method_name=field_name_suffix_lower, |
| 197 setter_method_name='set' + field_name_suffix_upper, | 197 setter_method_name='set' + field_name_suffix_upper, |
| 198 initial_method_name='initial' + field_name_suffix_upper, | 198 initial_method_name='initial' + field_name_suffix_upper, |
| 199 resetter_method_name='reset' + field_name_suffix_upper, | 199 resetter_method_name='reset' + field_name_suffix_upper, |
| 200 ) | 200 ) |
| 201 | 201 |
| 202 | 202 |
| 203 def _create_nonproperty_field(field_name): | 203 def _create_nonproperty_field(property_): |
| 204 """ | 204 """ |
| 205 Create a nonproperty field from its name and return the Field object. | 205 Create a nonproperty field from a property dict and return the Field object. |
|
meade_UTC10
2017/03/15 06:24:54
What's a property dict? Need a comment or rewordin
shend
2017/03/16 02:08:01
Done
| |
| 206 """ | 206 """ |
| 207 member_name = 'm_' + field_name | 207 # TODO(shend): Make this work for nonflags |
| 208 field_name_upper = upper_first_letter(field_name) | 208 assert property_['field_template'] in ('flag', 'monotonic_flag'), \ |
| 209 "Nonproperties with arbitrary templates are not yet supported" | |
| 210 member_name = 'm_' + property_['name'] | |
| 211 field_name_upper = upper_first_letter(property_['name']) | |
| 209 | 212 |
| 210 return Field( | 213 return Field( |
| 211 'nonproperty', | 214 'nonproperty', |
| 212 name=member_name, | 215 name=member_name, |
| 213 property_name=field_name, | 216 property_name=property_['name'], |
| 214 type_name='bool', | 217 type_name='bool', |
| 215 field_template='monotonic_flag', | 218 field_template=property_['field_template'], |
| 216 size=1, | 219 size=1, |
| 217 default_value='false', | 220 default_value='false', |
| 218 getter_method_name=field_name, | 221 getter_method_name=property_['name'], |
| 219 setter_method_name='set' + field_name_upper, | 222 setter_method_name='set' + field_name_upper, |
| 220 initial_method_name='initial' + field_name_upper, | 223 initial_method_name='initial' + field_name_upper, |
| 221 resetter_method_name='reset' + field_name_upper, | 224 resetter_method_name='reset' + field_name_upper, |
| 222 ) | 225 ) |
| 223 | 226 |
| 224 | 227 |
| 225 def _create_fields(properties): | 228 def _create_fields(properties): |
| 226 """ | 229 """ |
| 227 Create ComputedStyle fields from CSS properties and return a list of Field o bjects. | 230 Create ComputedStyle fields from CSS properties and return a list of Field o bjects. |
| 228 """ | 231 """ |
| 229 fields = [] | 232 fields = [] |
| 230 for property_ in properties: | 233 for property_ in properties: |
| 231 # Only generate properties that have a field template | 234 # Only generate properties that have a field template |
| 232 if property_['field_template'] is not None: | 235 if property_['field_template'] is not None: |
| 233 # If the property is independent, add the single-bit sized isInherit ed flag | 236 # If the property is independent, add the single-bit sized isInherit ed flag |
| 234 # to the list of Fields as well. | 237 # to the list of Fields as well. |
| 235 if property_['independent']: | 238 if property_['independent']: |
| 236 fields.append(_create_inherited_flag_field(property_)) | 239 fields.append(_create_inherited_flag_field(property_)) |
| 237 | 240 |
| 238 fields.append(_create_property_field(property_)) | 241 fields.append(_create_property_field(property_)) |
| 239 | 242 |
| 240 for field_name in NONPROPERTY_FIELDS: | 243 # TODO(shend): Merge NONPROPERTY_FIELDS with property_values so that propert ies and |
| 241 fields.append(_create_nonproperty_field(field_name)) | 244 # nonproperties can be treated uniformly. |
| 245 for property_ in NONPROPERTY_FIELDS: | |
| 246 fields.append(_create_nonproperty_field(property_)) | |
| 242 | 247 |
| 243 return fields | 248 return fields |
| 244 | 249 |
| 245 | 250 |
| 246 def _pack_fields(fields): | 251 def _pack_fields(fields): |
| 247 """ | 252 """ |
| 248 Group a list of fields into buckets to minimise padding. | 253 Group a list of fields into buckets to minimise padding. |
| 249 Returns a list of buckets, where each bucket is a list of Field objects. | 254 Returns a list of buckets, where each bucket is a list of Field objects. |
| 250 """ | 255 """ |
| 251 # Since fields cannot cross word boundaries, in order to minimize | 256 # Since fields cannot cross word boundaries, in order to minimize |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 370 'mapping': [('k' + camel_case(k), enum_for_css_keyword(k)) f or k in property_['keywords']], | 375 'mapping': [('k' + camel_case(k), enum_for_css_keyword(k)) f or k in property_['keywords']], |
| 371 } | 376 } |
| 372 | 377 |
| 373 return { | 378 return { |
| 374 'include_paths': self._include_paths, | 379 'include_paths': self._include_paths, |
| 375 'mappings': mappings, | 380 'mappings': mappings, |
| 376 } | 381 } |
| 377 | 382 |
| 378 if __name__ == '__main__': | 383 if __name__ == '__main__': |
| 379 json5_generator.Maker(ComputedStyleBaseWriter).main() | 384 json5_generator.Maker(ComputedStyleBaseWriter).main() |
| OLD | NEW |