OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "core/layout/ng/ng_block_layout_algorithm.h" | 5 #include "core/layout/ng/ng_block_layout_algorithm.h" |
6 | 6 |
7 #include "core/dom/NodeComputedStyle.h" | 7 #include "core/dom/NodeComputedStyle.h" |
8 #include "core/dom/TagCollection.h" | 8 #include "core/dom/TagCollection.h" |
9 #include "core/layout/ng/layout_ng_block_flow.h" | 9 #include "core/layout/ng/layout_ng_block_flow.h" |
10 #include "core/layout/ng/ng_block_node.h" | 10 #include "core/layout/ng/ng_block_node.h" |
(...skipping 797 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
808 EXPECT_EQ(LayoutUnit(kPaddingLeft + 10), child->LeftOffset()); | 808 EXPECT_EQ(LayoutUnit(kPaddingLeft + 10), child->LeftOffset()); |
809 EXPECT_EQ(LayoutUnit(0), child->TopOffset()); | 809 EXPECT_EQ(LayoutUnit(0), child->TopOffset()); |
810 } | 810 } |
811 | 811 |
812 // Verifies that floats can be correctly positioned if they are inside of nested | 812 // Verifies that floats can be correctly positioned if they are inside of nested |
813 // empty blocks. | 813 // empty blocks. |
814 TEST_F(NGBlockLayoutAlgorithmTest, PositionFloatInsideEmptyBlocks) { | 814 TEST_F(NGBlockLayoutAlgorithmTest, PositionFloatInsideEmptyBlocks) { |
815 setBodyInnerHTML(R"HTML( | 815 setBodyInnerHTML(R"HTML( |
816 <style> | 816 <style> |
817 #container { | 817 #container { |
818 height: 200px; | 818 height: 300px; |
819 width: 200px; | 819 width: 300px; |
| 820 outline: blue solid; |
820 } | 821 } |
821 #empty1 { | 822 #empty1 { |
822 margin: 20px; | 823 margin: 20px; |
823 padding: 0 20px; | 824 padding: 0 20px; |
824 } | 825 } |
825 #empty2 { | 826 #empty2 { |
826 margin: 15px; | 827 margin: 15px; |
827 padding: 0 15px; | 828 padding: 0 15px; |
828 } | 829 } |
829 #float { | 830 #left-float { |
830 float: left; | 831 float: left; |
831 height: 5px; | 832 height: 5px; |
832 width: 5px; | 833 width: 5px; |
833 padding: 10px; | 834 padding: 10px; |
834 margin: 10px; | 835 margin: 10px; |
835 background-color: green; | 836 background-color: green; |
836 } | 837 } |
| 838 #right-float { |
| 839 float: right; |
| 840 height: 15px; |
| 841 width: 15px; |
| 842 margin: 15px 10px; |
| 843 background-color: red; |
| 844 } |
837 </style> | 845 </style> |
838 <div id='container'> | 846 <div id='container'> |
839 <div id='empty1'> | 847 <div id='empty1'> |
840 <div id='empty2'> | 848 <div id='empty2'> |
841 <div id='float'></div> | 849 <div id='left-float'></div> |
| 850 <div id='right-float'></div> |
842 </div> | 851 </div> |
843 </div> | 852 </div> |
844 </div> | 853 </div> |
845 )HTML"); | 854 )HTML"); |
846 | 855 |
847 // ** Run LayoutNG algorithm ** | 856 // ** Run LayoutNG algorithm ** |
848 NGConstraintSpace* space; | 857 NGConstraintSpace* space; |
849 RefPtr<NGPhysicalBoxFragment> fragment; | 858 RefPtr<NGPhysicalBoxFragment> fragment; |
850 std::tie(fragment, space) = RunBlockLayoutAlgorithmForElement( | 859 std::tie(fragment, space) = RunBlockLayoutAlgorithmForElement( |
851 document().getElementsByTagName("html")->item(0)); | 860 document().getElementsByTagName("html")->item(0)); |
(...skipping 17 matching lines...) Expand all Loading... |
869 ASSERT_EQ(empty1_fragment->Children().size(), 1UL); | 878 ASSERT_EQ(empty1_fragment->Children().size(), 1UL); |
870 | 879 |
871 auto* empty2_fragment = | 880 auto* empty2_fragment = |
872 toNGPhysicalBoxFragment(empty1_fragment->Children()[0].get()); | 881 toNGPhysicalBoxFragment(empty1_fragment->Children()[0].get()); |
873 // 0, vertical margins got collapsed | 882 // 0, vertical margins got collapsed |
874 EXPECT_THAT(LayoutUnit(), empty2_fragment->TopOffset()); | 883 EXPECT_THAT(LayoutUnit(), empty2_fragment->TopOffset()); |
875 // 35 = empty1's padding(20) + empty2's padding(15) | 884 // 35 = empty1's padding(20) + empty2's padding(15) |
876 int empty2_inline_offset = 35; | 885 int empty2_inline_offset = 35; |
877 EXPECT_THAT(LayoutUnit(empty2_inline_offset), empty2_fragment->LeftOffset()); | 886 EXPECT_THAT(LayoutUnit(empty2_inline_offset), empty2_fragment->LeftOffset()); |
878 | 887 |
879 ASSERT_EQ(1UL, container_fragment->PositionedFloats().size()); | 888 ASSERT_EQ(2UL, container_fragment->PositionedFloats().size()); |
880 auto float_fragment = container_fragment->PositionedFloats().at(0)->fragment; | 889 RefPtr<NGPhysicalFragment> left_float_fragment = |
881 // 10 = float's padding | 890 container_fragment->PositionedFloats().at(0)->fragment; |
882 EXPECT_THAT(LayoutUnit(10), float_fragment->TopOffset()); | 891 // inline 25 = empty2's padding(15) + left float's margin(10) |
883 // 25 = empty2's padding(15) + float's padding(10) | 892 // block 10 = left float's margin |
884 int float_inline_offset = 25; | 893 EXPECT_THAT(NGPhysicalOffset(LayoutUnit(25), LayoutUnit(10)), |
885 EXPECT_THAT(float_fragment->LeftOffset(), LayoutUnit(float_inline_offset)); | 894 left_float_fragment->Offset()); |
| 895 |
| 896 auto right_float_fragment = |
| 897 container_fragment->PositionedFloats().at(1)->fragment; |
| 898 LayoutUnit right_float_offset = LayoutUnit(125); |
| 899 // inline offset 150 = empty2's padding(15) + right float's margin(10) + right |
| 900 // float offset(125) |
| 901 // block offset 15 = right float's margin |
| 902 EXPECT_THAT( |
| 903 NGPhysicalOffset(LayoutUnit(25) + right_float_offset, LayoutUnit(15)), |
| 904 right_float_fragment->Offset()); |
886 | 905 |
887 // ** Verify layout tree ** | 906 // ** Verify layout tree ** |
888 Element* left_float = document().getElementById("float"); | 907 Element* left_float = document().getElementById("left-float"); |
889 // 88 = body's margin(8) + | 908 // 88 = body's margin(8) + |
890 // empty1's padding and margin + empty2's padding and margins + float's | 909 // empty1's padding and margin + empty2's padding and margins + float's |
891 // padding | 910 // padding |
892 EXPECT_THAT(left_float->offsetLeft(), 88); | 911 EXPECT_THAT(left_float->offsetLeft(), 88); |
893 // 30 = body_top_offset(collapsed margins result) + float's padding | 912 // 30 = body_top_offset(collapsed margins result) + float's padding |
894 EXPECT_THAT(body_top_offset + 10, left_float->offsetTop()); | 913 EXPECT_THAT(body_top_offset + 10, left_float->offsetTop()); |
895 | 914 |
896 // ** Legacy Floating objects ** | 915 // ** Legacy Floating objects ** |
897 Element* body = document().getElementsByTagName("body")->item(0); | 916 // #container is the 1st non-empty block so floats are attached to it. |
| 917 Element* container = document().getElementById("container"); |
898 auto& floating_objects = | 918 auto& floating_objects = |
899 const_cast<FloatingObjects*>( | 919 const_cast<FloatingObjects*>( |
900 toLayoutBlockFlow(body->layoutObject())->floatingObjects()) | 920 toLayoutBlockFlow(container->layoutObject())->floatingObjects()) |
901 ->mutableSet(); | 921 ->mutableSet(); |
902 ASSERT_EQ(1UL, floating_objects.size()); | 922 ASSERT_EQ(2UL, floating_objects.size()); |
903 auto floating_object = floating_objects.takeFirst(); | 923 auto left_floating_object = floating_objects.takeFirst(); |
904 ASSERT_TRUE(floating_object->isPlaced()); | 924 ASSERT_TRUE(left_floating_object->isPlaced()); |
905 // 80 = float_inline_offset(25) + accumulative offset of empty blocks(35 + 20) | 925 // 80 = float_inline_offset(25) + accumulative offset of empty blocks(35 + 20) |
906 EXPECT_THAT(LayoutUnit(80), floating_object->x()); | 926 EXPECT_THAT(LayoutUnit(80), left_floating_object->x()); |
907 // 10 = float's padding | 927 // 10 = left float's margin |
908 EXPECT_THAT(LayoutUnit(10), floating_object->y()); | 928 EXPECT_THAT(LayoutUnit(10), left_floating_object->y()); |
| 929 |
| 930 auto right_floating_object = floating_objects.takeFirst(); |
| 931 ASSERT_TRUE(right_floating_object->isPlaced()); |
| 932 // 205 = float_inline_offset(25) + |
| 933 // accumulative offset of empty blocks(35 + 20) |
| 934 // + right float offset(125) |
| 935 EXPECT_THAT(LayoutUnit(80) + right_float_offset, right_floating_object->x()); |
| 936 // 15 = right float's margin |
| 937 EXPECT_THAT(LayoutUnit(15), right_floating_object->y()); |
909 } | 938 } |
910 | 939 |
911 // Verifies that left/right floating and regular blocks can be positioned | 940 // Verifies that left/right floating and regular blocks can be positioned |
912 // correctly by the algorithm. | 941 // correctly by the algorithm. |
913 TEST_F(NGBlockLayoutAlgorithmTest, PositionFloatFragments) { | 942 TEST_F(NGBlockLayoutAlgorithmTest, PositionFloatFragments) { |
914 setBodyInnerHTML(R"HTML( | 943 setBodyInnerHTML(R"HTML( |
915 <style> | 944 <style> |
916 #container { | 945 #container { |
917 height: 200px; | 946 height: 200px; |
918 width: 200px; | 947 width: 200px; |
(...skipping 1108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2027 #empty-block2 { | 2056 #empty-block2 { |
2028 margin-top: 50px; | 2057 margin-top: 50px; |
2029 } | 2058 } |
2030 </style> | 2059 </style> |
2031 <div id="container"> | 2060 <div id="container"> |
2032 <div id="left-float"></div> | 2061 <div id="left-float"></div> |
2033 <div id="empty-block1"></div> | 2062 <div id="empty-block1"></div> |
2034 <div id="empty-block2"></div> | 2063 <div id="empty-block2"></div> |
2035 </div> | 2064 </div> |
2036 )HTML"); | 2065 )HTML"); |
2037 Element* body = document().getElementsByTagName("body")->item(0); | 2066 // #container is the new parent for our float because it's height != 0. |
| 2067 Element* container = document().getElementById("container"); |
2038 auto& floating_objects = | 2068 auto& floating_objects = |
2039 const_cast<FloatingObjects*>( | 2069 const_cast<FloatingObjects*>( |
2040 toLayoutBlockFlow(body->layoutObject())->floatingObjects()) | 2070 toLayoutBlockFlow(container->layoutObject())->floatingObjects()) |
2041 ->mutableSet(); | 2071 ->mutableSet(); |
2042 ASSERT_EQ(1UL, floating_objects.size()); | 2072 ASSERT_EQ(1UL, floating_objects.size()); |
2043 auto floating_object = floating_objects.takeFirst(); | 2073 auto floating_object = floating_objects.takeFirst(); |
2044 // left-float's margin = 15. | 2074 // left-float's margin = 15. |
2045 EXPECT_THAT(LayoutUnit(15), floating_object->x()); | 2075 EXPECT_THAT(LayoutUnit(15), floating_object->x()); |
2046 EXPECT_THAT(LayoutUnit(15), floating_object->y()); | 2076 EXPECT_THAT(LayoutUnit(15), floating_object->y()); |
2047 | 2077 |
2048 RefPtr<const NGPhysicalBoxFragment> html_fragment; | 2078 RefPtr<const NGPhysicalBoxFragment> html_fragment; |
2049 std::tie(html_fragment, std::ignore) = RunBlockLayoutAlgorithmForElement( | 2079 std::tie(html_fragment, std::ignore) = RunBlockLayoutAlgorithmForElement( |
2050 document().getElementsByTagName("html")->item(0)); | 2080 document().getElementsByTagName("html")->item(0)); |
2051 auto* body_fragment = | 2081 auto* body_fragment = |
2052 toNGPhysicalBoxFragment(html_fragment->Children()[0].get()); | 2082 toNGPhysicalBoxFragment(html_fragment->Children()[0].get()); |
2053 auto* container_fragment = | 2083 auto* container_fragment = |
2054 toNGPhysicalBoxFragment(body_fragment->Children()[0].get()); | 2084 toNGPhysicalBoxFragment(body_fragment->Children()[0].get()); |
2055 auto* empty_block1 = | 2085 auto* empty_block1 = |
2056 toNGPhysicalBoxFragment(container_fragment->Children()[0].get()); | 2086 toNGPhysicalBoxFragment(container_fragment->Children()[0].get()); |
2057 // empty-block1's margin == 8 | 2087 // empty-block1's margin == 8 |
2058 EXPECT_THAT(NGPhysicalOffset(LayoutUnit(8), LayoutUnit(8)), | 2088 EXPECT_THAT(NGPhysicalOffset(LayoutUnit(8), LayoutUnit(8)), |
2059 empty_block1->Offset()); | 2089 empty_block1->Offset()); |
2060 | 2090 |
2061 auto* empty_block2 = | 2091 auto* empty_block2 = |
2062 toNGPhysicalBoxFragment(container_fragment->Children()[1].get()); | 2092 toNGPhysicalBoxFragment(container_fragment->Children()[1].get()); |
2063 // empty-block2's margin == 50 | 2093 // empty-block2's margin == 50 |
2064 EXPECT_THAT(NGPhysicalOffset(LayoutUnit(0), LayoutUnit(50)), | 2094 EXPECT_THAT(NGPhysicalOffset(LayoutUnit(0), LayoutUnit(50)), |
2065 empty_block2->Offset()); | 2095 empty_block2->Offset()); |
2066 } | 2096 } |
2067 | 2097 |
2068 } // namespace | 2098 } // namespace |
2069 } // namespace blink | 2099 } // namespace blink |
OLD | NEW |