Document.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include <AK/FileSystemPath.h>
  2. #include <AK/StringBuilder.h>
  3. #include <LibHTML/CSS/StyleResolver.h>
  4. #include <LibHTML/DOM/Document.h>
  5. #include <LibHTML/DOM/DocumentType.h>
  6. #include <LibHTML/DOM/Element.h>
  7. #include <LibHTML/DOM/ElementFactory.h>
  8. #include <LibHTML/DOM/HTMLBodyElement.h>
  9. #include <LibHTML/DOM/HTMLHeadElement.h>
  10. #include <LibHTML/DOM/HTMLHtmlElement.h>
  11. #include <LibHTML/DOM/HTMLTitleElement.h>
  12. #include <LibHTML/Frame.h>
  13. #include <LibHTML/Layout/LayoutDocument.h>
  14. #include <LibHTML/Layout/LayoutTreeBuilder.h>
  15. #include <stdio.h>
  16. Document::Document()
  17. : ParentNode(*this, NodeType::DOCUMENT_NODE)
  18. , m_style_resolver(make<StyleResolver>(*this))
  19. {
  20. }
  21. Document::~Document()
  22. {
  23. }
  24. bool Document::is_child_allowed(const Node& node) const
  25. {
  26. switch (node.type()) {
  27. case NodeType::DOCUMENT_NODE:
  28. case NodeType::TEXT_NODE:
  29. return false;
  30. case NodeType::COMMENT_NODE:
  31. return true;
  32. case NodeType::DOCUMENT_TYPE_NODE:
  33. return !first_child_of_type<DocumentType>();
  34. case NodeType::ELEMENT_NODE:
  35. return !first_child_of_type<Element>();
  36. default:
  37. return false;
  38. }
  39. }
  40. void Document::fixup()
  41. {
  42. if (!is<DocumentType>(first_child()))
  43. prepend_child(adopt(*new DocumentType(*this)));
  44. if (is<HTMLHtmlElement>(first_child()->next_sibling()))
  45. return;
  46. auto body = create_element(*this, "body");
  47. auto html = create_element(*this, "html");
  48. html->append_child(body);
  49. this->donate_all_children_to(body);
  50. this->append_child(html);
  51. }
  52. const HTMLHtmlElement* Document::document_element() const
  53. {
  54. return first_child_of_type<HTMLHtmlElement>();
  55. }
  56. const HTMLHeadElement* Document::head() const
  57. {
  58. auto* html = document_element();
  59. if (!html)
  60. return nullptr;
  61. return html->first_child_of_type<HTMLHeadElement>();
  62. }
  63. const HTMLBodyElement* Document::body() const
  64. {
  65. auto* html = document_element();
  66. if (!html)
  67. return nullptr;
  68. return html->first_child_of_type<HTMLBodyElement>();
  69. }
  70. String Document::title() const
  71. {
  72. auto* head_element = head();
  73. if (!head_element)
  74. return {};
  75. auto* title_element = head_element->first_child_of_type<HTMLTitleElement>();
  76. if (!title_element)
  77. return {};
  78. return title_element->text_content();
  79. }
  80. void Document::attach_to_frame(Badge<Frame>, Frame& frame)
  81. {
  82. m_frame = frame.make_weak_ptr();
  83. layout();
  84. }
  85. void Document::detach_from_frame(Badge<Frame>, Frame&)
  86. {
  87. m_layout_root = nullptr;
  88. m_frame = nullptr;
  89. }
  90. Color Document::background_color() const
  91. {
  92. auto* body_element = body();
  93. if (!body_element)
  94. return Color::White;
  95. auto* body_layout_node = body_element->layout_node();
  96. if (!body_layout_node)
  97. return Color::White;
  98. auto background_color = body_layout_node->style().property(CSS::PropertyID::BackgroundColor);
  99. if (!background_color.has_value() || !background_color.value()->is_color())
  100. return Color::White;
  101. return background_color.value()->to_color(*this);
  102. }
  103. URL Document::complete_url(const String& string) const
  104. {
  105. URL url(string);
  106. if (url.is_valid())
  107. return url;
  108. FileSystemPath fspath(m_url.path());
  109. StringBuilder builder;
  110. builder.append('/');
  111. bool document_url_ends_in_slash = m_url.path()[m_url.path().length() - 1] == '/';
  112. for (int i = 0; i < fspath.parts().size(); ++i) {
  113. if (i == fspath.parts().size() - 1 && !document_url_ends_in_slash)
  114. break;
  115. builder.append(fspath.parts()[i]);
  116. builder.append('/');
  117. }
  118. builder.append(string);
  119. auto built = builder.to_string();
  120. fspath = FileSystemPath(built);
  121. url = m_url;
  122. url.set_path(fspath.string());
  123. return url;
  124. }
  125. void Document::layout()
  126. {
  127. if (!m_layout_root) {
  128. LayoutTreeBuilder tree_builder;
  129. m_layout_root = tree_builder.build(*this);
  130. }
  131. m_layout_root->layout();
  132. }
  133. void Document::update_style()
  134. {
  135. m_layout_root = nullptr;
  136. update_layout();
  137. }
  138. void Document::update_layout()
  139. {
  140. layout();
  141. if (on_layout_updated)
  142. on_layout_updated();
  143. }
  144. RefPtr<LayoutNode> Document::create_layout_node(const StyleProperties*) const
  145. {
  146. return adopt(*new LayoutDocument(*this, StyleProperties::create()));
  147. }
  148. void Document::set_link_color(Color color)
  149. {
  150. m_link_color = color;
  151. }
  152. void Document::set_active_link_color(Color color)
  153. {
  154. m_active_link_color = color;
  155. }
  156. void Document::set_visited_link_color(Color color)
  157. {
  158. m_visited_link_color = color;
  159. }
  160. const LayoutDocument* Document::layout_node() const
  161. {
  162. return static_cast<const LayoutDocument*>(Node::layout_node());
  163. }
  164. void Document::set_hovered_node(Node* node)
  165. {
  166. if (m_hovered_node == node)
  167. return;
  168. RefPtr<Node> old_hovered_node = move(m_hovered_node);
  169. m_hovered_node = node;
  170. if (old_hovered_node)
  171. old_hovered_node->invalidate_style();
  172. if (m_hovered_node)
  173. m_hovered_node->invalidate_style();
  174. }