CSSNamespace.cpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/ErrorTypes.h>
  7. #include <LibJS/Runtime/GlobalObject.h>
  8. #include <LibJS/Runtime/VM.h>
  9. #include <LibJS/Runtime/Value.h>
  10. #include <LibWeb/Bindings/CSSNamespace.h>
  11. #include <LibWeb/CSS/Parser/Parser.h>
  12. namespace Web::Bindings {
  13. CSSNamespace::CSSNamespace(JS::Realm& realm)
  14. : JS::Object(ConstructWithPrototypeTag::Tag, *realm.intrinsics().object_prototype())
  15. {
  16. }
  17. JS::ThrowCompletionOr<void> CSSNamespace::initialize(JS::Realm& realm)
  18. {
  19. MUST_OR_THROW_OOM(Object::initialize(realm));
  20. u8 attr = JS::Attribute::Enumerable;
  21. define_native_function(realm, "escape", escape, 1, attr);
  22. define_native_function(realm, "supports", supports, 2, attr);
  23. return {};
  24. }
  25. // https://www.w3.org/TR/cssom-1/#dom-css-escape
  26. JS_DEFINE_NATIVE_FUNCTION(CSSNamespace::escape)
  27. {
  28. if (!vm.argument_count())
  29. return vm.throw_completion<JS::TypeError>(JS::ErrorType::BadArgCountAtLeastOne, "CSS.escape");
  30. auto identifier = TRY(vm.argument(0).to_string(vm));
  31. return JS::PrimitiveString::create(vm, TRY_OR_THROW_OOM(vm, Web::CSS::serialize_an_identifier(identifier)));
  32. }
  33. // https://www.w3.org/TR/css-conditional-3/#dom-css-supports
  34. JS_DEFINE_NATIVE_FUNCTION(CSSNamespace::supports)
  35. {
  36. auto& realm = *vm.current_realm();
  37. if (!vm.argument_count())
  38. return vm.throw_completion<JS::TypeError>(JS::ErrorType::BadArgCountAtLeastOne, "CSS.supports");
  39. if (vm.argument_count() >= 2) {
  40. // When the supports(property, value) method is invoked with two arguments property and value:
  41. auto property_name = TRY(vm.argument(0).to_deprecated_string(vm));
  42. // If property is an ASCII case-insensitive match for any defined CSS property that the UA supports,
  43. // and value successfully parses according to that property’s grammar, return true.
  44. auto property = CSS::property_id_from_string(property_name);
  45. if (property != CSS::PropertyID::Invalid) {
  46. auto value_string = TRY(vm.argument(1).to_deprecated_string(vm));
  47. if (parse_css_value(CSS::Parser::ParsingContext { realm }, value_string, property))
  48. return JS::Value(true);
  49. }
  50. // Otherwise, if property is a custom property name string, return true.
  51. // FIXME: This check is not enough to make sure this is a valid custom property name, but it's close enough.
  52. else if (property_name.starts_with("--"sv) && property_name.length() >= 3) {
  53. return JS::Value(true);
  54. }
  55. // Otherwise, return false.
  56. return JS::Value(false);
  57. } else {
  58. // When the supports(conditionText) method is invoked with a single conditionText argument:
  59. auto supports_text = TRY(vm.argument(0).to_deprecated_string(vm));
  60. // If conditionText, parsed and evaluated as a <supports-condition>, would return true, return true.
  61. if (auto supports = parse_css_supports(CSS::Parser::ParsingContext { realm }, supports_text); supports && supports->matches())
  62. return JS::Value(true);
  63. // Otherwise, If conditionText, wrapped in parentheses and then parsed and evaluated as a <supports-condition>, would return true, return true.
  64. if (auto supports = parse_css_supports(CSS::Parser::ParsingContext { realm }, DeprecatedString::formatted("({})", supports_text)); supports && supports->matches())
  65. return JS::Value(true);
  66. // Otherwise, return false.
  67. return JS::Value(false);
  68. }
  69. }
  70. }