CSSNamespace.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "CSS.escape");
  32. return {};
  33. }
  34. String result = Web::CSS::serialize_an_identifier(vm.argument(0).to_string(global_object));
  35. if (vm.exception())
  36. return {};
  37. return JS::Value(JS::js_string(vm, result));
  38. }
  39. // https://www.w3.org/TR/css-conditional-3/#dom-css-supports
  40. JS_DEFINE_NATIVE_FUNCTION(CSSNamespace::supports)
  41. {
  42. if (!vm.argument_count()) {
  43. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "CSS.supports");
  44. return {};
  45. }
  46. if (vm.argument_count() >= 2) {
  47. // When the supports(property, value) method is invoked with two arguments property and value:
  48. String property_name = vm.argument(0).to_string(global_object);
  49. if (vm.exception())
  50. return {};
  51. // If property is an ASCII case-insensitive match for any defined CSS property that the UA supports,
  52. // and value successfully parses according to that property’s grammar, return true.
  53. auto property = CSS::property_id_from_string(property_name);
  54. if (property != CSS::PropertyID::Invalid) {
  55. auto value_string = vm.argument(1).to_string(global_object);
  56. if (vm.exception())
  57. return {};
  58. if (parse_css_value({}, value_string, property))
  59. return JS::Value(true);
  60. }
  61. // Otherwise, if property is a custom property name string, return true.
  62. // FIXME: This check is not enough to make sure this is a valid custom property name, but it's close enough.
  63. else if (property_name.starts_with("--") && property_name.length() >= 3) {
  64. return JS::Value(true);
  65. }
  66. // Otherwise, return false.
  67. return JS::Value(false);
  68. } else {
  69. // When the supports(conditionText) method is invoked with a single conditionText argument:
  70. String supports_text = vm.argument(0).to_string(global_object);
  71. if (vm.exception())
  72. return {};
  73. // If conditionText, parsed and evaluated as a <supports-condition>, would return true, return true.
  74. if (auto supports = parse_css_supports({}, supports_text); supports && supports->matches())
  75. return JS::Value(true);
  76. // Otherwise, If conditionText, wrapped in parentheses and then parsed and evaluated as a <supports-condition>, would return true, return true.
  77. if (auto supports = parse_css_supports({}, String::formatted("({})", supports_text)); supports && supports->matches())
  78. return JS::Value(true);
  79. // Otherwise, return false.
  80. return JS::Value(false);
  81. }
  82. }
  83. }