Bug 1273706 - Part 15: Add nsCSSProps::LookupCustomProperty for looking up a CSSProperty given a custom property name and registrations. r?heycam
MozReview-Commit-ID: 2SGSOWS6ptg
--- a/layout/style/nsCSSProps.cpp
+++ b/layout/style/nsCSSProps.cpp
@@ -18,16 +18,19 @@
#include "nsThemeConstants.h" // For system widget appearance types
#include "mozilla/dom/AnimationEffectReadOnlyBinding.h" // for PlaybackDirection
#include "mozilla/LookAndFeel.h" // for system colors
#include "nsString.h"
#include "nsStaticNameTable.h"
+#include "mozilla/CSSProperty.h"
+#include "mozilla/CSSVariableRegistration.h"
+#include "mozilla/CSSVariableRegistrations.h"
#include "mozilla/Preferences.h"
using namespace mozilla;
typedef nsCSSProps::KTableEntry KTableEntry;
// By wrapping internal-only properties in this macro, we are not
// exposing them in the CSSOM. Since currently it is not necessary to
@@ -602,16 +605,38 @@ nsCSSPropertyID
nsCSSProps::LookupPropertyByIDLName(const nsAString& aPropertyIDLName,
EnabledState aEnabled)
{
MOZ_ASSERT(gPropertyIDLNameTable, "no lookup table, needs addref");
return LookupPropertyByIDLName(NS_ConvertUTF16toUTF8(aPropertyIDLName),
aEnabled);
}
+/* static */ mozilla::CSSProperty
+nsCSSProps::LookupCustomProperty(const CSSVariableRegistrations* aRegistrations,
+ const nsAString& aName,
+ EnabledState aEnabled)
+{
+ MOZ_ASSERT(IsCustomPropertyName(aName));
+ if (!aRegistrations ||
+ (!nsLayoutUtils::CSSVariablesEnabled() &&
+ aEnabled != EnabledState::eIgnoreEnabledState)) {
+ return mozilla::CSSProperty(eCSSProperty_UNKNOWN);
+ }
+
+ CSSVariableRegistration* registration;
+ if (!aRegistrations->mData.Get(Substring(aName,
+ CSS_CUSTOM_NAME_PREFIX_LENGTH),
+ ®istration)) {
+ return mozilla::CSSProperty(eCSSProperty_UNKNOWN);
+ }
+
+ return mozilla::CSSProperty(registration->mAtom);
+}
+
nsCSSFontDesc
nsCSSProps::LookupFontDesc(const nsACString& aFontDesc)
{
MOZ_ASSERT(gFontDescTable, "no lookup table, needs addref");
nsCSSFontDesc which = nsCSSFontDesc(gFontDescTable->Lookup(aFontDesc));
if (which == eCSSFontDesc_Display &&
!Preferences::GetBool("layout.css.font-display.enabled")) {
--- a/layout/style/nsCSSProps.h
+++ b/layout/style/nsCSSProps.h
@@ -15,16 +15,21 @@
#include <type_traits>
#include "nsString.h"
#include "nsCSSPropertyID.h"
#include "nsStyleStructFwd.h"
#include "nsCSSKeywords.h"
#include "mozilla/CSSEnabledState.h"
#include "mozilla/UseCounter.h"
+namespace mozilla {
+struct CSSVariableRegistrations;
+class CSSProperty;
+} // namespace mozilla
+
// Length of the "--" prefix on custom names (such as custom property names,
// and, in the future, custom media query names).
#define CSS_CUSTOM_NAME_PREFIX_LENGTH 2
// Flags for ParseVariant method
#define VARIANT_KEYWORD 0x000001 // K
#define VARIANT_LENGTH 0x000002 // L
#define VARIANT_PERCENT 0x000004 // P
@@ -392,16 +397,25 @@ public:
// eCSSPropertyExtra_variable won't be returned from these methods.
static nsCSSPropertyID LookupPropertyByIDLName(
const nsAString& aPropertyIDLName,
EnabledState aEnabled);
static nsCSSPropertyID LookupPropertyByIDLName(
const nsACString& aPropertyIDLName,
EnabledState aEnabled);
+ // Look up a registered custom property as a CSSProperty by its full name
+ // (with the '--' prefix). If the given custom property has not been
+ // registered, or if custom properties are not enabled, we return
+ // CSSProperty(eCSSProperty_UNKNOWN).
+ static mozilla::CSSProperty
+ LookupCustomProperty(const mozilla::CSSVariableRegistrations* aRegistrations,
+ const nsAString& aName,
+ EnabledState aEnabled);
+
// Returns whether aProperty is a custom property name, i.e. begins with
// "--". This assumes that the CSS Variables pref has been enabled.
static bool IsCustomPropertyName(const nsAString& aProperty);
static bool IsCustomPropertyName(const nsACString& aProperty);
static inline bool IsShorthand(nsCSSPropertyID aProperty) {
MOZ_ASSERT(0 <= aProperty && aProperty < eCSSProperty_COUNT,
"out of range");