1/*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4 *           (C) 2001 Dirk Mueller (mueller@kde.org)
5 * Copyright (C) 2004, 2007, 2013 Apple Inc. All rights reserved.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB.  If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 *
22 */
23
24#ifndef ChildNodeList_h
25#define ChildNodeList_h
26
27#include "CollectionIndexCache.h"
28#include "NodeList.h"
29#include <wtf/Ref.h>
30#include <wtf/RefPtr.h>
31
32namespace WebCore {
33
34class ContainerNode;
35
36class EmptyNodeList final : public NodeList {
37public:
38    static PassRef<EmptyNodeList> create(Node& owner)
39    {
40        return adoptRef(*new EmptyNodeList(owner));
41    }
42    virtual ~EmptyNodeList();
43
44    Node& ownerNode() { return m_owner.get(); }
45
46private:
47    explicit EmptyNodeList(Node& owner) : m_owner(owner) { }
48
49    virtual unsigned length() const override { return 0; }
50    virtual Node* item(unsigned) const override { return nullptr; }
51    virtual Node* namedItem(const AtomicString&) const override { return nullptr; }
52    virtual size_t memoryCost() const override { return 0; }
53
54    virtual bool isEmptyNodeList() const override { return true; }
55
56    Ref<Node> m_owner;
57};
58
59class ChildNodeList final : public NodeList {
60public:
61    static PassRef<ChildNodeList> create(ContainerNode& parent)
62    {
63        return adoptRef(*new ChildNodeList(parent));
64    }
65
66    virtual ~ChildNodeList();
67
68    ContainerNode& ownerNode() { return m_parent.get(); }
69
70    void invalidateCache();
71
72    // For CollectionIndexCache
73    Node* collectionBegin() const;
74    Node* collectionLast() const;
75    Node* collectionEnd() const { return nullptr; }
76    void collectionTraverseForward(Node*&, unsigned count, unsigned& traversedCount) const;
77    void collectionTraverseBackward(Node*&, unsigned count) const;
78    bool collectionCanTraverseBackward() const { return true; }
79    void willValidateIndexCache() const { }
80
81private:
82    explicit ChildNodeList(ContainerNode& parent);
83
84    virtual unsigned length() const override;
85    virtual Node* item(unsigned index) const override;
86    virtual Node* namedItem(const AtomicString&) const override;
87    virtual size_t memoryCost() const override { return m_indexCache.memoryCost(); }
88
89    virtual bool isChildNodeList() const override { return true; }
90
91    Ref<ContainerNode> m_parent;
92    mutable CollectionIndexCache<ChildNodeList, Node*> m_indexCache;
93};
94
95} // namespace WebCore
96
97#endif // ChildNodeList_h
98