PageRenderTime 52ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/ext/scintilla/lexers/LexHTML.cxx

https://gitlab.com/info29/tortoisegit
C++ | 2192 lines | 1959 code | 119 blank | 114 comment | 1450 complexity | 94f835e1d38be345f0ddec838ba124b2 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, MPL-2.0-no-copyleft-exception

Large files files are truncated, but you can click here to view the full file

  1. // Scintilla source code edit control
  2. /** @file LexHTML.cxx
  3. ** Lexer for HTML.
  4. **/
  5. // Copyright 1998-2005 by Neil Hodgson <neilh@scintilla.org>
  6. // The License.txt file describes the conditions under which this software may be distributed.
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <stdarg.h>
  11. #include <assert.h>
  12. #include <ctype.h>
  13. #include "ILexer.h"
  14. #include "Scintilla.h"
  15. #include "SciLexer.h"
  16. #include "StringCopy.h"
  17. #include "WordList.h"
  18. #include "LexAccessor.h"
  19. #include "Accessor.h"
  20. #include "StyleContext.h"
  21. #include "CharacterSet.h"
  22. #include "LexerModule.h"
  23. #ifdef SCI_NAMESPACE
  24. using namespace Scintilla;
  25. #endif
  26. #define SCE_HA_JS (SCE_HJA_START - SCE_HJ_START)
  27. #define SCE_HA_VBS (SCE_HBA_START - SCE_HB_START)
  28. #define SCE_HA_PYTHON (SCE_HPA_START - SCE_HP_START)
  29. enum script_type { eScriptNone = 0, eScriptJS, eScriptVBS, eScriptPython, eScriptPHP, eScriptXML, eScriptSGML, eScriptSGMLblock, eScriptComment };
  30. enum script_mode { eHtml = 0, eNonHtmlScript, eNonHtmlPreProc, eNonHtmlScriptPreProc };
  31. static inline bool IsAWordChar(const int ch) {
  32. return (ch < 0x80) && (isalnum(ch) || ch == '.' || ch == '_');
  33. }
  34. static inline bool IsAWordStart(const int ch) {
  35. return (ch < 0x80) && (isalnum(ch) || ch == '_');
  36. }
  37. inline bool IsOperator(int ch) {
  38. if (IsASCII(ch) && isalnum(ch))
  39. return false;
  40. // '.' left out as it is used to make up numbers
  41. if (ch == '%' || ch == '^' || ch == '&' || ch == '*' ||
  42. ch == '(' || ch == ')' || ch == '-' || ch == '+' ||
  43. ch == '=' || ch == '|' || ch == '{' || ch == '}' ||
  44. ch == '[' || ch == ']' || ch == ':' || ch == ';' ||
  45. ch == '<' || ch == '>' || ch == ',' || ch == '/' ||
  46. ch == '?' || ch == '!' || ch == '.' || ch == '~')
  47. return true;
  48. return false;
  49. }
  50. static void GetTextSegment(Accessor &styler, unsigned int start, unsigned int end, char *s, size_t len) {
  51. unsigned int i = 0;
  52. for (; (i < end - start + 1) && (i < len-1); i++) {
  53. s[i] = static_cast<char>(MakeLowerCase(styler[start + i]));
  54. }
  55. s[i] = '\0';
  56. }
  57. static const char *GetNextWord(Accessor &styler, unsigned int start, char *s, size_t sLen) {
  58. unsigned int i = 0;
  59. for (; i < sLen-1; i++) {
  60. char ch = static_cast<char>(styler.SafeGetCharAt(start + i));
  61. if ((i == 0) && !IsAWordStart(ch))
  62. break;
  63. if ((i > 0) && !IsAWordChar(ch))
  64. break;
  65. s[i] = ch;
  66. }
  67. s[i] = '\0';
  68. return s;
  69. }
  70. static script_type segIsScriptingIndicator(Accessor &styler, unsigned int start, unsigned int end, script_type prevValue) {
  71. char s[100];
  72. GetTextSegment(styler, start, end, s, sizeof(s));
  73. //Platform::DebugPrintf("Scripting indicator [%s]\n", s);
  74. if (strstr(s, "src")) // External script
  75. return eScriptNone;
  76. if (strstr(s, "vbs"))
  77. return eScriptVBS;
  78. if (strstr(s, "pyth"))
  79. return eScriptPython;
  80. if (strstr(s, "javas"))
  81. return eScriptJS;
  82. if (strstr(s, "jscr"))
  83. return eScriptJS;
  84. if (strstr(s, "php"))
  85. return eScriptPHP;
  86. if (strstr(s, "xml")) {
  87. const char *xml = strstr(s, "xml");
  88. for (const char *t=s; t<xml; t++) {
  89. if (!IsASpace(*t)) {
  90. return prevValue;
  91. }
  92. }
  93. return eScriptXML;
  94. }
  95. return prevValue;
  96. }
  97. static int PrintScriptingIndicatorOffset(Accessor &styler, unsigned int start, unsigned int end) {
  98. int iResult = 0;
  99. char s[100];
  100. GetTextSegment(styler, start, end, s, sizeof(s));
  101. if (0 == strncmp(s, "php", 3)) {
  102. iResult = 3;
  103. }
  104. return iResult;
  105. }
  106. static script_type ScriptOfState(int state) {
  107. if ((state >= SCE_HP_START) && (state <= SCE_HP_IDENTIFIER)) {
  108. return eScriptPython;
  109. } else if ((state >= SCE_HB_START) && (state <= SCE_HB_STRINGEOL)) {
  110. return eScriptVBS;
  111. } else if ((state >= SCE_HJ_START) && (state <= SCE_HJ_REGEX)) {
  112. return eScriptJS;
  113. } else if ((state >= SCE_HPHP_DEFAULT) && (state <= SCE_HPHP_COMMENTLINE)) {
  114. return eScriptPHP;
  115. } else if ((state >= SCE_H_SGML_DEFAULT) && (state < SCE_H_SGML_BLOCK_DEFAULT)) {
  116. return eScriptSGML;
  117. } else if (state == SCE_H_SGML_BLOCK_DEFAULT) {
  118. return eScriptSGMLblock;
  119. } else {
  120. return eScriptNone;
  121. }
  122. }
  123. static int statePrintForState(int state, script_mode inScriptType) {
  124. int StateToPrint = state;
  125. if (state >= SCE_HJ_START) {
  126. if ((state >= SCE_HP_START) && (state <= SCE_HP_IDENTIFIER)) {
  127. StateToPrint = state + ((inScriptType == eNonHtmlScript) ? 0 : SCE_HA_PYTHON);
  128. } else if ((state >= SCE_HB_START) && (state <= SCE_HB_STRINGEOL)) {
  129. StateToPrint = state + ((inScriptType == eNonHtmlScript) ? 0 : SCE_HA_VBS);
  130. } else if ((state >= SCE_HJ_START) && (state <= SCE_HJ_REGEX)) {
  131. StateToPrint = state + ((inScriptType == eNonHtmlScript) ? 0 : SCE_HA_JS);
  132. }
  133. }
  134. return StateToPrint;
  135. }
  136. static int stateForPrintState(int StateToPrint) {
  137. int state;
  138. if ((StateToPrint >= SCE_HPA_START) && (StateToPrint <= SCE_HPA_IDENTIFIER)) {
  139. state = StateToPrint - SCE_HA_PYTHON;
  140. } else if ((StateToPrint >= SCE_HBA_START) && (StateToPrint <= SCE_HBA_STRINGEOL)) {
  141. state = StateToPrint - SCE_HA_VBS;
  142. } else if ((StateToPrint >= SCE_HJA_START) && (StateToPrint <= SCE_HJA_REGEX)) {
  143. state = StateToPrint - SCE_HA_JS;
  144. } else {
  145. state = StateToPrint;
  146. }
  147. return state;
  148. }
  149. static inline bool IsNumber(unsigned int start, Accessor &styler) {
  150. return IsADigit(styler[start]) || (styler[start] == '.') ||
  151. (styler[start] == '-') || (styler[start] == '#');
  152. }
  153. static inline bool isStringState(int state) {
  154. bool bResult;
  155. switch (state) {
  156. case SCE_HJ_DOUBLESTRING:
  157. case SCE_HJ_SINGLESTRING:
  158. case SCE_HJA_DOUBLESTRING:
  159. case SCE_HJA_SINGLESTRING:
  160. case SCE_HB_STRING:
  161. case SCE_HBA_STRING:
  162. case SCE_HP_STRING:
  163. case SCE_HP_CHARACTER:
  164. case SCE_HP_TRIPLE:
  165. case SCE_HP_TRIPLEDOUBLE:
  166. case SCE_HPA_STRING:
  167. case SCE_HPA_CHARACTER:
  168. case SCE_HPA_TRIPLE:
  169. case SCE_HPA_TRIPLEDOUBLE:
  170. case SCE_HPHP_HSTRING:
  171. case SCE_HPHP_SIMPLESTRING:
  172. case SCE_HPHP_HSTRING_VARIABLE:
  173. case SCE_HPHP_COMPLEX_VARIABLE:
  174. bResult = true;
  175. break;
  176. default :
  177. bResult = false;
  178. break;
  179. }
  180. return bResult;
  181. }
  182. static inline bool stateAllowsTermination(int state) {
  183. bool allowTermination = !isStringState(state);
  184. if (allowTermination) {
  185. switch (state) {
  186. case SCE_HB_COMMENTLINE:
  187. case SCE_HPHP_COMMENT:
  188. case SCE_HP_COMMENTLINE:
  189. case SCE_HPA_COMMENTLINE:
  190. allowTermination = false;
  191. }
  192. }
  193. return allowTermination;
  194. }
  195. // not really well done, since it's only comments that should lex the %> and <%
  196. static inline bool isCommentASPState(int state) {
  197. bool bResult;
  198. switch (state) {
  199. case SCE_HJ_COMMENT:
  200. case SCE_HJ_COMMENTLINE:
  201. case SCE_HJ_COMMENTDOC:
  202. case SCE_HB_COMMENTLINE:
  203. case SCE_HP_COMMENTLINE:
  204. case SCE_HPHP_COMMENT:
  205. case SCE_HPHP_COMMENTLINE:
  206. bResult = true;
  207. break;
  208. default :
  209. bResult = false;
  210. break;
  211. }
  212. return bResult;
  213. }
  214. static void classifyAttribHTML(unsigned int start, unsigned int end, WordList &keywords, Accessor &styler) {
  215. bool wordIsNumber = IsNumber(start, styler);
  216. char chAttr = SCE_H_ATTRIBUTEUNKNOWN;
  217. if (wordIsNumber) {
  218. chAttr = SCE_H_NUMBER;
  219. } else {
  220. char s[100];
  221. GetTextSegment(styler, start, end, s, sizeof(s));
  222. if (keywords.InList(s))
  223. chAttr = SCE_H_ATTRIBUTE;
  224. }
  225. if ((chAttr == SCE_H_ATTRIBUTEUNKNOWN) && !keywords)
  226. // No keywords -> all are known
  227. chAttr = SCE_H_ATTRIBUTE;
  228. styler.ColourTo(end, chAttr);
  229. }
  230. static int classifyTagHTML(unsigned int start, unsigned int end,
  231. WordList &keywords, Accessor &styler, bool &tagDontFold,
  232. bool caseSensitive, bool isXml, bool allowScripts) {
  233. char withSpace[30 + 2] = " ";
  234. const char *s = withSpace + 1;
  235. // Copy after the '<'
  236. unsigned int i = 1;
  237. for (unsigned int cPos = start; cPos <= end && i < 30; cPos++) {
  238. char ch = styler[cPos];
  239. if ((ch != '<') && (ch != '/')) {
  240. withSpace[i++] = caseSensitive ? ch : static_cast<char>(MakeLowerCase(ch));
  241. }
  242. }
  243. //The following is only a quick hack, to see if this whole thing would work
  244. //we first need the tagname with a trailing space...
  245. withSpace[i] = ' ';
  246. withSpace[i+1] = '\0';
  247. // if the current language is XML, I can fold any tag
  248. // if the current language is HTML, I don't want to fold certain tags (input, meta, etc.)
  249. //...to find it in the list of no-container-tags
  250. tagDontFold = (!isXml) && (NULL != strstr(" area base basefont br col command embed frame hr img input isindex keygen link meta param source track wbr ", withSpace));
  251. //now we can remove the trailing space
  252. withSpace[i] = '\0';
  253. // No keywords -> all are known
  254. char chAttr = SCE_H_TAGUNKNOWN;
  255. if (s[0] == '!') {
  256. chAttr = SCE_H_SGML_DEFAULT;
  257. } else if (!keywords || keywords.InList(s)) {
  258. chAttr = SCE_H_TAG;
  259. }
  260. styler.ColourTo(end, chAttr);
  261. if (chAttr == SCE_H_TAG) {
  262. if (allowScripts && 0 == strcmp(s, "script")) {
  263. // check to see if this is a self-closing tag by sniffing ahead
  264. bool isSelfClose = false;
  265. for (unsigned int cPos = end; cPos <= end + 200; cPos++) {
  266. char ch = styler.SafeGetCharAt(cPos, '\0');
  267. if (ch == '\0' || ch == '>')
  268. break;
  269. else if (ch == '/' && styler.SafeGetCharAt(cPos + 1, '\0') == '>') {
  270. isSelfClose = true;
  271. break;
  272. }
  273. }
  274. // do not enter a script state if the tag self-closed
  275. if (!isSelfClose)
  276. chAttr = SCE_H_SCRIPT;
  277. } else if (!isXml && 0 == strcmp(s, "comment")) {
  278. chAttr = SCE_H_COMMENT;
  279. }
  280. }
  281. return chAttr;
  282. }
  283. static void classifyWordHTJS(unsigned int start, unsigned int end,
  284. WordList &keywords, Accessor &styler, script_mode inScriptType) {
  285. char s[30 + 1];
  286. unsigned int i = 0;
  287. for (; i < end - start + 1 && i < 30; i++) {
  288. s[i] = styler[start + i];
  289. }
  290. s[i] = '\0';
  291. char chAttr = SCE_HJ_WORD;
  292. bool wordIsNumber = IsADigit(s[0]) || ((s[0] == '.') && IsADigit(s[1]));
  293. if (wordIsNumber) {
  294. chAttr = SCE_HJ_NUMBER;
  295. } else if (keywords.InList(s)) {
  296. chAttr = SCE_HJ_KEYWORD;
  297. }
  298. styler.ColourTo(end, statePrintForState(chAttr, inScriptType));
  299. }
  300. static int classifyWordHTVB(unsigned int start, unsigned int end, WordList &keywords, Accessor &styler, script_mode inScriptType) {
  301. char chAttr = SCE_HB_IDENTIFIER;
  302. bool wordIsNumber = IsADigit(styler[start]) || (styler[start] == '.');
  303. if (wordIsNumber) {
  304. chAttr = SCE_HB_NUMBER;
  305. } else {
  306. char s[100];
  307. GetTextSegment(styler, start, end, s, sizeof(s));
  308. if (keywords.InList(s)) {
  309. chAttr = SCE_HB_WORD;
  310. if (strcmp(s, "rem") == 0)
  311. chAttr = SCE_HB_COMMENTLINE;
  312. }
  313. }
  314. styler.ColourTo(end, statePrintForState(chAttr, inScriptType));
  315. if (chAttr == SCE_HB_COMMENTLINE)
  316. return SCE_HB_COMMENTLINE;
  317. else
  318. return SCE_HB_DEFAULT;
  319. }
  320. static void classifyWordHTPy(unsigned int start, unsigned int end, WordList &keywords, Accessor &styler, char *prevWord, script_mode inScriptType, bool isMako) {
  321. bool wordIsNumber = IsADigit(styler[start]);
  322. char s[30 + 1];
  323. unsigned int i = 0;
  324. for (; i < end - start + 1 && i < 30; i++) {
  325. s[i] = styler[start + i];
  326. }
  327. s[i] = '\0';
  328. char chAttr = SCE_HP_IDENTIFIER;
  329. if (0 == strcmp(prevWord, "class"))
  330. chAttr = SCE_HP_CLASSNAME;
  331. else if (0 == strcmp(prevWord, "def"))
  332. chAttr = SCE_HP_DEFNAME;
  333. else if (wordIsNumber)
  334. chAttr = SCE_HP_NUMBER;
  335. else if (keywords.InList(s))
  336. chAttr = SCE_HP_WORD;
  337. else if (isMako && 0 == strcmp(s, "block"))
  338. chAttr = SCE_HP_WORD;
  339. styler.ColourTo(end, statePrintForState(chAttr, inScriptType));
  340. strcpy(prevWord, s);
  341. }
  342. // Update the word colour to default or keyword
  343. // Called when in a PHP word
  344. static void classifyWordHTPHP(unsigned int start, unsigned int end, WordList &keywords, Accessor &styler) {
  345. char chAttr = SCE_HPHP_DEFAULT;
  346. bool wordIsNumber = IsADigit(styler[start]) || (styler[start] == '.' && start+1 <= end && IsADigit(styler[start+1]));
  347. if (wordIsNumber) {
  348. chAttr = SCE_HPHP_NUMBER;
  349. } else {
  350. char s[100];
  351. GetTextSegment(styler, start, end, s, sizeof(s));
  352. if (keywords.InList(s))
  353. chAttr = SCE_HPHP_WORD;
  354. }
  355. styler.ColourTo(end, chAttr);
  356. }
  357. static bool isWordHSGML(unsigned int start, unsigned int end, WordList &keywords, Accessor &styler) {
  358. char s[30 + 1];
  359. unsigned int i = 0;
  360. for (; i < end - start + 1 && i < 30; i++) {
  361. s[i] = styler[start + i];
  362. }
  363. s[i] = '\0';
  364. return keywords.InList(s);
  365. }
  366. static bool isWordCdata(unsigned int start, unsigned int end, Accessor &styler) {
  367. char s[30 + 1];
  368. unsigned int i = 0;
  369. for (; i < end - start + 1 && i < 30; i++) {
  370. s[i] = styler[start + i];
  371. }
  372. s[i] = '\0';
  373. return (0 == strcmp(s, "[CDATA["));
  374. }
  375. // Return the first state to reach when entering a scripting language
  376. static int StateForScript(script_type scriptLanguage) {
  377. int Result;
  378. switch (scriptLanguage) {
  379. case eScriptVBS:
  380. Result = SCE_HB_START;
  381. break;
  382. case eScriptPython:
  383. Result = SCE_HP_START;
  384. break;
  385. case eScriptPHP:
  386. Result = SCE_HPHP_DEFAULT;
  387. break;
  388. case eScriptXML:
  389. Result = SCE_H_TAGUNKNOWN;
  390. break;
  391. case eScriptSGML:
  392. Result = SCE_H_SGML_DEFAULT;
  393. break;
  394. case eScriptComment:
  395. Result = SCE_H_COMMENT;
  396. break;
  397. default :
  398. Result = SCE_HJ_START;
  399. break;
  400. }
  401. return Result;
  402. }
  403. static inline bool issgmlwordchar(int ch) {
  404. return !IsASCII(ch) ||
  405. (isalnum(ch) || ch == '.' || ch == '_' || ch == ':' || ch == '!' || ch == '#' || ch == '[');
  406. }
  407. static inline bool IsPhpWordStart(int ch) {
  408. return (IsASCII(ch) && (isalpha(ch) || (ch == '_'))) || (ch >= 0x7f);
  409. }
  410. static inline bool IsPhpWordChar(int ch) {
  411. return IsADigit(ch) || IsPhpWordStart(ch);
  412. }
  413. static bool InTagState(int state) {
  414. return state == SCE_H_TAG || state == SCE_H_TAGUNKNOWN ||
  415. state == SCE_H_SCRIPT ||
  416. state == SCE_H_ATTRIBUTE || state == SCE_H_ATTRIBUTEUNKNOWN ||
  417. state == SCE_H_NUMBER || state == SCE_H_OTHER ||
  418. state == SCE_H_DOUBLESTRING || state == SCE_H_SINGLESTRING;
  419. }
  420. static bool IsCommentState(const int state) {
  421. return state == SCE_H_COMMENT || state == SCE_H_SGML_COMMENT;
  422. }
  423. static bool IsScriptCommentState(const int state) {
  424. return state == SCE_HJ_COMMENT || state == SCE_HJ_COMMENTLINE || state == SCE_HJA_COMMENT ||
  425. state == SCE_HJA_COMMENTLINE || state == SCE_HB_COMMENTLINE || state == SCE_HBA_COMMENTLINE;
  426. }
  427. static bool isLineEnd(int ch) {
  428. return ch == '\r' || ch == '\n';
  429. }
  430. static bool isMakoBlockEnd(const int ch, const int chNext, const char *blockType) {
  431. if (strlen(blockType) == 0) {
  432. return ((ch == '%') && (chNext == '>'));
  433. } else if ((0 == strcmp(blockType, "inherit")) ||
  434. (0 == strcmp(blockType, "namespace")) ||
  435. (0 == strcmp(blockType, "include")) ||
  436. (0 == strcmp(blockType, "page"))) {
  437. return ((ch == '/') && (chNext == '>'));
  438. } else if (0 == strcmp(blockType, "%")) {
  439. if (ch == '/' && isLineEnd(chNext))
  440. return 1;
  441. else
  442. return isLineEnd(ch);
  443. } else if (0 == strcmp(blockType, "{")) {
  444. return ch == '}';
  445. } else {
  446. return (ch == '>');
  447. }
  448. }
  449. static bool isDjangoBlockEnd(const int ch, const int chNext, const char *blockType) {
  450. if (strlen(blockType) == 0) {
  451. return 0;
  452. } else if (0 == strcmp(blockType, "%")) {
  453. return ((ch == '%') && (chNext == '}'));
  454. } else if (0 == strcmp(blockType, "{")) {
  455. return ((ch == '}') && (chNext == '}'));
  456. } else {
  457. return 0;
  458. }
  459. }
  460. static bool isPHPStringState(int state) {
  461. return
  462. (state == SCE_HPHP_HSTRING) ||
  463. (state == SCE_HPHP_SIMPLESTRING) ||
  464. (state == SCE_HPHP_HSTRING_VARIABLE) ||
  465. (state == SCE_HPHP_COMPLEX_VARIABLE);
  466. }
  467. static int FindPhpStringDelimiter(char *phpStringDelimiter, const int phpStringDelimiterSize, int i, const int lengthDoc, Accessor &styler, bool &isSimpleString) {
  468. int j;
  469. const int beginning = i - 1;
  470. bool isValidSimpleString = false;
  471. while (i < lengthDoc && (styler[i] == ' ' || styler[i] == '\t'))
  472. i++;
  473. char ch = styler.SafeGetCharAt(i);
  474. const char chNext = styler.SafeGetCharAt(i + 1);
  475. if (!IsPhpWordStart(ch)) {
  476. if (ch == '\'' && IsPhpWordStart(chNext)) {
  477. i++;
  478. ch = chNext;
  479. isSimpleString = true;
  480. } else {
  481. phpStringDelimiter[0] = '\0';
  482. return beginning;
  483. }
  484. }
  485. phpStringDelimiter[0] = ch;
  486. i++;
  487. for (j = i; j < lengthDoc && !isLineEnd(styler[j]); j++) {
  488. if (!IsPhpWordChar(styler[j])) {
  489. if (isSimpleString && (styler[j] == '\'') && isLineEnd(styler.SafeGetCharAt(j + 1))) {
  490. isValidSimpleString = true;
  491. j++;
  492. break;
  493. } else {
  494. phpStringDelimiter[0] = '\0';
  495. return beginning;
  496. }
  497. }
  498. if (j - i < phpStringDelimiterSize - 2)
  499. phpStringDelimiter[j-i+1] = styler[j];
  500. else
  501. i++;
  502. }
  503. if (isSimpleString && !isValidSimpleString) {
  504. phpStringDelimiter[0] = '\0';
  505. return beginning;
  506. }
  507. phpStringDelimiter[j-i+1 - (isSimpleString ? 1 : 0)] = '\0';
  508. return j - 1;
  509. }
  510. static void ColouriseHyperTextDoc(unsigned int startPos, int length, int initStyle, WordList *keywordlists[],
  511. Accessor &styler, bool isXml) {
  512. WordList &keywords = *keywordlists[0];
  513. WordList &keywords2 = *keywordlists[1];
  514. WordList &keywords3 = *keywordlists[2];
  515. WordList &keywords4 = *keywordlists[3];
  516. WordList &keywords5 = *keywordlists[4];
  517. WordList &keywords6 = *keywordlists[5]; // SGML (DTD) keywords
  518. styler.StartAt(startPos);
  519. char prevWord[200];
  520. prevWord[0] = '\0';
  521. char phpStringDelimiter[200]; // PHP is not limited in length, we are
  522. phpStringDelimiter[0] = '\0';
  523. int StateToPrint = initStyle;
  524. int state = stateForPrintState(StateToPrint);
  525. char makoBlockType[200];
  526. makoBlockType[0] = '\0';
  527. int makoComment = 0;
  528. char djangoBlockType[2];
  529. djangoBlockType[0] = '\0';
  530. // If inside a tag, it may be a script tag, so reread from the start of line starting tag to ensure any language tags are seen
  531. if (InTagState(state)) {
  532. while ((startPos > 0) && (InTagState(styler.StyleAt(startPos - 1)))) {
  533. int backLineStart = styler.LineStart(styler.GetLine(startPos-1));
  534. length += startPos - backLineStart;
  535. startPos = backLineStart;
  536. }
  537. state = SCE_H_DEFAULT;
  538. }
  539. // String can be heredoc, must find a delimiter first. Reread from beginning of line containing the string, to get the correct lineState
  540. if (isPHPStringState(state)) {
  541. while (startPos > 0 && (isPHPStringState(state) || !isLineEnd(styler[startPos - 1]))) {
  542. startPos--;
  543. length++;
  544. state = styler.StyleAt(startPos);
  545. }
  546. if (startPos == 0)
  547. state = SCE_H_DEFAULT;
  548. }
  549. styler.StartAt(startPos);
  550. int lineCurrent = styler.GetLine(startPos);
  551. int lineState;
  552. if (lineCurrent > 0) {
  553. lineState = styler.GetLineState(lineCurrent-1);
  554. } else {
  555. // Default client and ASP scripting language is JavaScript
  556. lineState = eScriptJS << 8;
  557. // property asp.default.language
  558. // Script in ASP code is initially assumed to be in JavaScript.
  559. // To change this to VBScript set asp.default.language to 2. Python is 3.
  560. lineState |= styler.GetPropertyInt("asp.default.language", eScriptJS) << 4;
  561. }
  562. script_mode inScriptType = script_mode((lineState >> 0) & 0x03); // 2 bits of scripting mode
  563. bool tagOpened = (lineState >> 2) & 0x01; // 1 bit to know if we are in an opened tag
  564. bool tagClosing = (lineState >> 3) & 0x01; // 1 bit to know if we are in a closing tag
  565. bool tagDontFold = false; //some HTML tags should not be folded
  566. script_type aspScript = script_type((lineState >> 4) & 0x0F); // 4 bits of script name
  567. script_type clientScript = script_type((lineState >> 8) & 0x0F); // 4 bits of script name
  568. int beforePreProc = (lineState >> 12) & 0xFF; // 8 bits of state
  569. script_type scriptLanguage = ScriptOfState(state);
  570. // If eNonHtmlScript coincides with SCE_H_COMMENT, assume eScriptComment
  571. if (inScriptType == eNonHtmlScript && state == SCE_H_COMMENT) {
  572. scriptLanguage = eScriptComment;
  573. }
  574. script_type beforeLanguage = ScriptOfState(beforePreProc);
  575. // property fold.html
  576. // Folding is turned on or off for HTML and XML files with this option.
  577. // The fold option must also be on for folding to occur.
  578. const bool foldHTML = styler.GetPropertyInt("fold.html", 0) != 0;
  579. const bool fold = foldHTML && styler.GetPropertyInt("fold", 0);
  580. // property fold.html.preprocessor
  581. // Folding is turned on or off for scripts embedded in HTML files with this option.
  582. // The default is on.
  583. const bool foldHTMLPreprocessor = foldHTML && styler.GetPropertyInt("fold.html.preprocessor", 1);
  584. const bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
  585. // property fold.hypertext.comment
  586. // Allow folding for comments in scripts embedded in HTML.
  587. // The default is off.
  588. const bool foldComment = fold && styler.GetPropertyInt("fold.hypertext.comment", 0) != 0;
  589. // property fold.hypertext.heredoc
  590. // Allow folding for heredocs in scripts embedded in HTML.
  591. // The default is off.
  592. const bool foldHeredoc = fold && styler.GetPropertyInt("fold.hypertext.heredoc", 0) != 0;
  593. // property html.tags.case.sensitive
  594. // For XML and HTML, setting this property to 1 will make tags match in a case
  595. // sensitive way which is the expected behaviour for XML and XHTML.
  596. const bool caseSensitive = styler.GetPropertyInt("html.tags.case.sensitive", 0) != 0;
  597. // property lexer.xml.allow.scripts
  598. // Set to 0 to disable scripts in XML.
  599. const bool allowScripts = styler.GetPropertyInt("lexer.xml.allow.scripts", 1) != 0;
  600. // property lexer.html.mako
  601. // Set to 1 to enable the mako template language.
  602. const bool isMako = styler.GetPropertyInt("lexer.html.mako", 0) != 0;
  603. // property lexer.html.django
  604. // Set to 1 to enable the django template language.
  605. const bool isDjango = styler.GetPropertyInt("lexer.html.django", 0) != 0;
  606. const CharacterSet setHTMLWord(CharacterSet::setAlphaNum, ".-_:!#", 0x80, true);
  607. const CharacterSet setTagContinue(CharacterSet::setAlphaNum, ".-_:!#[", 0x80, true);
  608. const CharacterSet setAttributeContinue(CharacterSet::setAlphaNum, ".-_:!#/", 0x80, true);
  609. // TODO: also handle + and - (except if they're part of ++ or --) and return keywords
  610. const CharacterSet setOKBeforeJSRE(CharacterSet::setNone, "([{=,:;!%^&*|?~");
  611. int levelPrev = styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK;
  612. int levelCurrent = levelPrev;
  613. int visibleChars = 0;
  614. int lineStartVisibleChars = 0;
  615. int chPrev = ' ';
  616. int ch = ' ';
  617. int chPrevNonWhite = ' ';
  618. // look back to set chPrevNonWhite properly for better regex colouring
  619. if (scriptLanguage == eScriptJS && startPos > 0) {
  620. int back = startPos;
  621. int style = 0;
  622. while (--back) {
  623. style = styler.StyleAt(back);
  624. if (style < SCE_HJ_DEFAULT || style > SCE_HJ_COMMENTDOC)
  625. // includes SCE_HJ_COMMENT & SCE_HJ_COMMENTLINE
  626. break;
  627. }
  628. if (style == SCE_HJ_SYMBOLS) {
  629. chPrevNonWhite = static_cast<unsigned char>(styler.SafeGetCharAt(back));
  630. }
  631. }
  632. styler.StartSegment(startPos);
  633. const int lengthDoc = startPos + length;
  634. for (int i = startPos; i < lengthDoc; i++) {
  635. const int chPrev2 = chPrev;
  636. chPrev = ch;
  637. if (!IsASpace(ch) && state != SCE_HJ_COMMENT &&
  638. state != SCE_HJ_COMMENTLINE && state != SCE_HJ_COMMENTDOC)
  639. chPrevNonWhite = ch;
  640. ch = static_cast<unsigned char>(styler[i]);
  641. int chNext = static_cast<unsigned char>(styler.SafeGetCharAt(i + 1));
  642. const int chNext2 = static_cast<unsigned char>(styler.SafeGetCharAt(i + 2));
  643. // Handle DBCS codepages
  644. if (styler.IsLeadByte(static_cast<char>(ch))) {
  645. chPrev = ' ';
  646. i += 1;
  647. continue;
  648. }
  649. if ((!IsASpace(ch) || !foldCompact) && fold)
  650. visibleChars++;
  651. if (!IsASpace(ch))
  652. lineStartVisibleChars++;
  653. // decide what is the current state to print (depending of the script tag)
  654. StateToPrint = statePrintForState(state, inScriptType);
  655. // handle script folding
  656. if (fold) {
  657. switch (scriptLanguage) {
  658. case eScriptJS:
  659. case eScriptPHP:
  660. //not currently supported case eScriptVBS:
  661. if ((state != SCE_HPHP_COMMENT) && (state != SCE_HPHP_COMMENTLINE) && (state != SCE_HJ_COMMENT) && (state != SCE_HJ_COMMENTLINE) && (state != SCE_HJ_COMMENTDOC) && (!isStringState(state))) {
  662. //Platform::DebugPrintf("state=%d, StateToPrint=%d, initStyle=%d\n", state, StateToPrint, initStyle);
  663. //if ((state == SCE_HPHP_OPERATOR) || (state == SCE_HPHP_DEFAULT) || (state == SCE_HJ_SYMBOLS) || (state == SCE_HJ_START) || (state == SCE_HJ_DEFAULT)) {
  664. if (ch == '#') {
  665. int j = i + 1;
  666. while ((j < lengthDoc) && IsASpaceOrTab(styler.SafeGetCharAt(j))) {
  667. j++;
  668. }
  669. if (styler.Match(j, "region") || styler.Match(j, "if")) {
  670. levelCurrent++;
  671. } else if (styler.Match(j, "end")) {
  672. levelCurrent--;
  673. }
  674. } else if ((ch == '{') || (ch == '}') || (foldComment && (ch == '/') && (chNext == '*'))) {
  675. levelCurrent += (((ch == '{') || (ch == '/')) ? 1 : -1);
  676. }
  677. } else if (((state == SCE_HPHP_COMMENT) || (state == SCE_HJ_COMMENT)) && foldComment && (ch == '*') && (chNext == '/')) {
  678. levelCurrent--;
  679. }
  680. break;
  681. case eScriptPython:
  682. if (state != SCE_HP_COMMENTLINE && !isMako) {
  683. if ((ch == ':') && ((chNext == '\n') || (chNext == '\r' && chNext2 == '\n'))) {
  684. levelCurrent++;
  685. } else if ((ch == '\n') && !((chNext == '\r') && (chNext2 == '\n')) && (chNext != '\n')) {
  686. // check if the number of tabs is lower than the level
  687. int Findlevel = (levelCurrent & ~SC_FOLDLEVELBASE) * 8;
  688. for (int j = 0; Findlevel > 0; j++) {
  689. char chTmp = styler.SafeGetCharAt(i + j + 1);
  690. if (chTmp == '\t') {
  691. Findlevel -= 8;
  692. } else if (chTmp == ' ') {
  693. Findlevel--;
  694. } else {
  695. break;
  696. }
  697. }
  698. if (Findlevel > 0) {
  699. levelCurrent -= Findlevel / 8;
  700. if (Findlevel % 8)
  701. levelCurrent--;
  702. }
  703. }
  704. }
  705. break;
  706. default:
  707. break;
  708. }
  709. }
  710. if ((ch == '\r' && chNext != '\n') || (ch == '\n')) {
  711. // Trigger on CR only (Mac style) or either on LF from CR+LF (Dos/Win) or on LF alone (Unix)
  712. // Avoid triggering two times on Dos/Win
  713. // New line -> record any line state onto /next/ line
  714. if (fold) {
  715. int lev = levelPrev;
  716. if (visibleChars == 0)
  717. lev |= SC_FOLDLEVELWHITEFLAG;
  718. if ((levelCurrent > levelPrev) && (visibleChars > 0))
  719. lev |= SC_FOLDLEVELHEADERFLAG;
  720. styler.SetLevel(lineCurrent, lev);
  721. visibleChars = 0;
  722. levelPrev = levelCurrent;
  723. }
  724. styler.SetLineState(lineCurrent,
  725. ((inScriptType & 0x03) << 0) |
  726. ((tagOpened ? 1 : 0) << 2) |
  727. ((tagClosing ? 1 : 0) << 3) |
  728. ((aspScript & 0x0F) << 4) |
  729. ((clientScript & 0x0F) << 8) |
  730. ((beforePreProc & 0xFF) << 12));
  731. lineCurrent++;
  732. lineStartVisibleChars = 0;
  733. }
  734. // handle start of Mako comment line
  735. if (isMako && ch == '#' && chNext == '#') {
  736. makoComment = 1;
  737. state = SCE_HP_COMMENTLINE;
  738. }
  739. // handle end of Mako comment line
  740. else if (isMako && makoComment && (ch == '\r' || ch == '\n')) {
  741. makoComment = 0;
  742. styler.ColourTo(i, StateToPrint);
  743. if (scriptLanguage == eScriptPython) {
  744. state = SCE_HP_DEFAULT;
  745. } else {
  746. state = SCE_H_DEFAULT;
  747. }
  748. }
  749. // Allow falling through to mako handling code if newline is going to end a block
  750. if (((ch == '\r' && chNext != '\n') || (ch == '\n')) &&
  751. (!isMako || (0 != strcmp(makoBlockType, "%")))) {
  752. }
  753. // Ignore everything in mako comment until the line ends
  754. else if (isMako && makoComment) {
  755. }
  756. // generic end of script processing
  757. else if ((inScriptType == eNonHtmlScript) && (ch == '<') && (chNext == '/')) {
  758. // Check if it's the end of the script tag (or any other HTML tag)
  759. switch (state) {
  760. // in these cases, you can embed HTML tags (to confirm !!!!!!!!!!!!!!!!!!!!!!)
  761. case SCE_H_DOUBLESTRING:
  762. case SCE_H_SINGLESTRING:
  763. case SCE_HJ_COMMENT:
  764. case SCE_HJ_COMMENTDOC:
  765. //case SCE_HJ_COMMENTLINE: // removed as this is a common thing done to hide
  766. // the end of script marker from some JS interpreters.
  767. case SCE_HB_COMMENTLINE:
  768. case SCE_HBA_COMMENTLINE:
  769. case SCE_HJ_DOUBLESTRING:
  770. case SCE_HJ_SINGLESTRING:
  771. case SCE_HJ_REGEX:
  772. case SCE_HB_STRING:
  773. case SCE_HBA_STRING:
  774. case SCE_HP_STRING:
  775. case SCE_HP_TRIPLE:
  776. case SCE_HP_TRIPLEDOUBLE:
  777. case SCE_HPHP_HSTRING:
  778. case SCE_HPHP_SIMPLESTRING:
  779. case SCE_HPHP_COMMENT:
  780. case SCE_HPHP_COMMENTLINE:
  781. break;
  782. default :
  783. // check if the closing tag is a script tag
  784. if (const char *tag =
  785. state == SCE_HJ_COMMENTLINE || isXml ? "script" :
  786. state == SCE_H_COMMENT ? "comment" : 0) {
  787. int j = i + 2;
  788. int chr;
  789. do {
  790. chr = static_cast<int>(*tag++);
  791. } while (chr != 0 && chr == MakeLowerCase(styler.SafeGetCharAt(j++)));
  792. if (chr != 0) break;
  793. }
  794. // closing tag of the script (it's a closing HTML tag anyway)
  795. styler.ColourTo(i - 1, StateToPrint);
  796. state = SCE_H_TAGUNKNOWN;
  797. inScriptType = eHtml;
  798. scriptLanguage = eScriptNone;
  799. clientScript = eScriptJS;
  800. i += 2;
  801. visibleChars += 2;
  802. tagClosing = true;
  803. continue;
  804. }
  805. }
  806. /////////////////////////////////////
  807. // handle the start of PHP pre-processor = Non-HTML
  808. else if ((state != SCE_H_ASPAT) &&
  809. !isPHPStringState(state) &&
  810. (state != SCE_HPHP_COMMENT) &&
  811. (state != SCE_HPHP_COMMENTLINE) &&
  812. (ch == '<') &&
  813. (chNext == '?') &&
  814. !IsScriptCommentState(state)) {
  815. beforeLanguage = scriptLanguage;
  816. scriptLanguage = segIsScriptingIndicator(styler, i + 2, i + 6, isXml ? eScriptXML : eScriptPHP);
  817. if ((scriptLanguage != eScriptPHP) && (isStringState(state) || (state==SCE_H_COMMENT))) continue;
  818. styler.ColourTo(i - 1, StateToPrint);
  819. beforePreProc = state;
  820. i++;
  821. visibleChars++;
  822. i += PrintScriptingIndicatorOffset(styler, styler.GetStartSegment() + 2, i + 6);
  823. if (scriptLanguage == eScriptXML)
  824. styler.ColourTo(i, SCE_H_XMLSTART);
  825. else
  826. styler.ColourTo(i, SCE_H_QUESTION);
  827. state = StateForScript(scriptLanguage);
  828. if (inScriptType == eNonHtmlScript)
  829. inScriptType = eNonHtmlScriptPreProc;
  830. else
  831. inScriptType = eNonHtmlPreProc;
  832. // Fold whole script, but not if the XML first tag (all XML-like tags in this case)
  833. if (foldHTMLPreprocessor && (scriptLanguage != eScriptXML)) {
  834. levelCurrent++;
  835. }
  836. // should be better
  837. ch = static_cast<unsigned char>(styler.SafeGetCharAt(i));
  838. continue;
  839. }
  840. // handle the start Mako template Python code
  841. else if (isMako && scriptLanguage == eScriptNone && ((ch == '<' && chNext == '%') ||
  842. (lineStartVisibleChars == 1 && ch == '%') ||
  843. (lineStartVisibleChars == 1 && ch == '/' && chNext == '%') ||
  844. (ch == '$' && chNext == '{') ||
  845. (ch == '<' && chNext == '/' && chNext2 == '%'))) {
  846. if (ch == '%' || ch == '/')
  847. StringCopy(makoBlockType, "%");
  848. else if (ch == '$')
  849. StringCopy(makoBlockType, "{");
  850. else if (chNext == '/')
  851. GetNextWord(styler, i+3, makoBlockType, sizeof(makoBlockType));
  852. else
  853. GetNextWord(styler, i+2, makoBlockType, sizeof(makoBlockType));
  854. styler.ColourTo(i - 1, StateToPrint);
  855. beforePreProc = state;
  856. if (inScriptType == eNonHtmlScript)
  857. inScriptType = eNonHtmlScriptPreProc;
  858. else
  859. inScriptType = eNonHtmlPreProc;
  860. if (chNext == '/') {
  861. i += 2;
  862. visibleChars += 2;
  863. } else if (ch != '%') {
  864. i++;
  865. visibleChars++;
  866. }
  867. state = SCE_HP_START;
  868. scriptLanguage = eScriptPython;
  869. styler.ColourTo(i, SCE_H_ASP);
  870. if (ch != '%' && ch != '$' && ch != '/') {
  871. i += static_cast<int>(strlen(makoBlockType));
  872. visibleChars += static_cast<int>(strlen(makoBlockType));
  873. if (keywords4.InList(makoBlockType))
  874. styler.ColourTo(i, SCE_HP_WORD);
  875. else
  876. styler.ColourTo(i, SCE_H_TAGUNKNOWN);
  877. }
  878. ch = static_cast<unsigned char>(styler.SafeGetCharAt(i));
  879. continue;
  880. }
  881. // handle the start/end of Django comment
  882. else if (isDjango && state != SCE_H_COMMENT && (ch == '{' && chNext == '#')) {
  883. styler.ColourTo(i - 1, StateToPrint);
  884. beforePreProc = state;
  885. beforeLanguage = scriptLanguage;
  886. if (inScriptType == eNonHtmlScript)
  887. inScriptType = eNonHtmlScriptPreProc;
  888. else
  889. inScriptType = eNonHtmlPreProc;
  890. i += 1;
  891. visibleChars += 1;
  892. scriptLanguage = eScriptComment;
  893. state = SCE_H_COMMENT;
  894. styler.ColourTo(i, SCE_H_ASP);
  895. ch = static_cast<unsigned char>(styler.SafeGetCharAt(i));
  896. continue;
  897. } else if (isDjango && state == SCE_H_COMMENT && (ch == '#' && chNext == '}')) {
  898. styler.ColourTo(i - 1, StateToPrint);
  899. i += 1;
  900. visibleChars += 1;
  901. styler.ColourTo(i, SCE_H_ASP);
  902. state = beforePreProc;
  903. if (inScriptType == eNonHtmlScriptPreProc)
  904. inScriptType = eNonHtmlScript;
  905. else
  906. inScriptType = eHtml;
  907. scriptLanguage = beforeLanguage;
  908. continue;
  909. }
  910. // handle the start Django template code
  911. else if (isDjango && scriptLanguage != eScriptPython && (ch == '{' && (chNext == '%' || chNext == '{'))) {
  912. if (chNext == '%')
  913. StringCopy(djangoBlockType, "%");
  914. else
  915. StringCopy(djangoBlockType, "{");
  916. styler.ColourTo(i - 1, StateToPrint);
  917. beforePreProc = state;
  918. if (inScriptType == eNonHtmlScript)
  919. inScriptType = eNonHtmlScriptPreProc;
  920. else
  921. inScriptType = eNonHtmlPreProc;
  922. i += 1;
  923. visibleChars += 1;
  924. state = SCE_HP_START;
  925. beforeLanguage = scriptLanguage;
  926. scriptLanguage = eScriptPython;
  927. styler.ColourTo(i, SCE_H_ASP);
  928. ch = static_cast<unsigned char>(styler.SafeGetCharAt(i));
  929. continue;
  930. }
  931. // handle the start of ASP pre-processor = Non-HTML
  932. else if (!isMako && !isDjango && !isCommentASPState(state) && (ch == '<') && (chNext == '%') && !isPHPStringState(state)) {
  933. styler.ColourTo(i - 1, StateToPrint);
  934. beforePreProc = state;
  935. if (inScriptType == eNonHtmlScript)
  936. inScriptType = eNonHtmlScriptPreProc;
  937. else
  938. inScriptType = eNonHtmlPreProc;
  939. if (chNext2 == '@') {
  940. i += 2; // place as if it was the second next char treated
  941. visibleChars += 2;
  942. state = SCE_H_ASPAT;
  943. } else if ((chNext2 == '-') && (styler.SafeGetCharAt(i + 3) == '-')) {
  944. styler.ColourTo(i + 3, SCE_H_ASP);
  945. state = SCE_H_XCCOMMENT;
  946. scriptLanguage = eScriptVBS;
  947. continue;
  948. } else {
  949. if (chNext2 == '=') {
  950. i += 2; // place as if it was the second next char treated
  951. visibleChars += 2;
  952. } else {
  953. i++; // place as if it was the next char treated
  954. visibleChars++;
  955. }
  956. state = StateForScript(aspScript);
  957. }
  958. scriptLanguage = eScriptVBS;
  959. styler.ColourTo(i, SCE_H_ASP);
  960. // fold whole script
  961. if (foldHTMLPreprocessor)
  962. levelCurrent++;
  963. // should be better
  964. ch = static_cast<unsigned char>(styler.SafeGetCharAt(i));
  965. continue;
  966. }
  967. /////////////////////////////////////
  968. // handle the start of SGML language (DTD)
  969. else if (((scriptLanguage == eScriptNone) || (scriptLanguage == eScriptXML)) &&
  970. (chPrev == '<') &&
  971. (ch == '!') &&
  972. (StateToPrint != SCE_H_CDATA) &&
  973. (!IsCommentState(StateToPrint)) &&
  974. (!IsScriptCommentState(StateToPrint))) {
  975. beforePreProc = state;
  976. styler.ColourTo(i - 2, StateToPrint);
  977. if ((chNext == '-') && (chNext2 == '-')) {
  978. state = SCE_H_COMMENT; // wait for a pending command
  979. styler.ColourTo(i + 2, SCE_H_COMMENT);
  980. i += 2; // follow styling after the --
  981. } else if (isWordCdata(i + 1, i + 7, styler)) {
  982. state = SCE_H_CDATA;
  983. } else {
  984. styler.ColourTo(i, SCE_H_SGML_DEFAULT); // <! is default
  985. scriptLanguage = eScriptSGML;
  986. state = SCE_H_SGML_COMMAND; // wait for a pending command
  987. }
  988. // fold whole tag (-- when closing the tag)
  989. if (foldHTMLPreprocessor || state == SCE_H_COMMENT || state == SCE_H_CDATA)
  990. levelCurrent++;
  991. continue;
  992. }
  993. // handle the end of Mako Python code
  994. else if (isMako &&
  995. ((inScriptType == eNonHtmlPreProc) || (inScriptType == eNonHtmlScriptPreProc)) &&
  996. (scriptLanguage != eScriptNone) && stateAllowsTermination(state) &&
  997. isMakoBlockEnd(ch, chNext, makoBlockType)) {
  998. if (state == SCE_H_ASPAT) {
  999. aspScript = segIsScriptingIndicator(styler,
  1000. styler.GetStartSegment(), i - 1, aspScript);
  1001. }
  1002. if (state == SCE_HP_WORD) {
  1003. classifyWordHTPy(styler.GetStartSegment(), i - 1, keywords4, styler, prevWord, inScriptType, isMako);
  1004. } else {
  1005. styler.ColourTo(i - 1, StateToPrint);
  1006. }
  1007. if (0 != strcmp(makoBlockType, "%") && (0 != strcmp(makoBlockType, "{")) && ch != '>') {
  1008. i++;
  1009. visibleChars++;
  1010. }
  1011. else if (0 == strcmp(makoBlockType, "%") && ch == '/') {
  1012. i++;
  1013. visibleChars++;
  1014. }
  1015. if (0 != strcmp(makoBlockType, "%") || ch == '/') {
  1016. styler.ColourTo(i, SCE_H_ASP);
  1017. }
  1018. state = beforePreProc;
  1019. if (inScriptType == eNonHtmlScriptPreProc)
  1020. inScriptType = eNonHtmlScript;
  1021. else
  1022. inScriptType = eHtml;
  1023. scriptLanguage = eScriptNone;
  1024. continue;
  1025. }
  1026. // handle the end of Django template code
  1027. else if (isDjango &&
  1028. ((inScriptType == eNonHtmlPreProc) || (inScriptType == eNonHtmlScriptPreProc)) &&
  1029. (scriptLanguage != eScriptNone) && stateAllowsTermination(state) &&
  1030. isDjangoBlockEnd(ch, chNext, djangoBlockType)) {
  1031. if (state == SCE_H_ASPAT) {
  1032. aspScript = segIsScriptingIndicator(styler,
  1033. styler.GetStartSegment(), i - 1, aspScript);
  1034. }
  1035. if (state == SCE_HP_WORD) {
  1036. classifyWordHTPy(styler.GetStartSegment(), i - 1, keywords4, styler, prevWord, inScriptType, isMako);
  1037. } else {
  1038. styler.ColourTo(i - 1, StateToPrint);
  1039. }
  1040. i += 1;
  1041. visibleChars += 1;
  1042. styler.ColourTo(i, SCE_H_ASP);
  1043. state = beforePreProc;
  1044. if (inScriptType == eNonHtmlScriptPreProc)
  1045. inScriptType = eNonHtmlScript;
  1046. else
  1047. inScriptType = eHtml;
  1048. scriptLanguage = beforeLanguage;
  1049. continue;
  1050. }
  1051. // handle the end of a pre-processor = Non-HTML
  1052. else if ((!isMako && !isDjango && ((inScriptType == eNonHtmlPreProc) || (inScriptType == eNonHtmlScriptPreProc)) &&
  1053. (((scriptLanguage != eScriptNone) && stateAllowsTermination(state))) &&
  1054. (((ch == '%') || (ch == '?')) && (chNext == '>'))) ||
  1055. ((scriptLanguage == eScriptSGML) && (ch == '>') && (state != SCE_H_SGML_COMMENT))) {
  1056. if (state == SCE_H_ASPAT) {
  1057. aspScript = segIsScriptingIndicator(styler,
  1058. styler.GetStartSegment(), i - 1, aspScript);
  1059. }
  1060. // Bounce out of any ASP mode
  1061. switch (state) {
  1062. case SCE_HJ_WORD:
  1063. classifyWordHTJS(styler.GetStartSegment(), i - 1, keywords2, styler, inScriptType);
  1064. break;
  1065. case SCE_HB_WORD:
  1066. classifyWordHTVB(styler.GetStartSegment(), i - 1, keywords3, styler, inScriptType);
  1067. break;
  1068. case SCE_HP_WORD:
  1069. classifyWordHTPy(styler.GetStartSegment(), i - 1, keywords4, styler, prevWord, inScriptType, isMako);
  1070. break;
  1071. case SCE_HPHP_WORD:
  1072. classifyWordHTPHP(styler.GetStartSegment(), i - 1, keywords5, styler);
  1073. break;
  1074. case SCE_H_XCCOMMENT:
  1075. styler.ColourTo(i - 1, state);
  1076. break;
  1077. default :
  1078. styler.ColourTo(i - 1, StateToPrint);
  1079. break;
  1080. }
  1081. if (scriptLanguage != eScriptSGML) {
  1082. i++;
  1083. visibleChars++;
  1084. }
  1085. if (ch == '%')
  1086. styler.ColourTo(i, SCE_H_ASP);
  1087. else if (scriptLanguage == eScriptXML)
  1088. styler.ColourTo(i, SCE_H_XMLEND);
  1089. else if (scriptLanguage == eScriptSGML)
  1090. styler.ColourTo(i, SCE_H_SGML_DEFAULT);
  1091. else
  1092. styler.ColourTo(i, SCE_H_QUESTION);
  1093. state = beforePreProc;
  1094. if (inScriptType == eNonHtmlScriptPreProc)
  1095. inScriptType = eNonHtmlScript;
  1096. else
  1097. inScriptType = eHtml;
  1098. // Unfold all scripting languages, except for XML tag
  1099. if (foldHTMLPreprocessor && (scriptLanguage != eScriptXML)) {
  1100. levelCurrent--;
  1101. }
  1102. scriptLanguage = beforeLanguage;
  1103. continue;
  1104. }
  1105. /////////////////////////////////////
  1106. switch (state) {
  1107. case SCE_H_DEFAULT:
  1108. if (ch == '<') {
  1109. // in HTML, fold on tag open and unfold on tag close
  1110. tagOpened = true;
  1111. tagClosing = (chNext == '/');
  1112. styler.ColourTo(i - 1, StateToPrint);
  1113. if (chNext != '!')
  1114. state = SCE_H_TAGUNKNOWN;
  1115. } else if (ch == '&') {
  1116. styler.ColourTo(i - 1, SCE_H_DEFAULT);
  1117. state = SCE_H_ENTITY;
  1118. }
  1119. break;
  1120. case SCE_H_SGML_DEFAULT:
  1121. case SCE_H_SGML_BLOCK_DEFAULT:
  1122. // if (scriptLanguage == eScriptSGMLblock)
  1123. // StateToPrint = SCE_H_SGML_BLOCK_DEFAULT;
  1124. if (ch == '\"') {
  1125. styler.ColourTo(i - 1, StateToPrint);
  1126. state = SCE_H_SGML_DOUBLESTRING;
  1127. } else if (ch == '\'') {
  1128. styler.ColourTo(i - 1, StateToPrint);
  1129. state = SCE_H_SGML_SIMPLESTRING;
  1130. } else if ((ch == '-') && (chPrev == '-')) {
  1131. if (static_cast<int>(styler.GetStartSegment()) <= (i - 2)) {
  1132. styler.ColourTo(i - 2, StateToPrint);
  1133. }
  1134. state = SCE_H_SGML_COMMENT;
  1135. } else if (IsASCII(ch) && isalpha(ch) && (chPrev == '%')) {
  1136. styler.ColourTo(i - 2, StateToPrint);
  1137. state = SCE_H_SGML_ENTITY;
  1138. } else if (ch == '#') {
  1139. styler.ColourTo(i - 1, StateToPrint);
  1140. state = SCE_H_SGML_SPECIAL;
  1141. } else if (ch == '[') {
  1142. styler.ColourTo(i - 1, StateToPrint);
  1143. scriptLanguage = eScriptSGMLblock;
  1144. state = SCE_H_SGML_BLOCK_DEFAULT;
  1145. } else if (ch == ']') {
  1146. if (scriptLanguage == eScriptSGMLblock) {
  1147. styler.ColourTo(i, StateToPrint);
  1148. scriptLanguage = eScriptSGML;
  1149. } else {
  1150. styler.ColourTo(i - 1, StateToPrint);
  1151. styler.ColourTo(i, SCE_H_SGML_ERROR);
  1152. }
  1153. state = SCE_H_SGML_DEFAULT;
  1154. } else if (scriptLanguage == eScriptSGMLblock) {
  1155. if ((ch == '!') && (chPrev == '<')) {
  1156. styler.ColourTo(i - 2, StateToPrint);
  1157. styler.ColourTo(i, SCE_H_SGML_DEFAULT);
  1158. state = SCE_H_SGML_COMMAND;
  1159. } else if (ch == '>') {
  1160. styler.ColourTo(i - 1, StateToPrint);
  1161. styler.ColourTo(i, SCE_H_SGML_DEFAULT);
  1162. }
  1163. }
  1164. break;
  1165. case SCE_H_SGML_COMMAND:
  1166. if ((ch == '-') && (chPrev == '-')) {
  1167. styler.ColourTo(i - 2, StateToPrint);
  1168. state = SCE_H_SGML_COMMENT;
  1169. } else if (!issgmlwordchar(ch)) {
  1170. if (isWordHSGML(styler.GetStartSegment(), i - 1, keywords6, styler)) {
  1171. styler.ColourTo(i - 1, StateToPrint);
  1172. state = SCE_H_SGML_1ST_PARAM;
  1173. } else {
  1174. state = SCE_H_SGML_ERROR;
  1175. }
  1176. }
  1177. break;
  1178. case SCE_H_SGML_1ST_PARAM:
  1179. // wait for the beginning of the word
  1180. if ((ch == '-') && (chPrev == '-')) {
  1181. if (scriptLanguage == eScriptSGMLblock) {
  1182. styler.ColourTo(i - 2, SCE_H_SGML_BLOCK_DEFAULT);
  1183. } else {
  1184. styler.ColourTo(i - 2, SCE_H_SGML_DEFAULT);
  1185. }
  1186. state = SCE_H_SGML_1ST_PARAM_COMMENT;
  1187. } else if (issgmlwordchar(ch)) {
  1188. if (scriptLanguage == eScriptSGMLblock) {
  1189. styler.ColourTo(i - 1, SCE_H_SGML_BLOCK_DEFAULT);
  1190. } else {
  1191. styler.ColourTo(i - 1, SCE_H_SGML_DEFAULT);
  1192. }
  1193. // find the length of the word
  1194. int size = 1;
  1195. while (setHTMLWord.Contains(static_cast<unsigned char>(styler.SafeGetCharAt(i + size))))
  1196. size++;
  1197. styler.ColourTo(i + size - 1, StateToPrint);
  1198. i += size - 1;
  1199. visibleChars += size - 1;
  1200. ch = static_cast<unsigned char>(styler.SafeGetCharAt(i));
  1201. if (scriptLanguage == eScriptSGMLblock) {
  1202. state = SCE_H_SGML_BLOCK_DEFAULT;
  1203. } else {
  1204. state = SCE_H_SGML_DEFAULT;
  1205. }
  1206. continue;
  1207. }
  1208. break;
  1209. case SCE_H_SGML_ERROR:
  1210. if ((ch == '-') && (chPrev == '-')) {
  1211. styler.ColourTo(i - 2, StateToPrint);
  1212. state = SCE_H_SGML_COMMENT;
  1213. }
  1214. break;
  1215. case SCE_H_SGML_DOUBLESTRING:
  1216. if (ch == '\"') {
  1217. styler.ColourTo(i, StateToPrint);
  1218. state = SCE_H_SGML_DEFAULT;
  1219. }
  1220. break;
  1221. case SCE_H_SGML_SIMPLESTRING:
  1222. if (ch == '\'') {
  1223. styler.ColourTo(i, StateToPrint);
  1224. state = SCE_H_SGML_DEFAULT;
  1225. }
  1226. break;
  1227. case SCE_H_SGML_COMMENT:
  1228. if ((ch == '-') && (chPrev == '-')) {
  1229. styler.ColourTo(i, StateToPrint);
  1230. state = SCE_H_SGML_DEFAULT;
  1231. }
  1232. break;
  1233. case SCE_H_CDATA:
  1234. if ((chPrev2 == ']') && (chPrev == ']') && (ch == '>')) {
  1235. styler.ColourTo(i, StateToPrint);
  1236. state = SCE_H_DEFAULT;
  1237. levelCurrent--;
  1238. }
  1239. break;
  1240. case SCE_H_COMMENT:
  1241. if ((scriptLanguage != eScriptComment) && (chPrev2 == '-') && (chPrev == '-') && (ch == '>')) {
  1242. styler.ColourTo(i, StateToPrint);
  1243. state = SCE_H_DEFAULT;
  1244. levelCurrent--;
  1245. }
  1246. break;
  1247. case SCE_H_SGML_1ST_PARAM_COMMENT:
  1248. if ((ch == '-') && (chPrev == '-')) {
  1249. styler.ColourTo(i, SCE_H_SGML_COMMENT);
  1250. state = SCE_H_SGML_1ST_PARAM;
  1251. }
  1252. break;
  1253. case SCE_H_SGML_SPECIAL:
  1254. if (!(IsASCII(ch) && isupper(ch))) {
  1255. styler.ColourTo(i - 1, StateToPrint);
  1256. if (isalnum(ch)) {
  1257. state = SCE_H_SGML_ERROR;
  1258. } else {
  1259. state = SCE_H_SGML_DEFAULT;
  1260. }
  1261. }
  1262. break;
  1263. case SCE_H_SGML_ENTITY:
  1264. if (ch == ';') {
  1265. styler.ColourTo(i, StateToPrint);
  1266. state = SCE_H_SGML_DEFAULT;
  1267. } else if (!(IsASCII(ch) && isalnum(ch)) && ch != '-' && ch != '.') {
  1268. styler.ColourTo(i, SCE_H_SGML_ERROR);
  1269. state = SCE_H_SGML_DEFAULT;
  1270. }
  1271. break;
  1272. case SCE_H_ENTITY:
  1273. if (ch == ';') {
  1274. styler.ColourTo(i, StateToPrint);
  1275. state = SCE_H_DEFAULT;
  1276. }
  1277. if (ch != '#' && !(IsASCII(ch) && isalnum(ch)) // Should check that '#' follows '&', but it is unlikely anyway...
  1278. && ch != '.' && ch != '-' && ch != '_' && ch != ':') { // valid in XML
  1279. if (!IsASCII(ch)) // Possibly start of a multibyte character so don't allow this byte to be in entity style
  1280. styler.ColourTo(i-1, SCE_H_TAGUNKNOWN);
  1281. else
  1282. styler.ColourTo(i, SCE_H_TAGUNKNOWN);
  1283. state = SCE_H_DEFAULT;
  1284. }
  1285. break;
  1286. case SCE_H_TAGUNKNOWN:
  1287. if (!setTagContinue.Contains(ch) && !((ch == '/') && (chPrev == '<'))) {
  1288. int eClass = classifyTagHTML(styler.GetStartSegment(),
  1289. i - 1, keywords, styler, tagDontFold, caseSensitive, isXml, allowScripts);
  1290. if (eClass == SCE_H_SCRIPT || eClass == SCE_H_COMMENT) {
  1291. if (!tagClosing) {
  1292. inScriptType = eNonHtmlScript;
  1293. scriptLanguage = eClass == SCE_H_SCRIPT ? clientScript : eScriptComment;
  1294. } else {
  1295. scriptLanguage = eScriptNone;
  1296. }
  1297. eClass = SCE_H_TAG;
  1298. }
  1299. if (ch == '>') {
  1300. styler.ColourTo(i, eClass);
  1301. if (inScriptType == eNonHtmlScript) {
  1302. state = StateForScript(scriptLanguage);
  1303. } else {
  1304. state = SCE_H_DEFAULT;
  1305. }
  1306. tagOpened = false;
  1307. if (!tagDontFold) {
  1308. if (tagClosing) {
  1309. levelCurrent--;
  1310. } else {
  1311. levelCurrent++;
  1312. }
  1313. }
  1314. tagClosing = false;
  1315. } else if (ch == '/' && chNext == '>') {
  1316. if (eClass == SCE_H_TAGUNKNOWN) {
  1317. styler.ColourTo(i + 1, SCE_H_TAGUNKNOWN);
  1318. } else {
  1319. styler.ColourTo(i - 1, StateToPrint);
  1320. styler.ColourTo(i + 1, SCE_H_TAGEND);
  1321. }
  1322. i++;
  1323. ch = chNext;
  1324. state = SCE_H_DEFAULT;
  1325. tagOpened = false;
  1326. } else {
  1327. if (eClass != SCE_H_TAGUNKNOWN) {
  1328. if (eClass == SCE_H_SGML_DEFAULT) {
  1329. state = SCE_H_SGML_DEFAULT;
  1330. } else {
  1331. state = SCE_H_OTHER;
  1332. }
  1333. }
  1334. }
  1335. }
  1336. break;
  1337. case SCE_H_ATTRIBUTE:
  1338. if (!setAttributeContinue.Contains(ch)) {
  1339. if (inScriptType == eNonHtmlScript) {
  1340. int scriptLanguagePrev = scriptLanguage;
  1341. clientScript = segIsScriptingIndicator(styler, styler.GetStartSegment(), i - 1, scriptLanguage);
  1342. scriptLanguage = clientScript;
  1343. if ((scriptLanguagePrev != scriptLanguage) && (scriptLanguage == eScriptNone))
  1344. inScriptType = eHtml;
  1345. }
  1346. classifyAttribHTML(styler.GetStartSegment(), i - 1, keywords, styler);
  1347. if (ch == '>') {
  1348. styler.ColourTo(i, SCE_H_TAG);
  1349. if (inScriptType == eNonHtmlScript) {
  1350. state = StateForScript(scriptLanguage);
  1351. } else {
  1352. state = SCE_H_DEFAULT;
  1353. }
  1354. tagOpened = false;
  1355. if (!tagDontFold) {
  1356. if (tagClosing) {
  1357. levelCurrent--;
  1358. } else {
  1359. levelCurrent++;
  1360. }
  1361. }
  1362. tagClosing = false;
  1363. } else if (ch == '=') {
  1364. styler.ColourTo(i, SCE_H_OTHER);
  1365. state = SCE_H_VALUE;
  1366. } else {
  1367. state = SCE_H_OTHER;
  1368. }
  1369. }
  1370. break;
  1371. case SCE_H_OTHER:
  1372. if (ch == '>') {
  1373. styler.ColourTo(i - 1, StateToPrint);
  1374. styler.ColourTo(i, SCE_H_TAG);
  1375. if (inScriptType == eNonHtmlScript) {
  1376. state = StateForScript(scriptLanguage);
  1377. } else {
  1378. state = SCE_H_DEFAULT;
  1379. }
  1380. tagOpened = false;
  1381. if (!tagDontFold) {
  1382. if (tagClosing) {
  1383. levelCurrent--;
  1384. } else {
  1385. levelCurrent++;
  1386. }
  1387. }
  1388. tagClosing = false;
  1389. } else if (ch == '\"') {
  1390. styler.ColourTo(i - 1, StateToPrint);
  1391. state = SCE_H_DOUBLESTRING;
  1392. } else if (ch == '\'') {
  1393. styler.ColourTo(i - 1, StateToPrint);
  1394. state = SCE_H_SINGLESTRING;
  1395. } else if (ch == '=') {
  1396. styler.ColourTo(i, StateToPrint);
  1397. state = SCE_H_VALUE;
  1398. } else if (ch == '/' && chNext == '>') {
  1399. styler.ColourTo(i - 1, StateToPrint);
  1400. styler.ColourTo(i + 1, SCE_H_TAGEND);
  1401. i++;
  1402. ch = chNext;
  1403. state = SCE_H_DEFAULT;
  1404. tagOpened = false;
  1405. } else if (ch == '?' && chNext == '>') {
  1406. styler.ColourTo(i - 1, StateToPrint);
  1407. styler.ColourTo(i + 1, SCE_H_XMLEND);
  1408. i++;
  1409. ch = chNext;
  1410. state = SCE_H_DEFAULT;
  1411. } else if (setHTMLWord.Contains(ch)) {
  1412. styler.ColourTo(i - 1, StateToPrint);
  1413. state = SCE_H_ATTRIBUTE;
  1414. }
  1415. break;
  1416. case SCE_H_DOUBLESTRING:
  1417. if (ch == '\"') {
  1418. if (inScriptType == eNonHtmlS

Large files files are truncated, but you can click here to view the full file