CustomElementName.cpp 2.3 KB

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