CSSStyleDeclarationWrapperCustom.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/CSSStyleDeclarationWrapper.h>
  7. #include <LibWeb/DOM/Element.h>
  8. namespace Web::Bindings {
  9. bool CSSStyleDeclarationWrapper::internal_has_property(JS::PropertyName const& name) const
  10. {
  11. if (!name.is_string())
  12. return Base::internal_has_property(name);
  13. // FIXME: These should actually use camelCase versions of the property names!
  14. auto property_id = CSS::property_id_from_string(name.to_string());
  15. return property_id != CSS::PropertyID::Invalid;
  16. }
  17. JS::Value CSSStyleDeclarationWrapper::internal_get(JS::PropertyName const& name, JS::Value receiver) const
  18. {
  19. if (!name.is_string())
  20. return Base::internal_get(name, receiver);
  21. // FIXME: These should actually use camelCase versions of the property names!
  22. auto property_id = CSS::property_id_from_string(name.to_string());
  23. if (property_id == CSS::PropertyID::Invalid)
  24. return Base::internal_get(name, receiver);
  25. if (auto maybe_property = impl().property(property_id); maybe_property.has_value())
  26. return js_string(vm(), maybe_property->value->to_string());
  27. return js_string(vm(), String::empty());
  28. }
  29. bool CSSStyleDeclarationWrapper::internal_set(JS::PropertyName const& name, JS::Value value, JS::Value receiver)
  30. {
  31. if (!name.is_string())
  32. return Base::internal_set(name, value, receiver);
  33. // FIXME: These should actually use camelCase versions of the property names!
  34. auto property_id = CSS::property_id_from_string(name.to_string());
  35. if (property_id == CSS::PropertyID::Invalid)
  36. return Base::internal_set(name, value, receiver);
  37. auto css_text = value.to_string(global_object());
  38. if (vm().exception())
  39. return false;
  40. impl().set_property(property_id, css_text);
  41. return true;
  42. }
  43. }