CSSFontFaceRule.cpp 5.1 KB

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