OSDN Git Service

CrystalEdit: Prevent comment blocks (<!-- -->) inside <style>...</style> and <script...
[winmerge-jp/winmerge-jp.git] / Externals / crystaledit / editlib / parsers / html.cpp
1 ///////////////////////////////////////////////////////////////////////////
2 //  File:    html.cpp
3 //  Version: 1.1.0.4
4 //  Updated: 19-Jul-1998
5 //
6 //  Copyright:  Ferdinand Prantl, portions by Stcherbatchenko Andrei
7 //  E-mail:     prantl@ff.cuni.cz
8 //
9 //  HTML syntax highlighing definition
10 //
11 //  You are free to use or modify this code to the following restrictions:
12 //  - Acknowledge me somewhere in your about box, simple "Parts of code by.."
13 //  will be enough. If you can't (or don't want to), contact me personally.
14 //  - LEAVE THIS HEADER INTACT
15 ////////////////////////////////////////////////////////////////////////////
16
17 #include "StdAfx.h"
18 #include "crystallineparser.h"
19 #include "../SyntaxColors.h"
20 #include "../utils/string_util.h"
21
22 #ifdef _DEBUG
23 #define new DEBUG_NEW
24 #endif
25
26 unsigned
27 CrystalLineParser::ParseLineHtmlEx (unsigned dwCookie, const TCHAR *pszChars, int nLength, TEXTBLOCK * pBuf, int &nActualItems, int nEmbeddedLanguage)
28 {
29   if (nLength == 0)
30     return dwCookie & (COOKIE_EXT_COMMENT|COOKIE_EXT_USER1|COOKIE_ELEMENT|COOKIE_BLOCK_SCRIPT|COOKIE_BLOCK_STYLE);
31
32   bool bRedefineBlock = true;
33   if (!(dwCookie & COOKIE_ELEMENT))
34     bRedefineBlock = !(dwCookie & (COOKIE_EXT_USER1|COOKIE_BLOCK_SCRIPT|COOKIE_BLOCK_STYLE));
35   bool bDecIndex = false;
36   int nIdentBegin = -1;
37   int nPrevI = -1;
38   int I=0;
39   for (I = 0;; nPrevI = I, I = static_cast<int>(::CharNext(pszChars+I) - pszChars))
40     {
41       if (I == nPrevI)
42         {
43           // CharNext did not advance, so we're at the end of the string
44           // and we already handled this character, so stop
45           break;
46         }
47
48       if (bRedefineBlock)
49         {
50           int nPos = I;
51           if (bDecIndex)
52             nPos = nPrevI;
53           if (dwCookie & COOKIE_EXT_COMMENT)
54             {
55               DEFINE_BLOCK (nPos, COLORINDEX_COMMENT);
56             }
57           else if (dwCookie & (COOKIE_CHAR | COOKIE_STRING))
58             {
59               DEFINE_BLOCK (nPos, COLORINDEX_STRING);
60             }
61           else if (dwCookie & COOKIE_ELEMENT)
62             {
63               DEFINE_BLOCK (nPos, COLORINDEX_PREPROCESSOR);
64             }
65           else
66             {
67               if (xisalnum (pszChars[nPos]) || pszChars[nPos] == '.')
68                 {
69                   DEFINE_BLOCK (nPos, COLORINDEX_NORMALTEXT);
70                 }
71               else
72                 {
73                   DEFINE_BLOCK (nPos, COLORINDEX_OPERATOR);
74                   bRedefineBlock = true;
75                   bDecIndex = true;
76                   goto out;
77                 }
78             }
79           bRedefineBlock = false;
80           bDecIndex = false;
81         }
82 out:
83
84       // Can be bigger than length if there is binary data
85       // See bug #1474782 Crash when comparing SQL with with binary data
86       if (I >= nLength || pszChars[I] == 0)
87         break;
88
89       if (!(dwCookie & COOKIE_ELEMENT) && (dwCookie & (COOKIE_EXT_USER1|COOKIE_BLOCK_SCRIPT|COOKIE_BLOCK_STYLE)))
90         {
91           if (dwCookie & COOKIE_BLOCK_SCRIPT)
92             {
93               const TCHAR *pszEnd = _tcsstr(pszChars + I, _T("</script>"));
94               int nextI = pszEnd ? (pszEnd - pszChars) : nLength;
95               dwCookie = ParseLineJava(dwCookie & ~COOKIE_BLOCK_SCRIPT, pszChars + I, nextI - I, pBuf, nActualItems);
96               if (!pszEnd)
97                 dwCookie |= COOKIE_BLOCK_SCRIPT;
98               else
99                 {
100                   dwCookie = 0;
101                   bRedefineBlock = true;
102                 }
103               I = nextI - 1;
104             }
105           else if (dwCookie & COOKIE_BLOCK_STYLE)
106             {
107               const TCHAR *pszEnd = _tcsstr(pszChars + I, _T("</style>"));
108               int nextI = pszEnd ? (pszEnd - pszChars) : nLength;
109               dwCookie = ParseLineCss(dwCookie & ~COOKIE_BLOCK_STYLE, pszChars + I, nextI - I, pBuf, nActualItems);
110               if (!pszEnd)
111                 dwCookie |= COOKIE_BLOCK_STYLE;
112               else
113                 {
114                   dwCookie = 0;
115                   bRedefineBlock = true;
116                 }
117               I = nextI - 1;
118             }
119           else if ((dwCookie & COOKIE_EXT_USER1))
120             {
121               const TCHAR *pszEnd = _tcsstr(pszChars + I, _T("?>"));
122               if (!pszEnd)
123                 pszEnd = _tcsstr(pszChars + I, _T("%>"));
124               int nextI = pszEnd ? (pszEnd - pszChars) : nLength;
125               
126               unsigned (*pParseLineFunc)(unsigned, const TCHAR *, int, TEXTBLOCK *, int &);
127               switch (nEmbeddedLanguage)
128               {
129               case SRC_BASIC: pParseLineFunc = ParseLineBasic; break;
130               case SRC_PHP: pParseLineFunc = ParseLinePhpLanguage; break;
131               default: pParseLineFunc = ParseLineJava; break;
132               }
133               dwCookie = pParseLineFunc(dwCookie & ~COOKIE_EXT_USER1, pszChars + I, nextI - I, pBuf, nActualItems);
134               if (!pszEnd)
135                 dwCookie |= COOKIE_EXT_USER1;
136               else
137                 {
138                   dwCookie = 0;
139                   bRedefineBlock = true;
140                 }
141               I = nextI - 1;
142             }
143           continue;
144         }
145
146       //  String constant "...."
147       if (dwCookie & COOKIE_STRING)
148         {
149           if (pszChars[I] == '"' && (I == 0 || I == 1 && pszChars[nPrevI] != '\\' || I >= 2 && (pszChars[nPrevI] != '\\' || *::CharPrev(pszChars, pszChars + nPrevI) == '\\')))
150             {
151               dwCookie &= ~COOKIE_STRING;
152               bRedefineBlock = true;
153             }
154           continue;
155         }
156
157       //  Char constant '..'
158       if (dwCookie & COOKIE_CHAR)
159         {
160           if (pszChars[I] == '\'' && (I == 0 || I == 1 && pszChars[nPrevI] != '\\' || I >= 2 && (pszChars[nPrevI] != '\\' || *::CharPrev(pszChars, pszChars + nPrevI) == '\\')))
161             {
162               dwCookie &= ~COOKIE_CHAR;
163               bRedefineBlock = true;
164             }
165           continue;
166         }
167
168       //  Extended comment <!--....-->
169       if (dwCookie & COOKIE_EXT_COMMENT)
170         {
171           if (I > 1 && pszChars[I] == '>' && pszChars[nPrevI] == '-' && *::CharPrev(pszChars, pszChars + nPrevI) == '-')
172             {
173               dwCookie &= ~COOKIE_EXT_COMMENT;
174               bRedefineBlock = true;
175             }
176           continue;
177         }
178
179       //  Normal text
180       if ((dwCookie & COOKIE_ELEMENT) && pszChars[I] == '"')
181         {
182           DEFINE_BLOCK (I, COLORINDEX_STRING);
183           dwCookie |= COOKIE_STRING;
184           continue;
185         }
186
187       if ((dwCookie & COOKIE_ELEMENT) && pszChars[I] == '\'')
188         {
189           // if (I + 1 < nLength && pszChars[I + 1] == '\'' || I + 2 < nLength && pszChars[I + 1] != '\\' && pszChars[I + 2] == '\'' || I + 3 < nLength && pszChars[I + 1] == '\\' && pszChars[I + 3] == '\'')
190           if (!I || !xisalnum (pszChars[nPrevI]))
191             {
192               DEFINE_BLOCK (I, COLORINDEX_STRING);
193               dwCookie |= COOKIE_CHAR;
194               continue;
195             }
196         }
197
198       if (I < nLength - 3 && pszChars[I] == '<' && pszChars[I + 1] == '!' && pszChars[I + 2] == '-' && pszChars[I + 3] == '-')
199         {
200           DEFINE_BLOCK (I, COLORINDEX_COMMENT);
201           I += 3;
202           dwCookie |= COOKIE_EXT_COMMENT;
203           dwCookie &= ~COOKIE_ELEMENT;
204           continue;
205         }
206
207       //  User1 start: <?
208       if (I < nLength && pszChars[I] == '<' && I < nLength - 1 && (pszChars[I + 1] == '?' || pszChars[I + 1] == '%'))
209         {
210           DEFINE_BLOCK (I, COLORINDEX_NORMALTEXT);
211           dwCookie |= COOKIE_EXT_USER1;
212           nIdentBegin = -1;
213           continue;
214         }
215
216       if (pBuf == nullptr)
217         continue;               //  We don't need to extract keywords,
218       //  for faster parsing skip the rest of loop
219
220       if (xisalnum (pszChars[I]) || pszChars[I] == '.')
221         {
222           if (nIdentBegin == -1)
223             nIdentBegin = I;
224         }
225       else
226         {
227           if (nIdentBegin >= 0)
228             {
229               if (dwCookie & COOKIE_ELEMENT)
230                 {
231                   if (IsHtmlKeyword (pszChars + nIdentBegin, I - nIdentBegin) && (pszChars[nIdentBegin - 1] == _T ('<') || pszChars[nIdentBegin - 1] == _T ('/')))
232                     {
233                       DEFINE_BLOCK (nIdentBegin, COLORINDEX_KEYWORD);
234                       if (nIdentBegin > 0 && _tcsnicmp(pszChars + nIdentBegin - 1, _T("<script"), sizeof(_T("<script") - 1)) == 0)
235                         dwCookie |= COOKIE_BLOCK_SCRIPT;
236                       else if (nIdentBegin > 0 && _tcsnicmp(pszChars + nIdentBegin - 1, _T("<style"), sizeof(_T("<style") - 1)) == 0)
237                         dwCookie |= COOKIE_BLOCK_STYLE;
238                     }
239                   else if (IsHtmlUser1Keyword (pszChars + nIdentBegin, I - nIdentBegin))
240                     {
241                       DEFINE_BLOCK (nIdentBegin, COLORINDEX_USER1);
242                     }
243                   else if (IsXNumber (pszChars + nIdentBegin, I - nIdentBegin))
244                     {
245                       DEFINE_BLOCK (nIdentBegin, COLORINDEX_NUMBER);
246                     }
247                   else
248                     {
249                       goto next;
250                     }
251                 }
252               else if (dwCookie & COOKIE_USER1)
253                 {
254                   if (IsHtmlUser2Keyword (pszChars + nIdentBegin, I - nIdentBegin))
255                     {
256                       DEFINE_BLOCK (nIdentBegin, COLORINDEX_USER2);
257                     }
258                   else
259                     {
260                       goto next;
261                     }
262                 }
263               bRedefineBlock = true;
264               bDecIndex = true;
265               nIdentBegin = -1;
266 next:
267               ;
268             }
269
270           //  Preprocessor start: < or bracket
271           if (I < nLength && pszChars[I] == '<' && !(I < nLength - 3 && pszChars[I + 1] == '!' && pszChars[I + 2] == '-' && pszChars[I + 3] == '-'))
272             {
273               DEFINE_BLOCK (I, COLORINDEX_OPERATOR);
274               DEFINE_BLOCK (I + 1, COLORINDEX_PREPROCESSOR);
275               dwCookie |= COOKIE_ELEMENT;
276               nIdentBegin = -1;
277               continue;
278             }
279
280           //  Preprocessor end: > or bracket
281           if (dwCookie & COOKIE_ELEMENT)
282             {
283               if (pszChars[I] == '>')
284                 {
285                   dwCookie &= ~COOKIE_ELEMENT;
286                   nIdentBegin = -1;
287                   bRedefineBlock = true;
288                   bDecIndex = true;
289                   continue;
290                 }
291             }
292
293           //  Preprocessor start: &
294           if (pszChars[I] == '&')
295             {
296               dwCookie |= COOKIE_USER1;
297               nIdentBegin = -1;
298               continue;
299             }
300
301           //  Preprocessor end: ;
302           if (dwCookie & COOKIE_USER1)
303             {
304               if (pszChars[I] == ';')
305                 {
306                   dwCookie &= ~COOKIE_USER1;
307                   nIdentBegin = -1;
308                   continue;
309                 }
310             }
311         }
312     }
313
314   if (nIdentBegin >= 0 && (dwCookie & COOKIE_ELEMENT))
315     {
316       if (IsHtmlKeyword (pszChars + nIdentBegin, I - nIdentBegin) && (pszChars[nIdentBegin - 1] == _T ('<') || pszChars[nIdentBegin - 1] == _T ('/')))
317         {
318           DEFINE_BLOCK (nIdentBegin, COLORINDEX_KEYWORD);
319           if (nIdentBegin > 0 && _tcsnicmp(pszChars + nIdentBegin - 1, _T("<script"), sizeof(_T("<script") - 1)) == 0)
320             dwCookie |= COOKIE_BLOCK_SCRIPT;
321           else if (nIdentBegin > 0 && _tcsnicmp(pszChars + nIdentBegin - 1, _T("<style"), sizeof(_T("<style") - 1)) == 0)
322             dwCookie |= COOKIE_BLOCK_STYLE;
323         }
324       else if (IsHtmlUser1Keyword (pszChars + nIdentBegin, I - nIdentBegin))
325         {
326           DEFINE_BLOCK (nIdentBegin, COLORINDEX_USER1);
327         }
328       else if (IsHtmlUser2Keyword (pszChars + nIdentBegin, I - nIdentBegin))
329         {
330           DEFINE_BLOCK (nIdentBegin, COLORINDEX_USER2);
331         }
332       else if (IsXNumber (pszChars + nIdentBegin, I - nIdentBegin))
333         {
334           DEFINE_BLOCK (nIdentBegin, COLORINDEX_NUMBER);
335         }
336     }
337
338   dwCookie &= (COOKIE_EXT_COMMENT | COOKIE_STRING | COOKIE_ELEMENT | COOKIE_EXT_USER1 | COOKIE_BLOCK_SCRIPT | COOKIE_BLOCK_STYLE);
339   return dwCookie;
340 }
341
342 unsigned
343 CrystalLineParser::ParseLineHtml (unsigned dwCookie, const TCHAR *pszChars, int nLength, TEXTBLOCK * pBuf, int &nActualItems)
344 {
345   return ParseLineHtmlEx(dwCookie, pszChars, nLength, pBuf, nActualItems, SRC_JAVA);
346 }