ConsoleWidget.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (c) 2020, Hunter Salyer <thefalsehonesty@gmail.com>
  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 "ConsoleWidget.h"
  27. #include <AK/StringBuilder.h>
  28. #include <LibGUI/BoxLayout.h>
  29. #include <LibGUI/Button.h>
  30. #include <LibGUI/JSSyntaxHighlighter.h>
  31. #include <LibGUI/TextBox.h>
  32. #include <LibJS/Interpreter.h>
  33. #include <LibJS/MarkupGenerator.h>
  34. #include <LibJS/Parser.h>
  35. #include <LibJS/Runtime/Error.h>
  36. #include <LibWeb/DOM/DocumentType.h>
  37. #include <LibWeb/DOM/ElementFactory.h>
  38. #include <LibWeb/DOM/Text.h>
  39. #include <LibWeb/DOMTreeModel.h>
  40. #include <LibWeb/HTML/HTMLBodyElement.h>
  41. namespace Browser {
  42. ConsoleWidget::ConsoleWidget()
  43. {
  44. set_layout<GUI::VerticalBoxLayout>();
  45. set_fill_with_background_color(true);
  46. auto base_document = adopt(*new Web::DOM::Document);
  47. base_document->append_child(adopt(*new Web::DOM::DocumentType(base_document)));
  48. auto html_element = Web::DOM::create_element(base_document, "html");
  49. base_document->append_child(html_element);
  50. auto head_element = Web::DOM::create_element(base_document, "head");
  51. html_element->append_child(head_element);
  52. auto body_element = Web::DOM::create_element(base_document, "body");
  53. html_element->append_child(body_element);
  54. m_output_container = body_element;
  55. m_output_view = add<Web::InProcessWebView>();
  56. m_output_view->set_document(base_document);
  57. auto& bottom_container = add<GUI::Widget>();
  58. bottom_container.set_layout<GUI::HorizontalBoxLayout>();
  59. bottom_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  60. bottom_container.set_preferred_size(0, 22);
  61. m_input = bottom_container.add<GUI::TextBox>();
  62. m_input->set_syntax_highlighter(make<GUI::JSSyntaxHighlighter>());
  63. // FIXME: Syntax Highlighting breaks the cursor's position on non fixed-width fonts.
  64. m_input->set_font(Gfx::Font::default_fixed_width_font());
  65. m_input->set_history_enabled(true);
  66. m_input->on_return_pressed = [this] {
  67. auto js_source = m_input->text();
  68. // FIXME: An is_blank check to check if there is only whitespace would probably be preferable.
  69. if (js_source.is_empty())
  70. return;
  71. m_input->add_current_text_to_history();
  72. m_input->clear();
  73. print_source_line(js_source);
  74. auto parser = JS::Parser(JS::Lexer(js_source));
  75. auto program = parser.parse_program();
  76. StringBuilder output_html;
  77. if (parser.has_errors()) {
  78. auto error = parser.errors()[0];
  79. auto hint = error.source_location_hint(js_source);
  80. if (!hint.is_empty())
  81. output_html.append(String::formatted("<pre>{}</pre>", escape_html_entities(hint)));
  82. m_interpreter->vm().throw_exception<JS::SyntaxError>(m_interpreter->global_object(), error.to_string());
  83. } else {
  84. m_interpreter->run(m_interpreter->global_object(), *program);
  85. }
  86. if (m_interpreter->exception()) {
  87. output_html.append("Uncaught exception: ");
  88. output_html.append(JS::MarkupGenerator::html_from_value(m_interpreter->exception()->value()));
  89. print_html(output_html.string_view());
  90. m_interpreter->vm().clear_exception();
  91. return;
  92. }
  93. print_html(JS::MarkupGenerator::html_from_value(m_interpreter->vm().last_value()));
  94. };
  95. auto& clear_button = bottom_container.add<GUI::Button>();
  96. clear_button.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
  97. clear_button.set_preferred_size(22, 22);
  98. clear_button.set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/delete.png"));
  99. clear_button.set_tooltip("Clear the console output");
  100. clear_button.on_click = [this](auto) {
  101. clear_output();
  102. };
  103. }
  104. ConsoleWidget::~ConsoleWidget()
  105. {
  106. }
  107. void ConsoleWidget::set_interpreter(WeakPtr<JS::Interpreter> interpreter)
  108. {
  109. if (m_interpreter.ptr() == interpreter.ptr())
  110. return;
  111. m_interpreter = interpreter;
  112. m_console_client = make<BrowserConsoleClient>(interpreter->global_object().console(), *this);
  113. interpreter->global_object().console().set_client(*m_console_client.ptr());
  114. clear_output();
  115. }
  116. void ConsoleWidget::print_source_line(const StringView& source)
  117. {
  118. StringBuilder html;
  119. html.append("<span class=\"repl-indicator\">");
  120. html.append("&gt; ");
  121. html.append("</span>");
  122. html.append(JS::MarkupGenerator::html_from_source(source));
  123. print_html(html.string_view());
  124. }
  125. void ConsoleWidget::print_html(const StringView& line)
  126. {
  127. auto paragraph = create_element(m_output_container->document(), "p");
  128. paragraph->set_inner_html(line);
  129. m_output_container->append_child(paragraph);
  130. m_output_container->document().invalidate_layout();
  131. m_output_container->document().update_layout();
  132. m_output_view->scroll_to_bottom();
  133. }
  134. void ConsoleWidget::clear_output()
  135. {
  136. m_output_container->remove_all_children();
  137. m_output_view->update();
  138. }
  139. void ConsoleWidget::focusin_event(GUI::FocusEvent&)
  140. {
  141. m_input->set_focus(true);
  142. }
  143. }