CSSStyleDeclarationWrapperCustom.cpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/ScopeGuard.h>
  27. #include <LibWeb/Bindings/CSSStyleDeclarationWrapper.h>
  28. #include <LibWeb/CSS/Parser/DeprecatedCSSParser.h>
  29. #include <LibWeb/DOM/Element.h>
  30. namespace Web::Bindings {
  31. JS::Value CSSStyleDeclarationWrapper::get(const JS::PropertyName& name, JS::Value receiver) const
  32. {
  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::get(name, receiver);
  37. for (auto& property : impl().properties()) {
  38. if (property.property_id == property_id)
  39. return js_string(vm(), property.value->to_string());
  40. }
  41. return js_string(vm(), String::empty());
  42. }
  43. bool CSSStyleDeclarationWrapper::put(const JS::PropertyName& name, JS::Value value, JS::Value receiver)
  44. {
  45. // FIXME: These should actually use camelCase versions of the property names!
  46. auto property_id = CSS::property_id_from_string(name.to_string());
  47. if (property_id == CSS::PropertyID::Invalid)
  48. return Base::put(name, value, receiver);
  49. auto css_text = value.to_string(global_object());
  50. if (vm().exception())
  51. return false;
  52. auto new_value = parse_css_value(CSS::ParsingContext {}, css_text, property_id);
  53. // FIXME: What are we supposed to do if we can't parse it?
  54. if (!new_value)
  55. return false;
  56. ScopeGuard style_invalidation_guard = [&] {
  57. auto& declaration = downcast<CSS::ElementInlineCSSStyleDeclaration>(impl());
  58. if (auto* element = declaration.element())
  59. element->invalidate_style();
  60. };
  61. // FIXME: I don't think '!important' is being handled correctly here..
  62. for (auto& property : impl().m_properties) {
  63. if (property.property_id == property_id) {
  64. property.value = new_value.release_nonnull();
  65. return true;
  66. }
  67. }
  68. impl().m_properties.append(CSS::StyleProperty {
  69. .property_id = property_id,
  70. .value = new_value.release_nonnull(),
  71. .important = false,
  72. });
  73. return true;
  74. }
  75. }