Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1 | // Copyright 2014 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 | |
Dan Sinclair | 95bec80 | 2017-01-19 10:27:58 -0500 | [diff] [blame^] | 7 | #include "xfa/fde/css/cfde_csssyntaxparser.h" |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 8 | |
npm | 2399152 | 2016-11-28 12:49:29 -0800 | [diff] [blame] | 9 | #include <algorithm> |
| 10 | |
Dan Sinclair | 95bec80 | 2017-01-19 10:27:58 -0500 | [diff] [blame^] | 11 | #include "xfa/fde/css/cfde_cssdeclaration.h" |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 12 | #include "xfa/fde/css/fde_cssdatatable.h" |
| 13 | #include "xfa/fgas/crt/fgas_codepage.h" |
| 14 | |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 15 | namespace { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 16 | |
Dan Sinclair | 95bec80 | 2017-01-19 10:27:58 -0500 | [diff] [blame^] | 17 | bool IsSelectorStart(FX_WCHAR wch) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 18 | return wch == '.' || wch == '#' || wch == '*' || (wch >= 'a' && wch <= 'z') || |
| 19 | (wch >= 'A' && wch <= 'Z'); |
| 20 | } |
| 21 | |
Dan Sinclair | 95bec80 | 2017-01-19 10:27:58 -0500 | [diff] [blame^] | 22 | bool ParseCSSURI(const FX_WCHAR* pszValue, int32_t* iOffset, int32_t* iLength) { |
| 23 | ASSERT(pszValue && *iLength > 0); |
| 24 | if (*iLength < 6 || pszValue[*iLength - 1] != ')' || |
| 25 | FXSYS_wcsnicmp(L"url(", pszValue, 4)) { |
| 26 | return false; |
| 27 | } |
| 28 | if (CFDE_CSSDeclaration::ParseCSSString(pszValue + 4, *iLength - 5, iOffset, |
| 29 | iLength)) { |
| 30 | *iOffset += 4; |
| 31 | return true; |
| 32 | } |
| 33 | return false; |
| 34 | } |
| 35 | |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 36 | } // namespace |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 37 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 38 | CFDE_CSSSyntaxParser::CFDE_CSSSyntaxParser() |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame] | 39 | : m_iTextDatLen(0), |
tsepez | 736f28a | 2016-03-25 14:19:51 -0700 | [diff] [blame] | 40 | m_dwCheck((uint32_t)-1), |
Dan Sinclair | be59106 | 2017-01-18 08:58:25 -0500 | [diff] [blame] | 41 | m_eMode(FDE_CSSSyntaxMode::RuleSet), |
| 42 | m_eStatus(FDE_CSSSyntaxStatus::None) {} |
dsinclair | 3496545 | 2016-07-18 13:14:49 -0700 | [diff] [blame] | 43 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 44 | CFDE_CSSSyntaxParser::~CFDE_CSSSyntaxParser() { |
| 45 | m_TextData.Reset(); |
| 46 | m_TextPlane.Reset(); |
| 47 | } |
dsinclair | 3496545 | 2016-07-18 13:14:49 -0700 | [diff] [blame] | 48 | |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 49 | bool CFDE_CSSSyntaxParser::Init(const FX_WCHAR* pBuffer, |
| 50 | int32_t iBufferSize, |
| 51 | int32_t iTextDatSize, |
| 52 | bool bOnlyDeclaration) { |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 53 | ASSERT(pBuffer && iBufferSize > 0 && iTextDatSize > 0); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 54 | Reset(bOnlyDeclaration); |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame] | 55 | if (!m_TextData.EstimateSize(iTextDatSize)) |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 56 | return false; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 57 | return m_TextPlane.AttachBuffer(pBuffer, iBufferSize); |
| 58 | } |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame] | 59 | |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 60 | void CFDE_CSSSyntaxParser::Reset(bool bOnlyDeclaration) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 61 | m_TextPlane.Reset(); |
| 62 | m_TextData.Reset(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 63 | m_iTextDatLen = 0; |
tsepez | 736f28a | 2016-03-25 14:19:51 -0700 | [diff] [blame] | 64 | m_dwCheck = (uint32_t)-1; |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 65 | m_eStatus = FDE_CSSSyntaxStatus::None; |
Dan Sinclair | be59106 | 2017-01-18 08:58:25 -0500 | [diff] [blame] | 66 | m_eMode = bOnlyDeclaration ? FDE_CSSSyntaxMode::PropertyName |
| 67 | : FDE_CSSSyntaxMode::RuleSet; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 68 | } |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame] | 69 | |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 70 | FDE_CSSSyntaxStatus CFDE_CSSSyntaxParser::DoSyntaxParse() { |
| 71 | while (m_eStatus >= FDE_CSSSyntaxStatus::None) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 72 | if (m_TextPlane.IsEOF()) { |
Dan Sinclair | be59106 | 2017-01-18 08:58:25 -0500 | [diff] [blame] | 73 | if (m_eMode == FDE_CSSSyntaxMode::PropertyValue && |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame] | 74 | m_TextData.GetLength() > 0) { |
| 75 | SaveTextData(); |
| 76 | m_eStatus = FDE_CSSSyntaxStatus::PropertyValue; |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 77 | return m_eStatus; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 78 | } |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame] | 79 | m_eStatus = FDE_CSSSyntaxStatus::EOS; |
| 80 | return m_eStatus; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 81 | } |
| 82 | FX_WCHAR wch; |
| 83 | while (!m_TextPlane.IsEOF()) { |
| 84 | wch = m_TextPlane.GetChar(); |
| 85 | switch (m_eMode) { |
Dan Sinclair | be59106 | 2017-01-18 08:58:25 -0500 | [diff] [blame] | 86 | case FDE_CSSSyntaxMode::RuleSet: |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 87 | switch (wch) { |
| 88 | case '@': |
| 89 | m_TextPlane.MoveNext(); |
Dan Sinclair | be59106 | 2017-01-18 08:58:25 -0500 | [diff] [blame] | 90 | SwitchMode(FDE_CSSSyntaxMode::AtRule); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 91 | break; |
| 92 | case '}': |
| 93 | m_TextPlane.MoveNext(); |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame] | 94 | if (RestoreMode()) |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 95 | return FDE_CSSSyntaxStatus::DeclClose; |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame] | 96 | |
| 97 | m_eStatus = FDE_CSSSyntaxStatus::Error; |
| 98 | return m_eStatus; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 99 | case '/': |
| 100 | if (m_TextPlane.GetNextChar() == '*') { |
Dan Sinclair | be59106 | 2017-01-18 08:58:25 -0500 | [diff] [blame] | 101 | m_ModeStack.push(m_eMode); |
| 102 | SwitchMode(FDE_CSSSyntaxMode::Comment); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 103 | break; |
| 104 | } |
| 105 | default: |
| 106 | if (wch <= ' ') { |
| 107 | m_TextPlane.MoveNext(); |
Dan Sinclair | 95bec80 | 2017-01-19 10:27:58 -0500 | [diff] [blame^] | 108 | } else if (IsSelectorStart(wch)) { |
Dan Sinclair | be59106 | 2017-01-18 08:58:25 -0500 | [diff] [blame] | 109 | SwitchMode(FDE_CSSSyntaxMode::Selector); |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 110 | return FDE_CSSSyntaxStatus::StyleRule; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 111 | } else { |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 112 | m_eStatus = FDE_CSSSyntaxStatus::Error; |
| 113 | return m_eStatus; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 114 | } |
| 115 | break; |
| 116 | } |
| 117 | break; |
Dan Sinclair | be59106 | 2017-01-18 08:58:25 -0500 | [diff] [blame] | 118 | case FDE_CSSSyntaxMode::Selector: |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 119 | switch (wch) { |
| 120 | case ',': |
| 121 | m_TextPlane.MoveNext(); |
Dan Sinclair | be59106 | 2017-01-18 08:58:25 -0500 | [diff] [blame] | 122 | SwitchMode(FDE_CSSSyntaxMode::Selector); |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame] | 123 | if (m_iTextDatLen > 0) |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 124 | return FDE_CSSSyntaxStatus::Selector; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 125 | break; |
| 126 | case '{': |
| 127 | if (m_TextData.GetLength() > 0) { |
| 128 | SaveTextData(); |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 129 | return FDE_CSSSyntaxStatus::Selector; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 130 | } |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame] | 131 | m_TextPlane.MoveNext(); |
Dan Sinclair | be59106 | 2017-01-18 08:58:25 -0500 | [diff] [blame] | 132 | m_ModeStack.push(FDE_CSSSyntaxMode::RuleSet); |
| 133 | SwitchMode(FDE_CSSSyntaxMode::PropertyName); |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame] | 134 | return FDE_CSSSyntaxStatus::DeclOpen; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 135 | case '/': |
| 136 | if (m_TextPlane.GetNextChar() == '*') { |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame] | 137 | if (SwitchToComment() > 0) |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 138 | return FDE_CSSSyntaxStatus::Selector; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 139 | break; |
| 140 | } |
| 141 | default: |
| 142 | AppendChar(wch); |
| 143 | break; |
| 144 | } |
| 145 | break; |
Dan Sinclair | be59106 | 2017-01-18 08:58:25 -0500 | [diff] [blame] | 146 | case FDE_CSSSyntaxMode::PropertyName: |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 147 | switch (wch) { |
| 148 | case ':': |
| 149 | m_TextPlane.MoveNext(); |
Dan Sinclair | be59106 | 2017-01-18 08:58:25 -0500 | [diff] [blame] | 150 | SwitchMode(FDE_CSSSyntaxMode::PropertyValue); |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 151 | return FDE_CSSSyntaxStatus::PropertyName; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 152 | case '}': |
| 153 | m_TextPlane.MoveNext(); |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame] | 154 | if (RestoreMode()) |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 155 | return FDE_CSSSyntaxStatus::DeclClose; |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame] | 156 | |
| 157 | m_eStatus = FDE_CSSSyntaxStatus::Error; |
| 158 | return m_eStatus; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 159 | case '/': |
| 160 | if (m_TextPlane.GetNextChar() == '*') { |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame] | 161 | if (SwitchToComment() > 0) |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 162 | return FDE_CSSSyntaxStatus::PropertyName; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 163 | break; |
| 164 | } |
| 165 | default: |
| 166 | AppendChar(wch); |
| 167 | break; |
| 168 | } |
| 169 | break; |
Dan Sinclair | be59106 | 2017-01-18 08:58:25 -0500 | [diff] [blame] | 170 | case FDE_CSSSyntaxMode::PropertyValue: |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 171 | switch (wch) { |
| 172 | case ';': |
| 173 | m_TextPlane.MoveNext(); |
| 174 | case '}': |
Dan Sinclair | be59106 | 2017-01-18 08:58:25 -0500 | [diff] [blame] | 175 | SwitchMode(FDE_CSSSyntaxMode::PropertyName); |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 176 | return FDE_CSSSyntaxStatus::PropertyValue; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 177 | case '/': |
| 178 | if (m_TextPlane.GetNextChar() == '*') { |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame] | 179 | if (SwitchToComment() > 0) |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 180 | return FDE_CSSSyntaxStatus::PropertyValue; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 181 | break; |
| 182 | } |
| 183 | default: |
| 184 | AppendChar(wch); |
| 185 | break; |
| 186 | } |
| 187 | break; |
Dan Sinclair | be59106 | 2017-01-18 08:58:25 -0500 | [diff] [blame] | 188 | case FDE_CSSSyntaxMode::Comment: |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 189 | if (wch == '/' && m_TextData.GetLength() > 0 && |
| 190 | m_TextData.GetAt(m_TextData.GetLength() - 1) == '*') { |
| 191 | RestoreMode(); |
| 192 | } else { |
| 193 | m_TextData.AppendChar(wch); |
| 194 | } |
| 195 | m_TextPlane.MoveNext(); |
| 196 | break; |
Dan Sinclair | be59106 | 2017-01-18 08:58:25 -0500 | [diff] [blame] | 197 | case FDE_CSSSyntaxMode::MediaType: |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 198 | switch (wch) { |
| 199 | case ',': |
| 200 | m_TextPlane.MoveNext(); |
Dan Sinclair | be59106 | 2017-01-18 08:58:25 -0500 | [diff] [blame] | 201 | SwitchMode(FDE_CSSSyntaxMode::MediaType); |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame] | 202 | if (m_iTextDatLen > 0) |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 203 | return FDE_CSSSyntaxStatus::MediaType; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 204 | break; |
| 205 | case '{': { |
Dan Sinclair | be59106 | 2017-01-18 08:58:25 -0500 | [diff] [blame] | 206 | if (m_ModeStack.empty() || |
| 207 | m_ModeStack.top() != FDE_CSSSyntaxMode::MediaRule) { |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 208 | m_eStatus = FDE_CSSSyntaxStatus::Error; |
| 209 | return m_eStatus; |
| 210 | } |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 211 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 212 | if (m_TextData.GetLength() > 0) { |
| 213 | SaveTextData(); |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 214 | return FDE_CSSSyntaxStatus::MediaType; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 215 | } |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame] | 216 | m_TextPlane.MoveNext(); |
Dan Sinclair | be59106 | 2017-01-18 08:58:25 -0500 | [diff] [blame] | 217 | |
| 218 | // Replace the MediaRule with a RuleSet rule. |
| 219 | m_ModeStack.top() = FDE_CSSSyntaxMode::RuleSet; |
| 220 | |
| 221 | SwitchMode(FDE_CSSSyntaxMode::RuleSet); |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame] | 222 | return FDE_CSSSyntaxStatus::DeclOpen; |
| 223 | } |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 224 | case ';': { |
Dan Sinclair | be59106 | 2017-01-18 08:58:25 -0500 | [diff] [blame] | 225 | if (m_ModeStack.empty() || |
| 226 | m_ModeStack.top() != FDE_CSSSyntaxMode::Import) { |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 227 | m_eStatus = FDE_CSSSyntaxStatus::Error; |
| 228 | return m_eStatus; |
| 229 | } |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 230 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 231 | if (m_TextData.GetLength() > 0) { |
| 232 | SaveTextData(); |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame] | 233 | if (IsImportEnabled()) |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 234 | return FDE_CSSSyntaxStatus::MediaType; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 235 | } else { |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 236 | bool bEnabled = IsImportEnabled(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 237 | m_TextPlane.MoveNext(); |
Dan Sinclair | be59106 | 2017-01-18 08:58:25 -0500 | [diff] [blame] | 238 | m_ModeStack.pop(); |
| 239 | SwitchMode(FDE_CSSSyntaxMode::RuleSet); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 240 | if (bEnabled) { |
| 241 | DisableImport(); |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 242 | return FDE_CSSSyntaxStatus::ImportClose; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | } break; |
| 246 | case '/': |
| 247 | if (m_TextPlane.GetNextChar() == '*') { |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame] | 248 | if (SwitchToComment() > 0) |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 249 | return FDE_CSSSyntaxStatus::MediaType; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 250 | break; |
| 251 | } |
| 252 | default: |
| 253 | AppendChar(wch); |
| 254 | break; |
| 255 | } |
| 256 | break; |
Dan Sinclair | be59106 | 2017-01-18 08:58:25 -0500 | [diff] [blame] | 257 | case FDE_CSSSyntaxMode::URI: { |
| 258 | if (m_ModeStack.empty() || |
| 259 | m_ModeStack.top() != FDE_CSSSyntaxMode::Import) { |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 260 | m_eStatus = FDE_CSSSyntaxStatus::Error; |
| 261 | return m_eStatus; |
| 262 | } |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 263 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 264 | if (wch <= ' ' || wch == ';') { |
| 265 | int32_t iURIStart, iURILength = m_TextData.GetLength(); |
Dan Sinclair | 95bec80 | 2017-01-19 10:27:58 -0500 | [diff] [blame^] | 266 | if (iURILength > 0 && |
| 267 | ParseCSSURI(m_TextData.GetBuffer(), &iURIStart, &iURILength)) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 268 | m_TextData.Subtract(iURIStart, iURILength); |
Dan Sinclair | be59106 | 2017-01-18 08:58:25 -0500 | [diff] [blame] | 269 | SwitchMode(FDE_CSSSyntaxMode::MediaType); |
npm | 2399152 | 2016-11-28 12:49:29 -0800 | [diff] [blame] | 270 | if (IsImportEnabled()) |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 271 | return FDE_CSSSyntaxStatus::URI; |
npm | 2399152 | 2016-11-28 12:49:29 -0800 | [diff] [blame] | 272 | break; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | AppendChar(wch); |
| 276 | } break; |
Dan Sinclair | be59106 | 2017-01-18 08:58:25 -0500 | [diff] [blame] | 277 | case FDE_CSSSyntaxMode::AtRule: |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 278 | if (wch > ' ') { |
| 279 | AppendChar(wch); |
| 280 | } else { |
| 281 | int32_t iLen = m_TextData.GetLength(); |
| 282 | const FX_WCHAR* psz = m_TextData.GetBuffer(); |
| 283 | if (FXSYS_wcsncmp(L"charset", psz, iLen) == 0) { |
Dan Sinclair | be59106 | 2017-01-18 08:58:25 -0500 | [diff] [blame] | 284 | SwitchMode(FDE_CSSSyntaxMode::Charset); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 285 | } else if (FXSYS_wcsncmp(L"import", psz, iLen) == 0) { |
Dan Sinclair | be59106 | 2017-01-18 08:58:25 -0500 | [diff] [blame] | 286 | m_ModeStack.push(FDE_CSSSyntaxMode::Import); |
| 287 | SwitchMode(FDE_CSSSyntaxMode::URI); |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame] | 288 | if (IsImportEnabled()) |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 289 | return FDE_CSSSyntaxStatus::ImportRule; |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame] | 290 | break; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 291 | } else if (FXSYS_wcsncmp(L"media", psz, iLen) == 0) { |
Dan Sinclair | be59106 | 2017-01-18 08:58:25 -0500 | [diff] [blame] | 292 | m_ModeStack.push(FDE_CSSSyntaxMode::MediaRule); |
| 293 | SwitchMode(FDE_CSSSyntaxMode::MediaType); |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 294 | return FDE_CSSSyntaxStatus::MediaRule; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 295 | } else if (FXSYS_wcsncmp(L"font-face", psz, iLen) == 0) { |
Dan Sinclair | be59106 | 2017-01-18 08:58:25 -0500 | [diff] [blame] | 296 | SwitchMode(FDE_CSSSyntaxMode::Selector); |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 297 | return FDE_CSSSyntaxStatus::FontFaceRule; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 298 | } else if (FXSYS_wcsncmp(L"page", psz, iLen) == 0) { |
Dan Sinclair | be59106 | 2017-01-18 08:58:25 -0500 | [diff] [blame] | 299 | SwitchMode(FDE_CSSSyntaxMode::Selector); |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 300 | return FDE_CSSSyntaxStatus::PageRule; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 301 | } else { |
Dan Sinclair | be59106 | 2017-01-18 08:58:25 -0500 | [diff] [blame] | 302 | SwitchMode(FDE_CSSSyntaxMode::UnknownRule); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | break; |
Dan Sinclair | be59106 | 2017-01-18 08:58:25 -0500 | [diff] [blame] | 306 | case FDE_CSSSyntaxMode::Charset: |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 307 | if (wch == ';') { |
| 308 | m_TextPlane.MoveNext(); |
Dan Sinclair | be59106 | 2017-01-18 08:58:25 -0500 | [diff] [blame] | 309 | SwitchMode(FDE_CSSSyntaxMode::RuleSet); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 310 | if (IsCharsetEnabled()) { |
| 311 | DisableCharset(); |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame] | 312 | if (m_iTextDatLen > 0) |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 313 | return FDE_CSSSyntaxStatus::Charset; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 314 | } |
| 315 | } else { |
| 316 | AppendChar(wch); |
| 317 | } |
| 318 | break; |
Dan Sinclair | be59106 | 2017-01-18 08:58:25 -0500 | [diff] [blame] | 319 | case FDE_CSSSyntaxMode::UnknownRule: |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame] | 320 | if (wch == ';') |
Dan Sinclair | be59106 | 2017-01-18 08:58:25 -0500 | [diff] [blame] | 321 | SwitchMode(FDE_CSSSyntaxMode::RuleSet); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 322 | m_TextPlane.MoveNext(); |
| 323 | break; |
| 324 | default: |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 325 | ASSERT(false); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 326 | break; |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | return m_eStatus; |
| 331 | } |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame] | 332 | |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 333 | bool CFDE_CSSSyntaxParser::IsImportEnabled() const { |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame] | 334 | if ((m_dwCheck & FDE_CSSSYNTAXCHECK_AllowImport) == 0) |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 335 | return false; |
Dan Sinclair | be59106 | 2017-01-18 08:58:25 -0500 | [diff] [blame] | 336 | if (m_ModeStack.size() > 1) |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 337 | return false; |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 338 | return true; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 339 | } |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame] | 340 | |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 341 | bool CFDE_CSSSyntaxParser::AppendChar(FX_WCHAR wch) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 342 | m_TextPlane.MoveNext(); |
| 343 | if (m_TextData.GetLength() > 0 || wch > ' ') { |
| 344 | m_TextData.AppendChar(wch); |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 345 | return true; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 346 | } |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 347 | return false; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 348 | } |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame] | 349 | |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 350 | int32_t CFDE_CSSSyntaxParser::SaveTextData() { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 351 | m_iTextDatLen = m_TextData.TrimEnd(); |
| 352 | m_TextData.Clear(); |
| 353 | return m_iTextDatLen; |
| 354 | } |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame] | 355 | |
Dan Sinclair | be59106 | 2017-01-18 08:58:25 -0500 | [diff] [blame] | 356 | void CFDE_CSSSyntaxParser::SwitchMode(FDE_CSSSyntaxMode eMode) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 357 | m_eMode = eMode; |
| 358 | SaveTextData(); |
| 359 | } |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame] | 360 | |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 361 | int32_t CFDE_CSSSyntaxParser::SwitchToComment() { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 362 | int32_t iLength = m_TextData.GetLength(); |
Dan Sinclair | be59106 | 2017-01-18 08:58:25 -0500 | [diff] [blame] | 363 | m_ModeStack.push(m_eMode); |
| 364 | SwitchMode(FDE_CSSSyntaxMode::Comment); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 365 | return iLength; |
| 366 | } |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame] | 367 | |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 368 | bool CFDE_CSSSyntaxParser::RestoreMode() { |
Dan Sinclair | be59106 | 2017-01-18 08:58:25 -0500 | [diff] [blame] | 369 | if (m_ModeStack.empty()) |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 370 | return false; |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 371 | |
Dan Sinclair | be59106 | 2017-01-18 08:58:25 -0500 | [diff] [blame] | 372 | SwitchMode(m_ModeStack.top()); |
| 373 | m_ModeStack.pop(); |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 374 | return true; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 375 | } |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame] | 376 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 377 | const FX_WCHAR* CFDE_CSSSyntaxParser::GetCurrentString(int32_t& iLength) const { |
| 378 | iLength = m_iTextDatLen; |
| 379 | return m_TextData.GetBuffer(); |
| 380 | } |