HTMLParser.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <andreas@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibGfx/Color.h>
  8. #include <LibJS/Heap/Cell.h>
  9. #include <LibWeb/DOM/Node.h>
  10. #include <LibWeb/HTML/Parser/HTMLTokenizer.h>
  11. #include <LibWeb/HTML/Parser/ListOfActiveFormattingElements.h>
  12. #include <LibWeb/HTML/Parser/StackOfOpenElements.h>
  13. #include <LibWeb/MimeSniff/MimeType.h>
  14. namespace Web::HTML {
  15. #define ENUMERATE_INSERTION_MODES \
  16. __ENUMERATE_INSERTION_MODE(Initial) \
  17. __ENUMERATE_INSERTION_MODE(BeforeHTML) \
  18. __ENUMERATE_INSERTION_MODE(BeforeHead) \
  19. __ENUMERATE_INSERTION_MODE(InHead) \
  20. __ENUMERATE_INSERTION_MODE(InHeadNoscript) \
  21. __ENUMERATE_INSERTION_MODE(AfterHead) \
  22. __ENUMERATE_INSERTION_MODE(InBody) \
  23. __ENUMERATE_INSERTION_MODE(Text) \
  24. __ENUMERATE_INSERTION_MODE(InTable) \
  25. __ENUMERATE_INSERTION_MODE(InTableText) \
  26. __ENUMERATE_INSERTION_MODE(InCaption) \
  27. __ENUMERATE_INSERTION_MODE(InColumnGroup) \
  28. __ENUMERATE_INSERTION_MODE(InTableBody) \
  29. __ENUMERATE_INSERTION_MODE(InRow) \
  30. __ENUMERATE_INSERTION_MODE(InCell) \
  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. class HTMLParser final : public JS::Cell {
  38. GC_CELL(HTMLParser, JS::Cell);
  39. GC_DECLARE_ALLOCATOR(HTMLParser);
  40. friend class HTMLTokenizer;
  41. public:
  42. ~HTMLParser();
  43. static GC::Ref<HTMLParser> create_for_scripting(DOM::Document&);
  44. static GC::Ref<HTMLParser> create_with_uncertain_encoding(DOM::Document&, ByteBuffer const& input, Optional<MimeSniff::MimeType> maybe_mime_type = {});
  45. static GC::Ref<HTMLParser> create(DOM::Document&, StringView input, StringView encoding);
  46. void run(HTMLTokenizer::StopAtInsertionPoint = HTMLTokenizer::StopAtInsertionPoint::No);
  47. void run(const URL::URL&, HTMLTokenizer::StopAtInsertionPoint = HTMLTokenizer::StopAtInsertionPoint::No);
  48. static void the_end(GC::Ref<DOM::Document>, GC::Ptr<HTMLParser> = nullptr);
  49. DOM::Document& document();
  50. enum class AllowDeclarativeShadowRoots {
  51. No,
  52. Yes,
  53. };
  54. static Vector<GC::Root<DOM::Node>> parse_html_fragment(DOM::Element& context_element, StringView, AllowDeclarativeShadowRoots = AllowDeclarativeShadowRoots::No);
  55. enum class SerializableShadowRoots {
  56. No,
  57. Yes,
  58. };
  59. static String serialize_html_fragment(DOM::Node const&, SerializableShadowRoots, Vector<GC::Root<DOM::ShadowRoot>> const&, DOM::FragmentSerializationMode = DOM::FragmentSerializationMode::Inner);
  60. enum class InsertionMode {
  61. #define __ENUMERATE_INSERTION_MODE(mode) mode,
  62. ENUMERATE_INSERTION_MODES
  63. #undef __ENUMERATE_INSERTION_MODE
  64. };
  65. InsertionMode insertion_mode() const { return m_insertion_mode; }
  66. static bool is_special_tag(FlyString const& tag_name, Optional<FlyString> const& namespace_);
  67. HTMLTokenizer& tokenizer() { return m_tokenizer; }
  68. // https://html.spec.whatwg.org/multipage/parsing.html#abort-a-parser
  69. void abort();
  70. bool aborted() const { return m_aborted; }
  71. bool stopped() const { return m_stop_parsing; }
  72. size_t script_nesting_level() const { return m_script_nesting_level; }
  73. private:
  74. HTMLParser(DOM::Document&, StringView input, StringView encoding);
  75. HTMLParser(DOM::Document&);
  76. virtual void visit_edges(Cell::Visitor&) override;
  77. char const* insertion_mode_name() const;
  78. DOM::QuirksMode which_quirks_mode(HTMLToken const&) const;
  79. void handle_initial(HTMLToken&);
  80. void handle_before_html(HTMLToken&);
  81. void handle_before_head(HTMLToken&);
  82. void handle_in_head(HTMLToken&);
  83. void handle_in_head_noscript(HTMLToken&);
  84. void handle_after_head(HTMLToken&);
  85. void handle_in_body(HTMLToken&);
  86. void handle_after_body(HTMLToken&);
  87. void handle_after_after_body(HTMLToken&);
  88. void handle_text(HTMLToken&);
  89. void handle_in_table(HTMLToken&);
  90. void handle_in_table_body(HTMLToken&);
  91. void handle_in_row(HTMLToken&);
  92. void handle_in_cell(HTMLToken&);
  93. void handle_in_table_text(HTMLToken&);
  94. void handle_in_caption(HTMLToken&);
  95. void handle_in_column_group(HTMLToken&);
  96. void handle_in_template(HTMLToken&);
  97. void handle_in_frameset(HTMLToken&);
  98. void handle_after_frameset(HTMLToken&);
  99. void handle_after_after_frameset(HTMLToken&);
  100. void stop_parsing() { m_stop_parsing = true; }
  101. void generate_implied_end_tags(FlyString const& exception = {});
  102. void generate_all_implied_end_tags_thoroughly();
  103. GC::Ref<DOM::Element> create_element_for(HTMLToken const&, Optional<FlyString> const& namespace_, DOM::Node& intended_parent);
  104. struct AdjustedInsertionLocation {
  105. GC::Ptr<DOM::Node> parent;
  106. GC::Ptr<DOM::Node> insert_before_sibling;
  107. };
  108. AdjustedInsertionLocation find_appropriate_place_for_inserting_node(GC::Ptr<DOM::Element> override_target = nullptr);
  109. void insert_an_element_at_the_adjusted_insertion_location(GC::Ref<DOM::Element>);
  110. DOM::Text* find_character_insertion_node();
  111. void flush_character_insertions();
  112. enum class OnlyAddToElementStack {
  113. No,
  114. Yes,
  115. };
  116. GC::Ref<DOM::Element> insert_foreign_element(HTMLToken const&, Optional<FlyString> const& namespace_, OnlyAddToElementStack);
  117. GC::Ref<DOM::Element> insert_html_element(HTMLToken const&);
  118. [[nodiscard]] GC::Ptr<DOM::Element> current_node();
  119. [[nodiscard]] GC::Ptr<DOM::Element> adjusted_current_node();
  120. [[nodiscard]] GC::Ptr<DOM::Element> node_before_current_node();
  121. void insert_character(u32 data);
  122. void insert_comment(HTMLToken&);
  123. void reconstruct_the_active_formatting_elements();
  124. void close_a_p_element();
  125. void process_using_the_rules_for(InsertionMode, HTMLToken&);
  126. void process_using_the_rules_for_foreign_content(HTMLToken&);
  127. void parse_generic_raw_text_element(HTMLToken&);
  128. void increment_script_nesting_level();
  129. void decrement_script_nesting_level();
  130. void reset_the_insertion_mode_appropriately();
  131. void adjust_mathml_attributes(HTMLToken&);
  132. void adjust_svg_tag_names(HTMLToken&);
  133. void adjust_svg_attributes(HTMLToken&);
  134. static void adjust_foreign_attributes(HTMLToken&);
  135. enum AdoptionAgencyAlgorithmOutcome {
  136. DoNothing,
  137. RunAnyOtherEndTagSteps,
  138. };
  139. AdoptionAgencyAlgorithmOutcome run_the_adoption_agency_algorithm(HTMLToken&);
  140. void clear_the_stack_back_to_a_table_context();
  141. void clear_the_stack_back_to_a_table_body_context();
  142. void clear_the_stack_back_to_a_table_row_context();
  143. void close_the_cell();
  144. InsertionMode m_insertion_mode { InsertionMode::Initial };
  145. InsertionMode m_original_insertion_mode { InsertionMode::Initial };
  146. StackOfOpenElements m_stack_of_open_elements;
  147. Vector<InsertionMode> m_stack_of_template_insertion_modes;
  148. ListOfActiveFormattingElements m_list_of_active_formatting_elements;
  149. HTMLTokenizer m_tokenizer;
  150. bool m_foster_parenting { false };
  151. bool m_frameset_ok { true };
  152. bool m_parsing_fragment { false };
  153. // https://html.spec.whatwg.org/multipage/parsing.html#scripting-flag
  154. // The scripting flag is set to "enabled" if scripting was enabled for the Document with which the parser is associated when the parser was created, and "disabled" otherwise.
  155. bool m_scripting_enabled { true };
  156. bool m_invoked_via_document_write { false };
  157. bool m_aborted { false };
  158. bool m_parser_pause_flag { false };
  159. bool m_stop_parsing { false };
  160. size_t m_script_nesting_level { 0 };
  161. JS::Realm& realm();
  162. GC::Ptr<DOM::Document> m_document;
  163. GC::Ptr<HTMLHeadElement> m_head_element;
  164. GC::Ptr<HTMLFormElement> m_form_element;
  165. GC::Ptr<DOM::Element> m_context_element;
  166. Vector<HTMLToken> m_pending_table_character_tokens;
  167. GC::Ptr<DOM::Text> m_character_insertion_node;
  168. StringBuilder m_character_insertion_builder;
  169. };
  170. RefPtr<CSS::CSSStyleValue> parse_dimension_value(StringView);
  171. RefPtr<CSS::CSSStyleValue> parse_nonzero_dimension_value(StringView);
  172. Optional<Color> parse_legacy_color_value(StringView);
  173. }