Document.cpp 3.5 KB

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