Node.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. const HTMLAnchorElement* Node::enclosing_link_element() const
  20. {
  21. return first_ancestor_of_type<HTMLAnchorElement>();
  22. }
  23. const HTMLElement* Node::enclosing_html_element() const
  24. {
  25. return first_ancestor_of_type<HTMLElement>();
  26. }
  27. String Node::text_content() const
  28. {
  29. Vector<String> strings;
  30. StringBuilder builder;
  31. for (auto* child = first_child(); child; child = child->next_sibling()) {
  32. auto text = child->text_content();
  33. if (!text.is_empty()) {
  34. builder.append(child->text_content());
  35. builder.append(' ');
  36. }
  37. }
  38. if (builder.length() > 1)
  39. builder.trim(1);
  40. return builder.to_string();
  41. }
  42. const Element* Node::next_element_sibling() const
  43. {
  44. for (auto* node = next_sibling(); node; node = node->next_sibling()) {
  45. if (node->is_element())
  46. return static_cast<const Element*>(node);
  47. }
  48. return nullptr;
  49. }
  50. const Element* Node::previous_element_sibling() const
  51. {
  52. for (auto* node = previous_sibling(); node; node = node->previous_sibling()) {
  53. if (node->is_element())
  54. return static_cast<const Element*>(node);
  55. }
  56. return nullptr;
  57. }
  58. RefPtr<LayoutNode> Node::create_layout_node(const StyleProperties*) const
  59. {
  60. return nullptr;
  61. }
  62. void Node::invalidate_style()
  63. {
  64. for (auto* node = this; node; node = node->parent()) {
  65. if (is<Element>(*node))
  66. to<Element>(*node).recompute_style();
  67. }
  68. }