diff options
Diffstat (limited to 'xfa')
-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 |
3 files changed, 180 insertions, 69 deletions
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)); +} |