Document.cpp 3.8 KB

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