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, 2008 Apple Inc. All rights reserved.
6  * Copyright (C) 2014 Samsung Electronics. All rights reserved.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public License
19  * along with this library; see the file COPYING.LIB.  If not, write to
20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23 
24 #include "third_party/blink/renderer/core/dom/child_node_list.h"
25 
26 #include "third_party/blink/renderer/core/dom/element.h"
27 #include "third_party/blink/renderer/core/dom/node_rare_data.h"
28 
29 namespace blink {
30 
ChildNodeList(ContainerNode & parent)31 ChildNodeList::ChildNodeList(ContainerNode& parent) : parent_(parent) {}
32 
VirtualOwnerNode() const33 Node* ChildNodeList::VirtualOwnerNode() const {
34   return &OwnerNode();
35 }
36 
37 ChildNodeList::~ChildNodeList() = default;
38 
item(unsigned index) const39 Node* ChildNodeList::item(unsigned index) const {
40   return collection_index_cache_.NodeAt(*this, index);
41 }
42 
ChildrenChanged(const ContainerNode::ChildrenChange & change)43 void ChildNodeList::ChildrenChanged(
44     const ContainerNode::ChildrenChange& change) {
45   if (change.IsChildInsertion()) {
46     collection_index_cache_.NodeInserted();
47   } else if (change.IsChildRemoval()) {
48     collection_index_cache_.NodeRemoved();
49   } else {
50     collection_index_cache_.Invalidate();
51   }
52 }
53 
TraverseForwardToOffset(unsigned offset,Node & current_node,unsigned & current_offset) const54 Node* ChildNodeList::TraverseForwardToOffset(unsigned offset,
55                                              Node& current_node,
56                                              unsigned& current_offset) const {
57   DCHECK_LT(current_offset, offset);
58   DCHECK_EQ(OwnerNode().childNodes(), this);
59   DCHECK_EQ(&OwnerNode(), current_node.parentNode());
60   for (Node* next = current_node.nextSibling(); next;
61        next = next->nextSibling()) {
62     if (++current_offset == offset)
63       return next;
64   }
65   return nullptr;
66 }
67 
TraverseBackwardToOffset(unsigned offset,Node & current_node,unsigned & current_offset) const68 Node* ChildNodeList::TraverseBackwardToOffset(unsigned offset,
69                                               Node& current_node,
70                                               unsigned& current_offset) const {
71   DCHECK_GT(current_offset, offset);
72   DCHECK_EQ(OwnerNode().childNodes(), this);
73   DCHECK_EQ(&OwnerNode(), current_node.parentNode());
74   for (Node* previous = current_node.previousSibling(); previous;
75        previous = previous->previousSibling()) {
76     if (--current_offset == offset)
77       return previous;
78   }
79   return nullptr;
80 }
81 
Trace(Visitor * visitor) const82 void ChildNodeList::Trace(Visitor* visitor) const {
83   visitor->Trace(parent_);
84   visitor->Trace(collection_index_cache_);
85   NodeList::Trace(visitor);
86 }
87 
88 }  // namespace blink
89