diff options
author | dan sinclair <dsinclair@chromium.org> | 2018-04-16 21:24:57 +0000 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2018-04-16 21:24:57 +0000 |
commit | 8b0cf76f00c6e89e8bb7bf4bcf2189b27baac31c (patch) | |
tree | ec18d4aa800ccd9d32f113f8ec41d89948479ab3 /core/fxcrt/xml/cfx_xmlelement.cpp | |
parent | 262b496f25be27978abe7d98f20978820ecaeec8 (diff) | |
download | pdfium-8b0cf76f00c6e89e8bb7bf4bcf2189b27baac31c.tar.xz |
Merge CFX_XMLElement and CFX_XMLAttributeNode
CFX_XMLElement is the only subclass of CFX_XMLAttributeNode. This CL
merges the two classes together. The {Set|Get}String method has been
renamed to {Set|Get}Attribute to make it clearer what you're retrieving.
Change-Id: I158c961d4d8c5f563d937a3e7a35321a33622562
Reviewed-on: https://pdfium-review.googlesource.com/30710
Commit-Queue: dsinclair <dsinclair@chromium.org>
Reviewed-by: Henrique Nakashima <hnakashima@chromium.org>
Diffstat (limited to 'core/fxcrt/xml/cfx_xmlelement.cpp')
-rw-r--r-- | core/fxcrt/xml/cfx_xmlelement.cpp | 85 |
1 files changed, 56 insertions, 29 deletions
diff --git a/core/fxcrt/xml/cfx_xmlelement.cpp b/core/fxcrt/xml/cfx_xmlelement.cpp index 27d136305b..42588c6393 100644 --- a/core/fxcrt/xml/cfx_xmlelement.cpp +++ b/core/fxcrt/xml/cfx_xmlelement.cpp @@ -16,17 +16,19 @@ #include "third_party/base/stl_util.h" CFX_XMLElement::CFX_XMLElement(const WideString& wsTag) - : CFX_XMLAttributeNode(wsTag) {} + : CFX_XMLNode(), name_(wsTag) { + ASSERT(!name_.IsEmpty()); +} -CFX_XMLElement::~CFX_XMLElement() {} +CFX_XMLElement::~CFX_XMLElement() = default; FX_XMLNODETYPE CFX_XMLElement::GetType() const { return FX_XMLNODE_Element; } std::unique_ptr<CFX_XMLNode> CFX_XMLElement::Clone() { - auto pClone = pdfium::MakeUnique<CFX_XMLElement>(GetName()); - pClone->SetAttributes(GetAttributes()); + auto pClone = pdfium::MakeUnique<CFX_XMLElement>(name_); + pClone->attrs_ = attrs_; WideString wsText; for (CFX_XMLNode* pChild = GetFirstChild(); pChild; @@ -39,15 +41,14 @@ std::unique_ptr<CFX_XMLNode> CFX_XMLElement::Clone() { } WideString CFX_XMLElement::GetLocalTagName() const { - auto pos = GetName().Find(L':'); - return pos.has_value() - ? GetName().Right(GetName().GetLength() - pos.value() - 1) - : GetName(); + auto pos = name_.Find(L':'); + return pos.has_value() ? name_.Right(name_.GetLength() - pos.value() - 1) + : name_; } WideString CFX_XMLElement::GetNamespacePrefix() const { - auto pos = GetName().Find(L':'); - return pos.has_value() ? GetName().Left(pos.value()) : WideString(); + auto pos = name_.Find(L':'); + return pos.has_value() ? name_.Left(pos.value()) : WideString(); } WideString CFX_XMLElement::GetNamespaceURI() const { @@ -68,7 +69,7 @@ WideString CFX_XMLElement::GetNamespaceURI() const { pNode = pNode->GetParent(); continue; } - return pElement->GetString(wsAttri); + return pElement->GetAttribute(wsAttri); } return WideString(); } @@ -94,30 +95,28 @@ void CFX_XMLElement::SetTextData(const WideString& wsText) { void CFX_XMLElement::Save( const RetainPtr<CFX_SeekableStreamProxy>& pXMLStream) { - WideString ws(L"<"); - ws += GetName(); - pXMLStream->WriteString(ws.AsStringView()); + pXMLStream->WriteString(L"<"); + pXMLStream->WriteString(name_.AsStringView()); - for (auto it : GetAttributes()) { + for (auto it : attrs_) { pXMLStream->WriteString( AttributeToString(it.first, it.second).AsStringView()); } - if (GetFirstChild()) { - ws = L"\n>"; - pXMLStream->WriteString(ws.AsStringView()); + if (!GetFirstChild()) { + pXMLStream->WriteString(L" />"); + return; + } - for (CFX_XMLNode* pChild = GetFirstChild(); pChild; - pChild = pChild->GetNextSibling()) { - pChild->Save(pXMLStream); - } - ws = L"</"; - ws += GetName(); - ws += L"\n>"; - } else { - ws = L"\n/>"; + pXMLStream->WriteString(L">"); + + for (CFX_XMLNode* pChild = GetFirstChild(); pChild; + pChild = pChild->GetNextSibling()) { + pChild->Save(pXMLStream); } - pXMLStream->WriteString(ws.AsStringView()); + pXMLStream->WriteString(L"</"); + pXMLStream->WriteString(name_.AsStringView()); + pXMLStream->WriteString(L"\n>"); } CFX_XMLElement* CFX_XMLElement::GetFirstChildNamed( @@ -132,7 +131,7 @@ CFX_XMLElement* CFX_XMLElement::GetNthChildNamed(const WideStringView& name, continue; CFX_XMLElement* elem = static_cast<CFX_XMLElement*>(child); - if (elem->GetName() != name) + if (elem->name_ != name) continue; if (idx == 0) return elem; @@ -141,3 +140,31 @@ CFX_XMLElement* CFX_XMLElement::GetNthChildNamed(const WideStringView& name, } return nullptr; } + +bool CFX_XMLElement::HasAttribute(const WideString& name) const { + return attrs_.find(name) != attrs_.end(); +} + +WideString CFX_XMLElement::GetAttribute(const WideString& name) const { + auto it = attrs_.find(name); + return it != attrs_.end() ? it->second : L""; +} + +void CFX_XMLElement::SetAttribute(const WideString& name, + const WideString& value) { + attrs_[name] = value; +} + +void CFX_XMLElement::RemoveAttribute(const WideString& name) { + attrs_.erase(name); +} + +WideString CFX_XMLElement::AttributeToString(const WideString& name, + const WideString& value) { + WideString ret = L" "; + ret += name; + ret += L"=\""; + ret += EncodeEntities(value); + ret += L"\""; + return ret; +} |