CSSStyleDeclarationWrapperCustom.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/ScopeGuard.h>
  7. #include <LibWeb/Bindings/CSSStyleDeclarationWrapper.h>
  8. #include <LibWeb/CSS/Parser/Parser.h>
  9. #include <LibWeb/DOM/Element.h>
  10. namespace Web::Bindings {
  11. bool CSSStyleDeclarationWrapper::internal_has_property(JS::PropertyName const& name) const
  12. {
  13. if (!name.is_string())
  14. return Base::internal_has_property(name);
  15. // FIXME: These should actually use camelCase versions of the property names!
  16. auto property_id = CSS::property_id_from_string(name.to_string());
  17. return property_id != CSS::PropertyID::Invalid;
  18. }
  19. JS::Value CSSStyleDeclarationWrapper::internal_get(JS::PropertyName const& name, JS::Value receiver) const
  20. {
  21. if (!name.is_string())
  22. return Base::internal_get(name, receiver);
  23. // FIXME: These should actually use camelCase versions of the property names!
  24. auto property_id = CSS::property_id_from_string(name.to_string());
  25. if (property_id == CSS::PropertyID::Invalid)
  26. return Base::internal_get(name, receiver);
  27. for (auto& property : impl().properties()) {
  28. if (property.property_id == property_id)
  29. return js_string(vm(), property.value->to_string());
  30. }
  31. return js_string(vm(), String::empty());
  32. }
  33. bool CSSStyleDeclarationWrapper::internal_set(JS::PropertyName const& name, JS::Value value, JS::Value receiver)
  34. {
  35. if (!name.is_string())
  36. return Base::internal_set(name, value, receiver);
  37. // FIXME: These should actually use camelCase versions of the property names!
  38. auto property_id = CSS::property_id_from_string(name.to_string());
  39. if (property_id == CSS::PropertyID::Invalid)
  40. return Base::internal_set(name, value, receiver);
  41. auto css_text = value.to_string(global_object());
  42. if (vm().exception())
  43. return false;
  44. auto new_value = parse_css_value(CSS::ParsingContext {}, css_text, property_id);
  45. // FIXME: What are we supposed to do if we can't parse it?
  46. if (!new_value)
  47. return false;
  48. ScopeGuard style_invalidation_guard = [&] {
  49. auto& declaration = verify_cast<CSS::ElementInlineCSSStyleDeclaration>(impl());
  50. if (auto* element = declaration.element())
  51. element->invalidate_style();
  52. };
  53. // FIXME: I don't think '!important' is being handled correctly here..
  54. for (auto& property : impl().m_properties) {
  55. if (property.property_id == property_id) {
  56. property.value = new_value.release_nonnull();
  57. return true;
  58. }
  59. }
  60. impl().m_properties.append(CSS::StyleProperty {
  61. .property_id = property_id,
  62. .value = new_value.release_nonnull(),
  63. .important = false,
  64. });
  65. return true;
  66. }
  67. }