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 ( | 13 from name_utilities import ( |
14 enum_for_css_keyword, enum_type_name, enum_value_name, class_member_name, me thod_name, | 14 enum_for_css_keyword, enum_type_name, enum_value_name, class_member_name, me thod_name, |
15 join_name | 15 join_name |
16 ) | 16 ) |
17 from collections import OrderedDict | 17 from collections import OrderedDict, defaultdict |
18 | 18 |
19 | 19 |
20 # Temporary hard-coded list of fields that are not CSS properties. | 20 # Temporary hard-coded list of fields that are not CSS properties. |
21 # TODO(shend): Put this into its own JSON5 file. | 21 # TODO(shend): Put this into its own JSON5 file. |
22 NONPROPERTIES = [ | 22 NONPROPERTIES = [ |
23 {'name': 'IsLink', 'field_template': 'monotonic_flag', | 23 {'name': 'IsLink', 'field_template': 'monotonic_flag', |
24 'inherited': False, 'independent': False, 'default_value': False}, | 24 'inherited': False, 'independent': False, 'default_value': False}, |
25 {'name': 'OriginalDisplay', 'field_template': 'keyword', 'default_value': 'i nline', | 25 {'name': 'OriginalDisplay', 'field_template': 'keyword', 'default_value': 'i nline', |
26 'type_name': 'EDisplay', 'inherited': False, 'independent': False, | 26 'type_name': 'EDisplay', 'inherited': False, 'independent': False, |
27 'keywords': [ | 27 'keywords': [ |
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
304 def __init__(self, json5_file_path): | 304 def __init__(self, json5_file_path): |
305 super(ComputedStyleBaseWriter, self).__init__(json5_file_path) | 305 super(ComputedStyleBaseWriter, self).__init__(json5_file_path) |
306 self._outputs = { | 306 self._outputs = { |
307 'ComputedStyleBase.h': self.generate_base_computed_style_h, | 307 'ComputedStyleBase.h': self.generate_base_computed_style_h, |
308 'ComputedStyleBase.cpp': self.generate_base_computed_style_cpp, | 308 'ComputedStyleBase.cpp': self.generate_base_computed_style_cpp, |
309 'ComputedStyleBaseConstants.h': self.generate_base_computed_style_co nstants, | 309 'ComputedStyleBaseConstants.h': self.generate_base_computed_style_co nstants, |
310 } | 310 } |
311 | 311 |
312 # TODO(shend): Remove this once we move NONPROPERTIES to its own JSON fi le, | 312 # TODO(shend): Remove this once we move NONPROPERTIES to its own JSON fi le, |
313 # since the JSON5 reader will handle missing fields and defaults. | 313 # since the JSON5 reader will handle missing fields and defaults. |
314 for property_ in NONPROPERTIES: | 314 nonproperties = [defaultdict(lambda: None, item) for item in NONPROPERTI ES] |
alancutter (OOO until 2018)
2017/04/19 04:36:35
Not sure I'm in favour of a defaultdict. This make
shend
2017/04/19 05:26:05
Done in an explicit loop now.
| |
315 property_['name_for_methods'] = property_['name'] | 315 |
316 if 'field_type_path' not in property_: | 316 for property_ in nonproperties: |
317 property_['field_type_path'] = None | 317 self._apply_property_naming_defaults(property_) |
318 if 'type_name' not in property_: | |
319 property_['type_name'] = 'E' + enum_type_name(property_['name_fo r_methods']) | |
320 property_['getter'] = method_name(property_['name_for_methods']) | |
321 property_['setter'] = method_name(join_name('set', property_['name_f or_methods'])) | |
322 property_['initial'] = method_name(join_name('initial', property_['n ame_for_methods'])) | |
323 | 318 |
324 # Ignore shorthand properties | 319 # Ignore shorthand properties |
325 for property_ in self._properties.values(): | 320 for property_ in self._properties.values(): |
326 if property_['field_template'] is not None: | 321 if property_['field_template'] is not None: |
327 assert not property_['longhands'], \ | 322 assert not property_['longhands'], \ |
328 "Shorthand '{}' cannot have a field_template.".format(proper ty_['name']) | 323 "Shorthand '{}' cannot have a field_template.".format(proper ty_['name']) |
329 | 324 |
330 property_values = [value for value in self._properties.values() if not v alue['longhands']] | 325 property_values = [value for value in self._properties.values() if not v alue['longhands']] |
331 | 326 |
332 for property_ in property_values: | 327 for property_ in property_values: |
333 # Override the type name when field_type_path is specified | 328 # Override the type name when field_type_path is specified |
334 if property_['field_type_path']: | 329 if property_['field_type_path']: |
335 property_['type_name'] = property_['field_type_path'].split('/') [-1] | 330 property_['type_name'] = property_['field_type_path'].split('/') [-1] |
336 # CSS properties are not allowed to explicitly specify their field_s ize. | 331 # CSS properties are not allowed to explicitly specify their field_s ize. |
337 property_['field_size'] = None | 332 property_['field_size'] = None |
338 | 333 |
339 self._generated_enums = _create_enums(property_values + NONPROPERTIES) | 334 self._generated_enums = _create_enums(property_values + nonproperties) |
340 | 335 |
341 all_fields = (_create_fields('property', property_values) + | 336 all_fields = (_create_fields('property', property_values) + |
342 _create_fields('nonproperty', NONPROPERTIES)) | 337 _create_fields('nonproperty', nonproperties)) |
343 | 338 |
344 # Separate the normal fields from the bit fields | 339 # Separate the normal fields from the bit fields |
345 bit_fields = [field for field in all_fields if field.is_bit_field] | 340 bit_fields = [field for field in all_fields if field.is_bit_field] |
346 normal_fields = [field for field in all_fields if not field.is_bit_field ] | 341 normal_fields = [field for field in all_fields if not field.is_bit_field ] |
347 | 342 |
348 # Pack bit fields into buckets | 343 # Pack bit fields into buckets |
349 field_buckets = _pack_fields(bit_fields) | 344 field_buckets = _pack_fields(bit_fields) |
350 | 345 |
351 # The expected size of ComputedStyleBase is equivalent to as many words | 346 # The expected size of ComputedStyleBase is equivalent to as many words |
352 # as the total number of buckets. | 347 # as the total number of buckets. |
(...skipping 16 matching lines...) Expand all Loading... | |
369 (self._expected_bit_field_bytes, real_bit_field_bytes)) | 364 (self._expected_bit_field_bytes, real_bit_field_bytes)) |
370 | 365 |
371 # Normal fields go first, then the bit fields. | 366 # Normal fields go first, then the bit fields. |
372 self._fields = list(normal_fields) | 367 self._fields = list(normal_fields) |
373 | 368 |
374 # Order the fields so fields in each bucket are adjacent. | 369 # Order the fields so fields in each bucket are adjacent. |
375 for bucket in field_buckets: | 370 for bucket in field_buckets: |
376 for field in bucket: | 371 for field in bucket: |
377 self._fields.append(field) | 372 self._fields.append(field) |
378 | 373 |
379 self._include_paths = _get_include_paths(property_values + NONPROPERTIES ) | 374 self._include_paths = _get_include_paths(property_values + nonproperties ) |
380 | 375 |
381 | 376 |
382 @template_expander.use_jinja('ComputedStyleBase.h.tmpl') | 377 @template_expander.use_jinja('ComputedStyleBase.h.tmpl') |
383 def generate_base_computed_style_h(self): | 378 def generate_base_computed_style_h(self): |
384 return { | 379 return { |
385 'properties': self._properties, | 380 'properties': self._properties, |
386 'enums': self._generated_enums, | 381 'enums': self._generated_enums, |
387 'include_paths': self._include_paths, | 382 'include_paths': self._include_paths, |
388 'fields': self._fields, | 383 'fields': self._fields, |
389 } | 384 } |
(...skipping 10 matching lines...) Expand all Loading... | |
400 @template_expander.use_jinja('ComputedStyleBaseConstants.h.tmpl') | 395 @template_expander.use_jinja('ComputedStyleBaseConstants.h.tmpl') |
401 def generate_base_computed_style_constants(self): | 396 def generate_base_computed_style_constants(self): |
402 return { | 397 return { |
403 'properties': self._properties, | 398 'properties': self._properties, |
404 'enums': self._generated_enums, | 399 'enums': self._generated_enums, |
405 'fields': self._fields, | 400 'fields': self._fields, |
406 } | 401 } |
407 | 402 |
408 if __name__ == '__main__': | 403 if __name__ == '__main__': |
409 json5_generator.Maker(ComputedStyleBaseWriter).main() | 404 json5_generator.Maker(ComputedStyleBaseWriter).main() |
OLD | NEW |