PageRenderTime 68ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/lexers/LexHTML.cxx

https://bitbucket.org/kpozn/scintilla
C++ | 2190 lines | 1956 code | 121 blank | 113 comment | 1460 complexity | 4bf6ed7a874be7d7e29f9b6460f88291 MD5 | raw file

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

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