HTMLParser.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #include <AK/Function.h>
  2. #include <AK/NonnullRefPtrVector.h>
  3. #include <AK/StringBuilder.h>
  4. #include <LibHTML/DOM/DocumentType.h>
  5. #include <LibHTML/DOM/Element.h>
  6. #include <LibHTML/DOM/ElementFactory.h>
  7. #include <LibHTML/DOM/Text.h>
  8. #include <LibHTML/Parser/HTMLParser.h>
  9. #include <ctype.h>
  10. #include <stdio.h>
  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 StringView& 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 StringView& html, const URL& url)
  33. {
  34. NonnullRefPtrVector<ParentNode> node_stack;
  35. auto document = adopt(*new Document);
  36. document->set_url(url);
  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. bool is_exclamation_tag = false;
  57. auto move_to_state = [&](State new_state) {
  58. if (new_state == State::BeforeTagName) {
  59. is_slash_tag = false;
  60. is_exclamation_tag = false;
  61. tag_name_buffer.clear();
  62. attributes.clear();
  63. }
  64. if (new_state == State::InAttributeName)
  65. attribute_name_buffer.clear();
  66. if (new_state == State::BeforeAttributeValue)
  67. attribute_value_buffer.clear();
  68. if (state == State::Free && !text_buffer.string_view().is_empty()) {
  69. auto text_node = adopt(*new Text(document, text_buffer.to_string()));
  70. node_stack.last().append_child(text_node, false);
  71. }
  72. state = new_state;
  73. text_buffer.clear();
  74. };
  75. auto close_tag = [&] {
  76. if (node_stack.size() > 1)
  77. node_stack.take_last();
  78. };
  79. auto open_tag = [&] {
  80. auto new_element = create_element(document, String::copy(tag_name_buffer));
  81. tag_name_buffer.clear();
  82. new_element->set_attributes(move(attributes));
  83. node_stack.append(new_element);
  84. if (node_stack.size() != 1)
  85. node_stack[node_stack.size() - 2].append_child(new_element, false);
  86. if (is_self_closing_tag(new_element->tag_name()))
  87. close_tag();
  88. };
  89. auto handle_exclamation_tag = [&] {
  90. auto name = String::copy(tag_name_buffer);
  91. tag_name_buffer.clear();
  92. ASSERT(name == "DOCTYPE");
  93. if (node_stack.size() != 1)
  94. node_stack[node_stack.size() - 2].append_child(adopt(*new DocumentType(document)), false);
  95. close_tag();
  96. };
  97. auto commit_tag = [&] {
  98. if (is_exclamation_tag)
  99. handle_exclamation_tag();
  100. else if (is_slash_tag)
  101. close_tag();
  102. else
  103. open_tag();
  104. };
  105. auto commit_attribute = [&] {
  106. attributes.append({ String::copy(attribute_name_buffer), String::copy(attribute_value_buffer) });
  107. };
  108. for (int i = 0; i < html.length(); ++i) {
  109. char ch = html[i];
  110. switch (state) {
  111. case State::Free:
  112. if (ch == '<') {
  113. is_slash_tag = false;
  114. is_exclamation_tag = false;
  115. move_to_state(State::BeforeTagName);
  116. break;
  117. }
  118. if (ch != '&') {
  119. text_buffer.append(ch);
  120. } else {
  121. struct Escape {
  122. const char* code;
  123. const char* value;
  124. };
  125. static Escape escapes[] = {
  126. { "&lt;", "<" },
  127. { "&gt;", ">" },
  128. { "&amp;", "&" }
  129. };
  130. auto rest_of_html = html.substring_view(i, html.length() - i);
  131. bool found = false;
  132. for (auto& escape : escapes) {
  133. if (rest_of_html.starts_with(escape.code)) {
  134. text_buffer.append(escape.value);
  135. found = true;
  136. i += strlen(escape.code) - 1;
  137. break;
  138. }
  139. }
  140. if (!found)
  141. dbg() << "Unhandled escape sequence";
  142. }
  143. break;
  144. case State::BeforeTagName:
  145. if (ch == '/') {
  146. is_slash_tag = true;
  147. break;
  148. }
  149. if (ch == '!') {
  150. is_exclamation_tag = true;
  151. break;
  152. }
  153. if (ch == '>') {
  154. move_to_state(State::Free);
  155. break;
  156. }
  157. if (!isalpha(ch))
  158. break;
  159. move_to_state(State::InTagName);
  160. [[fallthrough]];
  161. case State::InTagName:
  162. if (isspace(ch)) {
  163. move_to_state(State::InAttributeList);
  164. break;
  165. }
  166. if (ch == '>') {
  167. commit_tag();
  168. move_to_state(State::Free);
  169. break;
  170. }
  171. tag_name_buffer.append(ch);
  172. break;
  173. case State::InAttributeList:
  174. if (ch == '>') {
  175. commit_tag();
  176. move_to_state(State::Free);
  177. break;
  178. }
  179. if (!isalpha(ch))
  180. break;
  181. move_to_state(State::InAttributeName);
  182. [[fallthrough]];
  183. case State::InAttributeName:
  184. if (is_valid_in_attribute_name(ch)) {
  185. attribute_name_buffer.append(ch);
  186. break;
  187. }
  188. if (isspace(ch)) {
  189. commit_attribute();
  190. break;
  191. }
  192. if (ch == '>') {
  193. commit_tag();
  194. move_to_state(State::Free);
  195. break;
  196. }
  197. if (ch == '=') {
  198. move_to_state(State::BeforeAttributeValue);
  199. break;
  200. }
  201. break;
  202. case State::BeforeAttributeValue:
  203. if (ch == '\'') {
  204. move_to_state(State::InAttributeValueSingleQuote);
  205. break;
  206. }
  207. if (ch == '"') {
  208. move_to_state(State::InAttributeValueDoubleQuote);
  209. break;
  210. }
  211. if (ch == '>') {
  212. commit_tag();
  213. move_to_state(State::Free);
  214. break;
  215. }
  216. if (isspace(ch)) {
  217. commit_attribute();
  218. move_to_state(State::InAttributeList);
  219. break;
  220. }
  221. break;
  222. case State::InAttributeValueSingleQuote:
  223. if (ch == '\'') {
  224. commit_attribute();
  225. move_to_state(State::InAttributeList);
  226. break;
  227. }
  228. attribute_value_buffer.append(ch);
  229. break;
  230. case State::InAttributeValueDoubleQuote:
  231. if (ch == '"') {
  232. commit_attribute();
  233. move_to_state(State::InAttributeList);
  234. break;
  235. }
  236. attribute_value_buffer.append(ch);
  237. break;
  238. case State::InAttributeValueNoQuote:
  239. if (isspace(ch)) {
  240. commit_attribute();
  241. move_to_state(State::InAttributeList);
  242. break;
  243. }
  244. if (ch == '>') {
  245. commit_tag();
  246. move_to_state(State::Free);
  247. break;
  248. }
  249. attribute_value_buffer.append(ch);
  250. break;
  251. default:
  252. fprintf(stderr, "Unhandled state %d\n", (int)state);
  253. ASSERT_NOT_REACHED();
  254. }
  255. }
  256. Function<void(Node&)> fire_insertion_callbacks = [&](Node& node) {
  257. for (auto* child = node.first_child(); child; child = child->next_sibling()) {
  258. fire_insertion_callbacks(*child);
  259. }
  260. if (node.parent())
  261. node.inserted_into(*node.parent());
  262. };
  263. fire_insertion_callbacks(*document);
  264. document->fixup();
  265. return document;
  266. }