HTMLParser.cpp 9.0 KB

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