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