ladybird/Userland/Libraries/LibWeb/CSS/CSSSupportsRule.cpp
Linus Groh 6e19ab2bbc AK+Everywhere: Rename String to DeprecatedString
We have a new, improved string type coming up in AK (OOM aware, no null
state), and while it's going to use UTF-8, the name UTF8String is a
mouthful - so let's free up the String name by renaming the existing
class.
Making the old one have an annoying name will hopefully also help with
quick adoption :^)
2022-12-06 08:54:33 +01:00

60 lines
1.7 KiB
C++

/*
* Copyright (c) 2021, 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);
}
CSSSupportsRule::CSSSupportsRule(JS::Realm& realm, NonnullRefPtr<Supports>&& supports, CSSRuleList& rules)
: CSSConditionRule(realm, rules)
, m_supports(move(supports))
{
set_prototype(&Bindings::ensure_web_prototype<Bindings::CSSSupportsRulePrototype>(realm, "CSSSupportsRule"));
}
DeprecatedString CSSSupportsRule::condition_text() const
{
return m_supports->to_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_string();
}
}