1 // Copyright 2016 The PDFium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "xfa/fxfa/parser/cxfa_attachnodelist.h"
8
9 #include "third_party/base/numerics/safe_conversions.h"
10 #include "xfa/fxfa/parser/cxfa_node.h"
11
CXFA_AttachNodeList(CXFA_Document * pDocument,CXFA_Node * pAttachNode)12 CXFA_AttachNodeList::CXFA_AttachNodeList(CXFA_Document* pDocument,
13 CXFA_Node* pAttachNode)
14 : CXFA_TreeList(pDocument), m_pAttachNode(pAttachNode) {}
15
16 CXFA_AttachNodeList::~CXFA_AttachNodeList() = default;
17
Trace(cppgc::Visitor * visitor) const18 void CXFA_AttachNodeList::Trace(cppgc::Visitor* visitor) const {
19 CXFA_TreeList::Trace(visitor);
20 visitor->Trace(m_pAttachNode);
21 }
22
GetLength()23 size_t CXFA_AttachNodeList::GetLength() {
24 return m_pAttachNode->CountChildren(
25 XFA_Element::Unknown,
26 m_pAttachNode->GetElementType() == XFA_Element::Subform);
27 }
28
Append(CXFA_Node * pNode)29 bool CXFA_AttachNodeList::Append(CXFA_Node* pNode) {
30 if (pNode->IsAncestorOf(m_pAttachNode))
31 return false;
32
33 CXFA_Node* pParent = pNode->GetParent();
34 if (pParent)
35 pParent->RemoveChildAndNotify(pNode, true);
36
37 m_pAttachNode->InsertChildAndNotify(pNode, nullptr);
38 return true;
39 }
40
Insert(CXFA_Node * pNewNode,CXFA_Node * pBeforeNode)41 bool CXFA_AttachNodeList::Insert(CXFA_Node* pNewNode, CXFA_Node* pBeforeNode) {
42 if (pNewNode->IsAncestorOf(m_pAttachNode))
43 return false;
44
45 if (pBeforeNode && pBeforeNode->GetParent() != m_pAttachNode)
46 return false;
47
48 CXFA_Node* pParent = pNewNode->GetParent();
49 if (pParent)
50 pParent->RemoveChildAndNotify(pNewNode, true);
51
52 m_pAttachNode->InsertChildAndNotify(pNewNode, pBeforeNode);
53 return true;
54 }
55
Remove(CXFA_Node * pNode)56 void CXFA_AttachNodeList::Remove(CXFA_Node* pNode) {
57 m_pAttachNode->RemoveChildAndNotify(pNode, true);
58 }
59
Item(size_t index)60 CXFA_Node* CXFA_AttachNodeList::Item(size_t index) {
61 return m_pAttachNode->GetChild<CXFA_Node>(
62 index, XFA_Element::Unknown,
63 m_pAttachNode->GetElementType() == XFA_Element::Subform);
64 }
65