Coverage Report

Created: 2025-11-02 07:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/serenity/Userland/Libraries/LibWeb/HTML/Parser/HTMLToken.cpp
Line
Count
Source
1
/*
2
 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
3
 *
4
 * SPDX-License-Identifier: BSD-2-Clause
5
 */
6
7
#include <AK/HashTable.h>
8
#include <LibWeb/HTML/Parser/HTMLToken.h>
9
10
namespace Web::HTML {
11
12
String HTMLToken::to_string() const
13
0
{
14
0
    StringBuilder builder;
15
16
0
    switch (type()) {
17
0
    case HTMLToken::Type::DOCTYPE:
18
0
        builder.append("DOCTYPE"sv);
19
0
        builder.append(" { name: '"sv);
20
0
        builder.append(doctype_data().name);
21
0
        builder.append("' }"sv);
22
0
        break;
23
0
    case HTMLToken::Type::StartTag:
24
0
        builder.append("StartTag"sv);
25
0
        break;
26
0
    case HTMLToken::Type::EndTag:
27
0
        builder.append("EndTag"sv);
28
0
        break;
29
0
    case HTMLToken::Type::Comment:
30
0
        builder.append("Comment"sv);
31
0
        break;
32
0
    case HTMLToken::Type::Character:
33
0
        builder.append("Character"sv);
34
0
        break;
35
0
    case HTMLToken::Type::EndOfFile:
36
0
        builder.append("EndOfFile"sv);
37
0
        break;
38
0
    case HTMLToken::Type::Invalid:
39
0
        VERIFY_NOT_REACHED();
40
0
    }
41
42
0
    if (type() == HTMLToken::Type::StartTag || type() == HTMLToken::Type::EndTag) {
43
0
        builder.append(" { name: '"sv);
44
0
        builder.append(tag_name());
45
0
        builder.append("', { "sv);
46
0
        for_each_attribute([&](auto& attribute) {
47
0
            builder.append(attribute.local_name);
48
0
            builder.append("=\""sv);
49
0
            builder.append(attribute.value);
50
0
            builder.append("\" "sv);
51
0
            return IterationDecision::Continue;
52
0
        });
53
0
        builder.append("} }"sv);
54
0
    }
55
56
0
    if (is_comment()) {
57
0
        builder.append(" { data: '"sv);
58
0
        builder.append(comment());
59
0
        builder.append("' }"sv);
60
0
    }
61
62
0
    if (is_character()) {
63
0
        builder.append(" { data: '"sv);
64
0
        builder.append_code_point(code_point());
65
0
        builder.append("' }"sv);
66
0
    }
67
68
0
    if (type() == HTMLToken::Type::Character) {
69
0
        builder.appendff("@{}:{}", m_start_position.line, m_start_position.column);
70
0
    } else {
71
0
        builder.appendff("@{}:{}-{}:{}", m_start_position.line, m_start_position.column, m_end_position.line, m_end_position.column);
72
0
    }
73
74
0
    return MUST(builder.to_string());
75
0
}
76
77
void HTMLToken::normalize_attributes()
78
0
{
79
    // From AttributeNameState: https://html.spec.whatwg.org/multipage/parsing.html#attribute-name-state
80
    //
81
    // When the user agent leaves the attribute name state (and before emitting the tag token, if appropriate),
82
    // the complete attribute's name must be compared to the other attributes on the same token;
83
    // if there is already an attribute on the token with the exact same name, then this is a duplicate-attribute
84
    // parse error and the new attribute must be removed from the token.
85
86
    // NOTE: If an attribute is so removed from a token, it, and the value that gets associated with it, if any,
87
    // are never subsequently used by the parser, and are therefore effectively discarded. Removing the attribute
88
    // in this way does not change its status as the "current attribute" for the purposes of the tokenizer, however.
89
90
0
    HashTable<FlyString> seen_attributes;
91
0
    auto* ptr = tag_attributes();
92
0
    if (!ptr)
93
0
        return;
94
0
    auto& tag_attributes = *ptr;
95
0
    for (size_t i = 0; i < tag_attributes.size(); ++i) {
96
0
        auto& attribute = tag_attributes[i];
97
0
        if (seen_attributes.set(attribute.local_name, AK::HashSetExistingEntryBehavior::Keep) == AK::HashSetResult::KeptExistingEntry) {
98
            // This is a duplicate attribute, remove it.
99
0
            tag_attributes.remove(i);
100
0
            --i;
101
0
        }
102
0
    }
103
0
}
104
105
}