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
12CXFA_AttachNodeList::CXFA_AttachNodeList(CXFA_Document* pDocument,
13                                         CXFA_Node* pAttachNode)
14    : CXFA_TreeList(pDocument) {
15  m_pAttachNode = pAttachNode;
16}
17
18size_t CXFA_AttachNodeList::GetLength() {
19  return m_pAttachNode->CountChildren(
20      XFA_Element::Unknown,
21      m_pAttachNode->GetElementType() == XFA_Element::Subform);
22}
23
24bool CXFA_AttachNodeList::Append(CXFA_Node* pNode) {
25  CXFA_Node* pParent = pNode->GetParent();
26  if (pParent)
27    pParent->RemoveChild(pNode, true);
28
29  return m_pAttachNode->InsertChild(pNode, nullptr);
30}
31
32bool CXFA_AttachNodeList::Insert(CXFA_Node* pNewNode, CXFA_Node* pBeforeNode) {
33  CXFA_Node* pParent = pNewNode->GetParent();
34  if (pParent)
35    pParent->RemoveChild(pNewNode, true);
36
37  return m_pAttachNode->InsertChild(pNewNode, pBeforeNode);
38}
39
40bool CXFA_AttachNodeList::Remove(CXFA_Node* pNode) {
41  return m_pAttachNode->RemoveChild(pNode, true);
42}
43
44CXFA_Node* CXFA_AttachNodeList::Item(size_t index) {
45  return m_pAttachNode->GetChild<CXFA_Node>(
46      index, XFA_Element::Unknown,
47      m_pAttachNode->GetElementType() == XFA_Element::Subform);
48}
49