Document.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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/HTMLBodyElement.h>
  8. #include <LibHTML/DOM/HTMLHeadElement.h>
  9. #include <LibHTML/DOM/HTMLHtmlElement.h>
  10. #include <LibHTML/DOM/HTMLTitleElement.h>
  11. #include <LibHTML/Frame.h>
  12. #include <LibHTML/Layout/LayoutDocument.h>
  13. #include <stdio.h>
  14. Document::Document()
  15. : ParentNode(*this, NodeType::DOCUMENT_NODE)
  16. {
  17. }
  18. Document::~Document()
  19. {
  20. }
  21. StyleResolver& Document::style_resolver()
  22. {
  23. if (!m_style_resolver)
  24. m_style_resolver = make<StyleResolver>(*this);
  25. return *m_style_resolver;
  26. }
  27. void Document::fixup()
  28. {
  29. if (!is<DocumentType>(first_child()))
  30. prepend_child(adopt(*new DocumentType(*this)));
  31. if (is<HTMLHtmlElement>(first_child()->next_sibling()))
  32. return;
  33. auto body = adopt(*new HTMLBodyElement(*this, "body"));
  34. auto html = adopt(*new HTMLHtmlElement(*this, "html"));
  35. html->append_child(body);
  36. this->donate_all_children_to(body);
  37. this->append_child(html);
  38. }
  39. const HTMLHtmlElement* Document::document_element() const
  40. {
  41. return first_child_of_type<HTMLHtmlElement>();
  42. }
  43. const HTMLHeadElement* Document::head() const
  44. {
  45. auto* html = document_element();
  46. if (!html)
  47. return nullptr;
  48. return html->first_child_of_type<HTMLHeadElement>();
  49. }
  50. const HTMLBodyElement* Document::body() const
  51. {
  52. auto* html = document_element();
  53. if (!html)
  54. return nullptr;
  55. return html->first_child_of_type<HTMLBodyElement>();
  56. }
  57. String Document::title() const
  58. {
  59. auto* head_element = head();
  60. if (!head_element)
  61. return {};
  62. auto* title_element = head_element->first_child_of_type<HTMLTitleElement>();
  63. if (!title_element)
  64. return {};
  65. return title_element->text_content();
  66. }
  67. void Document::attach_to_frame(Badge<Frame>, Frame& frame)
  68. {
  69. m_frame = frame.make_weak_ptr();
  70. }
  71. void Document::detach_from_frame(Badge<Frame>, Frame&)
  72. {
  73. m_frame = nullptr;
  74. }
  75. Color Document::background_color() const
  76. {
  77. auto* body_element = body();
  78. if (!body_element)
  79. return Color::White;
  80. auto* body_layout_node = body_element->layout_node();
  81. if (!body_layout_node)
  82. return Color::White;
  83. auto background_color = body_layout_node->style().property(CSS::PropertyID::BackgroundColor);
  84. if (!background_color.has_value() || !background_color.value()->is_color())
  85. return Color::White;
  86. return background_color.value()->to_color(*this);
  87. }
  88. URL Document::complete_url(const String& string) const
  89. {
  90. URL url(string);
  91. if (url.is_valid())
  92. return url;
  93. FileSystemPath fspath(m_url.path());
  94. StringBuilder builder;
  95. builder.append('/');
  96. bool document_url_ends_in_slash = m_url.path()[m_url.path().length() - 1] == '/';
  97. for (int i = 0; i < fspath.parts().size(); ++i) {
  98. if (i == fspath.parts().size() - 1 && !document_url_ends_in_slash)
  99. break;
  100. builder.append(fspath.parts()[i]);
  101. builder.append('/');
  102. }
  103. builder.append(string);
  104. auto built = builder.to_string();
  105. fspath = FileSystemPath(built);
  106. url = m_url;
  107. url.set_path(fspath.string());
  108. return url;
  109. }
  110. void Document::invalidate_layout()
  111. {
  112. if (on_invalidate_layout)
  113. on_invalidate_layout();
  114. }
  115. RefPtr<LayoutNode> Document::create_layout_node(const StyleResolver&, const StyleProperties*) const
  116. {
  117. return adopt(*new LayoutDocument(*this, StyleProperties::create()));
  118. }
  119. void Document::set_link_color(Color color)
  120. {
  121. m_link_color = color;
  122. }
  123. void Document::set_active_link_color(Color color)
  124. {
  125. m_active_link_color = color;
  126. }
  127. void Document::set_visited_link_color(Color color)
  128. {
  129. m_visited_link_color = color;
  130. }