Node.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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_node(const StyleResolver& resolver, const StyleProperties* parent_properties) const
  20. {
  21. if (is_document())
  22. return adopt(*new LayoutDocument(static_cast<const Document&>(*this), StyleProperties::create()));
  23. if (is_text())
  24. return adopt(*new LayoutText(static_cast<const Text&>(*this)));
  25. auto style_properties = resolver.resolve_style(static_cast<const Element&>(*this), parent_properties);
  26. auto display_property = style_properties->property("display");
  27. String display = display_property.has_value() ? display_property.release_value()->to_string() : "inline";
  28. if (display == "none")
  29. return nullptr;
  30. if (display == "block" || display == "list-item")
  31. return adopt(*new LayoutBlock(this, move(style_properties)));
  32. if (display == "inline")
  33. return adopt(*new LayoutInline(*this, move(style_properties)));
  34. ASSERT_NOT_REACHED();
  35. }
  36. RefPtr<LayoutNode> Node::create_layout_tree(const StyleResolver& resolver, const StyleProperties* parent_properties) const
  37. {
  38. auto layout_node = create_layout_node(resolver, parent_properties);
  39. if (!layout_node)
  40. return nullptr;
  41. if (!has_children())
  42. return layout_node;
  43. Vector<RefPtr<LayoutNode>> layout_children;
  44. bool have_inline_children = false;
  45. bool have_block_children = false;
  46. static_cast<const ParentNode&>(*this).for_each_child([&](const Node& child) {
  47. auto layout_child = child.create_layout_tree(resolver, &layout_node->style());
  48. if (!layout_child)
  49. return;
  50. if (!layout_child->is_block())
  51. have_inline_children = true;
  52. if (layout_child->is_block())
  53. have_block_children = true;
  54. layout_children.append(move(layout_child));
  55. });
  56. for (auto layout_child : layout_children)
  57. if (have_block_children && have_inline_children && !layout_child->is_block()) {
  58. if (layout_child->is_text() && static_cast<const LayoutText&>(*layout_child).text_for_style(*parent_properties) == " ")
  59. continue;
  60. layout_node->inline_wrapper().append_child(*layout_child);
  61. } else {
  62. layout_node->append_child(*layout_child);
  63. }
  64. return layout_node;
  65. }
  66. const HTMLAnchorElement* Node::enclosing_link_element() const
  67. {
  68. if (is_element() && tag_name().to_lowercase() == "a")
  69. return static_cast<const HTMLAnchorElement*>(this);
  70. return parent() ? parent()->enclosing_link_element() : nullptr;
  71. }
  72. const HTMLElement* Node::enclosing_html_element() const
  73. {
  74. if (is_html_element())
  75. return static_cast<const HTMLElement*>(this);
  76. return parent() ? parent()->enclosing_html_element() : nullptr;
  77. }
  78. String Node::text_content() const
  79. {
  80. Vector<String> strings;
  81. StringBuilder builder;
  82. for (auto* child = first_child(); child; child = child->next_sibling()) {
  83. auto text = child->text_content();
  84. if (!text.is_empty()) {
  85. builder.append(child->text_content());
  86. builder.append(' ');
  87. }
  88. }
  89. if (builder.length() > 1)
  90. builder.trim(1);
  91. return builder.to_string();
  92. }