HTMLParser.cpp 9.6 KB

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