HTMLDocumentParser.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/NonnullRefPtrVector.h>
  8. #include <LibWeb/DOM/Node.h>
  9. #include <LibWeb/HTML/Parser/HTMLTokenizer.h>
  10. #include <LibWeb/HTML/Parser/ListOfActiveFormattingElements.h>
  11. #include <LibWeb/HTML/Parser/StackOfOpenElements.h>
  12. namespace Web::HTML {
  13. #define ENUMERATE_INSERTION_MODES \
  14. __ENUMERATE_INSERTION_MODE(Initial) \
  15. __ENUMERATE_INSERTION_MODE(BeforeHTML) \
  16. __ENUMERATE_INSERTION_MODE(BeforeHead) \
  17. __ENUMERATE_INSERTION_MODE(InHead) \
  18. __ENUMERATE_INSERTION_MODE(InHeadNoscript) \
  19. __ENUMERATE_INSERTION_MODE(AfterHead) \
  20. __ENUMERATE_INSERTION_MODE(InBody) \
  21. __ENUMERATE_INSERTION_MODE(Text) \
  22. __ENUMERATE_INSERTION_MODE(InTable) \
  23. __ENUMERATE_INSERTION_MODE(InTableText) \
  24. __ENUMERATE_INSERTION_MODE(InCaption) \
  25. __ENUMERATE_INSERTION_MODE(InColumnGroup) \
  26. __ENUMERATE_INSERTION_MODE(InTableBody) \
  27. __ENUMERATE_INSERTION_MODE(InRow) \
  28. __ENUMERATE_INSERTION_MODE(InCell) \
  29. __ENUMERATE_INSERTION_MODE(InSelect) \
  30. __ENUMERATE_INSERTION_MODE(InSelectInTable) \
  31. __ENUMERATE_INSERTION_MODE(InTemplate) \
  32. __ENUMERATE_INSERTION_MODE(AfterBody) \
  33. __ENUMERATE_INSERTION_MODE(InFrameset) \
  34. __ENUMERATE_INSERTION_MODE(AfterFrameset) \
  35. __ENUMERATE_INSERTION_MODE(AfterAfterBody) \
  36. __ENUMERATE_INSERTION_MODE(AfterAfterFrameset)
  37. RefPtr<DOM::Document> parse_html_document(const StringView&, const URL&, const String& encoding);
  38. class HTMLDocumentParser {
  39. public:
  40. HTMLDocumentParser(DOM::Document&, const StringView& input, const String& encoding);
  41. ~HTMLDocumentParser();
  42. static NonnullOwnPtr<HTMLDocumentParser> create_with_uncertain_encoding(DOM::Document&, const ByteBuffer& input);
  43. void run(const URL&);
  44. DOM::Document& document();
  45. static NonnullRefPtrVector<DOM::Node> parse_html_fragment(DOM::Element& context_element, const StringView&);
  46. enum class InsertionMode {
  47. #define __ENUMERATE_INSERTION_MODE(mode) mode,
  48. ENUMERATE_INSERTION_MODES
  49. #undef __ENUMERATE_INSERTION_MODE
  50. };
  51. InsertionMode insertion_mode() const { return m_insertion_mode; }
  52. static bool is_special_tag(const FlyString& tag_name, const FlyString& namespace_);
  53. private:
  54. const char* insertion_mode_name() const;
  55. DOM::QuirksMode which_quirks_mode(const HTMLToken&) const;
  56. void handle_initial(HTMLToken&);
  57. void handle_before_html(HTMLToken&);
  58. void handle_before_head(HTMLToken&);
  59. void handle_in_head(HTMLToken&);
  60. void handle_in_head_noscript(HTMLToken&);
  61. void handle_after_head(HTMLToken&);
  62. void handle_in_body(HTMLToken&);
  63. void handle_after_body(HTMLToken&);
  64. void handle_after_after_body(HTMLToken&);
  65. void handle_text(HTMLToken&);
  66. void handle_in_table(HTMLToken&);
  67. void handle_in_table_body(HTMLToken&);
  68. void handle_in_row(HTMLToken&);
  69. void handle_in_cell(HTMLToken&);
  70. void handle_in_table_text(HTMLToken&);
  71. void handle_in_select_in_table(HTMLToken&);
  72. void handle_in_select(HTMLToken&);
  73. void handle_in_caption(HTMLToken&);
  74. void handle_in_column_group(HTMLToken&);
  75. void handle_in_template(HTMLToken&);
  76. void handle_in_frameset(HTMLToken&);
  77. void handle_after_frameset(HTMLToken&);
  78. void handle_after_after_frameset(HTMLToken&);
  79. void stop_parsing() { m_stop_parsing = true; }
  80. void generate_implied_end_tags(const FlyString& exception = {});
  81. void generate_all_implied_end_tags_thoroughly();
  82. bool stack_of_open_elements_has_element_with_tag_name_in_scope(const FlyString& tag_name);
  83. NonnullRefPtr<DOM::Element> create_element_for(const HTMLToken&, const FlyString& namespace_);
  84. struct AdjustedInsertionLocation {
  85. RefPtr<DOM::Node> parent;
  86. RefPtr<DOM::Node> insert_before_sibling;
  87. };
  88. AdjustedInsertionLocation find_appropriate_place_for_inserting_node();
  89. DOM::Text* find_character_insertion_node();
  90. void flush_character_insertions();
  91. NonnullRefPtr<DOM::Element> insert_foreign_element(const HTMLToken&, const FlyString&);
  92. NonnullRefPtr<DOM::Element> insert_html_element(const HTMLToken&);
  93. DOM::Element& current_node();
  94. DOM::Element& adjusted_current_node();
  95. DOM::Element& node_before_current_node();
  96. void insert_character(u32 data);
  97. void insert_comment(HTMLToken&);
  98. void reconstruct_the_active_formatting_elements();
  99. void close_a_p_element();
  100. void process_using_the_rules_for(InsertionMode, HTMLToken&);
  101. void process_using_the_rules_for_foreign_content(HTMLToken&);
  102. void parse_generic_raw_text_element(HTMLToken&);
  103. void increment_script_nesting_level();
  104. void decrement_script_nesting_level();
  105. size_t script_nesting_level() const { return m_script_nesting_level; }
  106. void reset_the_insertion_mode_appropriately();
  107. void adjust_mathml_attributes(HTMLToken&);
  108. void adjust_svg_tag_names(HTMLToken&);
  109. void adjust_svg_attributes(HTMLToken&);
  110. void adjust_foreign_attributes(HTMLToken&);
  111. enum AdoptionAgencyAlgorithmOutcome {
  112. DoNothing,
  113. RunAnyOtherEndTagSteps,
  114. };
  115. AdoptionAgencyAlgorithmOutcome run_the_adoption_agency_algorithm(HTMLToken&);
  116. void clear_the_stack_back_to_a_table_context();
  117. void clear_the_stack_back_to_a_table_body_context();
  118. void clear_the_stack_back_to_a_table_row_context();
  119. void close_the_cell();
  120. InsertionMode m_insertion_mode { InsertionMode::Initial };
  121. InsertionMode m_original_insertion_mode { InsertionMode::Initial };
  122. StackOfOpenElements m_stack_of_open_elements;
  123. Vector<InsertionMode> m_stack_of_template_insertion_modes;
  124. ListOfActiveFormattingElements m_list_of_active_formatting_elements;
  125. HTMLTokenizer m_tokenizer;
  126. bool m_foster_parenting { false };
  127. bool m_frameset_ok { true };
  128. bool m_parsing_fragment { false };
  129. bool m_scripting_enabled { true };
  130. bool m_invoked_via_document_write { false };
  131. bool m_aborted { false };
  132. bool m_parser_pause_flag { false };
  133. bool m_stop_parsing { false };
  134. size_t m_script_nesting_level { 0 };
  135. NonnullRefPtr<DOM::Document> m_document;
  136. RefPtr<HTMLHeadElement> m_head_element;
  137. RefPtr<HTMLFormElement> m_form_element;
  138. RefPtr<DOM::Element> m_context_element;
  139. Vector<HTMLToken> m_pending_table_character_tokens;
  140. RefPtr<DOM::Text> m_character_insertion_node;
  141. StringBuilder m_character_insertion_builder;
  142. };
  143. }