HTMLParser.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. #include <AK/Function.h>
  2. #include <AK/NonnullRefPtrVector.h>
  3. #include <AK/StringBuilder.h>
  4. #include <LibHTML/DOM/Comment.h>
  5. #include <LibHTML/DOM/DocumentFragment.h>
  6. #include <LibHTML/DOM/DocumentType.h>
  7. #include <LibHTML/DOM/Element.h>
  8. #include <LibHTML/DOM/ElementFactory.h>
  9. #include <LibHTML/DOM/Text.h>
  10. #include <LibHTML/Parser/HTMLParser.h>
  11. #include <ctype.h>
  12. #include <stdio.h>
  13. static bool is_valid_in_attribute_name(char ch)
  14. {
  15. return isalnum(ch) || ch == '_' || ch == '-';
  16. }
  17. static bool is_self_closing_tag(const StringView& tag_name)
  18. {
  19. return tag_name == "area"
  20. || tag_name == "base"
  21. || tag_name == "br"
  22. || tag_name == "col"
  23. || tag_name == "embed"
  24. || tag_name == "hr"
  25. || tag_name == "img"
  26. || tag_name == "input"
  27. || tag_name == "link"
  28. || tag_name == "meta"
  29. || tag_name == "param"
  30. || tag_name == "source"
  31. || tag_name == "track"
  32. || tag_name == "wbr";
  33. }
  34. static bool parse_html_document(const StringView& html, Document& document, ParentNode& root)
  35. {
  36. NonnullRefPtrVector<ParentNode> node_stack;
  37. node_stack.append(root);
  38. enum class State {
  39. Free = 0,
  40. BeforeTagName,
  41. InTagName,
  42. InDoctype,
  43. InComment,
  44. InAttributeList,
  45. InAttributeName,
  46. BeforeAttributeValue,
  47. InAttributeValueNoQuote,
  48. InAttributeValueSingleQuote,
  49. InAttributeValueDoubleQuote,
  50. };
  51. auto state = State::Free;
  52. StringBuilder text_buffer;
  53. Vector<char, 32> tag_name_buffer;
  54. Vector<Attribute> attributes;
  55. Vector<char, 256> attribute_name_buffer;
  56. Vector<char, 256> attribute_value_buffer;
  57. bool is_slash_tag = false;
  58. bool is_exclamation_tag = false;
  59. auto move_to_state = [&](State new_state) {
  60. if (new_state == State::BeforeTagName) {
  61. is_slash_tag = false;
  62. is_exclamation_tag = false;
  63. tag_name_buffer.clear();
  64. attributes.clear();
  65. }
  66. if (new_state == State::InAttributeName)
  67. attribute_name_buffer.clear();
  68. if (new_state == State::BeforeAttributeValue)
  69. attribute_value_buffer.clear();
  70. if (state == State::Free && !text_buffer.string_view().is_empty()) {
  71. auto text_node = adopt(*new Text(document, text_buffer.to_string()));
  72. node_stack.last().append_child(text_node, false);
  73. }
  74. state = new_state;
  75. text_buffer.clear();
  76. };
  77. auto close_tag = [&] {
  78. if (node_stack.size() > 1)
  79. node_stack.take_last();
  80. };
  81. auto open_tag = [&] {
  82. auto new_element = create_element(document, String::copy(tag_name_buffer));
  83. tag_name_buffer.clear();
  84. new_element->set_attributes(move(attributes));
  85. node_stack.append(new_element);
  86. if (node_stack.size() != 1)
  87. node_stack[node_stack.size() - 2].append_child(new_element, false);
  88. if (is_self_closing_tag(new_element->tag_name()))
  89. close_tag();
  90. };
  91. auto commit_doctype = [&] {
  92. node_stack.last().append_child(adopt(*new DocumentType(document)), false);
  93. };
  94. auto commit_comment = [&] {
  95. node_stack.last().append_child(adopt(*new Comment(document, text_buffer.to_string())), false);
  96. };
  97. auto commit_tag = [&] {
  98. if (is_slash_tag)
  99. close_tag();
  100. else
  101. open_tag();
  102. };
  103. auto commit_attribute = [&] {
  104. if (!attribute_name_buffer.is_empty()) {
  105. auto name = String::copy(attribute_name_buffer);
  106. String value;
  107. if (attribute_value_buffer.is_empty())
  108. value = String::empty();
  109. else
  110. value = String::copy(attribute_value_buffer);
  111. attributes.empend(name, value);
  112. }
  113. };
  114. for (int i = 0; i < html.length(); ++i) {
  115. auto peek = [&](int offset) -> char {
  116. if (i + offset >= html.length())
  117. return '\0';
  118. return html[i + offset];
  119. };
  120. char ch = html[i];
  121. switch (state) {
  122. case State::Free:
  123. if (ch == '<') {
  124. is_slash_tag = false;
  125. move_to_state(State::BeforeTagName);
  126. break;
  127. }
  128. if (ch != '&') {
  129. text_buffer.append(ch);
  130. } else {
  131. struct Escape {
  132. const char* code;
  133. const char* value;
  134. };
  135. static Escape escapes[] = {
  136. { "&lt;", "<" },
  137. { "&gt;", ">" },
  138. { "&amp;", "&" },
  139. { "&mdash;", "-" },
  140. };
  141. auto rest_of_html = html.substring_view(i, html.length() - i);
  142. bool found = false;
  143. for (auto& escape : escapes) {
  144. if (rest_of_html.starts_with(escape.code)) {
  145. text_buffer.append(escape.value);
  146. found = true;
  147. i += strlen(escape.code) - 1;
  148. break;
  149. }
  150. }
  151. if (!found)
  152. dbg() << "Unhandled escape sequence";
  153. }
  154. break;
  155. case State::BeforeTagName:
  156. if (ch == '/') {
  157. is_slash_tag = true;
  158. break;
  159. }
  160. if (ch == '!') {
  161. if (toupper(peek(1)) == 'D'
  162. && toupper(peek(2)) == 'O'
  163. && toupper(peek(3)) == 'C'
  164. && toupper(peek(4)) == 'T'
  165. && toupper(peek(5)) == 'Y'
  166. && toupper(peek(6)) == 'P'
  167. && toupper(peek(7)) == 'E') {
  168. i += 7;
  169. move_to_state(State::InDoctype);
  170. break;
  171. }
  172. if (peek(1) == '-' && peek(2) == '-') {
  173. i += 2;
  174. move_to_state(State::InComment);
  175. break;
  176. }
  177. break;
  178. }
  179. if (ch == '>') {
  180. move_to_state(State::Free);
  181. break;
  182. }
  183. if (!isalpha(ch))
  184. break;
  185. move_to_state(State::InTagName);
  186. [[fallthrough]];
  187. case State::InTagName:
  188. if (isspace(ch)) {
  189. move_to_state(State::InAttributeList);
  190. break;
  191. }
  192. if (ch == '>') {
  193. commit_tag();
  194. move_to_state(State::Free);
  195. break;
  196. }
  197. tag_name_buffer.append(ch);
  198. break;
  199. case State::InDoctype:
  200. if (ch == '>') {
  201. commit_doctype();
  202. move_to_state(State::Free);
  203. break;
  204. }
  205. break;
  206. case State::InComment:
  207. if (ch == '-' && peek(1) == '-' && peek(2) == '>') {
  208. commit_comment();
  209. i += 2;
  210. move_to_state(State::Free);
  211. break;
  212. }
  213. text_buffer.append(ch);
  214. break;
  215. case State::InAttributeList:
  216. if (ch == '>') {
  217. commit_tag();
  218. move_to_state(State::Free);
  219. break;
  220. }
  221. if (!isalpha(ch))
  222. break;
  223. move_to_state(State::InAttributeName);
  224. [[fallthrough]];
  225. case State::InAttributeName:
  226. if (is_valid_in_attribute_name(ch)) {
  227. attribute_name_buffer.append(ch);
  228. break;
  229. }
  230. if (isspace(ch)) {
  231. commit_attribute();
  232. break;
  233. }
  234. if (ch == '>') {
  235. commit_attribute();
  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. return true;
  300. }
  301. RefPtr<DocumentFragment> parse_html_fragment(Document& document, const StringView& html)
  302. {
  303. auto fragment = adopt(*new DocumentFragment(document));
  304. if (!parse_html_document(html, document, *fragment))
  305. return nullptr;
  306. return fragment;
  307. }
  308. RefPtr<Document> parse_html_document(const StringView& html, const URL& url)
  309. {
  310. auto document = adopt(*new Document);
  311. document->set_url(url);
  312. if (!parse_html_document(html, *document, *document))
  313. return nullptr;
  314. document->fixup();
  315. Function<void(Node&)> fire_insertion_callbacks = [&](Node& node) {
  316. for (auto* child = node.first_child(); child; child = child->next_sibling()) {
  317. fire_insertion_callbacks(*child);
  318. }
  319. if (node.parent())
  320. node.inserted_into(*node.parent());
  321. };
  322. fire_insertion_callbacks(document);
  323. return document;
  324. }
  325. String escape_html_entities(const StringView& html)
  326. {
  327. StringBuilder builder;
  328. for (int i = 0; i < html.length(); ++i) {
  329. if (html[i] == '<')
  330. builder.append("&lt;");
  331. else if (html[i] == '>')
  332. builder.append("&gt;");
  333. else if (html[i] == '&')
  334. builder.append("&amp;");
  335. else
  336. builder.append(html[i]);
  337. }
  338. return builder.to_string();
  339. }