Coverage Report

Created: 2025-03-04 07:22

/src/serenity/Userland/Libraries/LibWeb/HTML/CustomElements/CustomElementName.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2023, Srikavin Ramkumar <me@srikavin.me>
3
 *
4
 * SPDX-License-Identifier: BSD-2-Clause
5
 */
6
7
#include <AK/StringView.h>
8
#include <AK/Utf8View.h>
9
#include <LibWeb/HTML/CustomElements/CustomElementName.h>
10
11
namespace Web::HTML {
12
13
// https://html.spec.whatwg.org/multipage/custom-elements.html#custom-elements-core-concepts:prod-pcenchar
14
static bool is_pcen_char(u32 code_point)
15
0
{
16
0
    return code_point == '-'
17
0
        || code_point == '.'
18
0
        || (code_point >= '0' && code_point <= '9')
19
0
        || code_point == '_'
20
0
        || (code_point >= 'a' && code_point <= 'z')
21
0
        || code_point == 0xb7
22
0
        || (code_point >= 0xc0 && code_point <= 0xd6)
23
0
        || (code_point >= 0xd8 && code_point <= 0xf6)
24
0
        || (code_point >= 0xf8 && code_point <= 0x37d)
25
0
        || (code_point >= 0x37f && code_point <= 0x1fff)
26
0
        || (code_point >= 0x200c && code_point <= 0x200d)
27
0
        || (code_point >= 0x203f && code_point <= 0x2040)
28
0
        || (code_point >= 0x2070 && code_point <= 0x218f)
29
0
        || (code_point >= 0x2c00 && code_point <= 0x2fef)
30
0
        || (code_point >= 0x3001 && code_point <= 0xD7ff)
31
0
        || (code_point >= 0xf900 && code_point <= 0xfdcf)
32
0
        || (code_point >= 0xfdf0 && code_point <= 0xfffd)
33
0
        || (code_point >= 0x10000 && code_point <= 0xeffff);
34
0
}
35
36
// https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name
37
bool is_valid_custom_element_name(StringView name)
38
0
{
39
    // name must not be any of the following:
40
0
    if (name.is_one_of(
41
0
            "annotation-xml"sv,
42
0
            "color-profile"sv,
43
0
            "font-face"sv,
44
0
            "font-face-src"sv,
45
0
            "font-face-uri"sv,
46
0
            "font-face-format"sv,
47
0
            "font-face-name"sv,
48
0
            "missing-glyph"sv)) {
49
0
        return false;
50
0
    }
51
52
    // name must match the PotentialCustomElementName production:
53
    // PotentialCustomElementName ::=
54
    //      [a-z] (PCENChar)* '-' (PCENChar)*
55
56
0
    auto code_points = Utf8View { name };
57
0
    auto it = code_points.begin();
58
59
0
    if (code_points.is_empty() || *it < 'a' || *it > 'z')
60
0
        return false;
61
62
0
    ++it;
63
64
0
    bool found_hyphen = false;
65
66
0
    for (; it != code_points.end(); ++it) {
67
0
        if (!is_pcen_char(*it))
68
0
            return false;
69
0
        if (*it == '-')
70
0
            found_hyphen = true;
71
0
    }
72
73
0
    return found_hyphen;
74
0
}
75
76
}