CSSFontFaceRule.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org>
  3. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/Bindings/CSSFontFaceRulePrototype.h>
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/CSS/CSSFontFaceRule.h>
  10. #include <LibWeb/CSS/Serialize.h>
  11. #include <LibWeb/WebIDL/ExceptionOr.h>
  12. namespace Web::CSS {
  13. WebIDL::ExceptionOr<JS::NonnullGCPtr<CSSFontFaceRule>> CSSFontFaceRule::create(JS::Realm& realm, FontFace&& font_face)
  14. {
  15. return MUST_OR_THROW_OOM(realm.heap().allocate<CSSFontFaceRule>(realm, realm, move(font_face)));
  16. }
  17. CSSFontFaceRule::CSSFontFaceRule(JS::Realm& realm, FontFace&& font_face)
  18. : CSSRule(realm)
  19. , m_font_face(move(font_face))
  20. {
  21. }
  22. JS::ThrowCompletionOr<void> CSSFontFaceRule::initialize(JS::Realm& realm)
  23. {
  24. MUST_OR_THROW_OOM(Base::initialize(realm));
  25. set_prototype(&Bindings::ensure_web_prototype<Bindings::CSSFontFaceRulePrototype>(realm, "CSSFontFaceRule"));
  26. return {};
  27. }
  28. CSSStyleDeclaration* CSSFontFaceRule::style()
  29. {
  30. // FIXME: Return a CSSStyleDeclaration subclass that directs changes to the FontFace.
  31. return nullptr;
  32. }
  33. // https://www.w3.org/TR/cssom/#ref-for-cssfontfacerule
  34. DeprecatedString CSSFontFaceRule::serialized() const
  35. {
  36. StringBuilder builder;
  37. // The result of concatenating the following:
  38. // 1. The string "@font-face {", followed by a single SPACE (U+0020).
  39. builder.append("@font-face { "sv);
  40. // 2. The string "font-family:", followed by a single SPACE (U+0020).
  41. builder.append("font-family: "sv);
  42. // 3. The result of performing serialize a string on the rule’s font family name.
  43. serialize_a_string(builder, m_font_face.font_family()).release_value_but_fixme_should_propagate_errors();
  44. // 4. The string ";", i.e., SEMICOLON (U+003B).
  45. builder.append(';');
  46. // 5. If the rule’s associated source list is not empty, follow these substeps:
  47. if (!m_font_face.sources().is_empty()) {
  48. // 1. A single SPACE (U+0020), followed by the string "src:", followed by a single SPACE (U+0020).
  49. builder.append(" src: "sv);
  50. // 2. The result of invoking serialize a comma-separated list on performing serialize a URL or serialize a LOCAL for each source on the source list.
  51. serialize_a_comma_separated_list(builder, m_font_face.sources(), [&](StringBuilder& builder, FontFace::Source source) -> ErrorOr<void> {
  52. if (source.url.cannot_be_a_base_url()) {
  53. TRY(serialize_a_url(builder, source.url.to_deprecated_string()));
  54. } else {
  55. TRY(serialize_a_local(builder, source.url.to_deprecated_string()));
  56. }
  57. // NOTE: No spec currently exists for format()
  58. if (source.format.has_value()) {
  59. TRY(builder.try_append("format(\""sv));
  60. TRY(serialize_a_string(builder, source.format.value()));
  61. TRY(builder.try_append("\")"sv));
  62. }
  63. return {};
  64. }).release_value_but_fixme_should_propagate_errors();
  65. // 3. The string ";", i.e., SEMICOLON (U+003B).
  66. builder.append(';');
  67. }
  68. // 6. If rule’s associated unicode-range descriptor is present, a single SPACE (U+0020), followed by the string "unicode-range:", followed by a single SPACE (U+0020), followed by the result of performing serialize a <'unicode-range'>, followed by the string ";", i.e., SEMICOLON (U+003B).
  69. builder.append(" unicode-range: "sv);
  70. serialize_unicode_ranges(builder, m_font_face.unicode_ranges()).release_value_but_fixme_should_propagate_errors();
  71. builder.append(';');
  72. // FIXME: 7. If rule’s associated font-variant descriptor is present, a single SPACE (U+0020),
  73. // followed by the string "font-variant:", followed by a single SPACE (U+0020),
  74. // followed by the result of performing serialize a <'font-variant'>,
  75. // followed by the string ";", i.e., SEMICOLON (U+003B).
  76. // FIXME: 8. If rule’s associated font-feature-settings descriptor is present, a single SPACE (U+0020),
  77. // followed by the string "font-feature-settings:", followed by a single SPACE (U+0020),
  78. // followed by the result of performing serialize a <'font-feature-settings'>,
  79. // followed by the string ";", i.e., SEMICOLON (U+003B).
  80. // FIXME: 9. If rule’s associated font-stretch descriptor is present, a single SPACE (U+0020),
  81. // followed by the string "font-stretch:", followed by a single SPACE (U+0020),
  82. // followed by the result of performing serialize a <'font-stretch'>,
  83. // followed by the string ";", i.e., SEMICOLON (U+003B).
  84. // FIXME: 10. If rule’s associated font-weight descriptor is present, a single SPACE (U+0020),
  85. // followed by the string "font-weight:", followed by a single SPACE (U+0020),
  86. // followed by the result of performing serialize a <'font-weight'>,
  87. // followed by the string ";", i.e., SEMICOLON (U+003B).
  88. // FIXME: 11. If rule’s associated font-style descriptor is present, a single SPACE (U+0020),
  89. // followed by the string "font-style:", followed by a single SPACE (U+0020),
  90. // followed by the result of performing serialize a <'font-style'>,
  91. // followed by the string ";", i.e., SEMICOLON (U+003B).
  92. // 12. A single SPACE (U+0020), followed by the string "}", i.e., RIGHT CURLY BRACKET (U+007D).
  93. builder.append(" }"sv);
  94. return builder.to_deprecated_string();
  95. }
  96. }