GenerateCSS2PropertiesWebIDL.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. import sys
  5. import string
  6. import argparse
  7. import subprocess
  8. import buildconfig
  9. from mozbuild import shellutil
  10. # Generates a line of WebIDL with the given spelling of the property name
  11. # (whether camelCase, _underscorePrefixed, etc.) and the given array of
  12. # extended attributes.
  13. def generateLine(propName, extendedAttrs):
  14. return " [%s] attribute DOMString %s;\n" % (", ".join(extendedAttrs),
  15. propName)
  16. def generate(output, idlFilename, preprocessorHeader):
  17. print(idlFilename)
  18. sys.stdout.flush()
  19. cpp = list(buildconfig.substs['CPP'])
  20. cpp += shellutil.split(buildconfig.substs['ACDEFINES'])
  21. cpp.append(preprocessorHeader)
  22. preprocessed = subprocess.check_output(cpp)
  23. propList = eval(preprocessed)
  24. props = ""
  25. for [name, prop, id, flags, pref, proptype] in propList:
  26. if "CSS_PROPERTY_INTERNAL" in flags:
  27. continue
  28. # Unfortunately, even some of the getters here are fallible
  29. # (e.g. on nsComputedDOMStyle).
  30. extendedAttrs = ["Throws", "TreatNullAs=EmptyString"]
  31. if pref is not "":
  32. extendedAttrs.append('Pref="%s"' % pref)
  33. # webkit properties get a capitalized "WebkitFoo" accessor (added here)
  34. # as well as a camelcase "webkitFoo" accessor (added next).
  35. if (prop.startswith("Webkit")):
  36. props += generateLine(prop, extendedAttrs)
  37. # Generate a line with camelCase spelling of property-name (or capitalized,
  38. # for Moz-prefixed properties):
  39. if not prop.startswith("Moz"):
  40. prop = prop[0].lower() + prop[1:]
  41. props += generateLine(prop, extendedAttrs)
  42. # Per spec, what's actually supposed to happen here is that we're supposed
  43. # to have properties for:
  44. #
  45. # 1) Each supported CSS property name, camelCased.
  46. # 2) Each supported name that contains or starts with dashes,
  47. # without any changes to the name.
  48. # 3) cssFloat
  49. #
  50. # Note that "float" will cause a property called "float" to exist due to (1)
  51. # in that list.
  52. #
  53. # In practice, cssFloat is the only case in which "name" doesn't contain
  54. # "-" but also doesn't match "prop". So the above generatePropLine() call
  55. # covered (3) and all of (1) except "float". If we now output attributes
  56. # for all the cases where "name" doesn't match "prop", that will cover
  57. # "float" and (2).
  58. if prop != name:
  59. extendedAttrs.append('BinaryName="%s"' % prop)
  60. # Throw in a '_' before the attribute name, because some of these
  61. # property names collide with IDL reserved words.
  62. props += generateLine("_" + name, extendedAttrs)
  63. idlFile = open(idlFilename, "r")
  64. idlTemplate = idlFile.read()
  65. idlFile.close()
  66. output.write("/* THIS IS AN AUTOGENERATED FILE. DO NOT EDIT */\n\n" +
  67. string.Template(idlTemplate).substitute({"props": props}) + '\n')
  68. def main():
  69. parser = argparse.ArgumentParser()
  70. parser.add_argument('idlFilename', help='IDL property file template')
  71. parser.add_argument('preprocessorHeader', help='Header file to pass through the preprocessor')
  72. args = parser.parse_args()
  73. generate(sys.stdout, args.idlFilename, args.preprocessorHeader)
  74. if __name__ == '__main__':
  75. main()