HTMLParser.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #include <AK/NonnullRefPtrVector.h>
  2. #include <AK/StringBuilder.h>
  3. #include <LibHTML/DOM/Element.h>
  4. #include <LibHTML/DOM/Text.h>
  5. #include <LibHTML/Parser/HTMLParser.h>
  6. #include <ctype.h>
  7. #include <stdio.h>
  8. static NonnullRefPtr<Element> create_element(Document& document, const String& tag_name)
  9. {
  10. return adopt(*new Element(document, tag_name));
  11. }
  12. static bool is_valid_in_attribute_name(char ch)
  13. {
  14. return isalnum(ch) || ch == '_' || ch == '-';
  15. }
  16. static bool is_self_closing_tag(const String& tag_name)
  17. {
  18. return tag_name == "area"
  19. || tag_name == "base"
  20. || tag_name == "br"
  21. || tag_name == "col"
  22. || tag_name == "embed"
  23. || tag_name == "hr"
  24. || tag_name == "img"
  25. || tag_name == "input"
  26. || tag_name == "link"
  27. || tag_name == "meta"
  28. || tag_name == "param"
  29. || tag_name == "source"
  30. || tag_name == "track"
  31. || tag_name == "wbr";
  32. }
  33. NonnullRefPtr<Document> parse_html(const String& html)
  34. {
  35. NonnullRefPtrVector<ParentNode> node_stack;
  36. auto document = adopt(*new Document);
  37. node_stack.append(document);
  38. enum class State {
  39. Free = 0,
  40. BeforeTagName,
  41. InTagName,
  42. InAttributeList,
  43. InAttributeName,
  44. BeforeAttributeValue,
  45. InAttributeValueNoQuote,
  46. InAttributeValueSingleQuote,
  47. InAttributeValueDoubleQuote,
  48. };
  49. auto state = State::Free;
  50. StringBuilder text_buffer;
  51. Vector<char, 32> tag_name_buffer;
  52. Vector<Attribute> attributes;
  53. Vector<char, 256> attribute_name_buffer;
  54. Vector<char, 256> attribute_value_buffer;
  55. bool is_slash_tag = false;
  56. auto move_to_state = [&](State new_state) {
  57. if (new_state == State::BeforeTagName) {
  58. is_slash_tag = false;
  59. tag_name_buffer.clear();
  60. attributes.clear();
  61. }
  62. if (new_state == State::InAttributeName)
  63. attribute_name_buffer.clear();
  64. if (new_state == State::BeforeAttributeValue)
  65. attribute_value_buffer.clear();
  66. if (state == State::Free && !text_buffer.string_view().is_empty()) {
  67. auto text_node = adopt(*new Text(document, text_buffer.to_string()));
  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(document, 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. if (ch != '&') {
  106. text_buffer.append(ch);
  107. } else {
  108. struct Escape {
  109. const char* code;
  110. const char* value;
  111. };
  112. static Escape escapes[] = {
  113. { "&lt;", "<" },
  114. { "&gt;", ">" },
  115. { "&amp;", "&" }
  116. };
  117. auto rest_of_html = html.substring_view(i, html.length() - i);
  118. bool found = false;
  119. for (auto& escape : escapes) {
  120. if (rest_of_html.starts_with(escape.code)) {
  121. text_buffer.append(escape.value);
  122. found = true;
  123. i += strlen(escape.code) - 1;
  124. break;
  125. }
  126. }
  127. if (!found)
  128. dbg() << "Unhandled escape sequence";
  129. }
  130. break;
  131. case State::BeforeTagName:
  132. if (ch == '/') {
  133. is_slash_tag = true;
  134. break;
  135. }
  136. if (ch == '>') {
  137. move_to_state(State::Free);
  138. break;
  139. }
  140. if (!isalpha(ch))
  141. break;
  142. move_to_state(State::InTagName);
  143. [[fallthrough]];
  144. case State::InTagName:
  145. if (isspace(ch)) {
  146. move_to_state(State::InAttributeList);
  147. break;
  148. }
  149. if (ch == '>') {
  150. commit_tag();
  151. move_to_state(State::Free);
  152. break;
  153. }
  154. tag_name_buffer.append(ch);
  155. break;
  156. case State::InAttributeList:
  157. if (ch == '>') {
  158. commit_tag();
  159. move_to_state(State::Free);
  160. break;
  161. }
  162. if (!isalpha(ch))
  163. break;
  164. move_to_state(State::InAttributeName);
  165. [[fallthrough]];
  166. case State::InAttributeName:
  167. if (is_valid_in_attribute_name(ch)) {
  168. attribute_name_buffer.append(ch);
  169. break;
  170. }
  171. if (isspace(ch)) {
  172. commit_attribute();
  173. break;
  174. }
  175. if (ch == '>') {
  176. commit_tag();
  177. move_to_state(State::Free);
  178. break;
  179. }
  180. if (ch == '=') {
  181. move_to_state(State::BeforeAttributeValue);
  182. break;
  183. }
  184. break;
  185. case State::BeforeAttributeValue:
  186. if (ch == '\'') {
  187. move_to_state(State::InAttributeValueSingleQuote);
  188. break;
  189. }
  190. if (ch == '"') {
  191. move_to_state(State::InAttributeValueDoubleQuote);
  192. break;
  193. }
  194. if (ch == '>') {
  195. commit_tag();
  196. move_to_state(State::Free);
  197. break;
  198. }
  199. if (isspace(ch)) {
  200. commit_attribute();
  201. move_to_state(State::InAttributeList);
  202. break;
  203. }
  204. break;
  205. case State::InAttributeValueSingleQuote:
  206. if (ch == '\'') {
  207. commit_attribute();
  208. move_to_state(State::InAttributeList);
  209. break;
  210. }
  211. attribute_value_buffer.append(ch);
  212. break;
  213. case State::InAttributeValueDoubleQuote:
  214. if (ch == '"') {
  215. commit_attribute();
  216. move_to_state(State::InAttributeList);
  217. break;
  218. }
  219. attribute_value_buffer.append(ch);
  220. break;
  221. case State::InAttributeValueNoQuote:
  222. if (isspace(ch)) {
  223. commit_attribute();
  224. move_to_state(State::InAttributeList);
  225. break;
  226. }
  227. if (ch == '>') {
  228. commit_tag();
  229. move_to_state(State::Free);
  230. break;
  231. }
  232. attribute_value_buffer.append(ch);
  233. break;
  234. default:
  235. fprintf(stderr, "Unhandled state %d\n", (int)state);
  236. ASSERT_NOT_REACHED();
  237. }
  238. }
  239. return document;
  240. }