| 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 | |
| 30 | namespace WebCore { |
| 31 | |
| 32 | class DOMTokenList { |
| 33 | WTF_MAKE_FAST_ALLOCATED; |
| 34 | public: |
| 35 | using IsSupportedTokenFunction = WTF::Function<bool(Document&, StringView)>; |
| 36 | DOMTokenList(Element&, const QualifiedName& attributeName, IsSupportedTokenFunction&& isSupportedToken = { }); |
| 37 | |
| 38 | void associatedAttributeValueChanged(const AtomicString&); |
| 39 | |
| 40 | void ref() { m_element.ref(); } |
| 41 | void deref() { m_element.deref(); } |
| 42 | |
| 43 | unsigned length() const; |
| 44 | const AtomicString& item(unsigned index) const; |
| 45 | |
| 46 | WEBCORE_EXPORT bool contains(const AtomicString&) const; |
| 47 | ExceptionOr<void> add(const Vector<String>&); |
| 48 | ExceptionOr<void> add(const AtomicString&); |
| 49 | ExceptionOr<void> remove(const Vector<String>&); |
| 50 | ExceptionOr<void> remove(const AtomicString&); |
| 51 | WEBCORE_EXPORT ExceptionOr<bool> toggle(const AtomicString&, Optional<bool> force); |
| 52 | ExceptionOr<bool> replace(const AtomicString& token, const AtomicString& 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 AtomicString& value() const; |
| 59 | |
| 60 | private: |
| 61 | void updateTokensFromAttributeValue(const String&); |
| 62 | void updateAssociatedAttributeFromTokens(); |
| 63 | |
| 64 | WEBCORE_EXPORT Vector<AtomicString>& tokens(); |
| 65 | const Vector<AtomicString>& 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<AtomicString> m_tokens; |
| 77 | IsSupportedTokenFunction m_isSupportedToken; |
| 78 | }; |
| 79 | |
| 80 | inline unsigned DOMTokenList::length() const |
| 81 | { |
| 82 | return tokens().size(); |
| 83 | } |
| 84 | |
| 85 | inline const AtomicString& 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 | |