diff options
author | Srikavin Ramkumar <srikavinramkumar@gmail.com> | 2023-03-23 04:27:18 -0400 |
---|---|---|
committer | Tim Flynn <trflynn89@pm.me> | 2023-03-28 07:18:09 -0400 |
commit | f2dd878fe79779de3896cef4ade90acbd8dbf198 (patch) | |
tree | d80a4b6f4e67e826ec2656d32ac39abce8c907a2 /Userland/Libraries/LibWeb/HTML | |
parent | 149e442c2416c3997455073db2059607436203a1 (diff) | |
download | serenity-f2dd878fe79779de3896cef4ade90acbd8dbf198.zip |
LibWeb: Implement custom element name validation
Diffstat (limited to 'Userland/Libraries/LibWeb/HTML')
-rw-r--r-- | Userland/Libraries/LibWeb/HTML/CustomElements/CustomElementName.cpp | 76 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/HTML/CustomElements/CustomElementName.h | 15 |
2 files changed, 91 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/HTML/CustomElements/CustomElementName.cpp b/Userland/Libraries/LibWeb/HTML/CustomElements/CustomElementName.cpp new file mode 100644 index 0000000000..340fc51f3f --- /dev/null +++ b/Userland/Libraries/LibWeb/HTML/CustomElements/CustomElementName.cpp @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2023, Srikavin Ramkumar <me@srikavin.me> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <AK/StringView.h> +#include <AK/Utf8View.h> +#include <LibWeb/HTML/CustomElements/CustomElementName.h> + +namespace Web::HTML { + +// https://html.spec.whatwg.org/multipage/custom-elements.html#custom-elements-core-concepts:prod-pcenchar +static bool is_pcen_char(u32 code_point) +{ + return code_point == '-' + || code_point == '.' + || (code_point >= '0' && code_point <= '9') + || code_point == '_' + || (code_point >= 'a' && code_point <= 'z') + || code_point == 0xb7 + || (code_point >= 0xc0 && code_point <= 0xd6) + || (code_point >= 0xd8 && code_point <= 0xf6) + || (code_point >= 0xf8 && code_point <= 0x37d) + || (code_point >= 0x37f && code_point <= 0x1fff) + || (code_point >= 0x200c && code_point <= 0x200d) + || (code_point >= 0x203f && code_point <= 0x2040) + || (code_point >= 0x2070 && code_point <= 0x218f) + || (code_point >= 0x2c00 && code_point <= 0x2fef) + || (code_point >= 0x3001 && code_point <= 0xD7ff) + || (code_point >= 0xf900 && code_point <= 0xfdcf) + || (code_point >= 0xfdf0 && code_point <= 0xfffd) + || (code_point >= 0x10000 && code_point <= 0xeffff); +} + +// https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name +bool is_valid_custom_element_name(StringView name) +{ + // name must not be any of the following: + if (name.is_one_of( + "annotation-xml"sv, + "color-profile"sv, + "font-face"sv, + "font-face-src"sv, + "font-face-uri"sv, + "font-face-format"sv, + "font-face-name"sv, + "missing-glyph"sv)) { + return false; + } + + // name must match the PotentialCustomElementName production: + // PotentialCustomElementName ::= + // [a-z] (PCENChar)* '-' (PCENChar)* + + auto code_points = Utf8View { name }; + auto it = code_points.begin(); + + if (code_points.is_empty() || *it < 'a' || *it > 'z') + return false; + + ++it; + + bool found_hyphen = false; + + for (; it != code_points.end(); ++it) { + if (!is_pcen_char(*it)) + return false; + if (*it == '-') + found_hyphen = true; + } + + return found_hyphen; +} + +} diff --git a/Userland/Libraries/LibWeb/HTML/CustomElements/CustomElementName.h b/Userland/Libraries/LibWeb/HTML/CustomElements/CustomElementName.h new file mode 100644 index 0000000000..b16ea325cd --- /dev/null +++ b/Userland/Libraries/LibWeb/HTML/CustomElements/CustomElementName.h @@ -0,0 +1,15 @@ +/* + * Copyright (c) 2023, Srikavin Ramkumar <me@srikavin.me> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include <AK/StringView.h> + +namespace Web::HTML { + +bool is_valid_custom_element_name(StringView name); + +} |