Node.cpp 3.7 KB

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