blob: 1af53cca8ea6b8f0428640743a3146688fd882ce [file] [log] [blame]
weinig@apple.com1bed3cb2007-12-14 21:48:20 +00001/*
cdumez@apple.com406f1502015-08-21 02:10:29 +00002 * Copyright (C) 2007, 2014, 2015 Apple Inc. All rights reserved.
weinig@apple.com1bed3cb2007-12-14 21:48:20 +00003 * Copyright (C) 2007 David Smith (catfish.man@gmail.com)
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 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
mjs@apple.com92047332014-03-15 04:08:27 +000014 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
weinig@apple.com1bed3cb2007-12-14 21:48:20 +000015 * its contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
commit-queue@webkit.orgd862d772016-10-31 22:07:53 +000030#pragma once
weinig@apple.com1bed3cb2007-12-14 21:48:20 +000031
cdumez@apple.com406f1502015-08-21 02:10:29 +000032#include "CachedHTMLCollection.h"
antti@apple.com59718f52012-12-19 22:39:28 +000033#include "Element.h"
japhet@chromium.org8e9f23f2009-12-09 17:43:16 +000034#include "SpaceSplitString.h"
weinig@apple.com1bed3cb2007-12-14 21:48:20 +000035
36namespace WebCore {
37
cdumez@apple.com406f1502015-08-21 02:10:29 +000038class ClassCollection final : public CachedHTMLCollection<ClassCollection, CollectionTypeTraits<ByClass>::traversalType> {
antti@apple.com59718f52012-12-19 22:39:28 +000039public:
cdumez@apple.com406f1502015-08-21 02:10:29 +000040 static Ref<ClassCollection> create(ContainerNode&, CollectionType, const AtomicString& classNames);
weinig@apple.com1bed3cb2007-12-14 21:48:20 +000041
cdumez@apple.com406f1502015-08-21 02:10:29 +000042 virtual ~ClassCollection();
eric@webkit.org433116c2010-04-29 18:02:47 +000043
cdumez@apple.com406f1502015-08-21 02:10:29 +000044 bool elementMatches(Element&) const;
darin@apple.com642f5002008-06-07 22:51:37 +000045
antti@apple.com59718f52012-12-19 22:39:28 +000046private:
cdumez@apple.com406f1502015-08-21 02:10:29 +000047 ClassCollection(ContainerNode& rootNode, CollectionType, const AtomicString& classNames);
weinig@apple.com1bed3cb2007-12-14 21:48:20 +000048
antti@apple.com59718f52012-12-19 22:39:28 +000049 SpaceSplitString m_classNames;
cdumez@apple.com0f206612014-09-16 04:09:58 +000050 AtomicString m_originalClassNames;
antti@apple.com59718f52012-12-19 22:39:28 +000051};
52
cdumez@apple.com406f1502015-08-21 02:10:29 +000053inline ClassCollection::ClassCollection(ContainerNode& rootNode, CollectionType type, const AtomicString& classNames)
54 : CachedHTMLCollection<ClassCollection, CollectionTypeTraits<ByClass>::traversalType>(rootNode, type)
55 , m_classNames(classNames, rootNode.document().inQuirksMode())
cdumez@apple.com0f206612014-09-16 04:09:58 +000056 , m_originalClassNames(classNames)
57{
58}
59
cdumez@apple.com406f1502015-08-21 02:10:29 +000060inline bool ClassCollection::elementMatches(Element& element) const
antti@apple.com59718f52012-12-19 22:39:28 +000061{
cdumez@apple.com21022702014-09-18 16:32:41 +000062 if (!element.hasClass())
antti@apple.com59718f52012-12-19 22:39:28 +000063 return false;
cdumez@apple.comac8fffa2016-08-16 22:26:40 +000064 if (m_classNames.isEmpty())
antti@apple.com59718f52012-12-19 22:39:28 +000065 return false;
cdumez@apple.com21022702014-09-18 16:32:41 +000066 return element.classNames().containsAll(m_classNames);
antti@apple.com59718f52012-12-19 22:39:28 +000067}
weinig@apple.com1bed3cb2007-12-14 21:48:20 +000068
69} // namespace WebCore
70
cdumez@apple.com406f1502015-08-21 02:10:29 +000071SPECIALIZE_TYPE_TRAITS_HTMLCOLLECTION(ClassCollection, ByClass)