OSDN Git Service

Fix inline scripts in HTML file not being properly highlighted.
[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 static void AdjustCharPosInTextBlocks(CrystalLineParser::TEXTBLOCK* pBuf, int startBlock, int endBlock, int offset)
27 {
28   for (int i = startBlock; i <= endBlock; ++i)
29     pBuf[i].m_nCharPos += offset;
30 }
31
32 unsigned
33 CrystalLineParser::ParseLineHtmlEx (unsigned dwCookie, const TCHAR *pszChars, int nLength, TEXTBLOCK * pBuf, int &nActualItems, int nEmbeddedLanguage)
34 {
35   if (nLength == 0)
36     return dwCookie & (COOKIE_EXT_COMMENT|COOKIE_EXT_USER1|COOKIE_ELEMENT|COOKIE_BLOCK_SCRIPT|COOKIE_BLOCK_STYLE|COOKIE_EXT_DEFINITION|COOKIE_EXT_VALUE);
37
38   bool bRedefineBlock = true;
39   if (!(dwCookie & COOKIE_ELEMENT))
40     bRedefineBlock = !(dwCookie & (COOKIE_EXT_USER1|COOKIE_BLOCK_SCRIPT|COOKIE_BLOCK_STYLE));
41   bool bDecIndex = false;
42   int nIdentBegin = -1;
43   int nPrevI = -1;
44   int I=0;
45   for (I = 0;; nPrevI = I, I = static_cast<int>(::CharNext(pszChars+I) - pszChars))
46     {
47       if (I == nPrevI)
48         {
49           // CharNext did not advance, so we're at the end of the string
50           // and we already handled this character, so stop
51           break;
52         }
53
54       if (bRedefineBlock)
55         {
56           int nPos = I;
57           if (bDecIndex)
58             nPos = nPrevI;
59           if (dwCookie & COOKIE_EXT_COMMENT)
60             {
61               DEFINE_BLOCK (nPos, COLORINDEX_COMMENT);
62             }
63           else if (dwCookie & (COOKIE_CHAR | COOKIE_STRING))
64             {
65               DEFINE_BLOCK (nPos, COLORINDEX_STRING);
66             }
67           else if (dwCookie & COOKIE_ELEMENT)
68             {
69               DEFINE_BLOCK (nPos, COLORINDEX_PREPROCESSOR);
70             }
71           else
72             {
73               if (xisalnum (pszChars[nPos]) || pszChars[nPos] == '.')
74                 {
75                   DEFINE_BLOCK (nPos, COLORINDEX_NORMALTEXT);
76                 }
77               else
78                 {
79                   DEFINE_BLOCK (nPos, COLORINDEX_OPERATOR);
80                   bRedefineBlock = true;
81                   bDecIndex = true;
82                   goto out;
83                 }
84             }
85           bRedefineBlock = false;
86           bDecIndex = false;
87         }
88 out:
89
90       // Can be bigger than length if there is binary data
91       // See bug #1474782 Crash when comparing SQL with with binary data
92       if (I >= nLength || pszChars[I] == 0)
93         break;
94
95       if (!(dwCookie & COOKIE_ELEMENT) && (dwCookie & (COOKIE_EXT_USER1|COOKIE_BLOCK_SCRIPT|COOKIE_BLOCK_STYLE)))
96         {
97           if (dwCookie & COOKIE_BLOCK_SCRIPT)
98             {
99               const TCHAR *pszEnd = _tcsstr(pszChars + I, _T("</script>"));
100               int nextI = pszEnd ? static_cast<int>(pszEnd - pszChars) : nLength;
101               int nActualItemsEmbedded = 0;
102               dwCookie = ParseLineJavaScript(dwCookie & ~COOKIE_BLOCK_SCRIPT, pszChars + I, nextI - I, pBuf + nActualItems, nActualItemsEmbedded);
103               AdjustCharPosInTextBlocks(pBuf, nActualItems, nActualItems + nActualItemsEmbedded - 1, I);
104               nActualItems += nActualItemsEmbedded;
105               if (!pszEnd)
106                 dwCookie |= COOKIE_BLOCK_SCRIPT;
107               else
108                 {
109                   dwCookie = 0;
110                   bRedefineBlock = true;
111                 }
112               I = nextI - 1;
113             }
114           else if (dwCookie & COOKIE_BLOCK_STYLE)
115             {
116               const TCHAR *pszEnd = _tcsstr(pszChars + I, _T("</style>"));
117               int nextI = pszEnd ? static_cast<int>(pszEnd - pszChars) : nLength;
118               int nActualItemsEmbedded = 0;
119               dwCookie = ParseLineCss(dwCookie & ~COOKIE_BLOCK_STYLE, pszChars + I, nextI - I, pBuf + nActualItems, nActualItemsEmbedded);
120               AdjustCharPosInTextBlocks(pBuf, nActualItems, nActualItems + nActualItemsEmbedded - 1, I);
121               nActualItems += nActualItemsEmbedded;
122               if (!pszEnd)
123                 dwCookie |= COOKIE_BLOCK_STYLE;
124               else
125                 {
126                   dwCookie = 0;
127                   bRedefineBlock = true;
128                 }
129               I = nextI - 1;
130             }
131           else if ((dwCookie & COOKIE_EXT_USER1))
132             {
133               const TCHAR *pszEnd = _tcsstr(pszChars + I, _T("?>"));
134               if (!pszEnd)
135                 pszEnd = _tcsstr(pszChars + I, _T("%>"));
136               int nextI = pszEnd ? static_cast<int>(pszEnd - pszChars) : nLength;
137               unsigned (*pParseLineFunc)(unsigned, const TCHAR *, int, TEXTBLOCK *, int &);
138               switch (nEmbeddedLanguage)
139               {
140               case SRC_BASIC: pParseLineFunc = ParseLineBasic; break;
141               case SRC_PHP: pParseLineFunc = ParseLinePhpLanguage; break;
142               default: pParseLineFunc = ParseLineJavaScript; break;
143               }
144               int nActualItemsEmbedded = 0;
145               dwCookie = pParseLineFunc(dwCookie & ~COOKIE_EXT_USER1, pszChars + I, nextI - I, pBuf + nActualItems, nActualItemsEmbedded);
146               AdjustCharPosInTextBlocks(pBuf, nActualItems, nActualItems + nActualItemsEmbedded - 1, I);
147               nActualItems += nActualItemsEmbedded;
148               if (!pszEnd)
149                 dwCookie |= COOKIE_EXT_USER1;
150               else
151                 {
152                   dwCookie = 0;
153                   bRedefineBlock = true;
154                 }
155               I = nextI - 1;
156             }
157           continue;
158         }
159
160       //  String constant "...."
161       if (dwCookie & COOKIE_STRING)
162         {
163           if (pszChars[I] == '"' && (I == 0 || I == 1 && pszChars[nPrevI] != '\\' || I >= 2 && (pszChars[nPrevI] != '\\' || *::CharPrev(pszChars, pszChars + nPrevI) == '\\')))
164             {
165               dwCookie &= ~COOKIE_STRING;
166               bRedefineBlock = true;
167             }
168           continue;
169         }
170
171       //  Char constant '..'
172       if (dwCookie & COOKIE_CHAR)
173         {
174           if (pszChars[I] == '\'' && (I == 0 || I == 1 && pszChars[nPrevI] != '\\' || I >= 2 && (pszChars[nPrevI] != '\\' || *::CharPrev(pszChars, pszChars + nPrevI) == '\\')))
175             {
176               dwCookie &= ~COOKIE_CHAR;
177               bRedefineBlock = true;
178             }
179           continue;
180         }
181
182       //  Extended comment <!--....-->
183       if (dwCookie & COOKIE_EXT_COMMENT)
184         {
185           if (I > 1 && pszChars[I] == '>' && pszChars[nPrevI] == '-' && *::CharPrev(pszChars, pszChars + nPrevI) == '-')
186             {
187               dwCookie &= ~COOKIE_EXT_COMMENT;
188               bRedefineBlock = true;
189             }
190           continue;
191         }
192
193       //  Normal text
194       if ((dwCookie & COOKIE_ELEMENT) && pszChars[I] == '"')
195         {
196           DEFINE_BLOCK (I, COLORINDEX_STRING);
197           dwCookie |= COOKIE_STRING;
198           continue;
199         }
200
201       if ((dwCookie & COOKIE_ELEMENT) && pszChars[I] == '\'')
202         {
203           // 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] == '\'')
204           if (!I || !xisalnum (pszChars[nPrevI]))
205             {
206               DEFINE_BLOCK (I, COLORINDEX_STRING);
207               dwCookie |= COOKIE_CHAR;
208               continue;
209             }
210         }
211
212       if (I < nLength - 3 && pszChars[I] == '<' && pszChars[I + 1] == '!' && pszChars[I + 2] == '-' && pszChars[I + 3] == '-')
213         {
214           DEFINE_BLOCK (I, COLORINDEX_COMMENT);
215           I += 3;
216           dwCookie |= COOKIE_EXT_COMMENT;
217           dwCookie &= ~COOKIE_ELEMENT;
218           continue;
219         }
220
221       //  User1 start: <?
222       if (I < nLength && pszChars[I] == '<' && I < nLength - 1 && (pszChars[I + 1] == '?' || pszChars[I + 1] == '%'))
223         {
224           DEFINE_BLOCK (I, COLORINDEX_NORMALTEXT);
225           dwCookie |= COOKIE_EXT_USER1;
226           nIdentBegin = -1;
227           continue;
228         }
229
230       if (pBuf == nullptr)
231         continue;               //  We don't need to extract keywords,
232       //  for faster parsing skip the rest of loop
233
234       if (xisalnum (pszChars[I]) || pszChars[I] == '.')
235         {
236           if (nIdentBegin == -1)
237             nIdentBegin = I;
238         }
239       else
240         {
241           if (nIdentBegin >= 0)
242             {
243               if (dwCookie & COOKIE_ELEMENT)
244                 {
245                   if (IsHtmlKeyword (pszChars + nIdentBegin, I - nIdentBegin) && (pszChars[nIdentBegin - 1] == _T ('<') || pszChars[nIdentBegin - 1] == _T ('/')))
246                     {
247                       DEFINE_BLOCK (nIdentBegin, COLORINDEX_KEYWORD);
248                       if (nIdentBegin > 0 && _tcsnicmp(pszChars + nIdentBegin - 1, _T("<script"), sizeof("<script") - 1) == 0)
249                         dwCookie |= COOKIE_BLOCK_SCRIPT;
250                       else if (nIdentBegin > 0 && _tcsnicmp(pszChars + nIdentBegin - 1, _T("<style"), sizeof("<style") - 1) == 0)
251                         dwCookie |= COOKIE_BLOCK_STYLE;
252                     }
253                   else if (IsHtmlUser1Keyword (pszChars + nIdentBegin, I - nIdentBegin))
254                     {
255                       DEFINE_BLOCK (nIdentBegin, COLORINDEX_USER1);
256                     }
257                   else if (IsXNumber (pszChars + nIdentBegin, I - nIdentBegin))
258                     {
259                       DEFINE_BLOCK (nIdentBegin, COLORINDEX_NUMBER);
260                     }
261                   else
262                     {
263                       goto next;
264                     }
265                 }
266               else if (dwCookie & COOKIE_USER1)
267                 {
268                   if (IsHtmlUser2Keyword (pszChars + nIdentBegin, I - nIdentBegin))
269                     {
270                       DEFINE_BLOCK (nIdentBegin, COLORINDEX_USER2);
271                     }
272                   else
273                     {
274                       goto next;
275                     }
276                 }
277               bRedefineBlock = true;
278               bDecIndex = true;
279               nIdentBegin = -1;
280 next:
281               ;
282             }
283
284           //  Preprocessor start: < or bracket
285           if (I < nLength && pszChars[I] == '<' && !(I < nLength - 3 && pszChars[I + 1] == '!' && pszChars[I + 2] == '-' && pszChars[I + 3] == '-'))
286             {
287               DEFINE_BLOCK (I, COLORINDEX_OPERATOR);
288               DEFINE_BLOCK (I + 1, COLORINDEX_PREPROCESSOR);
289               dwCookie |= COOKIE_ELEMENT;
290               nIdentBegin = -1;
291               continue;
292             }
293
294           //  Preprocessor end: > or bracket
295           if (dwCookie & COOKIE_ELEMENT)
296             {
297               if (pszChars[I] == '>')
298                 {
299                   dwCookie &= ~COOKIE_ELEMENT;
300                   nIdentBegin = -1;
301                   bRedefineBlock = true;
302                   bDecIndex = true;
303                   continue;
304                 }
305             }
306
307           //  Preprocessor start: &
308           if (pszChars[I] == '&')
309             {
310               dwCookie |= COOKIE_USER1;
311               nIdentBegin = -1;
312               continue;
313             }
314
315           //  Preprocessor end: ;
316           if (dwCookie & COOKIE_USER1)
317             {
318               if (pszChars[I] == ';')
319                 {
320                   dwCookie &= ~COOKIE_USER1;
321                   nIdentBegin = -1;
322                   continue;
323                 }
324             }
325         }
326     }
327
328   if (nIdentBegin >= 0 && (dwCookie & COOKIE_ELEMENT))
329     {
330       if (IsHtmlKeyword (pszChars + nIdentBegin, I - nIdentBegin) && (pszChars[nIdentBegin - 1] == _T ('<') || pszChars[nIdentBegin - 1] == _T ('/')))
331         {
332           DEFINE_BLOCK (nIdentBegin, COLORINDEX_KEYWORD);
333           if (nIdentBegin > 0 && _tcsnicmp(pszChars + nIdentBegin - 1, _T("<script"), sizeof("<script") - 1) == 0)
334             dwCookie |= COOKIE_BLOCK_SCRIPT;
335           else if (nIdentBegin > 0 && _tcsnicmp(pszChars + nIdentBegin - 1, _T("<style"), sizeof("<style") - 1) == 0)
336             dwCookie |= COOKIE_BLOCK_STYLE;
337         }
338       else if (IsHtmlUser1Keyword (pszChars + nIdentBegin, I - nIdentBegin))
339         {
340           DEFINE_BLOCK (nIdentBegin, COLORINDEX_USER1);
341         }
342       else if (IsHtmlUser2Keyword (pszChars + nIdentBegin, I - nIdentBegin))
343         {
344           DEFINE_BLOCK (nIdentBegin, COLORINDEX_USER2);
345         }
346       else if (IsXNumber (pszChars + nIdentBegin, I - nIdentBegin))
347         {
348           DEFINE_BLOCK (nIdentBegin, COLORINDEX_NUMBER);
349         }
350     }
351
352   dwCookie &= (COOKIE_EXT_COMMENT | COOKIE_STRING | COOKIE_ELEMENT | COOKIE_EXT_USER1 | COOKIE_BLOCK_SCRIPT | COOKIE_BLOCK_STYLE | COOKIE_EXT_DEFINITION | COOKIE_EXT_VALUE);
353   return dwCookie;
354 }
355
356 unsigned
357 CrystalLineParser::ParseLineHtml (unsigned dwCookie, const TCHAR *pszChars, int nLength, TEXTBLOCK * pBuf, int &nActualItems)
358 {
359   return ParseLineHtmlEx(dwCookie, pszChars, nLength, pBuf, nActualItems, SRC_JAVA);
360 }