1/*
2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 * Copyright (C) 2015, 2016 Apple Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
21 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "Element.h"
29
30namespace WebCore {
31
32class DOMTokenList {
33 WTF_MAKE_FAST_ALLOCATED;
34public:
35 using IsSupportedTokenFunction = WTF::Function<bool(Document&, StringView)>;
36 DOMTokenList(Element&, const QualifiedName& attributeName, IsSupportedTokenFunction&& isSupportedToken = { });
37
38 void associatedAttributeValueChanged(const AtomString&);
39
40 void ref() { m_element.ref(); }
41 void deref() { m_element.deref(); }
42
43 unsigned length() const;
44 const AtomString& item(unsigned index) const;
45
46 WEBCORE_EXPORT bool contains(const AtomString&) const;
47 ExceptionOr<void> add(const Vector<String>&);
48 ExceptionOr<void> add(const AtomString&);
49 ExceptionOr<void> remove(const Vector<String>&);
50 ExceptionOr<void> remove(const AtomString&);
51 WEBCORE_EXPORT ExceptionOr<bool> toggle(const AtomString&, Optional<bool> force);
52 ExceptionOr<bool> replace(const AtomString& token, const AtomString& newToken);
53 ExceptionOr<bool> supports(StringView token);
54
55 Element& element() const { return m_element; }
56
57 WEBCORE_EXPORT void setValue(const String&);
58 WEBCORE_EXPORT const AtomString& value() const;
59
60private:
61 void updateTokensFromAttributeValue(const String&);
62 void updateAssociatedAttributeFromTokens();
63
64 WEBCORE_EXPORT Vector<AtomString>& tokens();
65 const Vector<AtomString>& tokens() const { return const_cast<DOMTokenList&>(*this).tokens(); }
66
67 static ExceptionOr<void> validateToken(const String&);
68 static ExceptionOr<void> validateTokens(const String* tokens, size_t length);
69 ExceptionOr<void> addInternal(const String* tokens, size_t length);
70 ExceptionOr<void> removeInternal(const String* tokens, size_t length);
71
72 Element& m_element;
73 const WebCore::QualifiedName& m_attributeName;
74 bool m_inUpdateAssociatedAttributeFromTokens { false };
75 bool m_tokensNeedUpdating { true };
76 Vector<AtomString> m_tokens;
77 IsSupportedTokenFunction m_isSupportedToken;
78};
79
80inline unsigned DOMTokenList::length() const
81{
82 return tokens().size();
83}
84
85inline const AtomString& DOMTokenList::item(unsigned index) const
86{
87 auto& tokens = this->tokens();
88 return index < tokens.size() ? tokens[index] : nullAtom();
89}
90
91} // namespace WebCore
92