HTMLParser.cpp 10 KB

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