HTMLParser.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #include <AK/NonnullRefPtrVector.h>
  2. #include <LibHTML/DOM/Element.h>
  3. #include <LibHTML/DOM/Text.h>
  4. #include <LibHTML/Parser/HTMLParser.h>
  5. #include <ctype.h>
  6. #include <stdio.h>
  7. static NonnullRefPtr<Element> create_element(const String& tag_name)
  8. {
  9. return adopt(*new Element(tag_name));
  10. }
  11. static bool is_valid_in_attribute_name(char ch)
  12. {
  13. return isalnum(ch) || ch == '_' || ch == '-';
  14. }
  15. static bool is_self_closing_tag(const String& tag_name)
  16. {
  17. return tag_name == "area"
  18. || tag_name == "base"
  19. || tag_name == "br"
  20. || tag_name == "col"
  21. || tag_name == "embed"
  22. || tag_name == "hr"
  23. || tag_name == "img"
  24. || tag_name == "input"
  25. || tag_name == "link"
  26. || tag_name == "meta"
  27. || tag_name == "param"
  28. || tag_name == "source"
  29. || tag_name == "track"
  30. || tag_name == "wbr";
  31. }
  32. NonnullRefPtr<Document> parse_html(const String& html)
  33. {
  34. NonnullRefPtrVector<ParentNode> node_stack;
  35. auto doc = adopt(*new Document);
  36. node_stack.append(doc);
  37. enum class State {
  38. Free = 0,
  39. BeforeTagName,
  40. InTagName,
  41. InAttributeList,
  42. InAttributeName,
  43. BeforeAttributeValue,
  44. InAttributeValueNoQuote,
  45. InAttributeValueSingleQuote,
  46. InAttributeValueDoubleQuote,
  47. };
  48. auto state = State::Free;
  49. Vector<char, 256> text_buffer;
  50. Vector<char, 32> tag_name_buffer;
  51. Vector<Attribute> attributes;
  52. Vector<char, 256> attribute_name_buffer;
  53. Vector<char, 256> attribute_value_buffer;
  54. bool is_slash_tag = false;
  55. auto move_to_state = [&](State new_state) {
  56. if (new_state == State::BeforeTagName) {
  57. is_slash_tag = false;
  58. tag_name_buffer.clear();
  59. attributes.clear();
  60. }
  61. if (new_state == State::InAttributeName)
  62. attribute_name_buffer.clear();
  63. if (new_state == State::BeforeAttributeValue)
  64. attribute_value_buffer.clear();
  65. if (state == State::Free && !text_buffer.is_empty()) {
  66. auto text_node = adopt(*new Text(String::copy(text_buffer)));
  67. text_buffer.clear();
  68. node_stack.last().append_child(text_node);
  69. }
  70. state = new_state;
  71. text_buffer.clear();
  72. };
  73. auto close_tag = [&] {
  74. if (node_stack.size() > 1)
  75. node_stack.take_last();
  76. };
  77. auto open_tag = [&] {
  78. auto new_element = create_element(String::copy(tag_name_buffer));
  79. tag_name_buffer.clear();
  80. new_element->set_attributes(move(attributes));
  81. node_stack.append(new_element);
  82. if (node_stack.size() != 1)
  83. node_stack[node_stack.size() - 2].append_child(new_element);
  84. if (is_self_closing_tag(new_element->tag_name()))
  85. close_tag();
  86. };
  87. auto commit_tag = [&] {
  88. if (is_slash_tag)
  89. close_tag();
  90. else
  91. open_tag();
  92. };
  93. auto commit_attribute = [&] {
  94. attributes.append({ String::copy(attribute_name_buffer), String::copy(attribute_value_buffer) });
  95. };
  96. for (int i = 0; i < html.length(); ++i) {
  97. char ch = html[i];
  98. switch (state) {
  99. case State::Free:
  100. if (ch == '<') {
  101. is_slash_tag = false;
  102. move_to_state(State::BeforeTagName);
  103. break;
  104. }
  105. text_buffer.append(ch);
  106. break;
  107. case State::BeforeTagName:
  108. if (ch == '/') {
  109. is_slash_tag = true;
  110. break;
  111. }
  112. if (ch == '>') {
  113. move_to_state(State::Free);
  114. break;
  115. }
  116. if (!isalpha(ch))
  117. break;
  118. move_to_state(State::InTagName);
  119. [[fallthrough]];
  120. case State::InTagName:
  121. if (isspace(ch)) {
  122. move_to_state(State::InAttributeList);
  123. break;
  124. }
  125. if (ch == '>') {
  126. commit_tag();
  127. move_to_state(State::Free);
  128. break;
  129. }
  130. tag_name_buffer.append(ch);
  131. break;
  132. case State::InAttributeList:
  133. if (ch == '>') {
  134. commit_tag();
  135. move_to_state(State::Free);
  136. break;
  137. }
  138. if (!isalpha(ch))
  139. break;
  140. move_to_state(State::InAttributeName);
  141. [[fallthrough]];
  142. case State::InAttributeName:
  143. if (is_valid_in_attribute_name(ch)) {
  144. attribute_name_buffer.append(ch);
  145. break;
  146. }
  147. if (isspace(ch)) {
  148. commit_attribute();
  149. break;
  150. }
  151. if (ch == '>') {
  152. commit_tag();
  153. move_to_state(State::Free);
  154. break;
  155. }
  156. if (ch == '=') {
  157. move_to_state(State::BeforeAttributeValue);
  158. break;
  159. }
  160. break;
  161. case State::BeforeAttributeValue:
  162. if (ch == '\'') {
  163. move_to_state(State::InAttributeValueSingleQuote);
  164. break;
  165. }
  166. if (ch == '"') {
  167. move_to_state(State::InAttributeValueDoubleQuote);
  168. break;
  169. }
  170. if (ch == '>') {
  171. commit_tag();
  172. move_to_state(State::Free);
  173. break;
  174. }
  175. if (isspace(ch)) {
  176. commit_attribute();
  177. move_to_state(State::InAttributeList);
  178. break;
  179. }
  180. break;
  181. case State::InAttributeValueSingleQuote:
  182. if (ch == '\'') {
  183. commit_attribute();
  184. move_to_state(State::InAttributeList);
  185. break;
  186. }
  187. attribute_value_buffer.append(ch);
  188. break;
  189. case State::InAttributeValueDoubleQuote:
  190. if (ch == '"') {
  191. commit_attribute();
  192. move_to_state(State::InAttributeList);
  193. break;
  194. }
  195. attribute_value_buffer.append(ch);
  196. break;
  197. case State::InAttributeValueNoQuote:
  198. if (isspace(ch)) {
  199. commit_attribute();
  200. move_to_state(State::InAttributeList);
  201. break;
  202. }
  203. if (ch == '>') {
  204. commit_tag();
  205. move_to_state(State::Free);
  206. break;
  207. }
  208. attribute_value_buffer.append(ch);
  209. break;
  210. default:
  211. fprintf(stderr, "Unhandled state %d\n", (int)state);
  212. ASSERT_NOT_REACHED();
  213. }
  214. }
  215. return doc;
  216. }