BrowserConsoleClient.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 "BrowserConsoleClient.h"
  27. #include "ConsoleWidget.h"
  28. #include <AK/StringBuilder.h>
  29. #include <LibGUI/BoxLayout.h>
  30. #include <LibGUI/JSSyntaxHighlighter.h>
  31. #include <LibGUI/TextBox.h>
  32. #include <LibWeb/DOM/DocumentType.h>
  33. #include <LibWeb/DOM/ElementFactory.h>
  34. #include <LibWeb/DOM/Text.h>
  35. #include <LibWeb/DOMTreeModel.h>
  36. #include <LibWeb/HTML/HTMLBodyElement.h>
  37. namespace Browser {
  38. JS::Value BrowserConsoleClient::log()
  39. {
  40. m_console_widget.print_html(vm().join_arguments());
  41. return JS::js_undefined();
  42. }
  43. JS::Value BrowserConsoleClient::info()
  44. {
  45. StringBuilder html;
  46. html.append("<span class=\"info\">");
  47. html.append("(i) ");
  48. html.append(vm().join_arguments());
  49. html.append("</span>");
  50. m_console_widget.print_html(html.string_view());
  51. return JS::js_undefined();
  52. }
  53. JS::Value BrowserConsoleClient::debug()
  54. {
  55. StringBuilder html;
  56. html.append("<span class=\"debug\">");
  57. html.append("(d) ");
  58. html.append(vm().join_arguments());
  59. html.append("</span>");
  60. m_console_widget.print_html(html.string_view());
  61. return JS::js_undefined();
  62. }
  63. JS::Value BrowserConsoleClient::warn()
  64. {
  65. StringBuilder html;
  66. html.append("<span class=\"warn\">");
  67. html.append("(w) ");
  68. html.append(vm().join_arguments());
  69. html.append("</span>");
  70. m_console_widget.print_html(html.string_view());
  71. return JS::js_undefined();
  72. }
  73. JS::Value BrowserConsoleClient::error()
  74. {
  75. StringBuilder html;
  76. html.append("<span class=\"error\">");
  77. html.append("(e) ");
  78. html.append(vm().join_arguments());
  79. html.append("</span>");
  80. m_console_widget.print_html(html.string_view());
  81. return JS::js_undefined();
  82. }
  83. JS::Value BrowserConsoleClient::clear()
  84. {
  85. m_console_widget.clear_output();
  86. return JS::js_undefined();
  87. }
  88. JS::Value BrowserConsoleClient::trace()
  89. {
  90. StringBuilder html;
  91. html.append(vm().join_arguments());
  92. auto trace = get_trace();
  93. for (auto& function_name : trace) {
  94. if (function_name.is_empty())
  95. function_name = "&lt;anonymous&gt;";
  96. html.appendff(" -> {}<br>", function_name);
  97. }
  98. m_console_widget.print_html(html.string_view());
  99. return JS::js_undefined();
  100. }
  101. JS::Value BrowserConsoleClient::count()
  102. {
  103. auto label = vm().argument_count() ? vm().argument(0).to_string_without_side_effects() : "default";
  104. auto counter_value = m_console.counter_increment(label);
  105. m_console_widget.print_html(String::formatted("{}: {}", label, counter_value));
  106. return JS::js_undefined();
  107. }
  108. JS::Value BrowserConsoleClient::count_reset()
  109. {
  110. auto label = vm().argument_count() ? vm().argument(0).to_string_without_side_effects() : "default";
  111. if (m_console.counter_reset(label)) {
  112. m_console_widget.print_html(String::formatted("{}: 0", label));
  113. } else {
  114. m_console_widget.print_html(String::formatted("\"{}\" doesn't have a count", label));
  115. }
  116. return JS::js_undefined();
  117. }
  118. }