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 | |
| 7 | #include "xfa/fde/css/fde_csssyntax.h" |
| 8 | |
npm | 2399152 | 2016-11-28 12:49:29 -0800 | [diff] [blame] | 9 | #include <algorithm> |
| 10 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 11 | #include "xfa/fde/css/fde_cssdatatable.h" |
| 12 | #include "xfa/fgas/crt/fgas_codepage.h" |
| 13 | |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 14 | namespace { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 15 | |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 16 | bool FDE_IsSelectorStart(FX_WCHAR wch) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 17 | return wch == '.' || wch == '#' || wch == '*' || (wch >= 'a' && wch <= 'z') || |
| 18 | (wch >= 'A' && wch <= 'Z'); |
| 19 | } |
| 20 | |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 21 | } // namespace |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 22 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 23 | CFDE_CSSSyntaxParser::CFDE_CSSSyntaxParser() |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 24 | : m_iTextDatLen(0), |
tsepez | 736f28a | 2016-03-25 14:19:51 -0700 | [diff] [blame] | 25 | m_dwCheck((uint32_t)-1), |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 26 | m_eMode(FDE_CSSSYNTAXMODE_RuleSet), |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 27 | m_eStatus(FDE_CSSSyntaxStatus::None), |
dsinclair | 3496545 | 2016-07-18 13:14:49 -0700 | [diff] [blame] | 28 | m_ModeStack(100) {} |
| 29 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 30 | CFDE_CSSSyntaxParser::~CFDE_CSSSyntaxParser() { |
| 31 | m_TextData.Reset(); |
| 32 | m_TextPlane.Reset(); |
| 33 | } |
dsinclair | 3496545 | 2016-07-18 13:14:49 -0700 | [diff] [blame] | 34 | |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 35 | bool CFDE_CSSSyntaxParser::Init(const FX_WCHAR* pBuffer, |
| 36 | int32_t iBufferSize, |
| 37 | int32_t iTextDatSize, |
| 38 | bool bOnlyDeclaration) { |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 39 | ASSERT(pBuffer && iBufferSize > 0 && iTextDatSize > 0); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 40 | Reset(bOnlyDeclaration); |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 41 | if (!m_TextData.EstimateSize(iTextDatSize)) |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 42 | return false; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 43 | return m_TextPlane.AttachBuffer(pBuffer, iBufferSize); |
| 44 | } |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 45 | |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 46 | void CFDE_CSSSyntaxParser::Reset(bool bOnlyDeclaration) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 47 | m_TextPlane.Reset(); |
| 48 | m_TextData.Reset(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 49 | m_iTextDatLen = 0; |
tsepez | 736f28a | 2016-03-25 14:19:51 -0700 | [diff] [blame] | 50 | m_dwCheck = (uint32_t)-1; |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 51 | m_eStatus = FDE_CSSSyntaxStatus::None; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 52 | m_eMode = bOnlyDeclaration ? FDE_CSSSYNTAXMODE_PropertyName |
| 53 | : FDE_CSSSYNTAXMODE_RuleSet; |
| 54 | } |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 55 | |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 56 | FDE_CSSSyntaxStatus CFDE_CSSSyntaxParser::DoSyntaxParse() { |
| 57 | while (m_eStatus >= FDE_CSSSyntaxStatus::None) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 58 | if (m_TextPlane.IsEOF()) { |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 59 | if (m_eMode == FDE_CSSSYNTAXMODE_PropertyValue && |
| 60 | m_TextData.GetLength() > 0) { |
| 61 | SaveTextData(); |
| 62 | m_eStatus = FDE_CSSSyntaxStatus::PropertyValue; |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 63 | return m_eStatus; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 64 | } |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 65 | m_eStatus = FDE_CSSSyntaxStatus::EOS; |
| 66 | return m_eStatus; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 67 | } |
| 68 | FX_WCHAR wch; |
| 69 | while (!m_TextPlane.IsEOF()) { |
| 70 | wch = m_TextPlane.GetChar(); |
| 71 | switch (m_eMode) { |
| 72 | case FDE_CSSSYNTAXMODE_RuleSet: |
| 73 | switch (wch) { |
| 74 | case '@': |
| 75 | m_TextPlane.MoveNext(); |
| 76 | SwitchMode(FDE_CSSSYNTAXMODE_AtRule); |
| 77 | break; |
| 78 | case '}': |
| 79 | m_TextPlane.MoveNext(); |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 80 | if (RestoreMode()) |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 81 | return FDE_CSSSyntaxStatus::DeclClose; |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 82 | |
| 83 | m_eStatus = FDE_CSSSyntaxStatus::Error; |
| 84 | return m_eStatus; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 85 | case '/': |
| 86 | if (m_TextPlane.GetNextChar() == '*') { |
| 87 | m_ModeStack.Push(m_eMode); |
| 88 | SwitchMode(FDE_CSSSYNTAXMODE_Comment); |
| 89 | break; |
| 90 | } |
| 91 | default: |
| 92 | if (wch <= ' ') { |
| 93 | m_TextPlane.MoveNext(); |
| 94 | } else if (FDE_IsSelectorStart(wch)) { |
| 95 | SwitchMode(FDE_CSSSYNTAXMODE_Selector); |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 96 | return FDE_CSSSyntaxStatus::StyleRule; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 97 | } else { |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 98 | m_eStatus = FDE_CSSSyntaxStatus::Error; |
| 99 | return m_eStatus; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 100 | } |
| 101 | break; |
| 102 | } |
| 103 | break; |
| 104 | case FDE_CSSSYNTAXMODE_Selector: |
| 105 | switch (wch) { |
| 106 | case ',': |
| 107 | m_TextPlane.MoveNext(); |
| 108 | SwitchMode(FDE_CSSSYNTAXMODE_Selector); |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 109 | if (m_iTextDatLen > 0) |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 110 | return FDE_CSSSyntaxStatus::Selector; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 111 | break; |
| 112 | case '{': |
| 113 | if (m_TextData.GetLength() > 0) { |
| 114 | SaveTextData(); |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 115 | return FDE_CSSSyntaxStatus::Selector; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 116 | } |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 117 | m_TextPlane.MoveNext(); |
| 118 | m_ModeStack.Push(FDE_CSSSYNTAXMODE_RuleSet); |
| 119 | SwitchMode(FDE_CSSSYNTAXMODE_PropertyName); |
| 120 | return FDE_CSSSyntaxStatus::DeclOpen; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 121 | case '/': |
| 122 | if (m_TextPlane.GetNextChar() == '*') { |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 123 | if (SwitchToComment() > 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 | } |
| 127 | default: |
| 128 | AppendChar(wch); |
| 129 | break; |
| 130 | } |
| 131 | break; |
| 132 | case FDE_CSSSYNTAXMODE_PropertyName: |
| 133 | switch (wch) { |
| 134 | case ':': |
| 135 | m_TextPlane.MoveNext(); |
| 136 | SwitchMode(FDE_CSSSYNTAXMODE_PropertyValue); |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 137 | return FDE_CSSSyntaxStatus::PropertyName; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 138 | case '}': |
| 139 | m_TextPlane.MoveNext(); |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 140 | if (RestoreMode()) |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 141 | return FDE_CSSSyntaxStatus::DeclClose; |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 142 | |
| 143 | m_eStatus = FDE_CSSSyntaxStatus::Error; |
| 144 | return m_eStatus; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 145 | case '/': |
| 146 | if (m_TextPlane.GetNextChar() == '*') { |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 147 | if (SwitchToComment() > 0) |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 148 | return FDE_CSSSyntaxStatus::PropertyName; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 149 | break; |
| 150 | } |
| 151 | default: |
| 152 | AppendChar(wch); |
| 153 | break; |
| 154 | } |
| 155 | break; |
| 156 | case FDE_CSSSYNTAXMODE_PropertyValue: |
| 157 | switch (wch) { |
| 158 | case ';': |
| 159 | m_TextPlane.MoveNext(); |
| 160 | case '}': |
| 161 | SwitchMode(FDE_CSSSYNTAXMODE_PropertyName); |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 162 | return FDE_CSSSyntaxStatus::PropertyValue; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 163 | case '/': |
| 164 | if (m_TextPlane.GetNextChar() == '*') { |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 165 | if (SwitchToComment() > 0) |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 166 | return FDE_CSSSyntaxStatus::PropertyValue; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 167 | break; |
| 168 | } |
| 169 | default: |
| 170 | AppendChar(wch); |
| 171 | break; |
| 172 | } |
| 173 | break; |
| 174 | case FDE_CSSSYNTAXMODE_Comment: |
| 175 | if (wch == '/' && m_TextData.GetLength() > 0 && |
| 176 | m_TextData.GetAt(m_TextData.GetLength() - 1) == '*') { |
| 177 | RestoreMode(); |
| 178 | } else { |
| 179 | m_TextData.AppendChar(wch); |
| 180 | } |
| 181 | m_TextPlane.MoveNext(); |
| 182 | break; |
| 183 | case FDE_CSSSYNTAXMODE_MediaType: |
| 184 | switch (wch) { |
| 185 | case ',': |
| 186 | m_TextPlane.MoveNext(); |
| 187 | SwitchMode(FDE_CSSSYNTAXMODE_MediaType); |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 188 | if (m_iTextDatLen > 0) |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 189 | return FDE_CSSSyntaxStatus::MediaType; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 190 | break; |
| 191 | case '{': { |
| 192 | FDE_CSSSYNTAXMODE* pMode = m_ModeStack.GetTopElement(); |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 193 | if (!pMode || *pMode != FDE_CSSSYNTAXMODE_MediaRule) { |
| 194 | m_eStatus = FDE_CSSSyntaxStatus::Error; |
| 195 | return m_eStatus; |
| 196 | } |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 197 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 198 | if (m_TextData.GetLength() > 0) { |
| 199 | SaveTextData(); |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 200 | return FDE_CSSSyntaxStatus::MediaType; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 201 | } |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 202 | m_TextPlane.MoveNext(); |
| 203 | *pMode = FDE_CSSSYNTAXMODE_RuleSet; |
| 204 | SwitchMode(FDE_CSSSYNTAXMODE_RuleSet); |
| 205 | return FDE_CSSSyntaxStatus::DeclOpen; |
| 206 | } |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 207 | case ';': { |
| 208 | FDE_CSSSYNTAXMODE* pMode = m_ModeStack.GetTopElement(); |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 209 | if (!pMode || *pMode != FDE_CSSSYNTAXMODE_Import) { |
| 210 | m_eStatus = FDE_CSSSyntaxStatus::Error; |
| 211 | return m_eStatus; |
| 212 | } |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 213 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 214 | if (m_TextData.GetLength() > 0) { |
| 215 | SaveTextData(); |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 216 | if (IsImportEnabled()) |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 217 | return FDE_CSSSyntaxStatus::MediaType; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 218 | } else { |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 219 | bool bEnabled = IsImportEnabled(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 220 | m_TextPlane.MoveNext(); |
| 221 | m_ModeStack.Pop(); |
| 222 | SwitchMode(FDE_CSSSYNTAXMODE_RuleSet); |
| 223 | if (bEnabled) { |
| 224 | DisableImport(); |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 225 | return FDE_CSSSyntaxStatus::ImportClose; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | } break; |
| 229 | case '/': |
| 230 | if (m_TextPlane.GetNextChar() == '*') { |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 231 | if (SwitchToComment() > 0) |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 232 | return FDE_CSSSyntaxStatus::MediaType; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 233 | break; |
| 234 | } |
| 235 | default: |
| 236 | AppendChar(wch); |
| 237 | break; |
| 238 | } |
| 239 | break; |
| 240 | case FDE_CSSSYNTAXMODE_URI: { |
| 241 | FDE_CSSSYNTAXMODE* pMode = m_ModeStack.GetTopElement(); |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 242 | if (!pMode || *pMode != FDE_CSSSYNTAXMODE_Import) { |
| 243 | m_eStatus = FDE_CSSSyntaxStatus::Error; |
| 244 | return m_eStatus; |
| 245 | } |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 246 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 247 | if (wch <= ' ' || wch == ';') { |
| 248 | int32_t iURIStart, iURILength = m_TextData.GetLength(); |
npm | 2399152 | 2016-11-28 12:49:29 -0800 | [diff] [blame] | 249 | if (iURILength > 0 && FDE_ParseCSSURI(m_TextData.GetBuffer(), |
| 250 | &iURIStart, &iURILength)) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 251 | m_TextData.Subtract(iURIStart, iURILength); |
| 252 | SwitchMode(FDE_CSSSYNTAXMODE_MediaType); |
npm | 2399152 | 2016-11-28 12:49:29 -0800 | [diff] [blame] | 253 | if (IsImportEnabled()) |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 254 | return FDE_CSSSyntaxStatus::URI; |
npm | 2399152 | 2016-11-28 12:49:29 -0800 | [diff] [blame] | 255 | break; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | AppendChar(wch); |
| 259 | } break; |
| 260 | case FDE_CSSSYNTAXMODE_AtRule: |
| 261 | if (wch > ' ') { |
| 262 | AppendChar(wch); |
| 263 | } else { |
| 264 | int32_t iLen = m_TextData.GetLength(); |
| 265 | const FX_WCHAR* psz = m_TextData.GetBuffer(); |
| 266 | if (FXSYS_wcsncmp(L"charset", psz, iLen) == 0) { |
| 267 | SwitchMode(FDE_CSSSYNTAXMODE_Charset); |
| 268 | } else if (FXSYS_wcsncmp(L"import", psz, iLen) == 0) { |
| 269 | m_ModeStack.Push(FDE_CSSSYNTAXMODE_Import); |
| 270 | SwitchMode(FDE_CSSSYNTAXMODE_URI); |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 271 | if (IsImportEnabled()) |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 272 | return FDE_CSSSyntaxStatus::ImportRule; |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 273 | break; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 274 | } else if (FXSYS_wcsncmp(L"media", psz, iLen) == 0) { |
| 275 | m_ModeStack.Push(FDE_CSSSYNTAXMODE_MediaRule); |
| 276 | SwitchMode(FDE_CSSSYNTAXMODE_MediaType); |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 277 | return FDE_CSSSyntaxStatus::MediaRule; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 278 | } else if (FXSYS_wcsncmp(L"font-face", psz, iLen) == 0) { |
| 279 | SwitchMode(FDE_CSSSYNTAXMODE_Selector); |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 280 | return FDE_CSSSyntaxStatus::FontFaceRule; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 281 | } else if (FXSYS_wcsncmp(L"page", psz, iLen) == 0) { |
| 282 | SwitchMode(FDE_CSSSYNTAXMODE_Selector); |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 283 | return FDE_CSSSyntaxStatus::PageRule; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 284 | } else { |
| 285 | SwitchMode(FDE_CSSSYNTAXMODE_UnknownRule); |
| 286 | } |
| 287 | } |
| 288 | break; |
| 289 | case FDE_CSSSYNTAXMODE_Charset: |
| 290 | if (wch == ';') { |
| 291 | m_TextPlane.MoveNext(); |
| 292 | SwitchMode(FDE_CSSSYNTAXMODE_RuleSet); |
| 293 | if (IsCharsetEnabled()) { |
| 294 | DisableCharset(); |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 295 | if (m_iTextDatLen > 0) |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 296 | return FDE_CSSSyntaxStatus::Charset; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 297 | } |
| 298 | } else { |
| 299 | AppendChar(wch); |
| 300 | } |
| 301 | break; |
| 302 | case FDE_CSSSYNTAXMODE_UnknownRule: |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 303 | if (wch == ';') |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 304 | SwitchMode(FDE_CSSSYNTAXMODE_RuleSet); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 305 | m_TextPlane.MoveNext(); |
| 306 | break; |
| 307 | default: |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 308 | ASSERT(false); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 309 | break; |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | return m_eStatus; |
| 314 | } |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 315 | |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 316 | bool CFDE_CSSSyntaxParser::IsImportEnabled() const { |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 317 | if ((m_dwCheck & FDE_CSSSYNTAXCHECK_AllowImport) == 0) |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 318 | return false; |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 319 | if (m_ModeStack.GetSize() > 1) |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 320 | return false; |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 321 | return true; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 322 | } |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 323 | |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 324 | bool CFDE_CSSSyntaxParser::AppendChar(FX_WCHAR wch) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 325 | m_TextPlane.MoveNext(); |
| 326 | if (m_TextData.GetLength() > 0 || wch > ' ') { |
| 327 | m_TextData.AppendChar(wch); |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 328 | return true; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 329 | } |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 330 | return false; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 331 | } |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 332 | |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 333 | int32_t CFDE_CSSSyntaxParser::SaveTextData() { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 334 | m_iTextDatLen = m_TextData.TrimEnd(); |
| 335 | m_TextData.Clear(); |
| 336 | return m_iTextDatLen; |
| 337 | } |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 338 | |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 339 | void CFDE_CSSSyntaxParser::SwitchMode(FDE_CSSSYNTAXMODE eMode) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 340 | m_eMode = eMode; |
| 341 | SaveTextData(); |
| 342 | } |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 343 | |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 344 | int32_t CFDE_CSSSyntaxParser::SwitchToComment() { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 345 | int32_t iLength = m_TextData.GetLength(); |
| 346 | m_ModeStack.Push(m_eMode); |
| 347 | SwitchMode(FDE_CSSSYNTAXMODE_Comment); |
| 348 | return iLength; |
| 349 | } |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 350 | |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 351 | bool CFDE_CSSSyntaxParser::RestoreMode() { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 352 | FDE_CSSSYNTAXMODE* pMode = m_ModeStack.GetTopElement(); |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 353 | if (!pMode) |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 354 | return false; |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 355 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 356 | SwitchMode(*pMode); |
| 357 | m_ModeStack.Pop(); |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 358 | return true; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 359 | } |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 360 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 361 | const FX_WCHAR* CFDE_CSSSyntaxParser::GetCurrentString(int32_t& iLength) const { |
| 362 | iLength = m_iTextDatLen; |
| 363 | return m_TextData.GetBuffer(); |
| 364 | } |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 365 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 366 | CFDE_CSSTextBuf::CFDE_CSSTextBuf() |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 367 | : m_bExtBuf(false), |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 368 | m_pBuffer(nullptr), |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 369 | m_iBufLen(0), |
| 370 | m_iDatLen(0), |
| 371 | m_iDatPos(0) {} |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 372 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 373 | CFDE_CSSTextBuf::~CFDE_CSSTextBuf() { |
| 374 | Reset(); |
| 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 | void CFDE_CSSTextBuf::Reset() { |
| 378 | if (!m_bExtBuf) { |
| 379 | FX_Free(m_pBuffer); |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 380 | m_pBuffer = nullptr; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 381 | } |
| 382 | m_iDatPos = m_iDatLen = m_iBufLen; |
| 383 | } |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 384 | |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 385 | bool CFDE_CSSTextBuf::AttachBuffer(const FX_WCHAR* pBuffer, int32_t iBufLen) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 386 | Reset(); |
brucedawson | 2f109ab | 2016-05-27 16:13:13 -0700 | [diff] [blame] | 387 | m_pBuffer = const_cast<FX_WCHAR*>(pBuffer); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 388 | m_iDatLen = m_iBufLen = iBufLen; |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 389 | return m_bExtBuf = true; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 390 | } |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 391 | |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 392 | bool CFDE_CSSTextBuf::EstimateSize(int32_t iAllocSize) { |
dsinclair | 43854a5 | 2016-04-27 12:26:00 -0700 | [diff] [blame] | 393 | ASSERT(iAllocSize > 0); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 394 | Clear(); |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 395 | m_bExtBuf = false; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 396 | return ExpandBuf(iAllocSize); |
| 397 | } |
tsepez | 7cda31a | 2016-12-07 12:10:20 -0800 | [diff] [blame] | 398 | |
| 399 | int32_t CFDE_CSSTextBuf::LoadFromStream( |
| 400 | const CFX_RetainPtr<IFGAS_Stream>& pTxtStream, |
| 401 | int32_t iStreamOffset, |
| 402 | int32_t iMaxChars, |
| 403 | bool& bEOS) { |
dsinclair | 43854a5 | 2016-04-27 12:26:00 -0700 | [diff] [blame] | 404 | ASSERT(iStreamOffset >= 0 && iMaxChars > 0); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 405 | Clear(); |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 406 | m_bExtBuf = false; |
tsepez | 7cda31a | 2016-12-07 12:10:20 -0800 | [diff] [blame] | 407 | if (!ExpandBuf(iMaxChars)) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 408 | return 0; |
tsepez | 7cda31a | 2016-12-07 12:10:20 -0800 | [diff] [blame] | 409 | |
| 410 | if (pTxtStream->GetPosition() != iStreamOffset) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 411 | pTxtStream->Seek(FX_STREAMSEEK_Begin, iStreamOffset); |
tsepez | 7cda31a | 2016-12-07 12:10:20 -0800 | [diff] [blame] | 412 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 413 | m_iDatLen = pTxtStream->ReadString(m_pBuffer, iMaxChars, bEOS); |
| 414 | return m_iDatLen; |
| 415 | } |
tsepez | 7cda31a | 2016-12-07 12:10:20 -0800 | [diff] [blame] | 416 | |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 417 | bool CFDE_CSSTextBuf::ExpandBuf(int32_t iDesiredSize) { |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 418 | if (m_bExtBuf) |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 419 | return false; |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 420 | if (!m_pBuffer) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 421 | m_pBuffer = FX_Alloc(FX_WCHAR, iDesiredSize); |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 422 | else if (m_iBufLen != iDesiredSize) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 423 | m_pBuffer = FX_Realloc(FX_WCHAR, m_pBuffer, iDesiredSize); |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 424 | else |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 425 | return true; |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 426 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 427 | if (!m_pBuffer) { |
| 428 | m_iBufLen = 0; |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 429 | return false; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 430 | } |
| 431 | m_iBufLen = iDesiredSize; |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 432 | return true; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 433 | } |
npm | 2399152 | 2016-11-28 12:49:29 -0800 | [diff] [blame] | 434 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 435 | void CFDE_CSSTextBuf::Subtract(int32_t iStart, int32_t iLength) { |
npm | 2399152 | 2016-11-28 12:49:29 -0800 | [diff] [blame] | 436 | ASSERT(iStart >= 0 && iLength >= 0); |
Dan Sinclair | 9dbc3c4 | 2017-01-18 08:58:00 -0500 | [diff] [blame^] | 437 | |
npm | 2399152 | 2016-11-28 12:49:29 -0800 | [diff] [blame] | 438 | iLength = std::max(std::min(iLength, m_iDatLen - iStart), 0); |
| 439 | FXSYS_memmove(m_pBuffer, m_pBuffer + iStart, iLength * sizeof(FX_WCHAR)); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 440 | m_iDatLen = iLength; |
| 441 | } |