Document.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include <LibHTML/CSS/StyleResolver.h>
  2. #include <LibHTML/DOM/Document.h>
  3. #include <LibHTML/DOM/Element.h>
  4. #include <LibHTML/DOM/HTMLBodyElement.h>
  5. #include <LibHTML/DOM/HTMLHeadElement.h>
  6. #include <LibHTML/DOM/HTMLHtmlElement.h>
  7. #include <LibHTML/DOM/HTMLTitleElement.h>
  8. #include <LibHTML/Frame.h>
  9. #include <LibHTML/Layout/LayoutDocument.h>
  10. #include <stdio.h>
  11. Document::Document()
  12. : ParentNode(*this, NodeType::DOCUMENT_NODE)
  13. {
  14. }
  15. Document::~Document()
  16. {
  17. }
  18. StyleResolver& Document::style_resolver()
  19. {
  20. if (!m_style_resolver)
  21. m_style_resolver = make<StyleResolver>(*this);
  22. return *m_style_resolver;
  23. }
  24. void Document::normalize()
  25. {
  26. if (first_child() != nullptr && first_child()->is_element()) {
  27. const Element& el = static_cast<const Element&>(*first_child());
  28. if (el.tag_name() == "html")
  29. return;
  30. }
  31. NonnullRefPtr<Element> body = adopt(*new Element(*this, "body"));
  32. NonnullRefPtr<Element> html = adopt(*new Element(*this, "html"));
  33. html->append_child(body);
  34. this->donate_all_children_to(body);
  35. this->append_child(html);
  36. }
  37. const HTMLHtmlElement* Document::document_element() const
  38. {
  39. return static_cast<const HTMLHtmlElement*>(first_child_with_tag_name("html"));
  40. }
  41. const HTMLHeadElement* Document::head() const
  42. {
  43. auto* html = document_element();
  44. if (!html)
  45. return nullptr;
  46. return static_cast<const HTMLHeadElement*>(html->first_child_with_tag_name("head"));
  47. }
  48. const HTMLBodyElement* Document::body() const
  49. {
  50. auto* html = document_element();
  51. if (!html)
  52. return nullptr;
  53. return static_cast<const HTMLBodyElement*>(html->first_child_with_tag_name("body"));
  54. }
  55. String Document::title() const
  56. {
  57. auto* head_element = head();
  58. if (!head_element)
  59. return {};
  60. auto* title_element = static_cast<const HTMLTitleElement*>(head_element->first_child_with_tag_name("title"));
  61. if (!title_element)
  62. return {};
  63. return title_element->text_content();
  64. }
  65. void Document::attach_to_frame(Badge<Frame>, Frame& frame)
  66. {
  67. m_frame = frame.make_weak_ptr();
  68. }
  69. void Document::detach_from_frame(Badge<Frame>, Frame&)
  70. {
  71. }
  72. Color Document::background_color() const
  73. {
  74. auto* body_element = body();
  75. if (!body_element)
  76. return Color::White;
  77. auto* body_layout_node = body_element->layout_node();
  78. if (!body_layout_node)
  79. return Color::White;
  80. auto background_color = body_layout_node->style().property("background-color");
  81. if (!background_color.has_value() || !background_color.value()->is_color())
  82. return Color::White;
  83. return background_color.value()->to_color();
  84. }