Bug 1446865 - Introduce nsStaticAtomUtils::Lookup. r=froydnj
This function captures a common usage pattern, generalizing the existing
IsMember() function.
MozReview-Commit-ID: 5Pt7kqyGD6Y
--- a/layout/style/nsCSSAnonBoxes.cpp
+++ b/layout/style/nsCSSAnonBoxes.cpp
@@ -58,23 +58,20 @@ nsCSSAnonBoxes::IsTreePseudoElement(nsAt
NS_LITERAL_STRING(":-moz-tree-"));
}
#endif
/* static*/ nsCSSAnonBoxes::NonInheriting
nsCSSAnonBoxes::NonInheritingTypeForPseudoTag(nsAtom* aPseudo)
{
MOZ_ASSERT(IsNonInheritingAnonBox(aPseudo));
- for (NonInheritingBase i = 0; i < ArrayLength(sCSSAnonBoxAtomSetup); ++i) {
- if (*sCSSAnonBoxAtomSetup[i].mAtomp == aPseudo) {
- return static_cast<NonInheriting>(i);
- }
- }
-
- MOZ_CRASH("Bogus pseudo passed to NonInheritingTypeForPseudoTag");
+ Maybe<uint32_t> index =
+ nsStaticAtomUtils::Lookup(aPseudo, sCSSAnonBoxAtomSetup);
+ MOZ_RELEASE_ASSERT(index.isSome());
+ return static_cast<NonInheriting>(*index);
}
/* static */ nsAtom*
nsCSSAnonBoxes::GetNonInheritingPseudoAtom(NonInheriting aBoxType)
{
MOZ_ASSERT(aBoxType < NonInheriting::_Count);
return *sCSSAnonBoxAtomSetup[static_cast<NonInheritingBase>(aBoxType)].mAtomp;
}
--- a/layout/style/nsCSSPseudoClasses.cpp
+++ b/layout/style/nsCSSPseudoClasses.cpp
@@ -117,20 +117,22 @@ nsCSSPseudoClasses::PseudoTypeToString(T
MOZ_ASSERT(aType < Type::Count, "Unexpected type");
auto idx = static_cast<CSSPseudoClassTypeBase>(aType);
(*sCSSPseudoClassAtomSetup[idx].mAtomp)->ToString(aString);
}
/* static */ CSSPseudoClassType
nsCSSPseudoClasses::GetPseudoType(nsAtom* aAtom, EnabledState aEnabledState)
{
- for (uint32_t i = 0; i < ArrayLength(sCSSPseudoClassAtomSetup); ++i) {
- if (*sCSSPseudoClassAtomSetup[i].mAtomp == aAtom) {
- Type type = Type(i);
- return IsEnabled(type, aEnabledState) ? type : Type::NotPseudo;
+ Maybe<uint32_t> index =
+ nsStaticAtomUtils::Lookup(aAtom, sCSSPseudoClassAtomSetup);
+ if (index.isSome()) {
+ Type type = Type(*index);
+ if (IsEnabled(type, aEnabledState)) {
+ return type;
}
}
return Type::NotPseudo;
}
/* static */ bool
nsCSSPseudoClasses::IsUserActionPseudoClass(Type aType)
{
--- a/layout/style/nsCSSPseudoElements.cpp
+++ b/layout/style/nsCSSPseudoElements.cpp
@@ -73,27 +73,25 @@ nsCSSPseudoElements::IsCSS2PseudoElement
CSS_PSEUDO_ELEMENT_IS_CSS2),
"result doesn't match flags");
return result;
}
/* static */ CSSPseudoElementType
nsCSSPseudoElements::GetPseudoType(nsAtom *aAtom, EnabledState aEnabledState)
{
- for (CSSPseudoElementTypeBase i = 0;
- i < ArrayLength(sCSSPseudoElementAtomSetup);
- ++i) {
- if (*sCSSPseudoElementAtomSetup[i].mAtomp == aAtom) {
- auto type = static_cast<Type>(i);
- // ::moz-placeholder is an alias for ::placeholder
- if (type == CSSPseudoElementType::mozPlaceholder) {
- type = CSSPseudoElementType::placeholder;
- }
- return IsEnabled(type, aEnabledState) ? type : Type::NotPseudo;
+ Maybe<uint32_t> index =
+ nsStaticAtomUtils::Lookup(aAtom, sCSSPseudoElementAtomSetup);
+ if (index.isSome()) {
+ auto type = static_cast<Type>(*index);
+ // ::moz-placeholder is an alias for ::placeholder
+ if (type == CSSPseudoElementType::mozPlaceholder) {
+ type = CSSPseudoElementType::placeholder;
}
+ return IsEnabled(type, aEnabledState) ? type : Type::NotPseudo;
}
if (nsCSSAnonBoxes::IsAnonBox(aAtom)) {
#ifdef MOZ_XUL
if (nsCSSAnonBoxes::IsTreePseudoElement(aAtom)) {
return Type::XULTree;
}
#endif
--- a/xpcom/ds/nsAtomTable.cpp
+++ b/xpcom/ds/nsAtomTable.cpp
@@ -860,20 +860,19 @@ nsAtomTable::GetStaticAtom(const nsAStri
void
NS_SetStaticAtomsDone()
{
MOZ_ASSERT(NS_IsMainThread());
gStaticAtomsDone = true;
}
-/* static */ bool
-nsStaticAtomUtils::IsMember(nsAtom* aAtom, const nsStaticAtomSetup* aSetup,
- uint32_t aCount)
+/* static */ Maybe<uint32_t>
+nsStaticAtomUtils::Lookup(nsAtom* aAtom, const nsStaticAtomSetup* aSetup,
+ uint32_t aCount)
{
for (uint32_t i = 0; i < aCount; i++) {
if (aAtom == *(aSetup[i].mAtomp)) {
- return true;
+ return Some(i);
}
}
- return false;
+ return Nothing();
}
-
--- a/xpcom/ds/nsStaticAtom.h
+++ b/xpcom/ds/nsStaticAtom.h
@@ -3,16 +3,17 @@
/* 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/. */
#ifndef nsStaticAtom_h__
#define nsStaticAtom_h__
#include <stdint.h>
+#include "mozilla/Maybe.h"
class nsAtom;
class nsStaticAtom;
// The following macros are used to define static atoms, typically in
// conjunction with a .h file that defines the names and values of the atoms.
//
// For example, the .h file might be called MyAtomList.h and look like this:
@@ -126,19 +127,27 @@ NS_RegisterStaticAtoms(const nsStaticAto
uint32_t aCount);
RegisterStaticAtoms(aSetup, N);
}
// This class holds basic operations on arrays of static atoms.
class nsStaticAtomUtils {
public:
template<uint32_t N>
+ static mozilla::Maybe<uint32_t> Lookup(nsAtom *aAtom,
+ const nsStaticAtomSetup (&aSetup)[N])
+ {
+ return Lookup(aAtom, aSetup, N);
+ }
+
+ template<uint32_t N>
static bool IsMember(nsAtom *aAtom, const nsStaticAtomSetup (&aSetup)[N])
{
- return IsMember(aAtom, aSetup, N);
+ return Lookup(aAtom, aSetup, N).isSome();
}
private:
- static bool IsMember(nsAtom* aAtom, const nsStaticAtomSetup* aSetup,
- uint32_t aCount);
+ static mozilla::Maybe<uint32_t> Lookup(nsAtom* aAtom,
+ const nsStaticAtomSetup* aSetup,
+ uint32_t aCount);
};
#endif