Document.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include <LibHTML/CSS/StyleResolver.h>
  2. #include <LibHTML/DOM/Document.h>
  3. #include <LibHTML/DOM/Element.h>
  4. #include <LibHTML/DOM/HTMLHeadElement.h>
  5. #include <LibHTML/DOM/HTMLHtmlElement.h>
  6. #include <LibHTML/DOM/HTMLTitleElement.h>
  7. #include <LibHTML/Layout/LayoutDocument.h>
  8. #include <stdio.h>
  9. Document::Document()
  10. : ParentNode(*this, NodeType::DOCUMENT_NODE)
  11. {
  12. }
  13. Document::~Document()
  14. {
  15. }
  16. StyleResolver& Document::style_resolver()
  17. {
  18. if (!m_style_resolver)
  19. m_style_resolver = make<StyleResolver>(*this);
  20. return *m_style_resolver;
  21. }
  22. void Document::normalize()
  23. {
  24. if (first_child() != nullptr && first_child()->is_element()) {
  25. const Element& el = static_cast<const Element&>(*first_child());
  26. if (el.tag_name() == "html")
  27. return;
  28. }
  29. NonnullRefPtr<Element> body = adopt(*new Element(*this, "body"));
  30. NonnullRefPtr<Element> html = adopt(*new Element(*this, "html"));
  31. html->append_child(body);
  32. this->donate_all_children_to(body);
  33. this->append_child(html);
  34. }
  35. const HTMLHtmlElement* Document::document_element() const
  36. {
  37. return static_cast<const HTMLHtmlElement*>(first_child_with_tag_name("html"));
  38. }
  39. const HTMLHeadElement* Document::head() const
  40. {
  41. auto* html = document_element();
  42. if (!html)
  43. return nullptr;
  44. return static_cast<const HTMLHeadElement*>(html->first_child_with_tag_name("head"));
  45. }
  46. String Document::title() const
  47. {
  48. auto* head_element = head();
  49. if (!head_element)
  50. return {};
  51. auto* title_element = static_cast<const HTMLTitleElement*>(head_element->first_child_with_tag_name("title"));
  52. if (!title_element)
  53. return {};
  54. return title_element->text_content();
  55. }