summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/CSS/CSSSupportsRule.cpp
blob: 54acba5ff4ed9edd00d663221745b91ad62b99bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
 * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibWeb/Bindings/CSSSupportsRulePrototype.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/CSS/CSSSupportsRule.h>
#include <LibWeb/CSS/Parser/Parser.h>

namespace Web::CSS {

CSSSupportsRule* CSSSupportsRule::create(JS::Realm& realm, NonnullRefPtr<Supports>&& supports, CSSRuleList& rules)
{
    return realm.heap().allocate<CSSSupportsRule>(realm, realm, move(supports), rules).release_allocated_value_but_fixme_should_propagate_errors();
}

CSSSupportsRule::CSSSupportsRule(JS::Realm& realm, NonnullRefPtr<Supports>&& supports, CSSRuleList& rules)
    : CSSConditionRule(realm, rules)
    , m_supports(move(supports))
{
}

JS::ThrowCompletionOr<void> CSSSupportsRule::initialize(JS::Realm& realm)
{
    MUST_OR_THROW_OOM(Base::initialize(realm));
    set_prototype(&Bindings::ensure_web_prototype<Bindings::CSSSupportsRulePrototype>(realm, "CSSSupportsRule"));

    return {};
}

DeprecatedString CSSSupportsRule::condition_text() const
{
    return m_supports->to_string().release_value_but_fixme_should_propagate_errors().to_deprecated_string();
}

void CSSSupportsRule::set_condition_text(DeprecatedString text)
{
    if (auto new_supports = parse_css_supports({}, text))
        m_supports = new_supports.release_nonnull();
}

// https://www.w3.org/TR/cssom-1/#serialize-a-css-rule
DeprecatedString CSSSupportsRule::serialized() const
{
    // Note: The spec doesn't cover this yet, so I'm roughly following the spec for the @media rule.
    // It should be pretty close!

    StringBuilder builder;

    builder.append("@supports "sv);
    builder.append(condition_text());
    builder.append(" {\n"sv);
    for (size_t i = 0; i < css_rules().length(); i++) {
        auto rule = css_rules().item(i);
        if (i != 0)
            builder.append("\n"sv);
        builder.append("  "sv);
        builder.append(rule->css_text());
    }
    builder.append("\n}"sv);

    return builder.to_deprecated_string();
}

}