ServoDeclarationBlock.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "mozilla/ServoDeclarationBlock.h"
  6. #include "mozilla/ServoBindings.h"
  7. #include "nsCSSProps.h"
  8. namespace mozilla {
  9. /* static */ already_AddRefed<ServoDeclarationBlock>
  10. ServoDeclarationBlock::FromCssText(const nsAString& aCssText)
  11. {
  12. NS_ConvertUTF16toUTF8 value(aCssText);
  13. RefPtr<RawServoDeclarationBlock>
  14. raw = Servo_ParseStyleAttribute(&value).Consume();
  15. RefPtr<ServoDeclarationBlock> decl = new ServoDeclarationBlock(raw.forget());
  16. return decl.forget();
  17. }
  18. /**
  19. * An RAII class holding an atom for the given property.
  20. */
  21. class MOZ_STACK_CLASS PropertyAtomHolder
  22. {
  23. public:
  24. explicit PropertyAtomHolder(const nsAString& aProperty)
  25. {
  26. nsCSSPropertyID propID =
  27. nsCSSProps::LookupProperty(aProperty, CSSEnabledState::eForAllContent);
  28. if (propID == eCSSPropertyExtra_variable) {
  29. mIsCustomProperty = true;
  30. mAtom = NS_Atomize(
  31. Substring(aProperty, CSS_CUSTOM_NAME_PREFIX_LENGTH)).take();
  32. } else {
  33. mIsCustomProperty = false;
  34. if (propID != eCSSProperty_UNKNOWN) {
  35. mAtom = nsCSSProps::AtomForProperty(propID);
  36. } else {
  37. mAtom = nullptr;
  38. }
  39. }
  40. }
  41. ~PropertyAtomHolder()
  42. {
  43. if (mIsCustomProperty) {
  44. NS_RELEASE(mAtom);
  45. }
  46. }
  47. explicit operator bool() const { return !!mAtom; }
  48. nsIAtom* Atom() const { MOZ_ASSERT(mAtom); return mAtom; }
  49. bool IsCustomProperty() const { return mIsCustomProperty; }
  50. private:
  51. nsIAtom* mAtom;
  52. bool mIsCustomProperty;
  53. };
  54. void
  55. ServoDeclarationBlock::GetPropertyValue(const nsAString& aProperty,
  56. nsAString& aValue) const
  57. {
  58. if (PropertyAtomHolder holder{aProperty}) {
  59. Servo_DeclarationBlock_GetPropertyValue(
  60. mRaw, holder.Atom(), holder.IsCustomProperty(), &aValue);
  61. }
  62. }
  63. void
  64. ServoDeclarationBlock::GetPropertyValueByID(nsCSSPropertyID aPropID,
  65. nsAString& aValue) const
  66. {
  67. nsIAtom* atom = nsCSSProps::AtomForProperty(aPropID);
  68. Servo_DeclarationBlock_GetPropertyValue(mRaw, atom, false, &aValue);
  69. }
  70. bool
  71. ServoDeclarationBlock::GetPropertyIsImportant(const nsAString& aProperty) const
  72. {
  73. if (PropertyAtomHolder holder{aProperty}) {
  74. return Servo_DeclarationBlock_GetPropertyIsImportant(
  75. mRaw, holder.Atom(), holder.IsCustomProperty());
  76. }
  77. return false;
  78. }
  79. void
  80. ServoDeclarationBlock::RemoveProperty(const nsAString& aProperty)
  81. {
  82. AssertMutable();
  83. if (PropertyAtomHolder holder{aProperty}) {
  84. Servo_DeclarationBlock_RemoveProperty(mRaw, holder.Atom(),
  85. holder.IsCustomProperty());
  86. }
  87. }
  88. void
  89. ServoDeclarationBlock::RemovePropertyByID(nsCSSPropertyID aPropID)
  90. {
  91. AssertMutable();
  92. nsIAtom* atom = nsCSSProps::AtomForProperty(aPropID);
  93. Servo_DeclarationBlock_RemoveProperty(mRaw, atom, false);
  94. }
  95. } // namespace mozilla