2021-09-29 08:48:04 -03:00
|
|
|
|
/*
|
|
|
|
|
|
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
|
2024-10-04 08:19:50 -03:00
|
|
|
|
* Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
|
2021-09-29 08:48:04 -03:00
|
|
|
|
*
|
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2022-09-24 19:34:04 -03:00
|
|
|
|
#include <LibJS/Runtime/Realm.h>
|
|
|
|
|
|
#include <LibWeb/Bindings/CSSMediaRulePrototype.h>
|
|
|
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
2021-09-29 08:48:04 -03:00
|
|
|
|
#include <LibWeb/CSS/CSSMediaRule.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace Web::CSS {
|
|
|
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
|
GC_DEFINE_ALLOCATOR(CSSMediaRule);
|
2023-11-19 15:47:52 -03:00
|
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
|
GC::Ref<CSSMediaRule> CSSMediaRule::create(JS::Realm& realm, MediaList& media_queries, CSSRuleList& rules)
|
2022-08-07 10:46:44 -03:00
|
|
|
|
{
|
2024-11-13 13:50:17 -03:00
|
|
|
|
return realm.create<CSSMediaRule>(realm, media_queries, rules);
|
2022-08-07 10:46:44 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-24 19:34:04 -03:00
|
|
|
|
CSSMediaRule::CSSMediaRule(JS::Realm& realm, MediaList& media, CSSRuleList& rules)
|
2024-10-28 16:16:28 -03:00
|
|
|
|
: CSSConditionRule(realm, rules, Type::Media)
|
2022-08-08 10:32:27 -03:00
|
|
|
|
, m_media(media)
|
2021-09-29 08:48:04 -03:00
|
|
|
|
{
|
2023-01-10 08:28:20 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-07 03:41:28 -03:00
|
|
|
|
void CSSMediaRule::initialize(JS::Realm& realm)
|
2023-01-10 08:28:20 -03:00
|
|
|
|
{
|
2024-03-16 09:13:08 -03:00
|
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(CSSMediaRule);
|
2025-04-20 11:22:57 -03:00
|
|
|
|
Base::initialize(realm);
|
2021-09-29 08:48:04 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-08 10:32:27 -03:00
|
|
|
|
void CSSMediaRule::visit_edges(Cell::Visitor& visitor)
|
|
|
|
|
|
{
|
|
|
|
|
|
Base::visit_edges(visitor);
|
2023-02-26 20:09:02 -03:00
|
|
|
|
visitor.visit(m_media);
|
2022-08-08 10:32:27 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-26 02:28:31 -03:00
|
|
|
|
String CSSMediaRule::condition_text() const
|
2021-09-29 08:48:04 -03:00
|
|
|
|
{
|
2023-12-01 13:45:31 -03:00
|
|
|
|
return m_media->media_text();
|
2021-09-29 08:48:04 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-10-15 12:42:26 -03:00
|
|
|
|
// https://www.w3.org/TR/cssom-1/#serialize-a-css-rule
|
2023-11-20 07:16:39 -03:00
|
|
|
|
String CSSMediaRule::serialized() const
|
2021-10-15 12:42:26 -03:00
|
|
|
|
{
|
|
|
|
|
|
// The result of concatenating the following:
|
|
|
|
|
|
StringBuilder builder;
|
|
|
|
|
|
|
|
|
|
|
|
// 1. The string "@media", followed by a single SPACE (U+0020).
|
2022-07-11 14:32:29 -03:00
|
|
|
|
builder.append("@media "sv);
|
2021-10-15 12:42:26 -03:00
|
|
|
|
// 2. The result of performing serialize a media query list on rule’s media query list.
|
|
|
|
|
|
builder.append(condition_text());
|
|
|
|
|
|
// 3. A single SPACE (U+0020), followed by the string "{", i.e., LEFT CURLY BRACKET (U+007B), followed by a newline.
|
2022-07-11 14:32:29 -03:00
|
|
|
|
builder.append(" {\n"sv);
|
2024-10-09 11:02:30 -03:00
|
|
|
|
// 4. The result of performing serialize a CSS rule on each rule in the rule’s cssRules list,
|
|
|
|
|
|
// filtering out empty strings, indenting each item with two spaces, all joined with newline.
|
2021-10-15 12:42:26 -03:00
|
|
|
|
for (size_t i = 0; i < css_rules().length(); i++) {
|
|
|
|
|
|
auto rule = css_rules().item(i);
|
2024-10-09 11:02:30 -03:00
|
|
|
|
auto result = rule->css_text();
|
|
|
|
|
|
|
|
|
|
|
|
if (result.is_empty())
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
2022-07-11 14:32:29 -03:00
|
|
|
|
builder.append(" "sv);
|
2024-10-09 11:02:30 -03:00
|
|
|
|
builder.append(result);
|
|
|
|
|
|
builder.append('\n');
|
2021-10-15 12:42:26 -03:00
|
|
|
|
}
|
|
|
|
|
|
// 5. A newline, followed by the string "}", i.e., RIGHT CURLY BRACKET (U+007D)
|
2024-10-09 11:02:30 -03:00
|
|
|
|
// AD-HOC: All modern browsers omit the ending newline if there are no CSS rules, so let's do the same.
|
|
|
|
|
|
// If there are rules, the required newline will be appended in the for-loop above.
|
|
|
|
|
|
builder.append('}');
|
2021-10-15 12:42:26 -03:00
|
|
|
|
|
2023-11-20 07:16:39 -03:00
|
|
|
|
return MUST(builder.to_string());
|
2021-10-15 12:42:26 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-09-29 08:48:04 -03:00
|
|
|
|
}
|