CSSStyleDeclarationWrapperCustom.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/Completion.h>
  7. #include <LibWeb/Bindings/CSSStyleDeclarationWrapper.h>
  8. #include <LibWeb/DOM/Element.h>
  9. namespace Web::Bindings {
  10. static CSS::PropertyID property_id_from_name(StringView name)
  11. {
  12. // FIXME: Perhaps this should go in the code generator.
  13. if (name == "cssFloat"sv)
  14. return CSS::PropertyID::Float;
  15. if (auto property_id = CSS::property_id_from_camel_case_string(name); property_id != CSS::PropertyID::Invalid)
  16. return property_id;
  17. if (auto property_id = CSS::property_id_from_string(name); property_id != CSS::PropertyID::Invalid)
  18. return property_id;
  19. return CSS::PropertyID::Invalid;
  20. }
  21. JS::ThrowCompletionOr<bool> CSSStyleDeclarationWrapper::internal_has_property(JS::PropertyKey const& name) const
  22. {
  23. if (!name.is_string())
  24. return Base::internal_has_property(name);
  25. return property_id_from_name(name.to_string()) != CSS::PropertyID::Invalid;
  26. }
  27. JS::ThrowCompletionOr<JS::Value> CSSStyleDeclarationWrapper::internal_get(JS::PropertyKey const& name, JS::Value receiver) const
  28. {
  29. if (!name.is_string())
  30. return Base::internal_get(name, receiver);
  31. auto property_id = property_id_from_name(name.to_string());
  32. if (property_id == CSS::PropertyID::Invalid)
  33. return Base::internal_get(name, receiver);
  34. if (auto maybe_property = impl().property(property_id); maybe_property.has_value())
  35. return { js_string(vm(), maybe_property->value->to_string()) };
  36. return { js_string(vm(), String::empty()) };
  37. }
  38. JS::ThrowCompletionOr<bool> CSSStyleDeclarationWrapper::internal_set(JS::PropertyKey const& name, JS::Value value, JS::Value receiver)
  39. {
  40. if (!name.is_string())
  41. return Base::internal_set(name, value, receiver);
  42. auto property_id = property_id_from_name(name.to_string());
  43. if (property_id == CSS::PropertyID::Invalid)
  44. return Base::internal_set(name, value, receiver);
  45. auto css_text = TRY(value.to_string(global_object()));
  46. impl().set_property(property_id, css_text);
  47. return true;
  48. }
  49. }