1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-19 14:15:08 +00:00
serenity/Userland/Libraries/LibWeb/Bindings/CSSNamespace.cpp
Linus Groh ecd163bdf1 LibJS+LibWeb: Replace GlobalObject with Realm in object constructors
No functional changes - we can still very easily get to the global
object via `Realm::global_object()`. This is in preparation of moving
the intrinsics to the realm and no longer having to pass a global
object when allocating any object.
In a few (now, and many more in subsequent commits) places we get a
realm using `GlobalObject::associated_realm()`, this is intended to be
temporary. For example, create() functions will later receive the same
treatment and are passed a realm instead of a global object.
2022-08-23 13:58:30 +01:00

82 lines
3.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/ErrorTypes.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/VM.h>
#include <LibJS/Runtime/Value.h>
#include <LibWeb/Bindings/CSSNamespace.h>
#include <LibWeb/CSS/Parser/Parser.h>
namespace Web::Bindings {
CSSNamespace::CSSNamespace(JS::Realm& realm)
: JS::Object(*realm.global_object().object_prototype())
{
}
void CSSNamespace::initialize(JS::GlobalObject& global_object)
{
Object::initialize(global_object);
u8 attr = JS::Attribute::Enumerable;
define_native_function("escape", escape, 1, attr);
define_native_function("supports", supports, 2, attr);
}
// https://www.w3.org/TR/cssom-1/#dom-css-escape
JS_DEFINE_NATIVE_FUNCTION(CSSNamespace::escape)
{
if (!vm.argument_count())
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "CSS.escape");
auto identifier = TRY(vm.argument(0).to_string(global_object));
return JS::js_string(vm, Web::CSS::serialize_an_identifier(identifier));
}
// https://www.w3.org/TR/css-conditional-3/#dom-css-supports
JS_DEFINE_NATIVE_FUNCTION(CSSNamespace::supports)
{
if (!vm.argument_count())
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "CSS.supports");
if (vm.argument_count() >= 2) {
// When the supports(property, value) method is invoked with two arguments property and value:
auto property_name = TRY(vm.argument(0).to_string(global_object));
// If property is an ASCII case-insensitive match for any defined CSS property that the UA supports,
// and value successfully parses according to that propertys grammar, return true.
auto property = CSS::property_id_from_string(property_name);
if (property != CSS::PropertyID::Invalid) {
auto value_string = TRY(vm.argument(1).to_string(global_object));
if (parse_css_value({}, value_string, property))
return JS::Value(true);
}
// Otherwise, if property is a custom property name string, return true.
// FIXME: This check is not enough to make sure this is a valid custom property name, but it's close enough.
else if (property_name.starts_with("--"sv) && property_name.length() >= 3) {
return JS::Value(true);
}
// Otherwise, return false.
return JS::Value(false);
} else {
// When the supports(conditionText) method is invoked with a single conditionText argument:
auto supports_text = TRY(vm.argument(0).to_string(global_object));
// If conditionText, parsed and evaluated as a <supports-condition>, would return true, return true.
if (auto supports = parse_css_supports({}, supports_text); supports && supports->matches())
return JS::Value(true);
// Otherwise, If conditionText, wrapped in parentheses and then parsed and evaluated as a <supports-condition>, would return true, return true.
if (auto supports = parse_css_supports({}, String::formatted("({})", supports_text)); supports && supports->matches())
return JS::Value(true);
// Otherwise, return false.
return JS::Value(false);
}
}
}