diff options
author | Lei Zhang <thestig@chromium.org> | 2017-12-13 14:42:16 +0000 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2017-12-13 14:42:16 +0000 |
commit | ad38ca5a030e17e48495e52457192914a749912b (patch) | |
tree | 9bf4c4d59b893bd15aaee1d603b2ff73118a6cf5 /core/fxcrt/xml/cxml_element.cpp | |
parent | 9a3a077e4820efa035eca4aec7e93fbd1298d6cb (diff) | |
download | pdfium-ad38ca5a030e17e48495e52457192914a749912b.tar.xz |
Fix some nits in CXML_Element.
Move FX_XML_SplitQualifiedName() out of CXML_Parser, and add
CXML_Element::MatchesElement().
Change-Id: I34adc6f3b19e745ae33db0edf65184346cc1c13f
Reviewed-on: https://pdfium-review.googlesource.com/21073
Commit-Queue: dsinclair <dsinclair@chromium.org>
Reviewed-by: dsinclair <dsinclair@chromium.org>
Diffstat (limited to 'core/fxcrt/xml/cxml_element.cpp')
-rw-r--r-- | core/fxcrt/xml/cxml_element.cpp | 44 |
1 files changed, 34 insertions, 10 deletions
diff --git a/core/fxcrt/xml/cxml_element.cpp b/core/fxcrt/xml/cxml_element.cpp index f11fd1fe14..ad9859e2c6 100644 --- a/core/fxcrt/xml/cxml_element.cpp +++ b/core/fxcrt/xml/cxml_element.cpp @@ -9,6 +9,25 @@ #include "core/fxcrt/xml/cxml_content.h" #include "core/fxcrt/xml/cxml_parser.h" +namespace { + +void SplitQualifiedName(const ByteStringView& bsFullName, + ByteStringView* bsSpace, + ByteStringView* bsName) { + if (bsFullName.IsEmpty()) + return; + + auto iStart = bsFullName.Find(':'); + if (iStart.has_value()) { + *bsSpace = bsFullName.Left(iStart.value()); + *bsName = bsFullName.Right(bsFullName.GetLength() - (iStart.value() + 1)); + } else { + *bsName = bsFullName; + } +} + +} // namespace + // static std::unique_ptr<CXML_Element> CXML_Element::Parse(const void* pBuffer, size_t size) { @@ -38,19 +57,19 @@ ByteString CXML_Element::GetTagName() const { } ByteString CXML_Element::GetNamespaceURI(const ByteString& qName) const { - const WideString* pwsSpace; const CXML_Element* pElement = this; do { + const WideString* pwsSpace; if (qName.IsEmpty()) pwsSpace = pElement->m_AttrMap.Lookup("", "xmlns"); else pwsSpace = pElement->m_AttrMap.Lookup("xmlns", qName); if (pwsSpace) - break; + return pwsSpace->UTF8Encode(); pElement = pElement->GetParent(); } while (pElement); - return pwsSpace ? pwsSpace->UTF8Encode() : ByteString(); + return ByteString(); } void CXML_Element::GetAttrByIndex(size_t index, @@ -69,7 +88,7 @@ void CXML_Element::GetAttrByIndex(size_t index, WideString CXML_Element::GetAttrValue(const ByteStringView& name) const { ByteStringView bsSpace; ByteStringView bsName; - FX_XML_SplitQualifiedName(name, bsSpace, bsName); + SplitQualifiedName(name, &bsSpace, &bsName); WideString attr; const WideString* pValue = @@ -82,7 +101,7 @@ WideString CXML_Element::GetAttrValue(const ByteStringView& name) const { int CXML_Element::GetAttrInteger(const ByteStringView& name) const { ByteStringView bsSpace; ByteStringView bsName; - FX_XML_SplitQualifiedName(name, bsSpace, bsName); + SplitQualifiedName(name, &bsSpace, &bsName); const WideString* pwsValue = m_AttrMap.Lookup(ByteString(bsSpace), ByteString(bsName)); @@ -94,10 +113,8 @@ size_t CXML_Element::CountElements(const ByteStringView& space, size_t count = 0; for (const auto& pChild : m_Children) { const CXML_Element* pKid = pChild->AsElement(); - if (pKid && pKid->m_TagName == tag && - (space.IsEmpty() || pKid->m_QSpaceName == space)) { + if (MatchesElement(pKid, space, tag)) count++; - } } return count; } @@ -111,8 +128,7 @@ CXML_Element* CXML_Element::GetElement(const ByteStringView& space, size_t nth) const { for (const auto& pChild : m_Children) { CXML_Element* pKid = pChild->AsElement(); - if (pKid && pKid->m_TagName == tag && - (space.IsEmpty() || pKid->m_QSpaceName == space)) { + if (MatchesElement(pKid, space, tag)) { if (nth == 0) return pKid; --nth; @@ -126,3 +142,11 @@ void CXML_Element::SetAttribute(const ByteString& space, const WideString& value) { m_AttrMap.SetAt(space, name, value); } + +// static +bool CXML_Element::MatchesElement(const CXML_Element* pKid, + const ByteStringView& space, + const ByteStringView& tag) { + return pKid && pKid->m_TagName == tag && + (space.IsEmpty() || pKid->m_QSpaceName == space); +} |