Document.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 (first_child() != nullptr && first_child()->is_element()) {
  29. const Element& el = static_cast<const Element&>(*first_child());
  30. if (el.tag_name() == "html")
  31. return;
  32. }
  33. NonnullRefPtr<Element> body = adopt(*new Element(*this, "body"));
  34. NonnullRefPtr<Element> html = adopt(*new Element(*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 static_cast<const HTMLHtmlElement*>(first_child_with_tag_name("html"));
  42. }
  43. const HTMLHeadElement* Document::head() const
  44. {
  45. auto* html = document_element();
  46. if (!html)
  47. return nullptr;
  48. return static_cast<const HTMLHeadElement*>(html->first_child_with_tag_name("head"));
  49. }
  50. const HTMLBodyElement* Document::body() const
  51. {
  52. auto* html = document_element();
  53. if (!html)
  54. return nullptr;
  55. return static_cast<const HTMLBodyElement*>(html->first_child_with_tag_name("body"));
  56. }
  57. String Document::title() const
  58. {
  59. auto* head_element = head();
  60. if (!head_element)
  61. return {};
  62. auto* title_element = static_cast<const HTMLTitleElement*>(head_element->first_child_with_tag_name("title"));
  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("background-color");
  83. if (!background_color.has_value() || !background_color.value()->is_color())
  84. return Color::White;
  85. return background_color.value()->to_color();
  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. RefPtr<LayoutNode> Document::create_layout_node(const StyleResolver&, const StyleProperties*) const
  109. {
  110. return adopt(*new LayoutDocument(*this, StyleProperties::create()));
  111. }