Node.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include <LibHTML/DOM/Node.h>
  2. #include <LibHTML/DOM/Element.h>
  3. #include <LibHTML/DOM/HTMLAnchorElement.h>
  4. #include <LibHTML/CSS/StyleResolver.h>
  5. #include <LibHTML/Layout/LayoutNode.h>
  6. #include <LibHTML/Layout/LayoutBlock.h>
  7. #include <LibHTML/Layout/LayoutDocument.h>
  8. #include <LibHTML/Layout/LayoutInline.h>
  9. #include <LibHTML/Layout/LayoutText.h>
  10. Node::Node(Document& document, NodeType type)
  11. : m_document(document)
  12. , m_type(type)
  13. {
  14. }
  15. Node::~Node()
  16. {
  17. }
  18. RefPtr<LayoutNode> Node::create_layout_node(const StyleResolver& resolver, const StyleProperties* parent_properties) const
  19. {
  20. if (is_document())
  21. return adopt(*new LayoutDocument(static_cast<const Document&>(*this), {}));
  22. auto style_properties = resolver.resolve_style(static_cast<const Element&>(*this), parent_properties);
  23. auto display_property = style_properties.property("display");
  24. String display = display_property.has_value() ? display_property.release_value()->to_string() : "inline";
  25. if (is_text())
  26. return adopt(*new LayoutText(static_cast<const Text&>(*this), move(style_properties)));
  27. if (display == "none")
  28. return nullptr;
  29. if (display == "block" || display == "list-item")
  30. return adopt(*new LayoutBlock(this, move(style_properties)));
  31. if (display == "inline")
  32. return adopt(*new LayoutInline(*this, move(style_properties)));
  33. ASSERT_NOT_REACHED();
  34. }
  35. RefPtr<LayoutNode> Node::create_layout_tree(const StyleResolver& resolver, const StyleProperties* parent_properties) const
  36. {
  37. auto layout_node = create_layout_node(resolver, parent_properties);
  38. if (!layout_node)
  39. return nullptr;
  40. if (!has_children())
  41. return layout_node;
  42. Vector<RefPtr<LayoutNode>> layout_children;
  43. bool have_inline_children = false;
  44. bool have_block_children = false;
  45. static_cast<const ParentNode&>(*this).for_each_child([&](const Node& child) {
  46. auto layout_child = child.create_layout_tree(resolver, &layout_node->style_properties());
  47. if (!layout_child)
  48. return;
  49. if (!layout_child->is_block())
  50. have_inline_children = true;
  51. if (layout_child->is_block())
  52. have_block_children = true;
  53. layout_children.append(move(layout_child));
  54. });
  55. for (auto layout_child : layout_children)
  56. if (have_block_children && have_inline_children && !layout_child->is_block()) {
  57. if (layout_child->is_text() && static_cast<const LayoutText&>(*layout_child).text() == " ")
  58. continue;
  59. layout_node->inline_wrapper().append_child(*layout_child);
  60. } else {
  61. layout_node->append_child(*layout_child);
  62. }
  63. return layout_node;
  64. }
  65. const HTMLAnchorElement* Node::enclosing_link_element() const
  66. {
  67. if (is_element() && tag_name().to_lowercase() == "a")
  68. return static_cast<const HTMLAnchorElement*>(this);
  69. return parent() ? parent()->enclosing_link_element() : nullptr;
  70. }
  71. const HTMLElement* Node::enclosing_html_element() const
  72. {
  73. if (is_html_element())
  74. return static_cast<const HTMLElement*>(this);
  75. return parent() ? parent()->enclosing_html_element() : nullptr;
  76. }