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/AtomicString.h> |
29 | |
30 | namespace WebCore { |
31 | |
32 | class Document; |
33 | |
34 | template <typename HTMLCollectionClass, CollectionTraversalType traversalType> |
35 | class HTMLNameCollection : public CachedHTMLCollection<HTMLCollectionClass, traversalType> { |
36 | WTF_MAKE_ISO_NONALLOCATABLE(HTMLNameCollection); |
37 | public: |
38 | virtual ~HTMLNameCollection(); |
39 | |
40 | Document& document() { return downcast<Document>(this->ownerNode()); } |
41 | |
42 | protected: |
43 | HTMLNameCollection(Document&, CollectionType, const AtomicString& name); |
44 | |
45 | AtomicString m_name; |
46 | }; |
47 | |
48 | template <typename HTMLCollectionClass, CollectionTraversalType traversalType> |
49 | HTMLNameCollection<HTMLCollectionClass, traversalType>::HTMLNameCollection(Document& document, CollectionType type, const AtomicString& name) |
50 | : CachedHTMLCollection<HTMLCollectionClass, traversalType>(document, type) |
51 | , m_name(name) |
52 | { |
53 | } |
54 | |
55 | template <typename HTMLCollectionClass, CollectionTraversalType traversalType> |
56 | HTMLNameCollection<HTMLCollectionClass, traversalType>::~HTMLNameCollection() |
57 | { |
58 | ASSERT(this->type() == WindowNamedItems || this->type() == DocumentNamedItems); |
59 | |
60 | document().nodeLists()->removeCachedCollection(this, m_name); |
61 | } |
62 | |
63 | class WindowNameCollection final : public HTMLNameCollection<WindowNameCollection, CollectionTraversalType::Descendants> { |
64 | WTF_MAKE_ISO_ALLOCATED(WindowNameCollection); |
65 | public: |
66 | static Ref<WindowNameCollection> create(Document& document, CollectionType type, const AtomicString& 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 AtomicStringImpl*); |
77 | |
78 | private: |
79 | WindowNameCollection(Document& document, CollectionType type, const AtomicString& name) |
80 | : HTMLNameCollection<WindowNameCollection, CollectionTraversalType::Descendants>(document, type, name) |
81 | { |
82 | ASSERT(type == WindowNamedItems); |
83 | } |
84 | }; |
85 | |
86 | class DocumentNameCollection final : public HTMLNameCollection<DocumentNameCollection, CollectionTraversalType::Descendants> { |
87 | WTF_MAKE_ISO_ALLOCATED(DocumentNameCollection); |
88 | public: |
89 | static Ref<DocumentNameCollection> create(Document& document, CollectionType type, const AtomicString& 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 AtomicStringImpl*); |
101 | |
102 | private: |
103 | DocumentNameCollection(Document& document, CollectionType type, const AtomicString& name) |
104 | : HTMLNameCollection<DocumentNameCollection, CollectionTraversalType::Descendants>(document, type, name) |
105 | { |
106 | ASSERT(type == DocumentNamedItems); |
107 | } |
108 | }; |
109 | |
110 | } // namespace WebCore |
111 | |
112 | SPECIALIZE_TYPE_TRAITS_HTMLCOLLECTION(WindowNameCollection, WindowNamedItems) |
113 | SPECIALIZE_TYPE_TRAITS_HTMLCOLLECTION(DocumentNameCollection, DocumentNamedItems) |
114 | |