tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

GenerateCSSPropsGenerated.py (3623B)


      1 # This Source Code Form is subject to the terms of the Mozilla Public
      2 # License, v. 2.0. If a copy of the MPL was not distributed with this file,
      3 # You can obtain one at http://mozilla.org/MPL/2.0/.
      4 
      5 import runpy
      6 import sys
      7 import string
      8 import argparse
      9 
     10 
     11 class PropertyWrapper(object):
     12    __slots__ = ["index", "prop", "idlname"]
     13 
     14    def __init__(self, index, prop):
     15        self.index = index
     16        self.prop = prop
     17        if "Internal" in prop.flags:
     18            self.idlname = None
     19        else:
     20            idl_name = prop.method
     21            if not idl_name.startswith("Moz"):
     22                idl_name = idl_name[0].lower() + idl_name[1:]
     23            self.idlname = idl_name
     24 
     25    def __getattr__(self, name):
     26        return getattr(self.prop, name)
     27 
     28 
     29 def generate(output, dataFile):
     30    output.write(
     31        """/* THIS IS AN AUTOGENERATED FILE.  DO NOT EDIT */
     32 
     33 /* processed file that defines CSS property tables that can't be generated
     34   with the pre-processor, designed to be #included in nsCSSProps.cpp */
     35 
     36 """
     37    )
     38 
     39    raw_properties = runpy.run_path(dataFile)["data"]
     40    properties = [
     41        PropertyWrapper(i, p)
     42        for i, p in enumerate(raw_properties.values())
     43        if p.type() != "alias"
     44    ]
     45 
     46    # Generate kIDLNameTable
     47    output.write(
     48        "const char* const nsCSSProps::" "kIDLNameTable[eCSSProperty_COUNT] = {\n"
     49    )
     50    for p in properties:
     51        if p.idlname is None:
     52            output.write("  nullptr,  // {}\n".format(p.name))
     53        else:
     54            output.write('  "{}",\n'.format(p.idlname))
     55    output.write("};\n\n")
     56 
     57    # Generate kIDLNameSortPositionTable
     58    ps = sorted(properties, key=lambda p: p.idlname if p.idlname else "")
     59    ps = [(p, position) for position, p in enumerate(ps)]
     60    ps.sort(key=lambda item: item[0].index)
     61    output.write(
     62        "const int32_t nsCSSProps::"
     63        "kIDLNameSortPositionTable[eCSSProperty_COUNT] = {\n"
     64    )
     65    for p, position in ps:
     66        output.write("  {},\n".format(position))
     67    output.write("};\n\n")
     68 
     69    # Generate preferences table
     70    output.write(
     71        "const nsCSSProps::PropertyPref " "nsCSSProps::kPropertyPrefTable[] = {\n"
     72    )
     73    for p in raw_properties.values():
     74        if not p.pref:
     75            continue
     76        if p.type() != "alias":
     77            prop_id = "eCSSProperty_" + p.id
     78        else:
     79            prop_id = "eCSSPropertyAlias_" + p.alias_id
     80        output.write('  {{ {}, "{}" }},\n'.format(prop_id, p.pref))
     81    output.write("  { eCSSProperty_UNKNOWN, nullptr },\n")
     82    output.write("};\n\n")
     83 
     84    # Generate shorthand subprop tables
     85    names = []
     86    for p in properties:
     87        if p.type() != "shorthand":
     88            continue
     89        name = "g{}SubpropTable".format(p.method)
     90        names.append(name)
     91        output.write("static const NonCustomCSSPropertyId {}[] = {{\n".format(name))
     92        for subprop in p.subprops:
     93            output.write("  eCSSProperty_{},\n".format(subprop))
     94        output.write("  eCSSProperty_UNKNOWN\n")
     95        output.write("};\n\n")
     96    output.write("const NonCustomCSSPropertyId* const\n")
     97    output.write(
     98        "nsCSSProps::kSubpropertyTable["
     99        "eCSSProperty_COUNT - eCSSProperty_COUNT_no_shorthands"
    100        "] = {\n"
    101    )
    102    for name in names:
    103        output.write("  {},\n".format(name))
    104    output.write("};\n\n")
    105 
    106    # Generate assertions
    107    msg = (
    108        "GenerateCSSPropsGenerated.py did not list properties "
    109        "in NonCustomCSSPropertyId order"
    110    )
    111    for p in properties:
    112        output.write(
    113            'static_assert(eCSSProperty_{} == {}, "{}");\n'.format(p.id, p.index, msg)
    114        )