SourceHighlighter.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/StringBuilder.h>
  7. #include <LibURL/URL.h>
  8. #include <LibWeb/HTML/Parser/HTMLTokenizer.h>
  9. #include <LibWebView/SourceHighlighter.h>
  10. namespace WebView {
  11. String highlight_source(URL::URL const& url, StringView source)
  12. {
  13. Web::HTML::HTMLTokenizer tokenizer { source, "utf-8"sv };
  14. StringBuilder builder;
  15. builder.append(R"~~~(
  16. <!DOCTYPE html>
  17. <html>
  18. <head>
  19. <meta name="color-scheme" content="dark light">)~~~"sv);
  20. builder.appendff("<title>View Source - {}</title>", url);
  21. builder.appendff("<style type=\"text/css\">{}</style>", HTML_HIGHLIGHTER_STYLE);
  22. builder.append(R"~~~(
  23. </head>
  24. <body>
  25. <pre class="html">
  26. )~~~"sv);
  27. size_t previous_position = 0;
  28. auto append_source = [&](auto end_position, Optional<StringView> const& class_name = {}) {
  29. if (end_position <= previous_position)
  30. return;
  31. auto segment = source.substring_view(previous_position, end_position - previous_position);
  32. if (class_name.has_value())
  33. builder.appendff("<span class=\"{}\">"sv, *class_name);
  34. for (auto code_point : Utf8View { segment }) {
  35. if (code_point == '&')
  36. builder.append("&amp;"sv);
  37. else if (code_point == 0xA0)
  38. builder.append("&nbsp;"sv);
  39. else if (code_point == '<')
  40. builder.append("&lt;"sv);
  41. else if (code_point == '>')
  42. builder.append("&gt;"sv);
  43. else
  44. builder.append_code_point(code_point);
  45. }
  46. if (class_name.has_value())
  47. builder.append("</span>"sv);
  48. previous_position = end_position;
  49. };
  50. for (auto token = tokenizer.next_token(); token.has_value() && !token->is_end_of_file(); token = tokenizer.next_token()) {
  51. if (token->is_comment()) {
  52. append_source(token->start_position().byte_offset);
  53. append_source(token->end_position().byte_offset, "comment"sv);
  54. } else if (token->is_start_tag() || token->is_end_tag()) {
  55. auto tag_name_start = token->start_position().byte_offset;
  56. append_source(tag_name_start);
  57. append_source(tag_name_start + token->tag_name().bytes().size(), "tag"sv);
  58. token->for_each_attribute([&](auto const& attribute) {
  59. append_source(attribute.name_start_position.byte_offset);
  60. append_source(attribute.name_end_position.byte_offset, "attribute-name"sv);
  61. append_source(attribute.value_start_position.byte_offset);
  62. append_source(attribute.value_end_position.byte_offset, "attribute-value"sv);
  63. return IterationDecision::Continue;
  64. });
  65. append_source(token->end_position().byte_offset);
  66. } else {
  67. append_source(token->end_position().byte_offset);
  68. }
  69. }
  70. builder.append(R"~~~(
  71. </pre>
  72. </body>
  73. </html>
  74. )~~~"sv);
  75. return MUST(builder.to_string());
  76. }
  77. }