HTMLToken.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  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 <LibWeb/Parser/HTMLToken.h>
  27. namespace Web {
  28. String HTMLToken::to_string() const
  29. {
  30. StringBuilder builder;
  31. switch (type()) {
  32. case HTMLToken::Type::DOCTYPE:
  33. builder.append("DOCTYPE");
  34. builder.append(" { name: '");
  35. builder.append(m_doctype.name.to_string());
  36. builder.append("' }");
  37. break;
  38. case HTMLToken::Type::StartTag:
  39. builder.append("StartTag");
  40. break;
  41. case HTMLToken::Type::EndTag:
  42. builder.append("EndTag");
  43. break;
  44. case HTMLToken::Type::Comment:
  45. builder.append("Comment");
  46. break;
  47. case HTMLToken::Type::Character:
  48. builder.append("Character");
  49. break;
  50. case HTMLToken::Type::EndOfFile:
  51. builder.append("EndOfFile");
  52. break;
  53. case HTMLToken::Type::Invalid:
  54. ASSERT_NOT_REACHED();
  55. }
  56. if (type() == HTMLToken::Type::StartTag || type() == HTMLToken::Type::EndTag) {
  57. builder.append(" { name: '");
  58. builder.append(m_tag.tag_name.to_string());
  59. builder.append("', { ");
  60. for (auto& attribute : m_tag.attributes) {
  61. builder.append(attribute.name_builder.to_string());
  62. builder.append("=\"");
  63. builder.append(attribute.value_builder.to_string());
  64. builder.append("\" ");
  65. }
  66. builder.append("} }");
  67. }
  68. if (type() == HTMLToken::Type::Comment || type() == HTMLToken::Type::Character) {
  69. builder.append(" { data: '");
  70. builder.append(m_comment_or_character.data.to_string());
  71. builder.append("' }");
  72. }
  73. return builder.to_string();
  74. //dbg() << "[" << String::format("%42s", state_name(m_state)) << "] " << builder.to_string();
  75. //m_current_token = {};
  76. }
  77. }