BrowserConsoleClient.cpp 4.1 KB

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