HTMLToken.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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/Function.h>
  10. #include <AK/String.h>
  11. #include <AK/Types.h>
  12. #include <AK/Utf8View.h>
  13. #include <AK/Vector.h>
  14. namespace Web::HTML {
  15. class HTMLToken {
  16. friend class HTMLDocumentParser;
  17. friend class HTMLTokenizer;
  18. public:
  19. enum class Type {
  20. Invalid,
  21. DOCTYPE,
  22. StartTag,
  23. EndTag,
  24. Comment,
  25. Character,
  26. EndOfFile,
  27. };
  28. struct Position {
  29. size_t line { 0 };
  30. size_t column { 0 };
  31. };
  32. struct Attribute {
  33. String prefix;
  34. String local_name;
  35. String namespace_;
  36. String value;
  37. Position name_start_position;
  38. Position value_start_position;
  39. Position name_end_position;
  40. Position value_end_position;
  41. };
  42. static HTMLToken make_character(u32 code_point)
  43. {
  44. HTMLToken token;
  45. token.m_type = Type::Character;
  46. token.set_code_point(code_point);
  47. return token;
  48. }
  49. static HTMLToken make_start_tag(FlyString const& tag_name)
  50. {
  51. HTMLToken token;
  52. token.m_type = Type::StartTag;
  53. token.set_tag_name(tag_name);
  54. return token;
  55. }
  56. bool is_doctype() const { return m_type == Type::DOCTYPE; }
  57. bool is_start_tag() const { return m_type == Type::StartTag; }
  58. bool is_end_tag() const { return m_type == Type::EndTag; }
  59. bool is_comment() const { return m_type == Type::Comment; }
  60. bool is_character() const { return m_type == Type::Character; }
  61. bool is_end_of_file() const { return m_type == Type::EndOfFile; }
  62. u32 code_point() const
  63. {
  64. VERIFY(is_character());
  65. Utf8View view(m_comment_or_character.data);
  66. VERIFY(view.length() == 1);
  67. return *view.begin();
  68. }
  69. bool is_parser_whitespace() const
  70. {
  71. // NOTE: The parser considers '\r' to be whitespace, while the tokenizer does not.
  72. if (!is_character())
  73. return false;
  74. switch (code_point()) {
  75. case '\t':
  76. case '\n':
  77. case '\f':
  78. case '\r':
  79. case ' ':
  80. return true;
  81. default:
  82. return false;
  83. }
  84. }
  85. void set_code_point(u32 code_point)
  86. {
  87. VERIFY(is_character());
  88. StringBuilder builder;
  89. builder.append_code_point(code_point);
  90. m_comment_or_character.data = builder.to_string();
  91. }
  92. String const& comment() const
  93. {
  94. VERIFY(is_comment());
  95. return m_comment_or_character.data;
  96. }
  97. void set_comment(String comment)
  98. {
  99. VERIFY(is_comment());
  100. m_comment_or_character.data = move(comment);
  101. }
  102. String const& tag_name() const
  103. {
  104. VERIFY(is_start_tag() || is_end_tag());
  105. return m_tag.tag_name;
  106. }
  107. void set_tag_name(String name)
  108. {
  109. VERIFY(is_start_tag() || is_end_tag());
  110. m_tag.tag_name = move(name);
  111. }
  112. bool is_self_closing() const
  113. {
  114. VERIFY(is_start_tag() || is_end_tag());
  115. return m_tag.self_closing;
  116. }
  117. void set_self_closing(bool self_closing)
  118. {
  119. VERIFY(is_start_tag() || is_end_tag());
  120. m_tag.self_closing = self_closing;
  121. }
  122. bool has_acknowledged_self_closing_flag() const
  123. {
  124. VERIFY(is_self_closing());
  125. return m_tag.self_closing_acknowledged;
  126. }
  127. void acknowledge_self_closing_flag_if_set()
  128. {
  129. if (is_self_closing())
  130. m_tag.self_closing_acknowledged = true;
  131. }
  132. bool has_attributes() const
  133. {
  134. VERIFY(is_start_tag() || is_end_tag());
  135. return !m_tag.attributes.is_empty();
  136. }
  137. size_t attribute_count() const
  138. {
  139. VERIFY(is_start_tag() || is_end_tag());
  140. return m_tag.attributes.size();
  141. }
  142. void add_attribute(Attribute attribute)
  143. {
  144. VERIFY(is_start_tag() || is_end_tag());
  145. m_tag.attributes.append(move(attribute));
  146. }
  147. Attribute const& last_attribute() const
  148. {
  149. VERIFY(is_start_tag() || is_end_tag());
  150. VERIFY(!m_tag.attributes.is_empty());
  151. return m_tag.attributes.last();
  152. }
  153. Attribute& last_attribute()
  154. {
  155. VERIFY(is_start_tag() || is_end_tag());
  156. VERIFY(!m_tag.attributes.is_empty());
  157. return m_tag.attributes.last();
  158. }
  159. void drop_attributes()
  160. {
  161. VERIFY(is_start_tag() || is_end_tag());
  162. m_tag.attributes.clear();
  163. }
  164. void for_each_attribute(Function<IterationDecision(Attribute const&)> callback) const
  165. {
  166. VERIFY(is_start_tag() || is_end_tag());
  167. for (auto& attribute : m_tag.attributes) {
  168. if (callback(attribute) == IterationDecision::Break)
  169. break;
  170. }
  171. }
  172. void for_each_attribute(Function<IterationDecision(Attribute&)> callback)
  173. {
  174. VERIFY(is_start_tag() || is_end_tag());
  175. for (auto& attribute : m_tag.attributes) {
  176. if (callback(attribute) == IterationDecision::Break)
  177. break;
  178. }
  179. }
  180. StringView attribute(FlyString const& attribute_name)
  181. {
  182. VERIFY(is_start_tag() || is_end_tag());
  183. for (auto& attribute : m_tag.attributes) {
  184. if (attribute_name == attribute.local_name)
  185. return attribute.value;
  186. }
  187. return {};
  188. }
  189. bool has_attribute(FlyString const& attribute_name)
  190. {
  191. return !attribute(attribute_name).is_null();
  192. }
  193. void adjust_tag_name(FlyString const& old_name, FlyString const& new_name)
  194. {
  195. VERIFY(is_start_tag() || is_end_tag());
  196. if (old_name == tag_name())
  197. set_tag_name(new_name);
  198. }
  199. void adjust_attribute_name(FlyString const& old_name, FlyString const& new_name)
  200. {
  201. VERIFY(is_start_tag() || is_end_tag());
  202. for_each_attribute([&](Attribute& attribute) {
  203. if (old_name == attribute.local_name)
  204. attribute.local_name = new_name;
  205. return IterationDecision::Continue;
  206. });
  207. }
  208. void adjust_foreign_attribute(FlyString const& old_name, FlyString const& prefix, FlyString const& local_name, FlyString const& namespace_)
  209. {
  210. VERIFY(is_start_tag() || is_end_tag());
  211. for_each_attribute([&](Attribute& attribute) {
  212. if (old_name == attribute.local_name) {
  213. attribute.prefix = prefix;
  214. attribute.local_name = local_name;
  215. attribute.namespace_ = namespace_;
  216. }
  217. return IterationDecision::Continue;
  218. });
  219. }
  220. Type type() const { return m_type; }
  221. String to_string() const;
  222. Position const& start_position() const { return m_start_position; }
  223. Position const& end_position() const { return m_end_position; }
  224. private:
  225. Type m_type { Type::Invalid };
  226. // Type::DOCTYPE
  227. struct {
  228. // NOTE: "Missing" is a distinct state from the empty string.
  229. String name;
  230. bool missing_name { true };
  231. String public_identifier;
  232. bool missing_public_identifier { true };
  233. String system_identifier;
  234. bool missing_system_identifier { true };
  235. bool force_quirks { false };
  236. } m_doctype;
  237. // Type::StartTag
  238. // Type::EndTag
  239. struct {
  240. String tag_name;
  241. bool self_closing { false };
  242. bool self_closing_acknowledged { false };
  243. Vector<Attribute> attributes;
  244. } m_tag;
  245. // Type::Comment
  246. // Type::Character
  247. struct {
  248. String data;
  249. } m_comment_or_character;
  250. Position m_start_position;
  251. Position m_end_position;
  252. };
  253. }