CSSStyleDeclarationWrapperCustom.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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/DeprecatedCSSParser.h>
  9. #include <LibWeb/DOM/Element.h>
  10. namespace Web::Bindings {
  11. JS::Value CSSStyleDeclarationWrapper::get(const JS::PropertyName& name, JS::Value receiver, JS::AllowSideEffects allow_side_effects) const
  12. {
  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. if (property_id == CSS::PropertyID::Invalid)
  16. return Base::get(name, receiver, allow_side_effects);
  17. for (auto& property : impl().properties()) {
  18. if (property.property_id == property_id)
  19. return js_string(vm(), property.value->to_string());
  20. }
  21. return js_string(vm(), String::empty());
  22. }
  23. bool CSSStyleDeclarationWrapper::put(const JS::PropertyName& name, JS::Value value, JS::Value receiver)
  24. {
  25. // FIXME: These should actually use camelCase versions of the property names!
  26. auto property_id = CSS::property_id_from_string(name.to_string());
  27. if (property_id == CSS::PropertyID::Invalid)
  28. return Base::put(name, value, receiver);
  29. auto css_text = value.to_string(global_object());
  30. if (vm().exception())
  31. return false;
  32. auto new_value = parse_css_value(CSS::ParsingContext {}, css_text, property_id);
  33. // FIXME: What are we supposed to do if we can't parse it?
  34. if (!new_value)
  35. return false;
  36. ScopeGuard style_invalidation_guard = [&] {
  37. auto& declaration = downcast<CSS::ElementInlineCSSStyleDeclaration>(impl());
  38. if (auto* element = declaration.element())
  39. element->invalidate_style();
  40. };
  41. // FIXME: I don't think '!important' is being handled correctly here..
  42. for (auto& property : impl().m_properties) {
  43. if (property.property_id == property_id) {
  44. property.value = new_value.release_nonnull();
  45. return true;
  46. }
  47. }
  48. impl().m_properties.append(CSS::StyleProperty {
  49. .property_id = property_id,
  50. .value = new_value.release_nonnull(),
  51. .important = false,
  52. });
  53. return true;
  54. }
  55. }