BrowserConsoleClient.cpp 4.4 KB

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