ConsoleWidget.cpp 5.4 KB

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