GlobalEventHandlers.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 <LibJS/Interpreter.h>
  27. #include <LibJS/Parser.h>
  28. #include <LibJS/Runtime/ScriptFunction.h>
  29. #include <LibWeb/DOM/Document.h>
  30. #include <LibWeb/DOM/EventListener.h>
  31. #include <LibWeb/HTML/EventHandler.h>
  32. #include <LibWeb/HTML/EventNames.h>
  33. #include <LibWeb/HTML/GlobalEventHandlers.h>
  34. #include <LibWeb/UIEvents/EventNames.h>
  35. namespace Web::HTML {
  36. #undef __ENUMERATE
  37. #define __ENUMERATE(attribute_name, event_name) \
  38. void GlobalEventHandlers::set_##attribute_name(HTML::EventHandler value) \
  39. { \
  40. set_event_handler_attribute(event_name, move(value)); \
  41. } \
  42. HTML::EventHandler GlobalEventHandlers::attribute_name() \
  43. { \
  44. return get_event_handler_attribute(event_name); \
  45. }
  46. ENUMERATE_GLOBAL_EVENT_HANDLERS(__ENUMERATE)
  47. #undef __ENUMERATE
  48. GlobalEventHandlers::~GlobalEventHandlers()
  49. {
  50. }
  51. void GlobalEventHandlers::set_event_handler_attribute(const FlyString& name, HTML::EventHandler value)
  52. {
  53. auto& self = global_event_handlers_to_event_target();
  54. RefPtr<DOM::EventListener> listener;
  55. if (!value.callback.is_null()) {
  56. listener = adopt(*new DOM::EventListener(move(value.callback)));
  57. } else {
  58. StringBuilder builder;
  59. builder.appendff("function {}(event) {{\n{}\n}}", name, value.string);
  60. auto parser = JS::Parser(JS::Lexer(builder.string_view()));
  61. auto program = parser.parse_function_node<JS::FunctionExpression>();
  62. if (parser.has_errors()) {
  63. dbgln("Failed to parse script in event handler attribute '{}'", name);
  64. return;
  65. }
  66. auto* function = JS::ScriptFunction::create(self.script_execution_context()->interpreter().global_object(), name, program->body(), program->parameters(), program->function_length(), nullptr, false, false);
  67. VERIFY(function);
  68. listener = adopt(*new DOM::EventListener(JS::make_handle(static_cast<JS::Function*>(function))));
  69. }
  70. if (listener) {
  71. for (auto& registered_listener : self.listeners()) {
  72. if (registered_listener.event_name == name && registered_listener.listener->is_attribute()) {
  73. self.remove_event_listener(name, registered_listener.listener);
  74. break;
  75. }
  76. }
  77. self.add_event_listener(name, listener.release_nonnull());
  78. }
  79. }
  80. HTML::EventHandler GlobalEventHandlers::get_event_handler_attribute(const FlyString& name)
  81. {
  82. auto& self = global_event_handlers_to_event_target();
  83. for (auto& listener : self.listeners()) {
  84. if (listener.event_name == name && listener.listener->is_attribute()) {
  85. return HTML::EventHandler { JS::make_handle(&listener.listener->function()) };
  86. }
  87. }
  88. return {};
  89. }
  90. }