summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/CSS/CSSSupportsRule.cpp
blob: 347852ff25cff44290d0cbc5233b21ba9e7c5c5d (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
/*
 * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibWeb/CSS/CSSSupportsRule.h>
#include <LibWeb/CSS/Parser/Parser.h>

namespace Web::CSS {

CSSSupportsRule::CSSSupportsRule(NonnullRefPtr<Supports>&& supports, NonnullRefPtrVector<CSSRule>&& rules)
    : CSSConditionRule(move(rules))
    , m_supports(move(supports))
{
}

CSSSupportsRule::~CSSSupportsRule()
{
}

String CSSSupportsRule::condition_text() const
{
    // FIXME: Serializing supports rules!
    return "<supports-condition>";
}

void CSSSupportsRule::set_condition_text(String 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
String 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 ");
    builder.append(condition_text());
    builder.append(" {\n");
    for (size_t i = 0; i < css_rules().length(); i++) {
        auto rule = css_rules().item(i);
        if (i != 0)
            builder.append("\n");
        builder.append("  ");
        builder.append(rule->css_text());
    }
    builder.append("\n}");

    return builder.to_string();
}

}