HTMLToken.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Max Wipfli <max.wipfli@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/FlyString.h>
  9. #include <AK/String.h>
  10. #include <AK/Types.h>
  11. #include <AK/Utf8View.h>
  12. #include <AK/Vector.h>
  13. namespace Web::HTML {
  14. class HTMLToken {
  15. friend class HTMLDocumentParser;
  16. friend class HTMLTokenizer;
  17. public:
  18. enum class Type {
  19. Invalid,
  20. DOCTYPE,
  21. StartTag,
  22. EndTag,
  23. Comment,
  24. Character,
  25. EndOfFile,
  26. };
  27. struct Position {
  28. size_t line { 0 };
  29. size_t column { 0 };
  30. };
  31. struct AttributeBuilder {
  32. String prefix;
  33. String local_name;
  34. String namespace_;
  35. String value;
  36. Position name_start_position;
  37. Position value_start_position;
  38. Position name_end_position;
  39. Position value_end_position;
  40. };
  41. static HTMLToken make_character(u32 code_point)
  42. {
  43. HTMLToken token;
  44. token.m_type = Type::Character;
  45. StringBuilder builder;
  46. // FIXME: This narrows code_point to char, should this be append_code_point() instead?
  47. builder.append(code_point);
  48. token.m_comment_or_character.data = builder.to_string();
  49. return token;
  50. }
  51. static HTMLToken make_start_tag(FlyString const& tag_name)
  52. {
  53. HTMLToken token;
  54. token.m_type = Type::StartTag;
  55. token.m_tag.tag_name = tag_name;
  56. return token;
  57. }
  58. bool is_doctype() const { return m_type == Type::DOCTYPE; }
  59. bool is_start_tag() const { return m_type == Type::StartTag; }
  60. bool is_end_tag() const { return m_type == Type::EndTag; }
  61. bool is_comment() const { return m_type == Type::Comment; }
  62. bool is_character() const { return m_type == Type::Character; }
  63. bool is_end_of_file() const { return m_type == Type::EndOfFile; }
  64. u32 code_point() const
  65. {
  66. VERIFY(is_character());
  67. Utf8View view(m_comment_or_character.data);
  68. VERIFY(view.length() == 1);
  69. return *view.begin();
  70. }
  71. bool is_parser_whitespace() const
  72. {
  73. // NOTE: The parser considers '\r' to be whitespace, while the tokenizer does not.
  74. if (!is_character())
  75. return false;
  76. switch (code_point()) {
  77. case '\t':
  78. case '\n':
  79. case '\f':
  80. case '\r':
  81. case ' ':
  82. return true;
  83. default:
  84. return false;
  85. }
  86. }
  87. String tag_name() const
  88. {
  89. VERIFY(is_start_tag() || is_end_tag());
  90. return m_tag.tag_name;
  91. }
  92. bool is_self_closing() const
  93. {
  94. VERIFY(is_start_tag() || is_end_tag());
  95. return m_tag.self_closing;
  96. }
  97. bool has_acknowledged_self_closing_flag() const
  98. {
  99. VERIFY(is_self_closing());
  100. return m_tag.self_closing_acknowledged;
  101. }
  102. void acknowledge_self_closing_flag_if_set()
  103. {
  104. if (is_self_closing())
  105. m_tag.self_closing_acknowledged = true;
  106. }
  107. StringView attribute(FlyString const& attribute_name)
  108. {
  109. VERIFY(is_start_tag() || is_end_tag());
  110. for (auto& attribute : m_tag.attributes) {
  111. if (attribute_name == attribute.local_name)
  112. return attribute.value;
  113. }
  114. return {};
  115. }
  116. bool has_attribute(FlyString const& attribute_name)
  117. {
  118. return !attribute(attribute_name).is_null();
  119. }
  120. void adjust_tag_name(FlyString const& old_name, FlyString const& new_name)
  121. {
  122. VERIFY(is_start_tag() || is_end_tag());
  123. if (old_name == m_tag.tag_name)
  124. m_tag.tag_name = new_name;
  125. }
  126. void adjust_attribute_name(FlyString const& old_name, FlyString const& new_name)
  127. {
  128. VERIFY(is_start_tag() || is_end_tag());
  129. for (auto& attribute : m_tag.attributes) {
  130. if (old_name == attribute.local_name) {
  131. attribute.local_name = new_name;
  132. }
  133. }
  134. }
  135. void adjust_foreign_attribute(FlyString const& old_name, FlyString const& prefix, FlyString const& local_name, FlyString const& namespace_)
  136. {
  137. VERIFY(is_start_tag() || is_end_tag());
  138. for (auto& attribute : m_tag.attributes) {
  139. if (old_name == attribute.local_name) {
  140. attribute.prefix = prefix;
  141. attribute.local_name = local_name;
  142. attribute.namespace_ = namespace_;
  143. }
  144. }
  145. }
  146. void drop_attributes()
  147. {
  148. VERIFY(is_start_tag() || is_end_tag());
  149. m_tag.attributes.clear();
  150. }
  151. Type type() const { return m_type; }
  152. String to_string() const;
  153. Position const& start_position() const { return m_start_position; }
  154. Position const& end_position() const { return m_end_position; }
  155. Vector<Attribute> const& attributes() const
  156. {
  157. VERIFY(is_start_tag() || is_end_tag());
  158. return m_tag.attributes;
  159. }
  160. private:
  161. Type m_type { Type::Invalid };
  162. // Type::DOCTYPE
  163. struct {
  164. // NOTE: "Missing" is a distinct state from the empty string.
  165. String name;
  166. bool missing_name { true };
  167. String public_identifier;
  168. bool missing_public_identifier { true };
  169. String system_identifier;
  170. bool missing_system_identifier { true };
  171. bool force_quirks { false };
  172. } m_doctype;
  173. // Type::StartTag
  174. // Type::EndTag
  175. struct {
  176. String tag_name;
  177. bool self_closing { false };
  178. bool self_closing_acknowledged { false };
  179. Vector<AttributeBuilder> attributes;
  180. } m_tag;
  181. // Type::Comment
  182. // Type::Character
  183. struct {
  184. String data;
  185. } m_comment_or_character;
  186. Position m_start_position;
  187. Position m_end_position;
  188. };
  189. }