1/*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * Copyright (C) 2003-2019 Apple Inc. All rights reserved.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 *
21 */
22
23#pragma once
24
25#include "CachedHTMLCollection.h"
26#include "NodeRareData.h"
27#include <wtf/IsoMalloc.h>
28#include <wtf/text/AtomString.h>
29
30namespace WebCore {
31
32class Document;
33
34template <typename HTMLCollectionClass, CollectionTraversalType traversalType>
35class HTMLNameCollection : public CachedHTMLCollection<HTMLCollectionClass, traversalType> {
36 WTF_MAKE_ISO_NONALLOCATABLE(HTMLNameCollection);
37public:
38 virtual ~HTMLNameCollection();
39
40 Document& document() { return downcast<Document>(this->ownerNode()); }
41
42protected:
43 HTMLNameCollection(Document&, CollectionType, const AtomString& name);
44
45 AtomString m_name;
46};
47
48template <typename HTMLCollectionClass, CollectionTraversalType traversalType>
49HTMLNameCollection<HTMLCollectionClass, traversalType>::HTMLNameCollection(Document& document, CollectionType type, const AtomString& name)
50 : CachedHTMLCollection<HTMLCollectionClass, traversalType>(document, type)
51 , m_name(name)
52{
53}
54
55template <typename HTMLCollectionClass, CollectionTraversalType traversalType>
56HTMLNameCollection<HTMLCollectionClass, traversalType>::~HTMLNameCollection()
57{
58 ASSERT(this->type() == WindowNamedItems || this->type() == DocumentNamedItems);
59
60 document().nodeLists()->removeCachedCollection(this, m_name);
61}
62
63class WindowNameCollection final : public HTMLNameCollection<WindowNameCollection, CollectionTraversalType::Descendants> {
64 WTF_MAKE_ISO_ALLOCATED(WindowNameCollection);
65public:
66 static Ref<WindowNameCollection> create(Document& document, CollectionType type, const AtomString& name)
67 {
68 return adoptRef(*new WindowNameCollection(document, type, name));
69 }
70
71 // For CachedHTMLCollection.
72 bool elementMatches(const Element& element) const { return elementMatches(element, m_name.impl()); }
73
74 static bool elementMatchesIfIdAttributeMatch(const Element&) { return true; }
75 static bool elementMatchesIfNameAttributeMatch(const Element&);
76 static bool elementMatches(const Element&, const AtomStringImpl*);
77
78private:
79 WindowNameCollection(Document& document, CollectionType type, const AtomString& name)
80 : HTMLNameCollection<WindowNameCollection, CollectionTraversalType::Descendants>(document, type, name)
81 {
82 ASSERT(type == WindowNamedItems);
83 }
84};
85
86class DocumentNameCollection final : public HTMLNameCollection<DocumentNameCollection, CollectionTraversalType::Descendants> {
87 WTF_MAKE_ISO_ALLOCATED(DocumentNameCollection);
88public:
89 static Ref<DocumentNameCollection> create(Document& document, CollectionType type, const AtomString& name)
90 {
91 return adoptRef(*new DocumentNameCollection(document, type, name));
92 }
93
94 static bool elementMatchesIfIdAttributeMatch(const Element&);
95 static bool elementMatchesIfNameAttributeMatch(const Element&);
96
97 // For CachedHTMLCollection.
98 bool elementMatches(const Element& element) const { return elementMatches(element, m_name.impl()); }
99
100 static bool elementMatches(const Element&, const AtomStringImpl*);
101
102private:
103 DocumentNameCollection(Document& document, CollectionType type, const AtomString& name)
104 : HTMLNameCollection<DocumentNameCollection, CollectionTraversalType::Descendants>(document, type, name)
105 {
106 ASSERT(type == DocumentNamedItems);
107 }
108};
109
110} // namespace WebCore
111
112SPECIALIZE_TYPE_TRAITS_HTMLCOLLECTION(WindowNameCollection, WindowNamedItems)
113SPECIALIZE_TYPE_TRAITS_HTMLCOLLECTION(DocumentNameCollection, DocumentNamedItems)
114