Bug 1273706 - Part 27: Add CSSPropertySet. r?heycam draft
authorJonathan Chan <jyc@eqv.io>
Thu, 18 Aug 2016 15:30:38 -0700
changeset 402916 f29749822533aa9721ba32612df7d17b4945f6a4
parent 402915 57f2da27a4b95fc6f2b59505da543022ce5e8af3
child 402917 31fa95a2e1fc8add2ae5ebf3e5f1196ec0f127b7
push id26775
push userjchan@mozilla.com
push dateThu, 18 Aug 2016 22:38:41 +0000
reviewersheycam
bugs1273706
milestone51.0a1
Bug 1273706 - Part 27: Add CSSPropertySet. r?heycam CSSPropertySet is just like nsCSSPropertyIDSet but for |CSSProperty|s. MozReview-Commit-ID: 3WzAGHWzRmy
layout/style/CSSPropertySet.cpp
layout/style/CSSPropertySet.h
layout/style/moz.build
new file mode 100644
--- /dev/null
+++ b/layout/style/CSSPropertySet.cpp
@@ -0,0 +1,80 @@
+/* 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/. */
+
+#include "mozilla/CSSPropertySet.h"
+#include "mozilla/Move.h"
+#include "mozilla/PodOperations.h"
+
+namespace mozilla {
+
+CSSPropertySet::CSSPropertySet(const CSSPropertySet& aOther)
+  : mFixedProps(aOther.mFixedProps)
+{
+  for (auto iter = aOther.mCustomProps.ConstIter(); !iter.Done(); iter.Next()) {
+    mCustomProps.Put(iter.Key(), iter.UserData());
+  }
+}
+
+void
+CSSPropertySet::AddProperty(CSSProperty aProperty)
+{
+  if (aProperty.IsFixed()) {
+    mFixedProps.AddProperty(aProperty.AsFixed());
+  } else {
+    nsIAtom* custom = aProperty.AsCustom();
+    mCustomProps.Put(custom, Move(custom));
+  }
+}
+
+void
+CSSPropertySet::RemoveProperty(CSSProperty aProperty)
+{
+  if (aProperty.IsFixed()) {
+    mFixedProps.RemoveProperty(aProperty.AsFixed());
+  } else {
+    nsIAtom* custom = aProperty.AsCustom();
+    mCustomProps.Remove(custom);
+  }
+}
+
+bool
+CSSPropertySet::HasProperty(CSSProperty aProperty) const
+{
+  if (aProperty.IsFixed()) {
+    return mFixedProps.HasProperty(aProperty.AsFixed());
+  } else {
+    nsIAtom* custom = aProperty.AsCustom();
+    return !!mCustomProps.Get(custom);
+  }
+}
+
+void
+CSSPropertySet::Empty()
+{
+  mFixedProps.Empty();
+  mCustomProps.Clear();
+}
+
+bool
+CSSPropertySet::Equals(const CSSPropertySet& aOther) const
+{
+  if (!mFixedProps.Equals(aOther.mFixedProps) ||
+      mCustomProps.Count() != aOther.mCustomProps.Count()) {
+    return false;
+  }
+  for (auto iter = mCustomProps.ConstIter(); !iter.Done(); iter.Next()) {
+    if (!aOther.mCustomProps.Get(iter.Key())) {
+      return false;
+    }
+  }
+  return true;
+}
+
+CSSPropertySet::Table::Iterator
+CSSPropertySet::IterCustomProps() const
+{
+  return mCustomProps.ConstIter();
+}
+
+} // naemspace mozilla
new file mode 100644
--- /dev/null
+++ b/layout/style/CSSPropertySet.h
@@ -0,0 +1,46 @@
+/* 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/. */
+
+/* sets of CSSProperty values (custom or 'fixed'/standard properties) */
+
+#ifndef mozilla_CSSPropertySet_h
+#define mozilla_CSSPropertySet_h
+
+#include "mozilla/CSSProperty.h"
+#include "nsCOMPtr.h"
+#include "nsCSSPropertyID.h"
+#include "nsCSSPropertyIDSet.h"
+#include "nsDataHashtable.h"
+#include "nsIAtom.h"
+
+namespace mozilla {
+
+/**
+ * The same as nsCSSPropertyIDSet, but also capable of holding custom properties.
+ */
+class CSSPropertySet {
+public:
+  typedef nsDataHashtable<nsPtrHashKey<nsIAtom>, nsCOMPtr<nsIAtom>> Table;
+
+  CSSPropertySet() = default;
+  CSSPropertySet(const CSSPropertySet& aOther);
+
+  void AddProperty(CSSProperty aProperty);
+  void RemoveProperty(CSSProperty aProperty);
+  bool HasProperty(CSSProperty aProperty) const;
+
+  void Empty();
+
+  bool Equals(const CSSPropertySet& aOther) const;
+
+  Table::Iterator IterCustomProps() const;
+
+private:
+  nsCSSPropertyIDSet mFixedProps;
+  Table mCustomProps;
+};
+
+} // namespace mozilla
+
+#endif // mozilla_CSSPropertySet_h
--- a/layout/style/moz.build
+++ b/layout/style/moz.build
@@ -80,16 +80,17 @@ EXPORTS += [
     'nsStyleUtil.h',
 ]
 
 EXPORTS.mozilla += [
     'AnimationCollection.h',
     'CSSComputedValue.h',
     'CSSEnabledState.h',
     'CSSProperty.h',
+    'CSSPropertySet.h',
     'CSSStyleSheet.h',
     'CSSValueType.h',
     'CSSVariableDeclarations.h',
     'CSSVariableRegistration.h',
     'CSSVariableRegistrations.h',
     'CSSVariableResolver.h',
     'CSSVariableSyntax.h',
     'CSSVariableValues.h',
@@ -143,16 +144,17 @@ EXPORTS.mozilla.css += [
 
 UNIFIED_SOURCES += [
     'AnimationCollection.cpp',
     'AnimationCommon.cpp',
     'CounterStyleManager.cpp',
     'CSS.cpp',
     'CSSComputedValue.cpp',
     'CSSLexer.cpp',
+    'CSSPropertySet.cpp',
     'CSSRuleList.cpp',
     'CSSStyleSheet.cpp',
     'CSSVariableDeclarations.cpp',
     'CSSVariableRegistrations.cpp',
     'CSSVariableResolver.cpp',
     'CSSVariableSyntax.cpp',
     'CSSVariableValues.cpp',
     'Declaration.cpp',