Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <LibWeb/Bindings/CSSStyleRulePrototype.h>
8#include <LibWeb/Bindings/Intrinsics.h>
9#include <LibWeb/CSS/CSSStyleRule.h>
10#include <LibWeb/CSS/Parser/Parser.h>
11#include <LibWeb/WebIDL/ExceptionOr.h>
12
13namespace Web::CSS {
14
15WebIDL::ExceptionOr<JS::NonnullGCPtr<CSSStyleRule>> CSSStyleRule::create(JS::Realm& realm, Vector<NonnullRefPtr<Web::CSS::Selector>>&& selectors, CSSStyleDeclaration& declaration)
16{
17 return MUST_OR_THROW_OOM(realm.heap().allocate<CSSStyleRule>(realm, realm, move(selectors), declaration));
18}
19
20CSSStyleRule::CSSStyleRule(JS::Realm& realm, Vector<NonnullRefPtr<Selector>>&& selectors, CSSStyleDeclaration& declaration)
21 : CSSRule(realm)
22 , m_selectors(move(selectors))
23 , m_declaration(declaration)
24{
25}
26
27JS::ThrowCompletionOr<void> CSSStyleRule::initialize(JS::Realm& realm)
28{
29 MUST_OR_THROW_OOM(Base::initialize(realm));
30 set_prototype(&Bindings::ensure_web_prototype<Bindings::CSSStyleRulePrototype>(realm, "CSSStyleRule"));
31
32 return {};
33}
34
35void CSSStyleRule::visit_edges(Cell::Visitor& visitor)
36{
37 Base::visit_edges(visitor);
38 visitor.visit(&m_declaration);
39}
40
41// https://www.w3.org/TR/cssom/#dom-cssstylerule-style
42CSSStyleDeclaration* CSSStyleRule::style()
43{
44 return &m_declaration;
45}
46
47// https://www.w3.org/TR/cssom/#serialize-a-css-rule
48DeprecatedString CSSStyleRule::serialized() const
49{
50 StringBuilder builder;
51
52 // 1. Let s initially be the result of performing serialize a group of selectors on the rule’s associated selectors,
53 // followed by the string " {", i.e., a single SPACE (U+0020), followed by LEFT CURLY BRACKET (U+007B).
54 builder.append(serialize_a_group_of_selectors(selectors()).release_value_but_fixme_should_propagate_errors());
55 builder.append(" {"sv);
56
57 // 2. Let decls be the result of performing serialize a CSS declaration block on the rule’s associated declarations, or null if there are no such declarations.
58 auto decls = declaration().serialized();
59
60 // FIXME: 3. Let rules be the result of performing serialize a CSS rule on each rule in the rule’s cssRules list, or null if there are no such rules.
61 DeprecatedString rules;
62
63 // 4. If decls and rules are both null, append " }" to s (i.e. a single SPACE (U+0020) followed by RIGHT CURLY BRACKET (U+007D)) and return s.
64 if (decls.is_null() && rules.is_null()) {
65 builder.append(" }"sv);
66 return builder.to_deprecated_string();
67 }
68
69 // 5. If rules is null:
70 if (rules.is_null()) {
71 // 1. Append a single SPACE (U+0020) to s
72 builder.append(' ');
73 // 2. Append decls to s
74 builder.append(decls);
75 // 3. Append " }" to s (i.e. a single SPACE (U+0020) followed by RIGHT CURLY BRACKET (U+007D)).
76 builder.append(" }"sv);
77 // 4. Return s.
78 return builder.to_deprecated_string();
79 }
80
81 // FIXME: 6. Otherwise:
82 // FIXME: 1. If decls is not null, prepend it to rules.
83 // FIXME: 2. For each rule in rules:
84 // FIXME: 1. Append a newline followed by two spaces to s.
85 // FIXME: 2. Append rule to s.
86 // FIXME: 3. Append a newline followed by RIGHT CURLY BRACKET (U+007D) to s.
87 // FIXME: 4. Return s.
88 TODO();
89}
90
91// https://www.w3.org/TR/cssom/#dom-cssstylerule-selectortext
92DeprecatedString CSSStyleRule::selector_text() const
93{
94 // The selectorText attribute, on getting, must return the result of serializing the associated group of selectors.
95 return serialize_a_group_of_selectors(selectors()).release_value_but_fixme_should_propagate_errors().to_deprecated_string();
96}
97
98// https://www.w3.org/TR/cssom/#dom-cssstylerule-selectortext
99void CSSStyleRule::set_selector_text(StringView selector_text)
100{
101 // 1. Run the parse a group of selectors algorithm on the given value.
102 auto parsed_selectors = parse_selector(Parser::ParsingContext { realm() }, selector_text);
103
104 // 2. If the algorithm returns a non-null value replace the associated group of selectors with the returned value.
105 if (parsed_selectors.has_value())
106 m_selectors = parsed_selectors.release_value();
107
108 // 3. Otherwise, if the algorithm returns a null value, do nothing.
109}
110
111}