summaryrefslogtreecommitdiffstats
path: root/layout/style/ServoDeclarationBlock.cpp
blob: 1dc691bfb1dcb678e54d3995dcbde29201a90d29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* -*- 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/. */

#include "mozilla/ServoDeclarationBlock.h"

#include "mozilla/ServoBindings.h"

#include "nsCSSProps.h"

namespace mozilla {

/* static */ already_AddRefed<ServoDeclarationBlock>
ServoDeclarationBlock::FromCssText(const nsAString& aCssText)
{
  NS_ConvertUTF16toUTF8 value(aCssText);
  RefPtr<RawServoDeclarationBlock>
    raw = Servo_ParseStyleAttribute(&value).Consume();
  RefPtr<ServoDeclarationBlock> decl = new ServoDeclarationBlock(raw.forget());
  return decl.forget();
}

/**
 * An RAII class holding an atom for the given property.
 */
class MOZ_STACK_CLASS PropertyAtomHolder
{
public:
  explicit PropertyAtomHolder(const nsAString& aProperty)
  {
    nsCSSPropertyID propID =
      nsCSSProps::LookupProperty(aProperty, CSSEnabledState::eForAllContent);
    if (propID == eCSSPropertyExtra_variable) {
      mIsCustomProperty = true;
      mAtom = NS_Atomize(
        Substring(aProperty, CSS_CUSTOM_NAME_PREFIX_LENGTH)).take();
    } else {
      mIsCustomProperty = false;
      if (propID != eCSSProperty_UNKNOWN) {
        mAtom = nsCSSProps::AtomForProperty(propID);
      } else {
        mAtom = nullptr;
      }
    }
  }

  ~PropertyAtomHolder()
  {
    if (mIsCustomProperty) {
      NS_RELEASE(mAtom);
    }
  }

  explicit operator bool() const { return !!mAtom; }
  nsIAtom* Atom() const { MOZ_ASSERT(mAtom); return mAtom; }
  bool IsCustomProperty() const { return mIsCustomProperty; }

private:
  nsIAtom* mAtom;
  bool mIsCustomProperty;
};

void
ServoDeclarationBlock::GetPropertyValue(const nsAString& aProperty,
                                        nsAString& aValue) const
{
  if (PropertyAtomHolder holder{aProperty}) {
    Servo_DeclarationBlock_GetPropertyValue(
      mRaw, holder.Atom(), holder.IsCustomProperty(), &aValue);
  }
}

void
ServoDeclarationBlock::GetPropertyValueByID(nsCSSPropertyID aPropID,
                                            nsAString& aValue) const
{
  nsIAtom* atom = nsCSSProps::AtomForProperty(aPropID);
  Servo_DeclarationBlock_GetPropertyValue(mRaw, atom, false, &aValue);
}

bool
ServoDeclarationBlock::GetPropertyIsImportant(const nsAString& aProperty) const
{
  if (PropertyAtomHolder holder{aProperty}) {
    return Servo_DeclarationBlock_GetPropertyIsImportant(
      mRaw, holder.Atom(), holder.IsCustomProperty());
  }
  return false;
}

void
ServoDeclarationBlock::RemoveProperty(const nsAString& aProperty)
{
  AssertMutable();
  if (PropertyAtomHolder holder{aProperty}) {
    Servo_DeclarationBlock_RemoveProperty(mRaw, holder.Atom(),
                                          holder.IsCustomProperty());
  }
}

void
ServoDeclarationBlock::RemovePropertyByID(nsCSSPropertyID aPropID)
{
  AssertMutable();
  nsIAtom* atom = nsCSSProps::AtomForProperty(aPropID);
  Servo_DeclarationBlock_RemoveProperty(mRaw, atom, false);
}

} // namespace mozilla