From d2a2d19a86f64a36b3adfe59f78a91de5d557898 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sat, 31 Oct 2020 17:25:29 +0000 Subject: [PATCH] LibJS: Handle multi-line source code in MarkupGenerator The previous approach (keeping track of the current source position manually) was only working for single line sources (which is fair considering this was developed for Browser's JS console). The new approach is much simpler: append token trivia (all whitespace and comments since the last token), then append styled token value. --- Libraries/LibJS/MarkupGenerator.cpp | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/Libraries/LibJS/MarkupGenerator.cpp b/Libraries/LibJS/MarkupGenerator.cpp index 95e5ea6f09..db6f01e767 100644 --- a/Libraries/LibJS/MarkupGenerator.cpp +++ b/Libraries/LibJS/MarkupGenerator.cpp @@ -38,24 +38,11 @@ namespace JS { String MarkupGenerator::html_from_source(const StringView& source) { StringBuilder builder; - size_t source_cursor = 0; - auto lexer = Lexer(source); for (auto token = lexer.next(); token.type() != TokenType::Eof; token = lexer.next()) { - auto length = token.value().length(); - auto start = token.line_column() - 1; - - if (start > source_cursor) { - builder.append(source.substring_view(source_cursor, start - source_cursor)); - } - + builder.append(token.trivia()); builder.append(wrap_string_in_style(token.value(), style_type_for_token(token))); - source_cursor = start + length; } - - if (source_cursor < source.length()) - builder.append(source.substring_view(source_cursor, source.length() - source_cursor)); - return builder.to_string(); }