1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 /*
8  * Implementation of DOMTokenList specified by HTML5.
9  */
10 
11 #ifndef nsDOMTokenList_h___
12 #define nsDOMTokenList_h___
13 
14 #include "nsCOMPtr.h"
15 #include "nsContentUtils.h"
16 #include "nsDOMString.h"
17 #include "nsWhitespaceTokenizer.h"
18 #include "nsWrapperCache.h"
19 #include "mozilla/dom/BindingDeclarations.h"
20 #include "mozilla/dom/DOMTokenListSupportedTokens.h"
21 
22 namespace mozilla {
23 class ErrorResult;
24 namespace dom {
25 class DocGroup;
26 class Element;
27 }  // namespace dom
28 }  // namespace mozilla
29 
30 class nsAttrValue;
31 class nsAtom;
32 
33 // nsISupports must be on the primary inheritance chain
34 
35 class nsDOMTokenList : public nsISupports, public nsWrapperCache {
36  protected:
37   typedef mozilla::dom::Element Element;
38   typedef mozilla::dom::DocGroup DocGroup;
39   typedef nsWhitespaceTokenizerTemplate<nsContentUtils::IsHTMLWhitespace>
40       WhitespaceTokenizer;
41 
42  public:
43   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
44   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(nsDOMTokenList)
45 
46   nsDOMTokenList(Element* aElement, nsAtom* aAttrAtom,
47                  const mozilla::dom::DOMTokenListSupportedTokenArray = nullptr);
48 
49   virtual JSObject* WrapObject(JSContext* cx,
50                                JS::Handle<JSObject*> aGivenProto) override;
51 
GetParentObject()52   Element* GetParentObject() { return mElement; }
53 
54   DocGroup* GetDocGroup() const;
55 
56   void RemoveDuplicates(const nsAttrValue* aAttr);
57   uint32_t Length();
Item(uint32_t aIndex,nsAString & aResult)58   void Item(uint32_t aIndex, nsAString& aResult) {
59     bool found;
60     IndexedGetter(aIndex, found, aResult);
61     if (!found) {
62       SetDOMStringToNull(aResult);
63     }
64   }
65   void IndexedGetter(uint32_t aIndex, bool& aFound, nsAString& aResult);
66   bool Contains(const nsAString& aToken);
67   void Add(const nsAString& aToken, mozilla::ErrorResult& aError);
68   void Add(const nsTArray<nsString>& aTokens, mozilla::ErrorResult& aError);
69   void Remove(const nsAString& aToken, mozilla::ErrorResult& aError);
70   void Remove(const nsTArray<nsString>& aTokens, mozilla::ErrorResult& aError);
71   bool Replace(const nsAString& aToken, const nsAString& aNewToken,
72                mozilla::ErrorResult& aError);
73   bool Toggle(const nsAString& aToken,
74               const mozilla::dom::Optional<bool>& force,
75               mozilla::ErrorResult& aError);
76   bool Supports(const nsAString& aToken, mozilla::ErrorResult& aError);
77 
78   void GetValue(nsAString& aResult);
79   void SetValue(const nsAString& aValue, mozilla::ErrorResult& rv);
80 
81  protected:
82   virtual ~nsDOMTokenList();
83 
84   nsresult CheckToken(const nsAString& aStr);
85   nsresult CheckTokens(const nsTArray<nsString>& aStr);
86   void AddInternal(const nsAttrValue* aAttr, const nsTArray<nsString>& aTokens);
87   void RemoveInternal(const nsAttrValue* aAttr,
88                       const nsTArray<nsString>& aTokens);
89   bool ReplaceInternal(const nsAttrValue* aAttr, const nsAString& aToken,
90                        const nsAString& aNewToken);
91   inline const nsAttrValue* GetParsedAttr();
92 
93   nsCOMPtr<Element> mElement;
94   RefPtr<nsAtom> mAttrAtom;
95   const mozilla::dom::DOMTokenListSupportedTokenArray mSupportedTokens;
96 };
97 
98 #endif  // nsDOMTokenList_h___
99