HTMLParser.cpp 11 KB

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