Coverage Report

Created: 2026-05-16 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/serenity/Userland/Libraries/LibMarkdown/CommentBlock.cpp
Line
Count
Source
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
14.7M
{
43
14.7M
    if (lines.is_end())
44
0
        return {};
45
46
14.7M
    constexpr auto comment_start = "<!--"sv;
47
14.7M
    constexpr auto comment_end = "-->"sv;
48
49
14.7M
    StringView line = *lines;
50
14.7M
    if (!line.starts_with(comment_start))
51
14.7M
        return {};
52
22.4k
    line = line.substring_view(comment_start.length());
53
54
22.4k
    StringBuilder builder;
55
56
524k
    while (true) {
57
        // Invariant: At the beginning of the loop, `line` is valid and should be added to the builder.
58
524k
        bool ends_here = line.ends_with(comment_end);
59
524k
        if (ends_here)
60
21.5k
            line = line.substring_view(0, line.length() - comment_end.length());
61
524k
        builder.append(line);
62
524k
        if (!ends_here)
63
502k
            builder.append('\n');
64
65
524k
        ++lines;
66
524k
        if (lines.is_end() || ends_here) {
67
22.4k
            break;
68
22.4k
        }
69
501k
        line = *lines;
70
501k
    }
71
72
22.4k
    return make<CommentBlock>(builder.to_byte_string());
73
14.7M
}
74
75
}