1 // Copyright 2017 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "core/fxcrt/css/cfx_cssvaluelistparser.h"
8
9 #include "core/fxcrt/fx_extension.h"
10
CFX_CSSValueListParser(const wchar_t * psz,int32_t iLen,wchar_t separator)11 CFX_CSSValueListParser::CFX_CSSValueListParser(const wchar_t* psz,
12 int32_t iLen,
13 wchar_t separator)
14 : m_Separator(separator), m_pCur(psz), m_pEnd(psz + iLen) {
15 ASSERT(psz);
16 ASSERT(iLen > 0);
17 }
18
NextValue(CFX_CSSPrimitiveType * eType,const wchar_t ** pStart,int32_t * iLength)19 bool CFX_CSSValueListParser::NextValue(CFX_CSSPrimitiveType* eType,
20 const wchar_t** pStart,
21 int32_t* iLength) {
22 while (m_pCur < m_pEnd && (*m_pCur <= ' ' || *m_pCur == m_Separator))
23 ++m_pCur;
24
25 if (m_pCur >= m_pEnd)
26 return false;
27
28 *eType = CFX_CSSPrimitiveType::Unknown;
29 *pStart = m_pCur;
30 *iLength = 0;
31 wchar_t wch = *m_pCur;
32 if (wch == '#') {
33 *iLength = SkipTo(' ', false, false);
34 if (*iLength == 4 || *iLength == 7)
35 *eType = CFX_CSSPrimitiveType::RGB;
36 } else if (FXSYS_IsDecimalDigit(wch) || wch == '.' || wch == '-' ||
37 wch == '+') {
38 while (m_pCur < m_pEnd && (*m_pCur > ' ' && *m_pCur != m_Separator))
39 ++m_pCur;
40
41 *iLength = m_pCur - *pStart;
42 *eType = CFX_CSSPrimitiveType::Number;
43 } else if (wch == '\"' || wch == '\'') {
44 ++(*pStart);
45 m_pCur++;
46 *iLength = SkipTo(wch, false, false);
47 m_pCur++;
48 *eType = CFX_CSSPrimitiveType::String;
49 } else if (m_pEnd - m_pCur > 5 && m_pCur[3] == '(') {
50 if (FXSYS_wcsnicmp(L"rgb", m_pCur, 3) == 0) {
51 *iLength = SkipTo(')', false, false) + 1;
52 m_pCur++;
53 *eType = CFX_CSSPrimitiveType::RGB;
54 }
55 } else {
56 *iLength = SkipTo(m_Separator, true, true);
57 *eType = CFX_CSSPrimitiveType::String;
58 }
59 return m_pCur <= m_pEnd && *iLength > 0;
60 }
61
SkipTo(wchar_t wch,bool breakOnSpace,bool matchBrackets)62 int32_t CFX_CSSValueListParser::SkipTo(wchar_t wch,
63 bool breakOnSpace,
64 bool matchBrackets) {
65 const wchar_t* pStart = m_pCur;
66 int32_t bracketCount = 0;
67 while (m_pCur < m_pEnd && *m_pCur != wch) {
68 if (breakOnSpace && *m_pCur <= ' ')
69 break;
70 if (!matchBrackets) {
71 m_pCur++;
72 continue;
73 }
74
75 if (*m_pCur == '(')
76 bracketCount++;
77 else if (*m_pCur == ')')
78 bracketCount--;
79
80 m_pCur++;
81 }
82
83 while (bracketCount > 0 && m_pCur < m_pEnd) {
84 if (*m_pCur == ')')
85 bracketCount--;
86 m_pCur++;
87 }
88 return m_pCur - pStart;
89 }
90