HTMLDocumentParser.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/NonnullRefPtrVector.h>
  28. #include <LibWeb/DOM/Node.h>
  29. #include <LibWeb/Parser/HTMLTokenizer.h>
  30. #include <LibWeb/Parser/ListOfActiveFormattingElements.h>
  31. #include <LibWeb/Parser/StackOfOpenElements.h>
  32. #define ENUMERATE_INSERTION_MODES \
  33. __ENUMERATE_INSERTION_MODE(Initial) \
  34. __ENUMERATE_INSERTION_MODE(BeforeHTML) \
  35. __ENUMERATE_INSERTION_MODE(BeforeHead) \
  36. __ENUMERATE_INSERTION_MODE(InHead) \
  37. __ENUMERATE_INSERTION_MODE(InHeadNoscript) \
  38. __ENUMERATE_INSERTION_MODE(AfterHead) \
  39. __ENUMERATE_INSERTION_MODE(InBody) \
  40. __ENUMERATE_INSERTION_MODE(Text) \
  41. __ENUMERATE_INSERTION_MODE(InTable) \
  42. __ENUMERATE_INSERTION_MODE(InTableText) \
  43. __ENUMERATE_INSERTION_MODE(InCaption) \
  44. __ENUMERATE_INSERTION_MODE(InColumnGroup) \
  45. __ENUMERATE_INSERTION_MODE(InTableBody) \
  46. __ENUMERATE_INSERTION_MODE(InRow) \
  47. __ENUMERATE_INSERTION_MODE(InCell) \
  48. __ENUMERATE_INSERTION_MODE(InSelect) \
  49. __ENUMERATE_INSERTION_MODE(InSelectInTable) \
  50. __ENUMERATE_INSERTION_MODE(InTemplate) \
  51. __ENUMERATE_INSERTION_MODE(AfterBody) \
  52. __ENUMERATE_INSERTION_MODE(InFrameset) \
  53. __ENUMERATE_INSERTION_MODE(AfterFrameset) \
  54. __ENUMERATE_INSERTION_MODE(AfterAfterBody) \
  55. __ENUMERATE_INSERTION_MODE(AfterAfterFrameset)
  56. namespace Web {
  57. RefPtr<Document> parse_html_document(const StringView&, const URL&, const String& encoding);
  58. class HTMLDocumentParser {
  59. public:
  60. HTMLDocumentParser(const StringView& input, const String& encoding);
  61. ~HTMLDocumentParser();
  62. void run(const URL&);
  63. Document& document();
  64. static NonnullRefPtrVector<Node> parse_html_fragment(Element& context_element, const StringView&);
  65. enum class InsertionMode {
  66. #define __ENUMERATE_INSERTION_MODE(mode) mode,
  67. ENUMERATE_INSERTION_MODES
  68. #undef __ENUMERATE_INSERTION_MODE
  69. };
  70. InsertionMode insertion_mode() const { return m_insertion_mode; }
  71. static bool is_special_tag(const FlyString& tag_name);
  72. private:
  73. const char* insertion_mode_name() const;
  74. QuirksMode which_quirks_mode(const HTMLToken&) const;
  75. void handle_initial(HTMLToken&);
  76. void handle_before_html(HTMLToken&);
  77. void handle_before_head(HTMLToken&);
  78. void handle_in_head(HTMLToken&);
  79. void handle_in_head_noscript(HTMLToken&);
  80. void handle_after_head(HTMLToken&);
  81. void handle_in_body(HTMLToken&);
  82. void handle_after_body(HTMLToken&);
  83. void handle_after_after_body(HTMLToken&);
  84. void handle_text(HTMLToken&);
  85. void handle_in_table(HTMLToken&);
  86. void handle_in_table_body(HTMLToken&);
  87. void handle_in_row(HTMLToken&);
  88. void handle_in_cell(HTMLToken&);
  89. void handle_in_table_text(HTMLToken&);
  90. void handle_in_select_in_table(HTMLToken&);
  91. void handle_in_select(HTMLToken&);
  92. void handle_in_caption(HTMLToken&);
  93. void handle_in_column_group(HTMLToken&);
  94. void handle_in_template(HTMLToken&);
  95. void handle_in_frameset(HTMLToken&);
  96. void handle_after_frameset(HTMLToken&);
  97. void handle_after_after_frameset(HTMLToken&);
  98. void stop_parsing() { m_stop_parsing = true; }
  99. void generate_implied_end_tags(const FlyString& exception = {});
  100. bool stack_of_open_elements_has_element_with_tag_name_in_scope(const FlyString& tag_name);
  101. NonnullRefPtr<Element> create_element_for(HTMLToken&);
  102. struct AdjustedInsertionLocation {
  103. RefPtr<Node> parent;
  104. RefPtr<Node> insert_before_sibling;
  105. };
  106. AdjustedInsertionLocation find_appropriate_place_for_inserting_node();
  107. Text* find_character_insertion_node();
  108. void flush_character_insertions();
  109. RefPtr<Element> insert_html_element(HTMLToken&);
  110. Element& current_node();
  111. Element& node_before_current_node();
  112. void insert_character(u32 data);
  113. void insert_comment(HTMLToken&);
  114. void reconstruct_the_active_formatting_elements();
  115. void close_a_p_element();
  116. void process_using_the_rules_for(InsertionMode, HTMLToken&);
  117. void parse_generic_raw_text_element(HTMLToken&);
  118. void increment_script_nesting_level();
  119. void decrement_script_nesting_level();
  120. size_t script_nesting_level() const { return m_script_nesting_level; }
  121. void reset_the_insertion_mode_appropriately();
  122. void adjust_mathml_attributes(HTMLToken&);
  123. void adjust_svg_attributes(HTMLToken&);
  124. void adjust_foreign_attributes(HTMLToken&);
  125. enum AdoptionAgencyAlgorithmOutcome {
  126. DoNothing,
  127. RunAnyOtherEndTagSteps,
  128. };
  129. AdoptionAgencyAlgorithmOutcome run_the_adoption_agency_algorithm(HTMLToken&);
  130. void clear_the_stack_back_to_a_table_context();
  131. void clear_the_stack_back_to_a_table_body_context();
  132. void clear_the_stack_back_to_a_table_row_context();
  133. void close_the_cell();
  134. InsertionMode m_insertion_mode { InsertionMode::Initial };
  135. InsertionMode m_original_insertion_mode { InsertionMode::Initial };
  136. StackOfOpenElements m_stack_of_open_elements;
  137. Vector<InsertionMode> m_stack_of_template_insertion_modes;
  138. ListOfActiveFormattingElements m_list_of_active_formatting_elements;
  139. HTMLTokenizer m_tokenizer;
  140. bool m_foster_parenting { false };
  141. bool m_frameset_ok { true };
  142. bool m_parsing_fragment { false };
  143. bool m_scripting_enabled { true };
  144. bool m_invoked_via_document_write { false };
  145. bool m_aborted { false };
  146. bool m_parser_pause_flag { false };
  147. bool m_stop_parsing { false };
  148. size_t m_script_nesting_level { 0 };
  149. RefPtr<Document> m_document;
  150. RefPtr<HTMLHeadElement> m_head_element;
  151. RefPtr<HTMLFormElement> m_form_element;
  152. RefPtr<Element> m_context_element;
  153. Vector<HTMLToken> m_pending_table_character_tokens;
  154. RefPtr<Text> m_character_insertion_node;
  155. StringBuilder m_character_insertion_builder;
  156. };
  157. }