HTMLToken.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/FlyString.h>
  8. #include <AK/String.h>
  9. #include <AK/StringBuilder.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. static HTMLToken make_character(u32 code_point)
  28. {
  29. HTMLToken token;
  30. token.m_type = Type::Character;
  31. token.m_comment_or_character.data.append(code_point);
  32. return token;
  33. }
  34. static HTMLToken make_start_tag(const FlyString& tag_name)
  35. {
  36. HTMLToken token;
  37. token.m_type = Type::StartTag;
  38. token.m_tag.tag_name.append(tag_name);
  39. return token;
  40. }
  41. bool is_doctype() const { return m_type == Type::DOCTYPE; }
  42. bool is_start_tag() const { return m_type == Type::StartTag; }
  43. bool is_end_tag() const { return m_type == Type::EndTag; }
  44. bool is_comment() const { return m_type == Type::Comment; }
  45. bool is_character() const { return m_type == Type::Character; }
  46. bool is_end_of_file() const { return m_type == Type::EndOfFile; }
  47. u32 code_point() const
  48. {
  49. VERIFY(is_character());
  50. Utf8View view(m_comment_or_character.data.string_view());
  51. VERIFY(view.length() == 1);
  52. return *view.begin();
  53. }
  54. bool is_parser_whitespace() const
  55. {
  56. // NOTE: The parser considers '\r' to be whitespace, while the tokenizer does not.
  57. if (!is_character())
  58. return false;
  59. switch (code_point()) {
  60. case '\t':
  61. case '\n':
  62. case '\f':
  63. case '\r':
  64. case ' ':
  65. return true;
  66. default:
  67. return false;
  68. }
  69. }
  70. String tag_name() const
  71. {
  72. VERIFY(is_start_tag() || is_end_tag());
  73. return m_tag.tag_name.to_string();
  74. }
  75. bool is_self_closing() const
  76. {
  77. VERIFY(is_start_tag() || is_end_tag());
  78. return m_tag.self_closing;
  79. }
  80. bool has_acknowledged_self_closing_flag() const
  81. {
  82. VERIFY(is_self_closing());
  83. return m_tag.self_closing_acknowledged;
  84. }
  85. void acknowledge_self_closing_flag_if_set()
  86. {
  87. if (is_self_closing())
  88. m_tag.self_closing_acknowledged = true;
  89. }
  90. StringView attribute(const FlyString& attribute_name)
  91. {
  92. VERIFY(is_start_tag() || is_end_tag());
  93. for (auto& attribute : m_tag.attributes) {
  94. if (attribute_name == attribute.local_name_builder.string_view())
  95. return attribute.value_builder.string_view();
  96. }
  97. return {};
  98. }
  99. bool has_attribute(const FlyString& attribute_name)
  100. {
  101. return !attribute(attribute_name).is_null();
  102. }
  103. void adjust_tag_name(const FlyString& old_name, const FlyString& new_name)
  104. {
  105. VERIFY(is_start_tag() || is_end_tag());
  106. if (old_name == m_tag.tag_name.string_view()) {
  107. m_tag.tag_name.clear();
  108. m_tag.tag_name.append(new_name);
  109. }
  110. }
  111. void adjust_attribute_name(const FlyString& old_name, const FlyString& new_name)
  112. {
  113. VERIFY(is_start_tag() || is_end_tag());
  114. for (auto& attribute : m_tag.attributes) {
  115. if (old_name == attribute.local_name_builder.string_view()) {
  116. attribute.local_name_builder.clear();
  117. attribute.local_name_builder.append(new_name);
  118. }
  119. }
  120. }
  121. void adjust_foreign_attribute(const FlyString& old_name, const FlyString& prefix, const FlyString& local_name, const FlyString& namespace_)
  122. {
  123. VERIFY(is_start_tag() || is_end_tag());
  124. for (auto& attribute : m_tag.attributes) {
  125. if (old_name == attribute.local_name_builder.string_view()) {
  126. attribute.prefix_builder.clear();
  127. attribute.prefix_builder.append(prefix);
  128. attribute.local_name_builder.clear();
  129. attribute.local_name_builder.append(local_name);
  130. attribute.namespace_builder.clear();
  131. attribute.namespace_builder.append(namespace_);
  132. }
  133. }
  134. }
  135. void drop_attributes()
  136. {
  137. VERIFY(is_start_tag() || is_end_tag());
  138. m_tag.attributes.clear();
  139. }
  140. Type type() const { return m_type; }
  141. String to_string() const;
  142. private:
  143. struct AttributeBuilder {
  144. StringBuilder prefix_builder;
  145. StringBuilder local_name_builder;
  146. StringBuilder namespace_builder;
  147. StringBuilder value_builder;
  148. };
  149. Type m_type { Type::Invalid };
  150. // Type::DOCTYPE
  151. struct {
  152. // NOTE: "Missing" is a distinct state from the empty string.
  153. StringBuilder name;
  154. bool missing_name { true };
  155. StringBuilder public_identifier;
  156. bool missing_public_identifier { true };
  157. StringBuilder system_identifier;
  158. bool missing_system_identifier { true };
  159. bool force_quirks { false };
  160. } m_doctype;
  161. // Type::StartTag
  162. // Type::EndTag
  163. struct {
  164. StringBuilder tag_name;
  165. bool self_closing { false };
  166. bool self_closing_acknowledged { false };
  167. Vector<AttributeBuilder> attributes;
  168. } m_tag;
  169. // Type::Comment
  170. // Type::Character
  171. struct {
  172. StringBuilder data;
  173. } m_comment_or_character;
  174. };
  175. }