HTMLParser.cpp 9.3 KB

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