diff options
author | Dan Sinclair <dsinclair@chromium.org> | 2017-02-07 11:47:24 -0500 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2017-02-08 14:07:04 +0000 |
commit | 36c18c872addaa2d5cc36d1b218f8d2dc22a38be (patch) | |
tree | 6eaeb93a8566b8efca547600cb20d4b6253b7949 | |
parent | 071d78690a4e2becffaeeb32fe210ee58ab3e532 (diff) | |
download | pdfium-36c18c872addaa2d5cc36d1b218f8d2dc22a38be.tar.xz |
Add CFDE_CSSValueListParser tests
This Cl adds some unit tests for the CFDE_CSSValueListParser prior to
refactoring.
Change-Id: I2b32775ea1e70b721f5902cd3d88d68a01166d5e
Reviewed-on: https://pdfium-review.googlesource.com/2535
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Nicolás Peña <npm@chromium.org>
Commit-Queue: dsinclair <dsinclair@chromium.org>
-rw-r--r-- | BUILD.gn | 1 | ||||
-rw-r--r-- | xfa/fde/css/cfde_cssvaluelistparser.cpp | 100 | ||||
-rw-r--r-- | xfa/fde/css/cfde_cssvaluelistparser.h | 7 | ||||
-rw-r--r-- | xfa/fde/css/cfde_cssvaluelistparser_unittest.cpp | 142 |
4 files changed, 181 insertions, 69 deletions
@@ -1782,6 +1782,7 @@ test("pdfium_unittests") { "xfa/fde/cfde_txtedtbuf_unittest.cpp", "xfa/fde/css/cfde_cssdeclaration_unittest.cpp", "xfa/fde/css/cfde_cssstylesheet_unittest.cpp", + "xfa/fde/css/cfde_cssvaluelistparser_unittest.cpp", "xfa/fde/xml/fde_xml_imp_unittest.cpp", "xfa/fxbarcode/pdf417/BC_PDF417HighLevelEncoder_unittest.cpp", "xfa/fxfa/app/cxfa_textparser_unittest.cpp", diff --git a/xfa/fde/css/cfde_cssvaluelistparser.cpp b/xfa/fde/css/cfde_cssvaluelistparser.cpp index 3c204b0d2b..42c3296865 100644 --- a/xfa/fde/css/cfde_cssvaluelistparser.cpp +++ b/xfa/fde/css/cfde_cssvaluelistparser.cpp @@ -16,50 +16,36 @@ CFDE_CSSValueListParser::CFDE_CSSValueListParser(const FX_WCHAR* psz, bool CFDE_CSSValueListParser::NextValue(FDE_CSSPrimitiveType& eType, const FX_WCHAR*& pStart, int32_t& iLength) { - while (m_pCur < m_pEnd && (*m_pCur <= ' ' || *m_pCur == m_Separator)) { + while (m_pCur < m_pEnd && (*m_pCur <= ' ' || *m_pCur == m_Separator)) ++m_pCur; - } - if (m_pCur >= m_pEnd) { + + if (m_pCur >= m_pEnd) return false; - } + eType = FDE_CSSPrimitiveType::Unknown; pStart = m_pCur; iLength = 0; FX_WCHAR wch = *m_pCur; if (wch == '#') { - iLength = SkipTo(' '); - if (iLength == 4 || iLength == 7) { + iLength = SkipTo(' ', false, false); + if (iLength == 4 || iLength == 7) eType = FDE_CSSPrimitiveType::RGB; - } } else if ((wch >= '0' && wch <= '9') || wch == '.' || wch == '-' || wch == '+') { - while (m_pCur < m_pEnd && (*m_pCur > ' ' && *m_pCur != m_Separator)) { + while (m_pCur < m_pEnd && (*m_pCur > ' ' && *m_pCur != m_Separator)) ++m_pCur; - } + iLength = m_pCur - pStart; - if (iLength > 0) { - eType = FDE_CSSPrimitiveType::Number; - } + eType = FDE_CSSPrimitiveType::Number; } else if (wch == '\"' || wch == '\'') { pStart++; - iLength = SkipTo(wch) - 1; + m_pCur++; + iLength = SkipTo(wch, false, false); m_pCur++; eType = FDE_CSSPrimitiveType::String; } else if (m_pEnd - m_pCur > 5 && m_pCur[3] == '(') { - if (FXSYS_wcsnicmp(L"url", m_pCur, 3) == 0) { - wch = m_pCur[4]; - if (wch == '\"' || wch == '\'') { - pStart += 5; - iLength = SkipTo(wch) - 6; - m_pCur += 2; - } else { - pStart += 4; - iLength = SkipTo(')') - 4; - m_pCur++; - } - eType = FDE_CSSPrimitiveType::String; - } else if (FXSYS_wcsnicmp(L"rgb", m_pCur, 3) == 0) { - iLength = SkipTo(')') + 1; + if (FXSYS_wcsnicmp(L"rgb", m_pCur, 3) == 0) { + iLength = SkipTo(')', false, false) + 1; m_pCur++; eType = FDE_CSSPrimitiveType::RGB; } @@ -69,48 +55,32 @@ bool CFDE_CSSValueListParser::NextValue(FDE_CSSPrimitiveType& eType, } return m_pCur <= m_pEnd && iLength > 0; } + int32_t CFDE_CSSValueListParser::SkipTo(FX_WCHAR wch, - bool bWSSeparator, - bool bBrContinue) { + bool breakOnSpace, + bool matchBrackets) { const FX_WCHAR* pStart = m_pCur; - if (!bBrContinue) { - if (bWSSeparator) { - while ((++m_pCur < m_pEnd) && (*m_pCur != wch) && (*m_pCur > ' ')) { - continue; - } - } else { - while (++m_pCur < m_pEnd && *m_pCur != wch) { - continue; - } - } - - } else { - int32_t iBracketCount = 0; - if (bWSSeparator) { - while ((m_pCur < m_pEnd) && (*m_pCur != wch) && (*m_pCur > ' ')) { - if (*m_pCur == '(') { - iBracketCount++; - } else if (*m_pCur == ')') { - iBracketCount--; - } - m_pCur++; - } - } else { - while (m_pCur < m_pEnd && *m_pCur != wch) { - if (*m_pCur == '(') { - iBracketCount++; - } else if (*m_pCur == ')') { - iBracketCount--; - } - m_pCur++; - } - } - while (iBracketCount > 0 && m_pCur < m_pEnd) { - if (*m_pCur == ')') { - iBracketCount--; - } + int32_t bracketCount = 0; + while (m_pCur < m_pEnd && *m_pCur != wch) { + if (breakOnSpace && *m_pCur <= ' ') + break; + if (!matchBrackets) { m_pCur++; + continue; } + + if (*m_pCur == '(') + bracketCount++; + else if (*m_pCur == ')') + bracketCount--; + + m_pCur++; + } + + while (bracketCount > 0 && m_pCur < m_pEnd) { + if (*m_pCur == ')') + bracketCount--; + m_pCur++; } return m_pCur - pStart; } diff --git a/xfa/fde/css/cfde_cssvaluelistparser.h b/xfa/fde/css/cfde_cssvaluelistparser.h index 731ac3c84b..734aed8a52 100644 --- a/xfa/fde/css/cfde_cssvaluelistparser.h +++ b/xfa/fde/css/cfde_cssvaluelistparser.h @@ -19,12 +19,11 @@ class CFDE_CSSValueListParser { bool NextValue(FDE_CSSPrimitiveType& eType, const FX_WCHAR*& pStart, int32_t& iLength); + FX_WCHAR m_Separator; - protected: - int32_t SkipTo(FX_WCHAR wch, - bool bWSSeparator = false, - bool bBrContinue = false); + private: + int32_t SkipTo(FX_WCHAR wch, bool breakOnSpace, bool matchBrackets); const FX_WCHAR* m_pCur; const FX_WCHAR* m_pEnd; diff --git a/xfa/fde/css/cfde_cssvaluelistparser_unittest.cpp b/xfa/fde/css/cfde_cssvaluelistparser_unittest.cpp new file mode 100644 index 0000000000..71bb807f3d --- /dev/null +++ b/xfa/fde/css/cfde_cssvaluelistparser_unittest.cpp @@ -0,0 +1,142 @@ +// Copyright 2017 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. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "xfa/fde/css/cfde_cssvaluelistparser.h" + +#include "testing/gtest/include/gtest/gtest.h" +#include "third_party/base/ptr_util.h" + +TEST(CFDE_CSSValueListParser, rgb_short) { + FDE_CSSPrimitiveType type; + const FX_WCHAR* start; + int32_t len; + + auto parser = pdfium::MakeUnique<CFDE_CSSValueListParser>(L"#abc", 4, L' '); + EXPECT_TRUE(parser->NextValue(type, start, len)); + EXPECT_EQ(FDE_CSSPrimitiveType::RGB, type); + EXPECT_EQ(L"#abc", CFX_WideString(start, len)); + EXPECT_FALSE(parser->NextValue(type, start, len)); + + parser = pdfium::MakeUnique<CFDE_CSSValueListParser>(L"#abcdef", 7, L' '); + EXPECT_TRUE(parser->NextValue(type, start, len)); + EXPECT_EQ(FDE_CSSPrimitiveType::RGB, type); + EXPECT_EQ(L"#abcdef", CFX_WideString(start, len)); + EXPECT_FALSE(parser->NextValue(type, start, len)); + + parser = + pdfium::MakeUnique<CFDE_CSSValueListParser>(L"rgb(1, 255, 4)", 14, L' '); + EXPECT_TRUE(parser->NextValue(type, start, len)); + EXPECT_EQ(FDE_CSSPrimitiveType::RGB, type); + EXPECT_EQ(L"rgb(1, 255, 4)", CFX_WideString(start, len)); + + parser = + pdfium::MakeUnique<CFDE_CSSValueListParser>(L"#abcdefghij", 11, L' '); + EXPECT_TRUE(parser->NextValue(type, start, len)); + EXPECT_EQ(FDE_CSSPrimitiveType::Unknown, type); + EXPECT_EQ(L"#abcdefghij", CFX_WideString(start, len)); + EXPECT_FALSE(parser->NextValue(type, start, len)); +} + +TEST(CFDE_CSSValueListParser, number_parsing) { + FDE_CSSPrimitiveType type; + const FX_WCHAR* start; + int32_t len; + + auto parser = pdfium::MakeUnique<CFDE_CSSValueListParser>(L"1234", 4, L' '); + EXPECT_TRUE(parser->NextValue(type, start, len)); + EXPECT_EQ(FDE_CSSPrimitiveType::Number, type); + EXPECT_EQ(L"1234", CFX_WideString(start, len)); + + parser = pdfium::MakeUnique<CFDE_CSSValueListParser>(L"-1234", 5, L' '); + EXPECT_TRUE(parser->NextValue(type, start, len)); + EXPECT_EQ(FDE_CSSPrimitiveType::Number, type); + EXPECT_EQ(L"-1234", CFX_WideString(start, len)); + + parser = pdfium::MakeUnique<CFDE_CSSValueListParser>(L"+1234", 5, L' '); + EXPECT_TRUE(parser->NextValue(type, start, len)); + EXPECT_EQ(FDE_CSSPrimitiveType::Number, type); + EXPECT_EQ(L"+1234", CFX_WideString(start, len)); + + parser = pdfium::MakeUnique<CFDE_CSSValueListParser>(L".1234", 5, L' '); + EXPECT_TRUE(parser->NextValue(type, start, len)); + EXPECT_EQ(FDE_CSSPrimitiveType::Number, type); + EXPECT_EQ(L".1234", CFX_WideString(start, len)); + + parser = pdfium::MakeUnique<CFDE_CSSValueListParser>(L"4321.1234", 9, L' '); + EXPECT_TRUE(parser->NextValue(type, start, len)); + EXPECT_EQ(FDE_CSSPrimitiveType::Number, type); + EXPECT_EQ(L"4321.1234", CFX_WideString(start, len)); + + // TODO(dsinclair): These should probably fail but currently don't. + parser = pdfium::MakeUnique<CFDE_CSSValueListParser>(L"4321.12.34", 10, L' '); + EXPECT_TRUE(parser->NextValue(type, start, len)); + EXPECT_EQ(FDE_CSSPrimitiveType::Number, type); + EXPECT_EQ(L"4321.12.34", CFX_WideString(start, len)); + + parser = pdfium::MakeUnique<CFDE_CSSValueListParser>(L"43a1.12.34", 10, L' '); + EXPECT_TRUE(parser->NextValue(type, start, len)); + EXPECT_EQ(FDE_CSSPrimitiveType::Number, type); + EXPECT_EQ(L"43a1.12.34", CFX_WideString(start, len)); +} + +TEST(CFDE_CSSValueListParser, string_parsing) { + FDE_CSSPrimitiveType type; + const FX_WCHAR* start; + int32_t len; + + auto parser = + pdfium::MakeUnique<CFDE_CSSValueListParser>(L"'string'", 8, L' '); + EXPECT_TRUE(parser->NextValue(type, start, len)); + EXPECT_EQ(FDE_CSSPrimitiveType::String, type); + EXPECT_EQ(L"string", CFX_WideString(start, len)); + + parser = pdfium::MakeUnique<CFDE_CSSValueListParser>(L"\"another string\"", + 16, L' '); + EXPECT_TRUE(parser->NextValue(type, start, len)); + EXPECT_EQ(FDE_CSSPrimitiveType::String, type); + EXPECT_EQ(L"another string", CFX_WideString(start, len)); + + parser = pdfium::MakeUnique<CFDE_CSSValueListParser>(L"standalone", 10, L' '); + EXPECT_TRUE(parser->NextValue(type, start, len)); + EXPECT_EQ(FDE_CSSPrimitiveType::String, type); + EXPECT_EQ(L"standalone", CFX_WideString(start, len)); +} + +TEST(CFDE_CSSValueListParser, multiparsing) { + FDE_CSSPrimitiveType type; + const FX_WCHAR* start; + int32_t len; + + auto parser = + pdfium::MakeUnique<CFDE_CSSValueListParser>(L"1, 2, 3", 7, L','); + EXPECT_TRUE(parser->NextValue(type, start, len)); + EXPECT_EQ(FDE_CSSPrimitiveType::Number, type); + EXPECT_EQ(L"1", CFX_WideString(start, len)); + + EXPECT_TRUE(parser->NextValue(type, start, len)); + EXPECT_EQ(FDE_CSSPrimitiveType::Number, type); + EXPECT_EQ(L"2", CFX_WideString(start, len)); + + EXPECT_TRUE(parser->NextValue(type, start, len)); + EXPECT_EQ(FDE_CSSPrimitiveType::Number, type); + EXPECT_EQ(L"3", CFX_WideString(start, len)); + + EXPECT_FALSE(parser->NextValue(type, start, len)); + + parser = pdfium::MakeUnique<CFDE_CSSValueListParser>( + L"'str', rgb(1, 2, 3), 4", 22, L','); + EXPECT_TRUE(parser->NextValue(type, start, len)); + EXPECT_EQ(FDE_CSSPrimitiveType::String, type); + EXPECT_EQ(L"str", CFX_WideString(start, len)); + + EXPECT_TRUE(parser->NextValue(type, start, len)); + EXPECT_EQ(FDE_CSSPrimitiveType::RGB, type); + EXPECT_EQ(L"rgb(1, 2, 3)", CFX_WideString(start, len)); + + EXPECT_TRUE(parser->NextValue(type, start, len)); + EXPECT_EQ(FDE_CSSPrimitiveType::Number, type); + EXPECT_EQ(L"4", CFX_WideString(start, len)); +} |