2
0

demo_css.pp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. program test_css;
  2. {$mode objfpc}{$H+}
  3. uses
  4. SysUtils, Classes, syntax.highlighter, syntax.css;
  5. procedure TestCssAtRules;
  6. const
  7. CssAtRules: array[0..9] of string = (
  8. '@charset', '@import', '@media', '@keyframes', '@font-face',
  9. '@supports', '@page', '@namespace', '@viewport', '@layer'
  10. );
  11. var
  12. highlighter: TCssSyntaxHighlighter;
  13. tokens: TSyntaxTokenArray;
  14. i: Integer;
  15. begin
  16. WriteLn('Testing CSS At-Rules:');
  17. WriteLn('====================');
  18. highlighter := TCssSyntaxHighlighter.Create;
  19. try
  20. for i := 0 to High(CssAtRules) do begin
  21. tokens := highlighter.Execute(CssAtRules[i]);
  22. if (Length(tokens) = 1) and (tokens[0].Kind = shDirective) then
  23. WriteLn(CssAtRules[i] + ': PASS - recognized as directive')
  24. else
  25. WriteLn(CssAtRules[i] + ': FAIL - kind=' + IntToStr(Ord(tokens[0].Kind)));
  26. end;
  27. finally
  28. highlighter.Free;
  29. end;
  30. WriteLn;
  31. end;
  32. procedure TestCssProperties;
  33. const
  34. CssProperties: array[0..9] of string = (
  35. 'color', 'background', 'margin', 'padding', 'border',
  36. 'font', 'width', 'height', 'position', 'display'
  37. );
  38. var
  39. highlighter: TCssSyntaxHighlighter;
  40. tokens: TSyntaxTokenArray;
  41. i: Integer;
  42. begin
  43. WriteLn('Testing CSS Properties:');
  44. WriteLn('======================');
  45. highlighter := TCssSyntaxHighlighter.Create;
  46. try
  47. for i := 0 to High(CssProperties) do begin
  48. tokens := highlighter.Execute(CssProperties[i]);
  49. if (Length(tokens) = 1) and (tokens[0].Kind = shKeyword) then
  50. WriteLn(CssProperties[i] + ': PASS - recognized as property')
  51. else
  52. WriteLn(CssProperties[i] + ': FAIL - kind=' + IntToStr(Ord(tokens[0].Kind)));
  53. end;
  54. finally
  55. highlighter.Free;
  56. end;
  57. WriteLn;
  58. end;
  59. procedure TestCssTokenTypes;
  60. var
  61. highlighter: TCssSyntaxHighlighter;
  62. tokens: TSyntaxTokenArray;
  63. begin
  64. WriteLn('Testing CSS Token Types:');
  65. WriteLn('=======================');
  66. highlighter := TCssSyntaxHighlighter.Create;
  67. try
  68. // Test single-quoted string
  69. tokens := highlighter.Execute('''Arial''');
  70. if (Length(tokens) = 1) and (tokens[0].Kind = shStrings) then
  71. WriteLn('Single-quoted string: PASS')
  72. else
  73. WriteLn('Single-quoted string: FAIL - kind=' + IntToStr(Ord(tokens[0].Kind)));
  74. // Test double-quoted string
  75. tokens := highlighter.Execute('"Helvetica"');
  76. if (Length(tokens) = 1) and (tokens[0].Kind = shStrings) then
  77. WriteLn('Double-quoted string: PASS')
  78. else
  79. WriteLn('Double-quoted string: FAIL');
  80. // Test hex color
  81. tokens := highlighter.Execute('#FF0000');
  82. if (Length(tokens) = 1) and (tokens[0].Kind = shNumbers) then
  83. WriteLn('Hex color: PASS')
  84. else
  85. WriteLn('Hex color: FAIL - kind=' + IntToStr(Ord(tokens[0].Kind)));
  86. // Test 3-digit hex color
  87. tokens := highlighter.Execute('#F00');
  88. if (Length(tokens) = 1) and (tokens[0].Kind = shNumbers) then
  89. WriteLn('3-digit hex color: PASS')
  90. else
  91. WriteLn('3-digit hex color: FAIL');
  92. // Test multi-line comment
  93. tokens := highlighter.Execute('/* This is a comment */');
  94. if (Length(tokens) = 1) and (tokens[0].Kind = shComment) then
  95. WriteLn('Multi-line comment: PASS')
  96. else
  97. WriteLn('Multi-line comment: FAIL');
  98. // Test number with unit
  99. tokens := highlighter.Execute('16px');
  100. if (Length(tokens) = 1) and (tokens[0].Kind = shNumbers) then
  101. WriteLn('Number with unit: PASS')
  102. else
  103. WriteLn('Number with unit: FAIL');
  104. // Test percentage
  105. tokens := highlighter.Execute('100%');
  106. if (Length(tokens) = 1) and (tokens[0].Kind = shNumbers) then
  107. WriteLn('Percentage: PASS')
  108. else
  109. WriteLn('Percentage: FAIL');
  110. // Test URL function
  111. tokens := highlighter.Execute('url(image.png)');
  112. if (Length(tokens) = 1) and (tokens[0].Kind = shStrings) then
  113. WriteLn('URL function: PASS')
  114. else
  115. WriteLn('URL function: FAIL');
  116. // Test class selector
  117. tokens := highlighter.Execute('.myClass');
  118. if (Length(tokens) = 1) and (tokens[0].Kind = shDefault) then
  119. WriteLn('Class selector: PASS')
  120. else
  121. WriteLn('Class selector: FAIL');
  122. // Test ID selector
  123. tokens := highlighter.Execute('#myId');
  124. if (Length(tokens) = 1) then
  125. WriteLn('ID selector: PASS')
  126. else
  127. WriteLn('ID selector: FAIL');
  128. finally
  129. highlighter.Free;
  130. end;
  131. WriteLn;
  132. end;
  133. procedure TestCssRule;
  134. var
  135. highlighter: TCssSyntaxHighlighter;
  136. tokens: TSyntaxTokenArray;
  137. i: Integer;
  138. cssRule: string;
  139. begin
  140. WriteLn('Testing Complete CSS Rule:');
  141. WriteLn('=========================');
  142. cssRule := '.container { width: 100%; color: #333; }';
  143. highlighter := TCssSyntaxHighlighter.Create;
  144. try
  145. tokens := highlighter.Execute(cssRule);
  146. WriteLn('Rule: ', cssRule);
  147. WriteLn('Tokens (', Length(tokens), '):');
  148. for i := 0 to High(tokens) do begin
  149. if Trim(tokens[i].Text) <> '' then
  150. WriteLn(' "', tokens[i].Text, '" - Kind: ', Ord(tokens[i].Kind),
  151. ' (', tokens[i].Kind, ') - Category: ', tokens[i].CategoriesAsString);
  152. end;
  153. finally
  154. highlighter.Free;
  155. end;
  156. WriteLn;
  157. end;
  158. procedure TestCssMediaQuery;
  159. var
  160. highlighter: TCssSyntaxHighlighter;
  161. tokens: TSyntaxTokenArray;
  162. i: Integer;
  163. mediaQuery: string;
  164. begin
  165. WriteLn('Testing CSS Media Query:');
  166. WriteLn('=======================');
  167. mediaQuery := '@media (max-width: 768px) { body { font-size: 14px; } }';
  168. highlighter := TCssSyntaxHighlighter.Create;
  169. try
  170. tokens := highlighter.Execute(mediaQuery);
  171. WriteLn('Media Query: ', mediaQuery);
  172. WriteLn('Tokens (', Length(tokens), '):');
  173. for i := 0 to High(tokens) do begin
  174. if Trim(tokens[i].Text) <> '' then
  175. WriteLn(' "', tokens[i].Text, '" - Kind: ', Ord(tokens[i].Kind),
  176. ' (', tokens[i].Kind, ') - Category: ', tokens[i].CategoriesAsString);
  177. end;
  178. finally
  179. highlighter.Free;
  180. end;
  181. WriteLn;
  182. end;
  183. procedure TestCategorySystem;
  184. var
  185. categories: TStringList;
  186. cssCategoryID: Integer;
  187. i: Integer;
  188. begin
  189. WriteLn('Testing Category System for CSS:');
  190. WriteLn('================================');
  191. cssCategoryID := TSyntaxHighlighter.RegisterCategory('CSS');
  192. WriteLn('CSS category ID: ', cssCategoryID);
  193. categories := TStringList.Create;
  194. try
  195. TSyntaxHighlighter.GetRegisteredCategories(categories);
  196. WriteLn('All registered categories:');
  197. for i := 0 to categories.Count - 1 do
  198. WriteLn(' ', categories[i], ' = ', PtrInt(categories.Objects[i]));
  199. finally
  200. categories.Free;
  201. end;
  202. WriteLn;
  203. end;
  204. begin
  205. WriteLn('CSS Syntax Highlighter Test');
  206. WriteLn('============================');
  207. WriteLn;
  208. TestCategorySystem;
  209. TestCssAtRules;
  210. TestCssProperties;
  211. TestCssTokenTypes;
  212. TestCssRule;
  213. TestCssMediaQuery;
  214. end.