From c7600f99490f83e544e37feb77d3b9e8428b0f68 Mon Sep 17 00:00:00 2001 From: dsinclair Date: Thu, 19 May 2016 14:12:30 -0700 Subject: Remove CFX_DSPATemplate usage in CXFA_TextParser This CL changes the check to use std::binary_search instead of the custom lookup method. The tag validation has been split out to a separate method with unit tests added. Review-Url: https://codereview.chromium.org/1996623002 --- xfa/fxfa/app/xfa_textlayout.cpp | 45 +++++++++++++++++++++----------- xfa/fxfa/app/xfa_textlayout.h | 7 +++-- xfa/fxfa/app/xfa_textlayout_unittest.cpp | 40 ++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 17 deletions(-) create mode 100644 xfa/fxfa/app/xfa_textlayout_unittest.cpp (limited to 'xfa') diff --git a/xfa/fxfa/app/xfa_textlayout.cpp b/xfa/fxfa/app/xfa_textlayout.cpp index b7dfa3f168..91dd814e8d 100644 --- a/xfa/fxfa/app/xfa_textlayout.cpp +++ b/xfa/fxfa/app/xfa_textlayout.cpp @@ -220,14 +220,14 @@ void CXFA_TextParser::DoParse(CFDE_XMLNode* pXMLContainer, } void CXFA_TextParser::ParseRichText(CFDE_XMLNode* pXMLNode, IFDE_CSSComputedStyle* pParentStyle) { - if (pXMLNode == NULL) { + if (!pXMLNode) return; - } + CXFA_CSSTagProvider tagProvider; ParseTagInfo(pXMLNode, tagProvider); - if (!tagProvider.m_bTagAviliable) { + if (!tagProvider.m_bTagAvailable) return; - } + IFDE_CSSComputedStyle* pNewStyle = NULL; if ((tagProvider.GetTagName() != FX_WSTRC(L"body")) || (tagProvider.GetTagName() != FX_WSTRC(L"html"))) { @@ -263,30 +263,45 @@ void CXFA_TextParser::ParseRichText(CFDE_XMLNode* pXMLNode, if (pNewStyle) pNewStyle->Release(); } -void CXFA_TextParser::ParseTagInfo(CFDE_XMLNode* pXMLNode, - CXFA_CSSTagProvider& tagProvider) { + +bool CXFA_TextParser::TagValidate(const CFX_WideString& wsName) const { static const uint32_t s_XFATagName[] = { - 0x61, 0x62, 0x69, 0x70, 0x0001f714, - 0x00022a55, 0x000239bb, 0x00025881, 0x0bd37faa, 0x0bd37fb8, - 0xa73e3af2, 0xb182eaae, 0xdb8ac455, + 0x61, // a + 0x62, // b + 0x69, // i + 0x70, // p + 0x0001f714, // br + 0x00022a55, // li + 0x000239bb, // ol + 0x00025881, // ul + 0x0bd37faa, // sub + 0x0bd37fb8, // sup + 0xa73e3af2, // span + 0xb182eaae, // body + 0xdb8ac455, // html }; + static const int32_t s_iCount = FX_ArraySize(s_XFATagName); + + return std::binary_search(s_XFATagName, s_XFATagName + s_iCount, + FX_HashCode_GetW(wsName.AsStringC(), true)); +} + +void CXFA_TextParser::ParseTagInfo(CFDE_XMLNode* pXMLNode, + CXFA_CSSTagProvider& tagProvider) { CFX_WideString wsName; if (pXMLNode->GetType() == FDE_XMLNODE_Element) { CFDE_XMLElement* pXMLElement = static_cast(pXMLNode); pXMLElement->GetLocalTagName(wsName); tagProvider.SetTagNameObj(wsName); - uint32_t dwHashCode = FX_HashCode_GetW(wsName.AsStringC(), true); - static const int32_t s_iCount = sizeof(s_XFATagName) / sizeof(uint32_t); - CFX_DSPATemplate lookup; - tagProvider.m_bTagAviliable = - lookup.Lookup(dwHashCode, s_XFATagName, s_iCount) > -1; + tagProvider.m_bTagAvailable = TagValidate(wsName); + CFX_WideString wsValue; pXMLElement->GetString(L"style", wsValue); if (!wsValue.IsEmpty()) { tagProvider.SetAttribute(L"style", wsValue); } } else if (pXMLNode->GetType() == FDE_XMLNODE_Text) { - tagProvider.m_bTagAviliable = TRUE; + tagProvider.m_bTagAvailable = TRUE; tagProvider.m_bContent = TRUE; } } diff --git a/xfa/fxfa/app/xfa_textlayout.h b/xfa/fxfa/app/xfa_textlayout.h index 4d03aee302..c34f181746 100644 --- a/xfa/fxfa/app/xfa_textlayout.h +++ b/xfa/fxfa/app/xfa_textlayout.h @@ -26,7 +26,7 @@ class CXFA_TextTabstopsContext; class CXFA_CSSTagProvider { public: - CXFA_CSSTagProvider() : m_bTagAviliable(FALSE), m_bContent(FALSE) {} + CXFA_CSSTagProvider() : m_bTagAvailable(FALSE), m_bContent(FALSE) {} ~CXFA_CSSTagProvider() {} CFX_WideString GetTagName() { return m_wsTagName; } @@ -43,7 +43,7 @@ class CXFA_CSSTagProvider { m_Attributes.insert({wsAttr, wsValue}); } - FX_BOOL m_bTagAviliable; + FX_BOOL m_bTagAvailable; FX_BOOL m_bContent; protected: @@ -124,6 +124,9 @@ class CXFA_TextParser { CFX_WideString& wsValue); CXFA_TextParseContext* GetParseContextFromMap(CFDE_XMLNode* pXMLNode); + protected: + bool TagValidate(const CFX_WideString& str) const; + private: void InitCSSData(CXFA_TextProvider* pTextProvider); void ParseRichText(CFDE_XMLNode* pXMLNode, diff --git a/xfa/fxfa/app/xfa_textlayout_unittest.cpp b/xfa/fxfa/app/xfa_textlayout_unittest.cpp new file mode 100644 index 0000000000..7354631dbb --- /dev/null +++ b/xfa/fxfa/app/xfa_textlayout_unittest.cpp @@ -0,0 +1,40 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "xfa/fxfa/app/xfa_textlayout.h" + +#include "testing/gtest/include/gtest/gtest.h" + +class CXFA_TestTextParser : public CXFA_TextParser { + public: + CXFA_TestTextParser() : CXFA_TextParser() {} + + private: + // Add test cases as friends to access protected member functions. + FRIEND_TEST(CXFA_TextParser, TagValidate); +}; + +TEST(CXFA_TextParser, TagValidate) { + CXFA_TestTextParser parser; + EXPECT_TRUE(parser.TagValidate(L"br")); + EXPECT_TRUE(parser.TagValidate(L"Br")); + EXPECT_TRUE(parser.TagValidate(L"BR")); + EXPECT_TRUE(parser.TagValidate(L"a")); + EXPECT_TRUE(parser.TagValidate(L"b")); + EXPECT_TRUE(parser.TagValidate(L"i")); + EXPECT_TRUE(parser.TagValidate(L"p")); + EXPECT_TRUE(parser.TagValidate(L"li")); + EXPECT_TRUE(parser.TagValidate(L"ol")); + EXPECT_TRUE(parser.TagValidate(L"ul")); + EXPECT_TRUE(parser.TagValidate(L"sub")); + EXPECT_TRUE(parser.TagValidate(L"sup")); + EXPECT_TRUE(parser.TagValidate(L"span")); + EXPECT_TRUE(parser.TagValidate(L"body")); + EXPECT_TRUE(parser.TagValidate(L"html")); + + EXPECT_FALSE(parser.TagValidate(L"")); + EXPECT_FALSE(parser.TagValidate(L"tml")); + EXPECT_FALSE(parser.TagValidate(L"xhtml")); + EXPECT_FALSE(parser.TagValidate(L"htmlx")); +} -- cgit v1.2.3