LayoutTreeBuilder.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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/LayoutNode.h>
  29. #include <LibWeb/Layout/LayoutTable.h>
  30. #include <LibWeb/Layout/LayoutText.h>
  31. #include <LibWeb/Layout/LayoutTreeBuilder.h>
  32. namespace Web {
  33. LayoutTreeBuilder::LayoutTreeBuilder()
  34. {
  35. }
  36. static RefPtr<LayoutNode> create_layout_tree(Node& node, const StyleProperties* parent_style)
  37. {
  38. auto layout_node = node.create_layout_node(parent_style);
  39. if (!layout_node)
  40. return nullptr;
  41. if (!node.has_children())
  42. return layout_node;
  43. NonnullRefPtrVector<LayoutNode> layout_children;
  44. bool have_inline_children = false;
  45. bool have_noninline_children = false;
  46. to<ParentNode>(node).for_each_child([&](Node& child) {
  47. auto layout_child = create_layout_tree(child, &layout_node->style());
  48. if (!layout_child)
  49. return;
  50. if (layout_child->is_inline())
  51. have_inline_children = true;
  52. else
  53. have_noninline_children = true;
  54. layout_children.append(layout_child.release_nonnull());
  55. });
  56. for (auto& layout_child : layout_children) {
  57. if (have_noninline_children && have_inline_children && layout_child.is_inline()) {
  58. if (is<LayoutText>(layout_child) && to<LayoutText>(layout_child).text_for_style(*parent_style) == " ")
  59. continue;
  60. layout_node->inline_wrapper().append_child(layout_child);
  61. } else {
  62. layout_node->append_child(layout_child);
  63. }
  64. }
  65. if (have_inline_children && !have_noninline_children)
  66. layout_node->set_children_are_inline(true);
  67. return layout_node;
  68. }
  69. RefPtr<LayoutNode> LayoutTreeBuilder::build(Node& node)
  70. {
  71. if (!is<Document>(node) && node.has_children()) {
  72. dbg() << "FIXME: Support building partial layout trees.";
  73. return nullptr;
  74. }
  75. return create_layout_tree(node, nullptr);
  76. }
  77. }