CSSStyleDeclarationWrapperCustom.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. auto property_id = CSS::property_id_from_camel_case_string(name.to_string());
  14. return property_id != CSS::PropertyID::Invalid;
  15. }
  16. JS::Value CSSStyleDeclarationWrapper::internal_get(JS::PropertyName const& name, JS::Value receiver) const
  17. {
  18. if (!name.is_string())
  19. return Base::internal_get(name, receiver);
  20. auto property_id = CSS::property_id_from_camel_case_string(name.to_string());
  21. if (property_id == CSS::PropertyID::Invalid)
  22. return Base::internal_get(name, receiver);
  23. if (auto maybe_property = impl().property(property_id); maybe_property.has_value())
  24. return js_string(vm(), maybe_property->value->to_string());
  25. return js_string(vm(), String::empty());
  26. }
  27. bool CSSStyleDeclarationWrapper::internal_set(JS::PropertyName const& name, JS::Value value, JS::Value receiver)
  28. {
  29. if (!name.is_string())
  30. return Base::internal_set(name, value, receiver);
  31. auto property_id = CSS::property_id_from_camel_case_string(name.to_string());
  32. if (property_id == CSS::PropertyID::Invalid)
  33. return Base::internal_set(name, value, receiver);
  34. auto css_text = value.to_string(global_object());
  35. if (vm().exception())
  36. return false;
  37. impl().set_property(property_id, css_text);
  38. return true;
  39. }
  40. }