Node.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include <AK/StringBuilder.h>
  2. #include <LibHTML/CSS/StyleResolver.h>
  3. #include <LibHTML/DOM/Element.h>
  4. #include <LibHTML/DOM/HTMLAnchorElement.h>
  5. #include <LibHTML/DOM/Node.h>
  6. #include <LibHTML/Layout/LayoutBlock.h>
  7. #include <LibHTML/Layout/LayoutDocument.h>
  8. #include <LibHTML/Layout/LayoutInline.h>
  9. #include <LibHTML/Layout/LayoutNode.h>
  10. #include <LibHTML/Layout/LayoutText.h>
  11. Node::Node(Document& document, NodeType type)
  12. : m_document(document)
  13. , m_type(type)
  14. {
  15. }
  16. Node::~Node()
  17. {
  18. }
  19. RefPtr<LayoutNode> Node::create_layout_tree(const StyleResolver& resolver, const StyleProperties* parent_properties) const
  20. {
  21. auto layout_node = create_layout_node(resolver, parent_properties);
  22. if (!layout_node)
  23. return nullptr;
  24. if (!has_children())
  25. return layout_node;
  26. Vector<RefPtr<LayoutNode>> layout_children;
  27. bool have_inline_children = false;
  28. bool have_block_children = false;
  29. static_cast<const ParentNode&>(*this).for_each_child([&](const Node& child) {
  30. auto layout_child = child.create_layout_tree(resolver, &layout_node->style());
  31. if (!layout_child)
  32. return;
  33. if (!layout_child->is_block())
  34. have_inline_children = true;
  35. if (layout_child->is_block())
  36. have_block_children = true;
  37. layout_children.append(move(layout_child));
  38. });
  39. for (auto layout_child : layout_children)
  40. if (have_block_children && have_inline_children && !layout_child->is_block()) {
  41. if (layout_child->is_text() && static_cast<const LayoutText&>(*layout_child).text_for_style(*parent_properties) == " ")
  42. continue;
  43. layout_node->inline_wrapper().append_child(*layout_child);
  44. } else {
  45. layout_node->append_child(*layout_child);
  46. }
  47. return layout_node;
  48. }
  49. const HTMLAnchorElement* Node::enclosing_link_element() const
  50. {
  51. if (is<HTMLAnchorElement>(*this))
  52. return static_cast<const HTMLAnchorElement*>(this);
  53. return parent() ? parent()->enclosing_link_element() : nullptr;
  54. }
  55. const HTMLElement* Node::enclosing_html_element() const
  56. {
  57. if (is_html_element())
  58. return static_cast<const HTMLElement*>(this);
  59. return parent() ? parent()->enclosing_html_element() : nullptr;
  60. }
  61. String Node::text_content() const
  62. {
  63. Vector<String> strings;
  64. StringBuilder builder;
  65. for (auto* child = first_child(); child; child = child->next_sibling()) {
  66. auto text = child->text_content();
  67. if (!text.is_empty()) {
  68. builder.append(child->text_content());
  69. builder.append(' ');
  70. }
  71. }
  72. if (builder.length() > 1)
  73. builder.trim(1);
  74. return builder.to_string();
  75. }
  76. const Element* Node::next_element_sibling() const
  77. {
  78. for (auto* node = next_sibling(); node; node = node->next_sibling()) {
  79. if (node->is_element())
  80. return static_cast<const Element*>(node);
  81. }
  82. return nullptr;
  83. }
  84. const Element* Node::previous_element_sibling() const
  85. {
  86. for (auto* node = previous_sibling(); node; node = node->previous_sibling()) {
  87. if (node->is_element())
  88. return static_cast<const Element*>(node);
  89. }
  90. return nullptr;
  91. }