HTMLParser.cpp 8.5 KB

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