UNPKG

4.88 kBPlain TextView Raw
1#!/usr/bin/env vpython
2# Copyright (c) 2014 Google Inc. All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met:
7#
8# * Redistributions of source code must retain the above copyright
9# notice, this list of conditions and the following disclaimer.
10# * Redistributions in binary form must reproduce the above
11# copyright notice, this list of conditions and the following disclaimer
12# in the documentation and/or other materials provided with the
13# distribution.
14# * Neither the name of Google Inc. nor the names of its
15# contributors may be used to endorse or promote products derived from
16# this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30import datetime
31import json
32import os
33import sys
34from os import path
35
36PYJSON5_DIR = os.path.join(os.path.dirname(__file__), '..', '..', 'third_party', 'pyjson5', 'src')
37sys.path.append(PYJSON5_DIR)
38
39import json5 # pylint: disable=import-error
40
41ROOT_DIRECTORY = path.join(path.dirname(__file__), '..', '..')
42GENERATED_LOCATION = path.join(ROOT_DIRECTORY, 'front_end', 'generated', 'SupportedCSSProperties.js')
43READ_LOCATION = path.join(ROOT_DIRECTORY, 'third_party', 'blink', 'renderer', 'core', 'css', 'css_properties.json5')
44
45
46def _keep_only_required_keys(entry):
47 for key in entry.keys():
48 if key not in ("name", "longhands", "svg", "inherited", "keywords"):
49 del entry[key]
50 return entry
51
52
53def properties_from_file(file_name):
54 with open(file_name) as json5_file:
55 doc = json5.loads(json5_file.read())
56
57 properties = []
58 property_names = {}
59 property_values = {}
60 aliases_for = []
61 for entry in doc["data"]:
62 if type(entry) is str:
63 entry = {"name": entry}
64 if "alias_for" in entry:
65 aliases_for.append([entry["name"], entry["alias_for"]])
66 continue
67 # Filter out internal properties.
68 if entry["name"].startswith("-internal-"):
69 continue
70 properties.append(_keep_only_required_keys(entry))
71 property_names[entry["name"]] = entry
72 if "keywords" in entry:
73 keywords = list(
74 filter(lambda keyword: not keyword.startswith("-internal-"),
75 entry["keywords"]))
76 property_values[entry["name"]] = {"values": keywords}
77
78 properties.sort(key=lambda entry: entry["name"])
79 aliases_for.sort(key=lambda entry: entry[0])
80
81 # Filter out unsupported longhands.
82 for property in properties:
83 longhands = property.get("longhands")
84 if not longhands:
85 continue
86 if type(longhands) is str:
87 longhands = longhands.split(";")
88 longhands = [longhand for longhand in longhands if longhand in property_names]
89 if not longhands:
90 del property["longhands"]
91 else:
92 property["longhands"] = longhands
93 all_inherited = True
94 for longhand in longhands:
95 longhand_property = property_names[longhand]
96 all_inherited = all_inherited and ("inherited" in longhand_property) and longhand_property["inherited"]
97 if all_inherited:
98 property["inherited"] = True
99
100 return properties, property_values, aliases_for
101
102
103properties, property_values, aliases_for = properties_from_file(READ_LOCATION)
104now = datetime.datetime.now()
105with open(GENERATED_LOCATION, "w+") as f:
106 f.write('// Copyright %d The Chromium Authors. All rights reserved.\n' % now.year)
107 f.write('// Use of this source code is governed by a BSD-style license that can be\n')
108 f.write('// found in the LICENSE file.\n')
109 f.write('\n')
110 f.write("export const generatedProperties = %s;\n" % json.dumps(properties))
111 # sort keys to ensure entries are generated in a deterministic way to avoid inconsistencies across different OS
112 f.write("export const generatedPropertyValues = %s;\n" % json.dumps(property_values, sort_keys=True))
113 f.write("export const generatedAliasesFor = new Map(%s);\n" % json.dumps(aliases_for))