tor-browser

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

GenerateCSSPropListWebIDL.py (5343B)


      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 # This script generates properties/descriptor binding webidl based on
      6 # ServoCSSPropList.
      7 
      8 import runpy
      9 
     10 
     11 # Generates a line of WebIDL with the given spelling of the property name
     12 # (whether camelCase, _underscorePrefixed, etc.) and the given array of
     13 # extended attributes.
     14 def generateLine(propName, extendedAttrs):
     15    return "  [%s] attribute [LegacyNullToEmptyString] UTF8String %s;\n" % (
     16        ", ".join(extendedAttrs),
     17        propName,
     18    )
     19 
     20 
     21 def generate(output, dataFile, ruleType, interfaceName, bindingTemplate, pref=None):
     22    propsData = runpy.run_path(dataFile)["data"]
     23    output.write(
     24        """/* THIS IS AN AUTOGENERATED FILE.  DO NOT EDIT */
     25 
     26 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     27 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
     28 /* This Source Code Form is subject to the terms of the Mozilla Public
     29 * License, v. 2.0. If a copy of the MPL was not distributed with this
     30 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
     31 
     32 [Exposed=Window"""
     33    )
     34    if pref:
     35        output.write(', Pref="' + pref + '"')
     36    output.write(
     37        """]
     38 interface """
     39        + interfaceName
     40        + " : CSSStyleDeclaration {\n"
     41    )
     42    for p in propsData.values():
     43        # Skip properties which aren't valid in style rules.
     44        if ruleType not in p.rules:
     45            continue
     46 
     47        pref = p.pref
     48 
     49        if p.type() == "alias":
     50            if p.method == "MozAppearance":
     51                # Hide MozAppearance from CSSStyleProperties to prevent outdated
     52                # special casing against Gecko. (Bug 1977489)
     53                pref = "layout.css.moz-appearance.webidl.enabled"
     54            elif p.pref == propsData[p.prop_id].pref:
     55                # We already added this as a BindingAlias for the original prop.
     56                continue
     57 
     58            propId = p.prop_id
     59        else:
     60            propId = p.id
     61        extendedAttrs = [
     62            "BindingTemplate=(%s, eCSSProperty_%s)" % (bindingTemplate, propId),
     63            "CEReactions",
     64            "SetterThrows",
     65            "SetterNeedsSubjectPrincipal=NonSystem",
     66        ]
     67 
     68        if pref != "":
     69            assert "Internal" not in p.flags
     70            # BackdropFilter is a special case where we want WebIDL to check
     71            # a function instead of checking the pref directly.
     72            if p.method == "BackdropFilter":
     73                extendedAttrs.append('Func="nsCSSProps::IsBackdropFilterAvailable"')
     74            else:
     75                extendedAttrs.append('Pref="%s"' % pref)
     76        elif "EnabledInUASheetsAndChrome" in p.flags:
     77            extendedAttrs.append("ChromeOnly")
     78        elif "Internal" in p.flags:
     79            continue
     80 
     81        def add_extra_accessors(p):
     82            prop = p.method
     83 
     84            # webkit properties get a camelcase "webkitFoo" accessor
     85            # as well as a capitalized "WebkitFoo" alias (added here).
     86            if prop.startswith("Webkit"):
     87                extendedAttrs.append('BindingAlias="%s"' % prop)
     88 
     89            # Generate a name with camelCase spelling of property-name (or capitalized,
     90            # for Moz-prefixed properties):
     91            if not prop.startswith("Moz"):
     92                prop = prop[0].lower() + prop[1:]
     93 
     94            # Per spec, what's actually supposed to happen here is that we're supposed
     95            # to have properties for:
     96            #
     97            # 1) Each supported CSS property name, camelCased.
     98            # 2) Each supported name that contains or starts with dashes,
     99            #    without any changes to the name.
    100            # 3) cssFloat
    101            #
    102            # Note that "float" will cause a property called "float" to exist due to (1)
    103            # in that list.
    104            #
    105            # In practice, cssFloat is the only case in which "name" doesn't contain
    106            # "-" but also doesn't match "prop".  So the generateLine() call will
    107            # cover (3) and all of (1) except "float".  If we now add an alias
    108            # for all the cases where "name" doesn't match "prop", that will cover
    109            # "float" and (2).
    110            if prop != p.name:
    111                extendedAttrs.append('BindingAlias="%s"' % p.name)
    112 
    113            return prop
    114 
    115        prop = add_extra_accessors(p)
    116 
    117        if p.type() != "alias":
    118            for a in p.aliases:
    119                if p.pref == propsData[a].pref:
    120                    newProp = add_extra_accessors(propsData[a])
    121                    extendedAttrs.append('BindingAlias="%s"' % newProp)
    122 
    123        output.write(generateLine(prop, extendedAttrs))
    124 
    125    output.write("};")
    126 
    127 
    128 def generateCSSStyleProperties(output, dataFile):
    129    generate(output, dataFile, "Style", "CSSStyleProperties", "CSS2Property")
    130 
    131 
    132 def generateCSSPageDescriptors(output, dataFile):
    133    generate(
    134        output,
    135        dataFile,
    136        "Page",
    137        "CSSPageDescriptors",
    138        "CSSPageDescriptor",
    139    )
    140 
    141 
    142 def generateCSSPositionTryDescriptors(output, dataFile):
    143    generate(
    144        output,
    145        dataFile,
    146        "PositionTry",
    147        "CSSPositionTryDescriptors",
    148        "CSSPositionTryDescriptor",
    149        "layout.css.anchor-positioning.enabled",
    150    )