/src/serenity/Userland/Libraries/LibMarkdown/CommentBlock.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2021, Ben Wiederhake <BenWiederhake.GitHub@gmx.de> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include <AK/Forward.h> |
8 | | #include <AK/StringBuilder.h> |
9 | | #include <LibMarkdown/CommentBlock.h> |
10 | | #include <LibMarkdown/Visitor.h> |
11 | | |
12 | | namespace Markdown { |
13 | | |
14 | | ByteString CommentBlock::render_to_html(bool) const |
15 | 0 | { |
16 | 0 | StringBuilder builder; |
17 | |
|
18 | 0 | builder.append("<!--"sv); |
19 | 0 | builder.append(escape_html_entities(m_comment)); |
20 | | // TODO: This is probably incorrect, because we technically need to escape "--" in some form. However, Browser does not care about this. |
21 | 0 | builder.append("-->\n"sv); |
22 | |
|
23 | 0 | return builder.to_byte_string(); |
24 | 0 | } |
25 | | |
26 | | Vector<ByteString> CommentBlock::render_lines_for_terminal(size_t) const |
27 | 0 | { |
28 | 0 | return Vector<ByteString> {}; |
29 | 0 | } |
30 | | |
31 | | RecursionDecision CommentBlock::walk(Visitor& visitor) const |
32 | 0 | { |
33 | 0 | RecursionDecision rd = visitor.visit(*this); |
34 | 0 | if (rd != RecursionDecision::Recurse) |
35 | 0 | return rd; |
36 | | |
37 | | // Normalize return value. |
38 | 0 | return RecursionDecision::Continue; |
39 | 0 | } |
40 | | |
41 | | OwnPtr<CommentBlock> CommentBlock::parse(LineIterator& lines) |
42 | 15.6M | { |
43 | 15.6M | if (lines.is_end()) |
44 | 0 | return {}; |
45 | | |
46 | 15.6M | constexpr auto comment_start = "<!--"sv; |
47 | 15.6M | constexpr auto comment_end = "-->"sv; |
48 | | |
49 | 15.6M | StringView line = *lines; |
50 | 15.6M | if (!line.starts_with(comment_start)) |
51 | 15.6M | return {}; |
52 | 37.7k | line = line.substring_view(comment_start.length()); |
53 | | |
54 | 37.7k | StringBuilder builder; |
55 | | |
56 | 343k | while (true) { |
57 | | // Invariant: At the beginning of the loop, `line` is valid and should be added to the builder. |
58 | 343k | bool ends_here = line.ends_with(comment_end); |
59 | 343k | if (ends_here) |
60 | 36.6k | line = line.substring_view(0, line.length() - comment_end.length()); |
61 | 343k | builder.append(line); |
62 | 343k | if (!ends_here) |
63 | 307k | builder.append('\n'); |
64 | | |
65 | 343k | ++lines; |
66 | 343k | if (lines.is_end() || ends_here) { |
67 | 37.7k | break; |
68 | 37.7k | } |
69 | 306k | line = *lines; |
70 | 306k | } |
71 | | |
72 | 37.7k | return make<CommentBlock>(builder.to_byte_string()); |
73 | 15.6M | } |
74 | | |
75 | | } |