1 // Copyright 2016 PDFium Authors. All rights reserved.
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
GetLength()18 size_t CXFA_AttachNodeList::GetLength() {
19 return m_pAttachNode->CountChildren(
20 XFA_Element::Unknown,
21 m_pAttachNode->GetElementType() == XFA_Element::Subform);
22 }
23
Append(CXFA_Node * pNode)24 void CXFA_AttachNodeList::Append(CXFA_Node* pNode) {
25 CXFA_Node* pParent = pNode->GetParent();
26 if (pParent)
27 pParent->RemoveChildAndNotify(pNode, true);
28
29 m_pAttachNode->InsertChildAndNotify(pNode, nullptr);
30 }
31
Insert(CXFA_Node * pNewNode,CXFA_Node * pBeforeNode)32 bool CXFA_AttachNodeList::Insert(CXFA_Node* pNewNode, CXFA_Node* pBeforeNode) {
33 if (pBeforeNode && pBeforeNode->GetParent() != m_pAttachNode)
34 return false;
35
36 CXFA_Node* pParent = pNewNode->GetParent();
37 if (pParent)
38 pParent->RemoveChildAndNotify(pNewNode, true);
39
40 m_pAttachNode->InsertChildAndNotify(pNewNode, pBeforeNode);
41 return true;
42 }
43
Remove(CXFA_Node * pNode)44 void CXFA_AttachNodeList::Remove(CXFA_Node* pNode) {
45 m_pAttachNode->RemoveChildAndNotify(pNode, true);
46 }
47
Item(size_t index)48 CXFA_Node* CXFA_AttachNodeList::Item(size_t index) {
49 return m_pAttachNode->GetChild<CXFA_Node>(
50 index, XFA_Element::Unknown,
51 m_pAttachNode->GetElementType() == XFA_Element::Subform);
52 }
53