HTMLParser.cpp 10 KB

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