HTMLParser.cpp 9.6 KB

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