2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "ClassList.h"
29 #include "HTMLNames.h"
30 #include "HTMLParserIdioms.h"
31 #include "SpaceSplitString.h"
32 #include <wtf/text/StringBuilder.h>
36 using namespace HTMLNames;
38 ClassList::ClassList(Element* element)
41 if (m_element->document()->inQuirksMode())
42 m_classNamesForQuirksMode.set(m_element->fastGetAttribute(classAttr), false);
50 void ClassList::deref()
55 unsigned ClassList::length() const
57 return m_element->hasClass() ? classNames().size() : 0;
60 const AtomicString ClassList::item(unsigned index) const
62 if (index >= length())
63 return AtomicString();
64 return classNames()[index];
67 bool ClassList::contains(const AtomicString& token, ExceptionCode& ec) const
69 if (!validateToken(token, ec))
71 return containsInternal(token);
74 bool ClassList::containsInternal(const AtomicString& token) const
76 return m_element->hasClass() && classNames().contains(token);
79 void ClassList::add(const AtomicString& token, ExceptionCode& ec)
81 if (!validateToken(token, ec))
86 void ClassList::addInternal(const AtomicString& token)
88 const AtomicString& oldClassName(m_element->fastGetAttribute(classAttr));
89 if (oldClassName.isEmpty())
90 m_element->setAttribute(classAttr, token);
91 else if (!containsInternal(token)) {
92 const AtomicString& newClassName(addToken(oldClassName, token));
93 m_element->setAttribute(classAttr, newClassName);
97 void ClassList::remove(const AtomicString& token, ExceptionCode& ec)
99 if (!validateToken(token, ec))
101 removeInternal(token);
104 void ClassList::removeInternal(const AtomicString& token)
106 // Check using contains first since it uses AtomicString comparisons instead
107 // of character by character testing.
108 if (!containsInternal(token))
110 const AtomicString& newClassName(removeToken(m_element->fastGetAttribute(classAttr), token));
111 m_element->setAttribute(classAttr, newClassName);
114 bool ClassList::toggle(const AtomicString& token, ExceptionCode& ec)
116 if (!validateToken(token, ec))
119 if (containsInternal(token)) {
120 removeInternal(token);
127 String ClassList::toString() const
129 return m_element->fastGetAttribute(classAttr);
132 void ClassList::reset(const String& newClassName)
134 if (!m_classNamesForQuirksMode.isNull())
135 m_classNamesForQuirksMode.set(newClassName, false);
138 const SpaceSplitString& ClassList::classNames() const
140 ASSERT(m_element->hasClass());
141 if (!m_classNamesForQuirksMode.isNull())
142 return m_classNamesForQuirksMode;
143 return m_element->attributeMap()->classNames();
146 } // namespace WebCore