CSSNamespace.cpp 3.3 KB

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