HTMLParser.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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)
  72. {
  73. NonnullRefPtrVector<ParentNode> node_stack;
  74. auto document = adopt(*new Document);
  75. node_stack.append(document);
  76. enum class State {
  77. Free = 0,
  78. BeforeTagName,
  79. InTagName,
  80. InAttributeList,
  81. InAttributeName,
  82. BeforeAttributeValue,
  83. InAttributeValueNoQuote,
  84. InAttributeValueSingleQuote,
  85. InAttributeValueDoubleQuote,
  86. };
  87. auto state = State::Free;
  88. StringBuilder text_buffer;
  89. Vector<char, 32> tag_name_buffer;
  90. Vector<Attribute> attributes;
  91. Vector<char, 256> attribute_name_buffer;
  92. Vector<char, 256> attribute_value_buffer;
  93. bool is_slash_tag = false;
  94. auto move_to_state = [&](State new_state) {
  95. if (new_state == State::BeforeTagName) {
  96. is_slash_tag = false;
  97. tag_name_buffer.clear();
  98. attributes.clear();
  99. }
  100. if (new_state == State::InAttributeName)
  101. attribute_name_buffer.clear();
  102. if (new_state == State::BeforeAttributeValue)
  103. attribute_value_buffer.clear();
  104. if (state == State::Free && !text_buffer.string_view().is_empty()) {
  105. auto text_node = adopt(*new Text(document, text_buffer.to_string()));
  106. node_stack.last().append_child(text_node, false);
  107. }
  108. state = new_state;
  109. text_buffer.clear();
  110. };
  111. auto close_tag = [&] {
  112. if (node_stack.size() > 1)
  113. node_stack.take_last();
  114. };
  115. auto open_tag = [&] {
  116. auto new_element = create_element(document, String::copy(tag_name_buffer));
  117. tag_name_buffer.clear();
  118. new_element->set_attributes(move(attributes));
  119. node_stack.append(new_element);
  120. if (node_stack.size() != 1)
  121. node_stack[node_stack.size() - 2].append_child(new_element, false);
  122. if (is_self_closing_tag(new_element->tag_name()))
  123. close_tag();
  124. };
  125. auto commit_tag = [&] {
  126. if (is_slash_tag)
  127. close_tag();
  128. else
  129. open_tag();
  130. };
  131. auto commit_attribute = [&] {
  132. attributes.append({ String::copy(attribute_name_buffer), String::copy(attribute_value_buffer) });
  133. };
  134. for (int i = 0; i < html.length(); ++i) {
  135. char ch = html[i];
  136. switch (state) {
  137. case State::Free:
  138. if (ch == '<') {
  139. is_slash_tag = false;
  140. move_to_state(State::BeforeTagName);
  141. break;
  142. }
  143. if (ch != '&') {
  144. text_buffer.append(ch);
  145. } else {
  146. struct Escape {
  147. const char* code;
  148. const char* value;
  149. };
  150. static Escape escapes[] = {
  151. { "&lt;", "<" },
  152. { "&gt;", ">" },
  153. { "&amp;", "&" }
  154. };
  155. auto rest_of_html = html.substring_view(i, html.length() - i);
  156. bool found = false;
  157. for (auto& escape : escapes) {
  158. if (rest_of_html.starts_with(escape.code)) {
  159. text_buffer.append(escape.value);
  160. found = true;
  161. i += strlen(escape.code) - 1;
  162. break;
  163. }
  164. }
  165. if (!found)
  166. dbg() << "Unhandled escape sequence";
  167. }
  168. break;
  169. case State::BeforeTagName:
  170. if (ch == '/') {
  171. is_slash_tag = true;
  172. break;
  173. }
  174. if (ch == '>') {
  175. move_to_state(State::Free);
  176. break;
  177. }
  178. if (!isalpha(ch))
  179. break;
  180. move_to_state(State::InTagName);
  181. [[fallthrough]];
  182. case State::InTagName:
  183. if (isspace(ch)) {
  184. move_to_state(State::InAttributeList);
  185. break;
  186. }
  187. if (ch == '>') {
  188. commit_tag();
  189. move_to_state(State::Free);
  190. break;
  191. }
  192. tag_name_buffer.append(ch);
  193. break;
  194. case State::InAttributeList:
  195. if (ch == '>') {
  196. commit_tag();
  197. move_to_state(State::Free);
  198. break;
  199. }
  200. if (!isalpha(ch))
  201. break;
  202. move_to_state(State::InAttributeName);
  203. [[fallthrough]];
  204. case State::InAttributeName:
  205. if (is_valid_in_attribute_name(ch)) {
  206. attribute_name_buffer.append(ch);
  207. break;
  208. }
  209. if (isspace(ch)) {
  210. commit_attribute();
  211. break;
  212. }
  213. if (ch == '>') {
  214. commit_tag();
  215. move_to_state(State::Free);
  216. break;
  217. }
  218. if (ch == '=') {
  219. move_to_state(State::BeforeAttributeValue);
  220. break;
  221. }
  222. break;
  223. case State::BeforeAttributeValue:
  224. if (ch == '\'') {
  225. move_to_state(State::InAttributeValueSingleQuote);
  226. break;
  227. }
  228. if (ch == '"') {
  229. move_to_state(State::InAttributeValueDoubleQuote);
  230. break;
  231. }
  232. if (ch == '>') {
  233. commit_tag();
  234. move_to_state(State::Free);
  235. break;
  236. }
  237. if (isspace(ch)) {
  238. commit_attribute();
  239. move_to_state(State::InAttributeList);
  240. break;
  241. }
  242. break;
  243. case State::InAttributeValueSingleQuote:
  244. if (ch == '\'') {
  245. commit_attribute();
  246. move_to_state(State::InAttributeList);
  247. break;
  248. }
  249. attribute_value_buffer.append(ch);
  250. break;
  251. case State::InAttributeValueDoubleQuote:
  252. if (ch == '"') {
  253. commit_attribute();
  254. move_to_state(State::InAttributeList);
  255. break;
  256. }
  257. attribute_value_buffer.append(ch);
  258. break;
  259. case State::InAttributeValueNoQuote:
  260. if (isspace(ch)) {
  261. commit_attribute();
  262. move_to_state(State::InAttributeList);
  263. break;
  264. }
  265. if (ch == '>') {
  266. commit_tag();
  267. move_to_state(State::Free);
  268. break;
  269. }
  270. attribute_value_buffer.append(ch);
  271. break;
  272. default:
  273. fprintf(stderr, "Unhandled state %d\n", (int)state);
  274. ASSERT_NOT_REACHED();
  275. }
  276. }
  277. Function<void(Node&)> fire_insertion_callbacks = [&](Node& node) {
  278. for (auto* child = node.first_child(); child; child = child->next_sibling()) {
  279. fire_insertion_callbacks(*child);
  280. }
  281. if (node.parent())
  282. node.inserted_into(*node.parent());
  283. };
  284. fire_insertion_callbacks(*document);
  285. return document;
  286. }