From 8a6fdadccd2eedf332ae3a72f0149c1b40cb5bd9 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Tue, 9 May 2017 15:03:33 -0700 Subject: Create common CXML_Object base class for CXML_Content and CXML_Element. They should each know what they are rather than having an external ChildRecord struct to track the type. Change-Id: Ic647ba45569764073e944d30af1a96dccdc29eb3 Reviewed-on: https://pdfium-review.googlesource.com/5210 Reviewed-by: Lei Zhang Commit-Queue: Tom Sepez --- core/fxcrt/xml/cxml_element.cpp | 85 +++++++++++++---------------------------- 1 file changed, 27 insertions(+), 58 deletions(-) (limited to 'core/fxcrt/xml/cxml_element.cpp') diff --git a/core/fxcrt/xml/cxml_element.cpp b/core/fxcrt/xml/cxml_element.cpp index 95a6dba147..ec0a73b00d 100644 --- a/core/fxcrt/xml/cxml_element.cpp +++ b/core/fxcrt/xml/cxml_element.cpp @@ -23,25 +23,16 @@ CXML_Element::CXML_Element(const CXML_Element* pParent, const CFX_ByteStringC& tagname) : m_pParent(pParent), m_QSpaceName(qSpace), m_TagName(tagname) {} -CXML_Element::~CXML_Element() { - Empty(); -} +CXML_Element::~CXML_Element() {} -void CXML_Element::Empty() { - RemoveChildren(); +CXML_Element* CXML_Element::AsElement() { + return this; } -void CXML_Element::RemoveChildren() { - for (const ChildRecord& record : m_Children) { - if (record.type == Content) { - delete static_cast(record.child); - } else if (record.type == Element) { - CXML_Element* child = static_cast(record.child); - child->RemoveChildren(); - delete child; - } - } - m_Children.clear(); + +const CXML_Element* CXML_Element::AsElement() const { + return this; } + CFX_ByteString CXML_Element::GetTagName(bool bQualified) const { if (!bQualified || m_QSpaceName.IsEmpty()) { return m_TagName; @@ -159,69 +150,47 @@ bool CXML_Element::GetAttrFloat(const CFX_ByteStringC& space, return true; } -CXML_Element::ChildType CXML_Element::GetChildType(uint32_t index) const { - return index < m_Children.size() ? m_Children[index].type : Invalid; -} - -CFX_WideString CXML_Element::GetContent(uint32_t index) const { - if (index < m_Children.size() && m_Children[index].type == Content) { - CXML_Content* pContent = - static_cast(m_Children[index].child); - if (pContent) - return pContent->m_Content; - } - return CFX_WideString(); -} - -CXML_Element* CXML_Element::GetElement(uint32_t index) const { - if (index < m_Children.size() && m_Children[index].type == Element) - return static_cast(m_Children[index].child); - return nullptr; -} - uint32_t CXML_Element::CountElements(const CFX_ByteStringC& space, const CFX_ByteStringC& tag) const { int count = 0; - for (const ChildRecord& record : m_Children) { - if (record.type != Element) - continue; - - CXML_Element* pKid = static_cast(record.child); - if ((space.IsEmpty() || pKid->m_QSpaceName == space) && - pKid->m_TagName == tag) { + for (const auto& pChild : m_Children) { + const CXML_Element* pKid = pChild->AsElement(); + if (pKid && pKid->m_TagName == tag && + (space.IsEmpty() || pKid->m_QSpaceName == space)) { count++; } } return count; } +CXML_Object* CXML_Element::GetChild(uint32_t index) const { + return index < m_Children.size() ? m_Children[index].get() : nullptr; +} + CXML_Element* CXML_Element::GetElement(const CFX_ByteStringC& space, const CFX_ByteStringC& tag, - int index) const { - if (index < 0) + int nth) const { + if (nth < 0) return nullptr; - for (const ChildRecord& record : m_Children) { - if (record.type != Element) - continue; - - CXML_Element* pKid = static_cast(record.child); - if ((space.IsEmpty() || pKid->m_QSpaceName == space) && - pKid->m_TagName == tag) { - if (index-- == 0) + for (const auto& pChild : m_Children) { + CXML_Element* pKid = pChild->AsElement(); + if (pKid && pKid->m_TagName == tag && + (space.IsEmpty() || pKid->m_QSpaceName == space)) { + if (nth-- == 0) return pKid; } } return nullptr; } -uint32_t CXML_Element::FindElement(CXML_Element* pChild) const { +uint32_t CXML_Element::FindElement(CXML_Element* pElement) const { int index = 0; - for (const ChildRecord& record : m_Children) { - if (record.type == Element && - static_cast(record.child) == pChild) { + for (const auto& pChild : m_Children) { + CXML_Element* pKid = pChild->AsElement(); + if (pKid && pKid == pElement) return index; - } + ++index; } return 0xFFFFFFFF; -- cgit v1.2.3