OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * Kinds of tokens that we care to highlight differently. The values of the | 6 * Kinds of tokens that we care to highlight differently. The values of the |
7 * fields here will be used as CSS class names for the generated spans. | 7 * fields here will be used as CSS class names for the generated spans. |
8 */ | 8 */ |
9 class Classification { | 9 class Classification { |
10 static final NONE = null; | 10 static final NONE = null; |
(...skipping 30 matching lines...) Expand all Loading... |
41 case TokenKind.STRING: | 41 case TokenKind.STRING: |
42 case TokenKind.STRING_PART: | 42 case TokenKind.STRING_PART: |
43 case TokenKind.INCOMPLETE_STRING: | 43 case TokenKind.INCOMPLETE_STRING: |
44 case TokenKind.INCOMPLETE_MULTILINE_STRING_DQ: | 44 case TokenKind.INCOMPLETE_MULTILINE_STRING_DQ: |
45 case TokenKind.INCOMPLETE_MULTILINE_STRING_SQ: | 45 case TokenKind.INCOMPLETE_MULTILINE_STRING_SQ: |
46 inString = true; | 46 inString = true; |
47 break; | 47 break; |
48 } | 48 } |
49 | 49 |
50 final kind = classify(token); | 50 final kind = classify(token); |
51 final text = htmlEscape(token.text); | 51 final text = md.escapeHtml(token.text); |
52 if (kind != null) { | 52 if (kind != null) { |
53 // Add a secondary class to tokens appearing within a string so that | 53 // Add a secondary class to tokens appearing within a string so that |
54 // we can highlight tokens in an interpolation specially. | 54 // we can highlight tokens in an interpolation specially. |
55 var stringClass = inString ? Classification.STRING_INTERPOLATION : ''; | 55 var stringClass = inString ? Classification.STRING_INTERPOLATION : ''; |
56 html.add('<span class="$kind $stringClass">$text</span>'); | 56 html.add('<span class="$kind $stringClass">$text</span>'); |
57 } else { | 57 } else { |
58 html.add('<span>$text</span>'); | 58 html.add('<span>$text</span>'); |
59 } | 59 } |
60 | 60 |
61 // Track whether or not we're in a string. | 61 // Track whether or not we're in a string. |
62 if (token.kind == TokenKind.STRING) { | 62 if (token.kind == TokenKind.STRING) { |
63 inString = false; | 63 inString = false; |
64 } | 64 } |
65 } | 65 } |
66 return html.toString(); | 66 return html.toString(); |
67 } | 67 } |
68 | 68 |
69 // TODO(rnystrom): should exist in standard lib somewhere | |
70 String htmlEscape(String text) { | |
71 return text.replaceAll('&', '&').replaceAll( | |
72 '>', '>').replaceAll('<', '<'); | |
73 } | |
74 | |
75 bool _looksLikeType(String name) { | 69 bool _looksLikeType(String name) { |
76 // If the name looks like an UppercaseName, assume it's a type. | 70 // If the name looks like an UppercaseName, assume it's a type. |
77 return _looksLikePublicType(name) || _looksLikePrivateType(name); | 71 return _looksLikePublicType(name) || _looksLikePrivateType(name); |
78 } | 72 } |
79 | 73 |
80 bool _looksLikePublicType(String name) { | 74 bool _looksLikePublicType(String name) { |
81 // If the name looks like an UppercaseName, assume it's a type. | 75 // If the name looks like an UppercaseName, assume it's a type. |
82 return name.length >= 2 && isUpper(name[0]) && isLower(name[1]); | 76 return name.length >= 2 && isUpper(name[0]) && isLower(name[1]); |
83 } | 77 } |
84 | 78 |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 return Classification.KEYWORD; | 235 return Classification.KEYWORD; |
242 | 236 |
243 case TokenKind.WHITESPACE: | 237 case TokenKind.WHITESPACE: |
244 case TokenKind.END_OF_FILE: | 238 case TokenKind.END_OF_FILE: |
245 return Classification.NONE; | 239 return Classification.NONE; |
246 | 240 |
247 default: | 241 default: |
248 return Classification.NONE; | 242 return Classification.NONE; |
249 } | 243 } |
250 } | 244 } |
OLD | NEW |