Bug 1206569 - Part 4: Add method to get a CSS property's IDL name. r=bzbarsky
authorCameron McCormack <cam@mcc.id.au>
Tue, 22 Sep 2015 15:58:20 +1000
changeset 263640 3dbdab6153284167fcdf2d0ab9403fcde5b8d9a4
parent 263639 30013b9e1a2ad2c65e6c2832052bb20003f98638
child 263641 625763308cc8030605dd946cf922aababeceb2af
push id65387
push usercmccormack@mozilla.com
push dateTue, 22 Sep 2015 05:58:22 +0000
treeherdermozilla-inbound@3dbdab615328 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbzbarsky
bugs1206569
milestone44.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1206569 - Part 4: Add method to get a CSS property's IDL name. r=bzbarsky
layout/style/GenerateCSSPropsGenerated.py
layout/style/Makefile.in
layout/style/moz.build
layout/style/nsCSSProps.cpp
layout/style/nsCSSProps.h
layout/style/nsCSSPropsGenerated.inc.in
new file mode 100644
--- /dev/null
+++ b/layout/style/GenerateCSSPropsGenerated.py
@@ -0,0 +1,58 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this file,
+# You can obtain one at http://mozilla.org/MPL/2.0/.
+
+import sys
+import string
+
+properties = [{"name":p[0], "prop":p[1], "id":p[2],
+               "flags":p[3], "pref":p[4], "proptype":p[5]}
+              for (i, p) in enumerate(eval(sys.stdin.read()))]
+
+# Sort the list so that longhand and logical properties are intermingled
+# first, shorthand properties follow, then aliases appear last.  This matches
+# the order of the nsCSSProperty enum.
+
+def property_compare(x, y):
+    property_order = {"longhand": 0, "logical": 0, "shorthand": 1, "alias": 2}
+    return property_order[x["proptype"]] - property_order[y["proptype"]]
+
+properties = sorted(properties, cmp=property_compare)
+
+for i, p in enumerate(properties):
+    p["index"] = i
+
+def generate_idl_names(properties):
+    names = []
+    for p in properties:
+        if p["proptype"] is "alias":
+            continue
+        if "CSS_PROPERTY_INTERNAL" in p["flags"]:
+            names.append("  nullptr,  // %s" % p["name"])
+        else:
+            idl_name = p["prop"]
+            if not idl_name.startswith("Moz"):
+                idl_name = idl_name[0].lower() + idl_name[1:]
+            names.append('  "%s",' % idl_name)
+    return "\n".join(names)
+
+def generate_assertions(properties):
+    def enum(p):
+        if p["proptype"] is "alias":
+            return "eCSSPropertyAlias_%s" % p["prop"]
+        else:
+            return "eCSSProperty_%s" % p["id"]
+    msg = ('static_assert(%s == %d, "GenerateCSSPropsGenerated.py did not list '
+           'properties in nsCSSProperty order");')
+    return "\n".join(map(lambda p: msg % (enum(p), p["index"]), properties))
+
+cppFile = open(sys.argv[1], "r")
+cppTemplate = cppFile.read()
+cppFile.close()
+
+substitutions = {
+  "idl_names": generate_idl_names(properties),
+  "assertions": generate_assertions(properties)
+}
+print ("/* THIS IS AN AUTOGENERATED FILE.  DO NOT EDIT */\n\n" +
+       string.Template(cppTemplate).substitute(substitutions))
--- a/layout/style/Makefile.in
+++ b/layout/style/Makefile.in
@@ -1,8 +1,26 @@
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 INSTALL_TARGETS += structlist
 structlist_FILES := nsStyleStructList.h
 structlist_DEST = $(DIST)/include
 structlist_TARGET := export
