HTMLParser.cpp 9.8 KB

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