CSSNamespace.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (c) 2021, 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::GlobalObject& global_object)
  14. : JS::Object(*global_object.object_prototype())
  15. {
  16. }
  17. CSSNamespace::~CSSNamespace()
  18. {
  19. }
  20. void CSSNamespace::initialize(JS::GlobalObject& global_object)
  21. {
  22. Object::initialize(global_object);
  23. u8 attr = JS::Attribute::Enumerable;
  24. define_native_function("escape", escape, 1, attr);
  25. define_native_function("supports", supports, 2, attr);
  26. }
  27. // https://www.w3.org/TR/cssom-1/#dom-css-escape
  28. JS_DEFINE_NATIVE_FUNCTION(CSSNamespace::escape)
  29. {
  30. if (!vm.argument_count())
  31. return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "CSS.escape");
  32. auto identifier = TRY(vm.argument(0).to_string(global_object));
  33. return JS::js_string(vm, Web::CSS::serialize_an_identifier(identifier));
  34. }
  35. // https://www.w3.org/TR/css-conditional-3/#dom-css-supports
  36. JS_DEFINE_NATIVE_FUNCTION(CSSNamespace::supports)
  37. {
  38. if (!vm.argument_count())
  39. return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "CSS.supports");
  40. if (vm.argument_count() >= 2) {
  41. // When the supports(property, value) method is invoked with two arguments property and value:
  42. auto property_name = TRY(vm.argument(0).to_string(global_object));
  43. // If property is an ASCII case-insensitive match for any defined CSS property that the UA supports,
  44. // and value successfully parses according to that property’s grammar, return true.
  45. auto property = CSS::property_id_from_string(property_name);
  46. if (property != CSS::PropertyID::Invalid) {
  47. auto value_string = TRY(vm.argument(1).to_string(global_object));
  48. if (parse_css_value({}, value_string, property))
  49. return JS::Value(true);
  50. }
  51. // Otherwise, if property is a custom property name string, return true.
  52. // FIXME: This check is not enough to make sure this is a valid custom property name, but it's close enough.
  53. else if (property_name.starts_with("--") && property_name.length() >= 3) {
  54. return JS::Value(true);
  55. }
  56. // Otherwise, return false.
  57. return JS::Value(false);
  58. } else {
  59. // When the supports(conditionText) method is invoked with a single conditionText argument:
  60. auto supports_text = TRY(vm.argument(0).to_string(global_object));
  61. // If conditionText, parsed and evaluated as a <supports-condition>, would return true, return true.
  62. if (auto supports = parse_css_supports({}, supports_text); supports && supports->matches())
  63. return JS::Value(true);
  64. // Otherwise, If conditionText, wrapped in parentheses and then parsed and evaluated as a <supports-condition>, would return true, return true.
  65. if (auto supports = parse_css_supports({}, String::formatted("({})", supports_text)); supports && supports->matches())
  66. return JS::Value(true);
  67. // Otherwise, return false.
  68. return JS::Value(false);
  69. }
  70. }
  71. }