CSSNamespace.cpp 3.4 KB

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