summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorMax Wipfli <mail@maxwipfli.ch>2021-06-04 11:38:57 +0200
committerAli Mohammad Pur <Ali.mpfard@gmail.com>2021-06-05 00:32:28 +0430
commit44c438d0ca1e44ed11be5ed96eb409e1760467f7 (patch)
treedba68c403652fd385d85b9ad872cff8347a4c7af /Userland
parent932161e5817561028bdb5685182ad3f78db5e1b5 (diff)
downloadserenity-44c438d0ca1e44ed11be5ed96eb409e1760467f7.zip
LibWeb: Fix off-by-one error in SyntaxHighlighter
This changes the HTML SyntaxHighlighter to conform to the now-fixed rendering of syntax highlighting spans in GUI::TextEditor. It also avoids emitting tokens if they have a zero or negative length. This fixes a bug where single-character tokens were not highlighted properly.
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibWeb/HTML/SyntaxHighlighter/SyntaxHighlighter.cpp13
1 files changed, 8 insertions, 5 deletions
diff --git a/Userland/Libraries/LibWeb/HTML/SyntaxHighlighter/SyntaxHighlighter.cpp b/Userland/Libraries/LibWeb/HTML/SyntaxHighlighter/SyntaxHighlighter.cpp
index 48759ff842..df66bede63 100644
--- a/Userland/Libraries/LibWeb/HTML/SyntaxHighlighter/SyntaxHighlighter.cpp
+++ b/Userland/Libraries/LibWeb/HTML/SyntaxHighlighter/SyntaxHighlighter.cpp
@@ -40,6 +40,10 @@ void SyntaxHighlighter::rehighlight(Palette const& palette)
Vector<GUI::TextDocumentSpan> spans;
auto highlight = [&](auto start_line, auto start_column, auto end_line, auto end_column, Gfx::TextAttributes attributes, AugmentedTokenKind kind) {
+ if (start_line > end_line || (start_line == end_line && start_column >= end_column)) {
+ dbgln_if(SYNTAX_HIGHLIGHTING_DEBUG, "(HTML::SyntaxHighlighter) discarding ({}-{}) to ({}-{}) because it has zero or negative length", start_line, start_column, end_line, end_column);
+ return;
+ }
dbgln_if(SYNTAX_HIGHLIGHTING_DEBUG, "(HTML::SyntaxHighlighter) highlighting ({}-{}) to ({}-{}) with color {}", start_line, start_column, end_line, end_column, attributes.color);
spans.empend(
GUI::TextRange {
@@ -59,7 +63,7 @@ void SyntaxHighlighter::rehighlight(Palette const& palette)
} state { State::HTML };
for (;;) {
auto token = tokenizer.next_token();
- if (!token.has_value())
+ if (!token.has_value() || token.value().is_end_of_file())
break;
dbgln_if(SYNTAX_HIGHLIGHTING_DEBUG, "(HTML::SyntaxHighlighter) got token of type {}", token->to_string());
@@ -88,17 +92,16 @@ void SyntaxHighlighter::rehighlight(Palette const& palette)
highlight(
token->start_position().line,
token->start_position().column,
- token->start_position().line,
- token->start_position().column,
+ token->end_position().line,
+ token->end_position().column,
{ palette.syntax_comment(), {} },
AugmentedTokenKind::Comment);
} else if (token->is_start_tag() || token->is_end_tag()) {
- // FIXME: This breaks with single-character tag names.
highlight(
token->start_position().line,
token->start_position().column + token_start_offset,
token->start_position().line,
- token->start_position().column + token->tag_name().length() + token_start_offset - 1,
+ token->start_position().column + token_start_offset + token->tag_name().length(),
{ palette.syntax_keyword(), {}, false, true },
token->is_start_tag() ? AugmentedTokenKind::OpenTag : AugmentedTokenKind::CloseTag);