ElementWrapper.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (c) 2020, 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/FlyString.h>
  27. #include <AK/Function.h>
  28. #include <LibJS/Interpreter.h>
  29. #include <LibJS/Runtime/Error.h>
  30. #include <LibJS/Runtime/PrimitiveString.h>
  31. #include <LibJS/Runtime/Value.h>
  32. #include <LibWeb/Bindings/ElementWrapper.h>
  33. #include <LibWeb/DOM/Element.h>
  34. namespace Web {
  35. namespace Bindings {
  36. ElementWrapper::ElementWrapper(Element& element)
  37. : NodeWrapper(element)
  38. {
  39. put_native_property("innerHTML", inner_html_getter, inner_html_setter);
  40. put_native_property("id", id_getter, id_setter);
  41. u8 attributes = JS::Attribute::Configurable | JS::Attribute::Enumerable | JS::Attribute::Writable;
  42. put_native_function("getAttribute", get_attribute, 1, attributes);
  43. put_native_function("setAttribute", set_attribute, 2, attributes);
  44. }
  45. ElementWrapper::~ElementWrapper()
  46. {
  47. }
  48. Element& ElementWrapper::node()
  49. {
  50. return static_cast<Element&>(NodeWrapper::node());
  51. }
  52. const Element& ElementWrapper::node() const
  53. {
  54. return static_cast<const Element&>(NodeWrapper::node());
  55. }
  56. static Element* impl_from(JS::Interpreter& interpreter)
  57. {
  58. auto* this_object = interpreter.this_value().to_object(interpreter);
  59. if (!this_object)
  60. return nullptr;
  61. // FIXME: Verify that it's an ElementWrapper somehow!
  62. return &static_cast<ElementWrapper*>(this_object)->node();
  63. }
  64. JS::Value ElementWrapper::get_attribute(JS::Interpreter& interpreter)
  65. {
  66. auto* impl = impl_from(interpreter);
  67. if (!impl)
  68. return {};
  69. if (interpreter.argument_count() < 1)
  70. return interpreter.throw_exception<JS::TypeError>("getAttribute() needs one argument");
  71. auto attribute_name = interpreter.argument(0).to_string(interpreter);
  72. if (interpreter.exception())
  73. return {};
  74. auto attribute_value = impl->attribute(attribute_name);
  75. if (attribute_value.is_null())
  76. return JS::js_null();
  77. return JS::js_string(interpreter, attribute_value);
  78. }
  79. JS::Value ElementWrapper::set_attribute(JS::Interpreter& interpreter)
  80. {
  81. auto* impl = impl_from(interpreter);
  82. if (!impl)
  83. return {};
  84. if (interpreter.argument_count() < 2)
  85. return interpreter.throw_exception<JS::TypeError>("setAttribute() needs two arguments");
  86. auto attribute_name = interpreter.argument(0).to_string(interpreter);
  87. if (interpreter.exception())
  88. return {};
  89. auto attribute_value = interpreter.argument(1).to_string(interpreter);
  90. if (interpreter.exception())
  91. return {};
  92. impl->set_attribute(attribute_name, attribute_value);
  93. return JS::js_undefined();
  94. }
  95. JS::Value ElementWrapper::inner_html_getter(JS::Interpreter& interpreter)
  96. {
  97. if (auto* impl = impl_from(interpreter))
  98. return JS::js_string(interpreter, impl->inner_html());
  99. return {};
  100. }
  101. void ElementWrapper::inner_html_setter(JS::Interpreter& interpreter, JS::Value value)
  102. {
  103. if (auto* impl = impl_from(interpreter)) {
  104. auto string = value.to_string(interpreter);
  105. if (interpreter.exception())
  106. return;
  107. impl->set_inner_html(string);
  108. }
  109. }
  110. JS::Value ElementWrapper::id_getter(JS::Interpreter& interpreter)
  111. {
  112. if (auto* impl = impl_from(interpreter))
  113. return JS::js_string(interpreter, impl->attribute("id"));
  114. return {};
  115. }
  116. void ElementWrapper::id_setter(JS::Interpreter& interpreter, JS::Value value)
  117. {
  118. if (auto* impl = impl_from(interpreter)) {
  119. auto string = value.to_string(interpreter);
  120. if (interpreter.exception())
  121. return;
  122. impl->set_attribute("id", string);
  123. }
  124. }
  125. }
  126. }