Coverage Report

Created: 2026-02-16 07:47

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
8.53M
{
43
8.53M
    if (lines.is_end())
44
0
        return {};
45
46
8.53M
    constexpr auto comment_start = "<!--"sv;
47
8.53M
    constexpr auto comment_end = "-->"sv;
48
49
8.53M
    StringView line = *lines;
50
8.53M
    if (!line.starts_with(comment_start))
51
8.51M
        return {};
52
14.1k
    line = line.substring_view(comment_start.length());
53
54
14.1k
    StringBuilder builder;
55
56
540k
    while (true) {
57
        // Invariant: At the beginning of the loop, `line` is valid and should be added to the builder.
58
540k
        bool ends_here = line.ends_with(comment_end);
59
540k
        if (ends_here)
60
13.3k
            line = line.substring_view(0, line.length() - comment_end.length());
61
540k
        builder.append(line);
62
540k
        if (!ends_here)
63
527k
            builder.append('\n');
64
65
540k
        ++lines;
66
540k
        if (lines.is_end() || ends_here) {
67
14.1k
            break;
68
14.1k
        }
69
526k
        line = *lines;
70
526k
    }
71
72
14.1k
    return make<CommentBlock>(builder.to_byte_string());
73
8.53M
}
74
75
}