+
+# TODO This list should be emitted to a .pp file via
+# GenerateCSSPropsGenerated.py.
+csspropsidlnames_dependencies = \
+  $(srcdir)/nsCSSPropList.h \
+  $(srcdir)/nsCSSPropAliasList.h \
+  $(srcdir)/nsCSSPropsGenerated.inc.in \
+  $(srcdir)/PythonCSSProps.h \
+  $(srcdir)/GenerateCSSPropsGenerated.py \
+  $(GLOBAL_DEPS) \
+  $(NULL)
+
+nsCSSPropsGenerated.inc : $(csspropsidlnames_dependencies)
+	$(CPP) $(DEFINES) $(ACDEFINES) \
+	  $(srcdir)/PythonCSSProps.h | \
+	    PYTHONDONTWRITEBYTECODE=1 $(PYTHON) \
+	      $(srcdir)/GenerateCSSPropsGenerated.py \
+	      $(srcdir)/nsCSSPropsGenerated.inc.in > $@
--- a/layout/style/moz.build
+++ b/layout/style/moz.build
@@ -206,15 +206,22 @@ JAR_MANIFESTS += ['jar.mn']
 RESOURCE_FILES += [
     'contenteditable.css',
     'designmode.css',
     'ImageDocument.css',
     'TopLevelImageDocument.css',
     'TopLevelVideoDocument.css',
 ]
 
-GENERATED_FILES += ['nsStyleStructList.h']
+# nsCSSPropsGenerated.inc is generated from nsCSSPropsGenerated.inc.in by
+# GenerateCSSPropsGenerated.py; see the corresponding rules in Makefile.in.
+
+GENERATED_FILES += [
+    'nsCSSPropsGenerated.inc',
+    'nsStyleStructList.h',
+]
+
 style_struct_list = GENERATED_FILES['nsStyleStructList.h']
 style_struct_list.script = 'generate-stylestructlist.py'
 
 if CONFIG['GNU_CC']:
     CFLAGS += ['-Wshadow']
     CXXFLAGS += ['-Wshadow']
--- a/layout/style/nsCSSProps.cpp
+++ b/layout/style/nsCSSProps.cpp
@@ -3013,8 +3013,10 @@ nsCSSProps::gPropertyUseCounter[eCSSProp
                 "on logical properties");                                   \
   static_assert(!(((flags_) & CSS_PROPERTY_LOGICAL_AXIS) &&                 \
                   ((flags_) & CSS_PROPERTY_LOGICAL_END_EDGE)),              \
                 "CSS_PROPERTY_LOGICAL_END_EDGE makes no sense when used "   \
                 "with CSS_PROPERTY_LOGICAL_AXIS");
 #include "nsCSSPropList.h"
 #undef CSS_PROP_LOGICAL
 #undef CSS_PROP
+
+#include "nsCSSPropsGenerated.inc"
--- a/layout/style/nsCSSProps.h
+++ b/layout/style/nsCSSProps.h
@@ -501,16 +501,38 @@ public:
    * getting too many of these properties, we should make kLogicalGroupTable
    * be a simple array of eCSSProperty_COUNT length.)
    */
   static const nsCSSProperty* LogicalGroup(nsCSSProperty aProperty);
 
 private:
   static bool gPropertyEnabled[eCSSProperty_COUNT_with_aliases];
 
+private:
+  // Defined in the generated nsCSSPropsGenerated.inc.
+  static const char* const kIDLNameTable[eCSSProperty_COUNT];
+
+public:
+  /**
+   * Returns the IDL name of the specified property, which must be a
+   * longhand, logical or shorthand property.  The IDL name is the property
+   * name with any hyphen-lowercase character pairs replaced by an
+   * uppercase character:
+   * https://drafts.csswg.org/cssom/#css-property-to-idl-attribute
+   *
+   * As a special case, the string "cssFloat" is returned for the float
+   * property.  nullptr is returned for internal properties.
+   */
+  static const char* PropertyIDLName(nsCSSProperty aProperty)
+  {
+    MOZ_ASSERT(0 <= aProperty && aProperty < eCSSProperty_COUNT,
+               "out of range");
+    return kIDLNameTable[aProperty];
+  }
+
 public:
 
   static bool IsEnabled(nsCSSProperty aProperty) {
     MOZ_ASSERT(0 <= aProperty && aProperty < eCSSProperty_COUNT_with_aliases,
                "out of range");
     return gPropertyEnabled[aProperty];
   }
 
new file mode 100644
--- /dev/null
+++ b/layout/style/nsCSSPropsGenerated.inc.in
@@ -0,0 +1,13 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/* processed file that defines CSS property tables that can't be generated
+   with the pre-processor, designed to be #included in nsCSSProps.cpp */
+
+const char* const nsCSSProps::kIDLNameTable[eCSSProperty_COUNT] = {
+${idl_names}
+};
+
+${assertions}