Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions lib/languages/lib/javascript.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import 'package:re_highlight/re_highlight.dart';

ModeCallback callbackOnBegin = (EnhancedMatch match, ModeCallbackResponse response) {
ModeCallback callbackOnBegin =
(EnhancedMatch match, ModeCallbackResponse response) {
final String? match0 = match[0];
if (match0 == null) {
return;
}
final int afterMatchIndex = match0.length + match.index;
final String nextChar = match.input.substring(afterMatchIndex, afterMatchIndex + 1);
final String? nextChar = afterMatchIndex < match.input.length
? match.input[afterMatchIndex]
: null;
if (
// HTML should not include another raw `<` inside a tag
// nested type?
// `<Array<Array<number>>`, etc.
nextChar == "<" ||
// the , gives away that this is not HTML
// `<T, A extends keyof T, V>`
nextChar == ",") {
// HTML should not include another raw `<` inside a tag
// nested type?
// `<Array<Array<number>>`, etc.
nextChar == "<" ||
// the , gives away that this is not HTML
// `<T, A extends keyof T, V>`
nextChar == ",") {
response.ignoreMatch();
return;
}
Expand Down Expand Up @@ -57,4 +60,4 @@ bool _hasClosingTag(EnhancedMatch match, int after) {
final String tag = "</${match[0]!.substring(1)}";
final int pos = match.input.indexOf(tag, after);
return pos != -1;
}
}
77 changes: 56 additions & 21 deletions test/api/highlight_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,78 @@ void main() {
group('.highlight()', () {
test('should support ignoreIllegals', () {
String code = "float # float";
HighlightResult result = highlight.highlight(code: code, language: 'java', ignoreIllegals: true);
expect(result.toHtml(), '<span class="hljs-type">float</span> # <span class="hljs-type">float</span>');
HighlightResult result = highlight.highlight(
code: code, language: 'java', ignoreIllegals: true);
expect(result.toHtml(),
'<span class="hljs-type">float</span> # <span class="hljs-type">float</span>');

code = 'float # float';
result = highlight.highlight(code: code, language: 'java', ignoreIllegals: false);
result = highlight.highlight(
code: code, language: 'java', ignoreIllegals: false);
expect(result.toHtml(), 'float # float');
expect(result.illegal, true);

// defaults to true
code = 'float # float';
result = highlight.highlight(code: code, language: 'java');
expect(result.toHtml(), '<span class="hljs-type">float</span> # <span class="hljs-type">float</span>');
expect(result.toHtml(),
'<span class="hljs-type">float</span> # <span class="hljs-type">float</span>');
expect(result.illegal, false);
});
test('should not crash on incomplete JSX-like tags at end of input', () {
const String code = 'const x = <Foo';
final Highlight safeHighlight = Highlight();
safeHighlight.registerLanguages(builtinAllLanguages);

HighlightResult result =
safeHighlight.highlight(code: code, language: 'javascript');
expect(result.errorRaised, isNull);
expect(() => result.toHtml(), returnsNormally);

result = safeHighlight.highlight(code: code, language: 'typescript');
expect(result.errorRaised, isNull);
expect(() => result.toHtml(), returnsNormally);

final Highlight debugHighlight = Highlight();
debugHighlight.debugMode();
debugHighlight.registerLanguages(builtinAllLanguages);

expect(
() => debugHighlight
.highlight(code: code, language: 'javascript')
.toHtml(),
returnsNormally);
expect(
() => debugHighlight
.highlight(code: code, language: 'typescript')
.toHtml(),
returnsNormally);
});
test('should use new API with options', () {
const String code = 'public void moveTo(int x, int y, int z);';
final HighlightResult result = highlight.highlight(code: code, language: 'java');
final HighlightResult result =
highlight.highlight(code: code, language: 'java');

expect(result.toHtml(),
'<span class="hljs-keyword">public</span> '
'<span class="hljs-keyword">void</span> <span class="hljs-title function_">moveTo</span>'
'<span class="hljs-params">(<span class="hljs-type">int</span> x, '
'<span class="hljs-type">int</span> y, '
'<span class="hljs-type">int</span> z)</span>;'
);
expect(
result.toHtml(),
'<span class="hljs-keyword">public</span> '
'<span class="hljs-keyword">void</span> <span class="hljs-title function_">moveTo</span>'
'<span class="hljs-params">(<span class="hljs-type">int</span> x, '
'<span class="hljs-type">int</span> y, '
'<span class="hljs-type">int</span> z)</span>;');
});
test('should works without continuation', () {
const String code = "public void moveTo(int x, int y, int z);";
final HighlightResult result = highlight.highlight(code: code, language: 'java');
final HighlightResult result =
highlight.highlight(code: code, language: 'java');

expect(result.toHtml(),
'<span class="hljs-keyword">public</span> '
'<span class="hljs-keyword">void</span> <span class="hljs-title function_">moveTo</span>'
'<span class="hljs-params">(<span class="hljs-type">int</span> x, '
'<span class="hljs-type">int</span> y, '
'<span class="hljs-type">int</span> z)</span>;'
);
expect(
result.toHtml(),
'<span class="hljs-keyword">public</span> '
'<span class="hljs-keyword">void</span> <span class="hljs-title function_">moveTo</span>'
'<span class="hljs-params">(<span class="hljs-type">int</span> x, '
'<span class="hljs-type">int</span> y, '
'<span class="hljs-type">int</span> z)</span>;');
});
});
}
}