Document.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. }
  74. Color Document::background_color() const
  75. {
  76. auto* body_element = body();
  77. if (!body_element)
  78. return Color::White;
  79. auto* body_layout_node = body_element->layout_node();
  80. if (!body_layout_node)
  81. return Color::White;
  82. auto background_color = body_layout_node->style().property(CSS::PropertyID::BackgroundColor);
  83. if (!background_color.has_value() || !background_color.value()->is_color())
  84. return Color::White;
  85. return background_color.value()->to_color(*this);
  86. }
  87. URL Document::complete_url(const String& string) const
  88. {
  89. URL url(string);
  90. if (url.is_valid())
  91. return url;
  92. FileSystemPath fspath(m_url.path());
  93. StringBuilder builder;
  94. builder.append('/');
  95. for (int i = 0; i < fspath.parts().size(); ++i) {
  96. if (i == fspath.parts().size() - 1)
  97. break;
  98. builder.append(fspath.parts()[i]);
  99. builder.append('/');
  100. }
  101. builder.append(string);
  102. auto built = builder.to_string();
  103. fspath = FileSystemPath(built);
  104. url = m_url;
  105. url.set_path(fspath.string());
  106. return url;
  107. }
  108. void Document::invalidate_layout()
  109. {
  110. if (on_invalidate_layout)
  111. on_invalidate_layout();
  112. }
  113. RefPtr<LayoutNode> Document::create_layout_node(const StyleResolver&, const StyleProperties*) const
  114. {
  115. return adopt(*new LayoutDocument(*this, StyleProperties::create()));
  116. }
  117. void Document::set_link_color(Color color)
  118. {
  119. m_link_color = color;
  120. }
  121. void Document::set_active_link_color(Color color)
  122. {
  123. m_active_link_color = color;
  124. }
  125. void Document::set_visited_link_color(Color color)
  126. {
  127. m_visited_link_color = color;
  128. }