2021-10-06 10:27:13 -03:00
|
|
|
|
/*
|
|
|
|
|
|
* 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>
|
2021-10-06 11:31:22 -03:00
|
|
|
|
#include <LibWeb/CSS/Parser/Parser.h>
|
2021-10-06 10:27:13 -03:00
|
|
|
|
|
|
|
|
|
|
namespace Web::Bindings {
|
|
|
|
|
|
|
2022-08-15 20:20:49 -03:00
|
|
|
|
CSSNamespace::CSSNamespace(JS::Realm& realm)
|
2022-08-26 20:54:55 -03:00
|
|
|
|
: JS::Object(*realm.intrinsics().object_prototype())
|
2021-10-06 10:27:13 -03:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-15 20:20:49 -03:00
|
|
|
|
void CSSNamespace::initialize(JS::Realm& realm)
|
2021-10-06 10:27:13 -03:00
|
|
|
|
{
|
2022-08-15 20:20:49 -03:00
|
|
|
|
Object::initialize(realm);
|
2021-10-06 10:27:13 -03:00
|
|
|
|
u8 attr = JS::Attribute::Enumerable;
|
2022-08-22 17:47:35 -03:00
|
|
|
|
define_native_function(realm, "escape", escape, 1, attr);
|
|
|
|
|
|
define_native_function(realm, "supports", supports, 2, attr);
|
2021-10-06 10:27:13 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// https://www.w3.org/TR/cssom-1/#dom-css-escape
|
2021-10-31 09:34:54 -03:00
|
|
|
|
JS_DEFINE_NATIVE_FUNCTION(CSSNamespace::escape)
|
2021-10-06 10:27:13 -03:00
|
|
|
|
{
|
2021-10-31 09:34:54 -03:00
|
|
|
|
if (!vm.argument_count())
|
2022-08-16 16:33:17 -03:00
|
|
|
|
return vm.throw_completion<JS::TypeError>(JS::ErrorType::BadArgCountAtLeastOne, "CSS.escape");
|
2021-10-06 10:27:13 -03:00
|
|
|
|
|
2022-08-21 10:00:56 -03:00
|
|
|
|
auto identifier = TRY(vm.argument(0).to_string(vm));
|
2021-10-09 21:55:19 -03:00
|
|
|
|
return JS::js_string(vm, Web::CSS::serialize_an_identifier(identifier));
|
2021-10-06 10:27:13 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// https://www.w3.org/TR/css-conditional-3/#dom-css-supports
|
2021-10-31 09:34:54 -03:00
|
|
|
|
JS_DEFINE_NATIVE_FUNCTION(CSSNamespace::supports)
|
2021-10-06 10:27:13 -03:00
|
|
|
|
{
|
2021-10-31 09:34:54 -03:00
|
|
|
|
if (!vm.argument_count())
|
2022-08-16 16:33:17 -03:00
|
|
|
|
return vm.throw_completion<JS::TypeError>(JS::ErrorType::BadArgCountAtLeastOne, "CSS.supports");
|
2021-10-06 10:27:13 -03:00
|
|
|
|
|
|
|
|
|
|
if (vm.argument_count() >= 2) {
|
|
|
|
|
|
// When the supports(property, value) method is invoked with two arguments property and value:
|
2022-08-21 10:00:56 -03:00
|
|
|
|
auto property_name = TRY(vm.argument(0).to_string(vm));
|
2021-10-06 11:31:22 -03:00
|
|
|
|
|
|
|
|
|
|
// If property is an ASCII case-insensitive match for any defined CSS property that the UA supports,
|
|
|
|
|
|
// and value successfully parses according to that property’s grammar, return true.
|
|
|
|
|
|
auto property = CSS::property_id_from_string(property_name);
|
|
|
|
|
|
if (property != CSS::PropertyID::Invalid) {
|
2022-08-21 10:00:56 -03:00
|
|
|
|
auto value_string = TRY(vm.argument(1).to_string(vm));
|
2021-10-06 11:31:22 -03:00
|
|
|
|
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.
|
2022-07-11 14:32:29 -03:00
|
|
|
|
else if (property_name.starts_with("--"sv) && property_name.length() >= 3) {
|
2021-10-06 11:31:22 -03:00
|
|
|
|
return JS::Value(true);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Otherwise, return false.
|
2021-10-06 10:27:13 -03:00
|
|
|
|
return JS::Value(false);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// When the supports(conditionText) method is invoked with a single conditionText argument:
|
2022-08-21 10:00:56 -03:00
|
|
|
|
auto supports_text = TRY(vm.argument(0).to_string(vm));
|
2021-10-08 12:00:05 -03:00
|
|
|
|
|
|
|
|
|
|
// 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.
|
2021-10-06 10:27:13 -03:00
|
|
|
|
return JS::Value(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|