TreeBuilder.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibWeb/DOM/Document.h>
  27. #include <LibWeb/DOM/ParentNode.h>
  28. #include <LibWeb/Layout/Node.h>
  29. #include <LibWeb/Layout/TableBox.h>
  30. #include <LibWeb/Layout/TextNode.h>
  31. #include <LibWeb/Layout/TreeBuilder.h>
  32. namespace Web::Layout {
  33. TreeBuilder::TreeBuilder()
  34. {
  35. }
  36. static NonnullRefPtr<CSS::StyleProperties> style_for_anonymous_block(Node& parent_box)
  37. {
  38. auto new_style = CSS::StyleProperties::create();
  39. parent_box.specified_style().for_each_property([&](auto property_id, auto& value) {
  40. if (CSS::StyleResolver::is_inherited_property(property_id))
  41. new_style->set_property(property_id, value);
  42. });
  43. return new_style;
  44. }
  45. static Layout::Node& insertion_parent_for_inline_node(Layout::Node& layout_parent, Layout::Node& layout_node)
  46. {
  47. if (layout_parent.is_inline())
  48. return layout_parent;
  49. if (!layout_parent.has_children() || layout_parent.children_are_inline())
  50. return layout_parent;
  51. // layout_parent has block children, insert into an anonymous wrapper block (and create it first if needed)
  52. if (!layout_parent.last_child()->is_anonymous() || !layout_parent.last_child()->children_are_inline()) {
  53. layout_parent.append_child(adopt(*new BlockBox(layout_node.document(), nullptr, style_for_anonymous_block(layout_parent))));
  54. }
  55. return *layout_parent.last_child();
  56. }
  57. static Layout::Node& insertion_parent_for_block_node(Layout::Node& layout_parent, Layout::Node& layout_node)
  58. {
  59. if (!layout_parent.has_children() || !layout_parent.children_are_inline())
  60. return layout_parent;
  61. // layout_parent has inline children, first move them into an anonymous wrapper block
  62. if (layout_parent.children_are_inline()) {
  63. NonnullRefPtrVector<Layout::Node> children;
  64. while (RefPtr<Layout::Node> child = layout_parent.first_child()) {
  65. layout_parent.remove_child(*child);
  66. children.append(child.release_nonnull());
  67. }
  68. layout_parent.append_child(adopt(*new BlockBox(layout_node.document(), nullptr, style_for_anonymous_block(layout_parent))));
  69. layout_parent.set_children_are_inline(false);
  70. for (auto& child : children) {
  71. layout_parent.last_child()->append_child(child);
  72. }
  73. layout_parent.last_child()->set_children_are_inline(true);
  74. }
  75. if (!layout_parent.last_child()->is_anonymous() || layout_parent.last_child()->children_are_inline()) {
  76. layout_parent.append_child(adopt(*new BlockBox(layout_node.document(), nullptr, style_for_anonymous_block(layout_parent))));
  77. }
  78. return *layout_parent.last_child();
  79. }
  80. void TreeBuilder::create_layout_tree(DOM::Node& dom_node)
  81. {
  82. // If the parent doesn't have a layout node, we don't need one either.
  83. if (dom_node.parent() && !dom_node.parent()->layout_node())
  84. return;
  85. const CSS::StyleProperties* parent_style = m_parent_stack.is_empty() ? nullptr : &m_parent_stack.last()->specified_style();
  86. auto layout_node = dom_node.create_layout_node(parent_style);
  87. if (!layout_node)
  88. return;
  89. // Discard empty whitespace nodes. This might not be ideal for correctness, but it does make the tree nicer.
  90. if (is<TextNode>(*layout_node) && downcast<TextNode>(*layout_node).text_for_style(*parent_style) == " ")
  91. return;
  92. if (!dom_node.parent()) {
  93. m_layout_root = layout_node;
  94. } else {
  95. if (layout_node->is_inline()) {
  96. // Inlines can be inserted into the nearest ancestor.
  97. auto& insertion_point = insertion_parent_for_inline_node(*m_parent_stack.last(), *layout_node);
  98. insertion_point.append_child(*layout_node);
  99. insertion_point.set_children_are_inline(true);
  100. } else {
  101. // Blocks can't be inserted into an inline parent, so find the nearest non-inline ancestor.
  102. auto& nearest_non_inline_ancestor = [&]() -> Layout::Node& {
  103. for (ssize_t i = m_parent_stack.size() - 1; i >= 0; --i) {
  104. if (!m_parent_stack[i]->is_inline())
  105. return *m_parent_stack[i];
  106. }
  107. ASSERT_NOT_REACHED();
  108. }();
  109. auto& insertion_point = insertion_parent_for_block_node(nearest_non_inline_ancestor, *layout_node);
  110. insertion_point.append_child(*layout_node);
  111. insertion_point.set_children_are_inline(false);
  112. }
  113. }
  114. if (dom_node.has_children() && layout_node->can_have_children()) {
  115. push_parent(*layout_node);
  116. downcast<DOM::ParentNode>(dom_node).for_each_child([&](auto& dom_child) {
  117. create_layout_tree(dom_child);
  118. });
  119. pop_parent();
  120. }
  121. }
  122. RefPtr<Node> TreeBuilder::build(DOM::Node& dom_node)
  123. {
  124. if (!is<DOM::Document>(dom_node) && dom_node.has_children()) {
  125. dbg() << "FIXME: Support building partial layout trees.";
  126. return nullptr;
  127. }
  128. create_layout_tree(dom_node);
  129. return move(m_layout_root);
  130. }
  131. }