summaryrefslogtreecommitdiffstats
path: root/dom/base/nsDOMTokenList.h
blob: 5a755ef8954dc61db3c4a1a4192fa542e3871e4a (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
/* -*- Mode: C++; tab-width: 8; 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/. */

/*
 * Implementation of DOMTokenList specified by HTML5.
 */

#ifndef nsDOMTokenList_h___
#define nsDOMTokenList_h___

#include "nsCOMPtr.h"
#include "nsContentUtils.h"
#include "nsDOMString.h"
#include "nsWhitespaceTokenizer.h"
#include "nsWrapperCache.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/BindingDeclarations.h"
#include "mozilla/dom/DOMTokenListSupportedTokens.h"

namespace mozilla {
class ErrorResult;
namespace dom {
class DocGroup;
} // namespace dom
} // namespace mozilla

class nsAttrValue;
class nsIAtom;

// nsISupports must be on the primary inheritance chain

class nsDOMTokenList : public nsISupports,
                       public nsWrapperCache
{
protected:
  typedef mozilla::dom::Element Element;
  typedef mozilla::dom::DocGroup DocGroup;
  typedef nsWhitespaceTokenizerTemplate<nsContentUtils::IsHTMLWhitespace>
    WhitespaceTokenizer;

public:
  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(nsDOMTokenList)

  nsDOMTokenList(Element* aElement, nsIAtom* aAttrAtom,
                 const mozilla::dom::DOMTokenListSupportedTokenArray = nullptr);

  virtual JSObject* WrapObject(JSContext *cx, JS::Handle<JSObject*> aGivenProto) override;

  Element* GetParentObject()
  {
    return mElement;
  }

  DocGroup* GetDocGroup() const;

  uint32_t Length();
  void Item(uint32_t aIndex, nsAString& aResult)
  {
    bool found;
    IndexedGetter(aIndex, found, aResult);
    if (!found) {
      SetDOMStringToNull(aResult);
    }
  }
  void IndexedGetter(uint32_t aIndex, bool& aFound, nsAString& aResult);
  bool Contains(const nsAString& aToken);
  void Add(const nsAString& aToken, mozilla::ErrorResult& aError);
  void Add(const nsTArray<nsString>& aTokens,
           mozilla::ErrorResult& aError);
  void Remove(const nsAString& aToken, mozilla::ErrorResult& aError);
  void Remove(const nsTArray<nsString>& aTokens,
              mozilla::ErrorResult& aError);
  void Replace(const nsAString& aToken,
               const nsAString& aNewToken,
               mozilla::ErrorResult& aError);
  bool Toggle(const nsAString& aToken,
              const mozilla::dom::Optional<bool>& force,
              mozilla::ErrorResult& aError);
  bool Supports(const nsAString& aToken,
                mozilla::ErrorResult& aError);

  void GetValue(nsAString& aResult) { Stringify(aResult); }
  void SetValue(const nsAString& aValue, mozilla::ErrorResult& rv);
  void Stringify(nsAString& aResult);

protected:
  virtual ~nsDOMTokenList();

  nsresult CheckToken(const nsAString& aStr);
  nsresult CheckTokens(const nsTArray<nsString>& aStr);
  void AddInternal(const nsAttrValue* aAttr,
                   const nsTArray<nsString>& aTokens);
  void RemoveInternal(const nsAttrValue* aAttr,
                      const nsTArray<nsString>& aTokens);
  void ReplaceInternal(const nsAttrValue* aAttr,
                       const nsAString& aToken,
                       const nsAString& aNewToken);
  inline const nsAttrValue* GetParsedAttr();

  nsCOMPtr<Element> mElement;
  nsCOMPtr<nsIAtom> mAttrAtom;
  const mozilla::dom::DOMTokenListSupportedTokenArray mSupportedTokens;
};

#endif // nsDOMTokenList_h___