OLD | NEW |
| (Empty) |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "core/layout/ng/ng_base_layout_algorithm_test.h" | |
6 | |
7 #include "core/dom/TagCollection.h" | |
8 #include "core/layout/line/InlineTextBox.h" | |
9 #include "core/layout/ng/ng_inline_node.h" | |
10 #include "core/layout/ng/ng_physical_line_box_fragment.h" | |
11 #include "core/layout/ng/ng_physical_text_fragment.h" | |
12 #include "platform/geometry/LayoutPoint.h" | |
13 #include "platform/geometry/LayoutRect.h" | |
14 | |
15 namespace blink { | |
16 namespace { | |
17 | |
18 class NGTextLayoutAlgorithmTest : public NGBaseLayoutAlgorithmTest {}; | |
19 | |
20 // Verifies that text can flow correctly around floats that were positioned | |
21 // before the inline block. | |
22 TEST_F(NGTextLayoutAlgorithmTest, TextFloatsAroundFloatsBefore) { | |
23 setBodyInnerHTML(R"HTML( | |
24 <!DOCTYPE html> | |
25 <style> | |
26 * { | |
27 font-family: "Arial", sans-serif; | |
28 font-size: 19px; | |
29 } | |
30 #container { | |
31 height: 200px; width: 200px; outline: solid blue; | |
32 } | |
33 #left-float1 { | |
34 float: left; width: 30px; height: 30px; background-color: blue; | |
35 } | |
36 #left-float2 { | |
37 float: left; width: 10px; height: 10px; | |
38 background-color: purple; | |
39 } | |
40 #right-float { | |
41 float: right; width: 40px; height: 40px; background-color: yellow; | |
42 } | |
43 </style> | |
44 <div id="container"> | |
45 <div id="left-float1"></div> | |
46 <div id="left-float2"></div> | |
47 <div id="right-float"></div> | |
48 <span id="text">The quick brown fox jumps over the lazy dog</span> | |
49 </div> | |
50 )HTML"); | |
51 // ** Run LayoutNG algorithm ** | |
52 RefPtr<NGConstraintSpace> space; | |
53 RefPtr<NGPhysicalBoxFragment> html_fragment; | |
54 std::tie(html_fragment, space) = RunBlockLayoutAlgorithmForElement( | |
55 document().getElementsByTagName("html")->item(0)); | |
56 auto* body_fragment = | |
57 toNGPhysicalBoxFragment(html_fragment->Children()[0].get()); | |
58 auto* container_fragment = | |
59 toNGPhysicalBoxFragment(body_fragment->Children()[0].get()); | |
60 auto* line_box_fragments_wrapper = | |
61 toNGPhysicalBoxFragment(container_fragment->Children()[0].get()); | |
62 Vector<NGPhysicalTextFragment*> text_fragments; | |
63 for (const auto& child : line_box_fragments_wrapper->Children()) { | |
64 auto* line_box = toNGPhysicalLineBoxFragment(child.get()); | |
65 EXPECT_EQ(1u, line_box->Children().size()); | |
66 for (const auto& text : line_box->Children()) | |
67 text_fragments.push_back(toNGPhysicalTextFragment(text.get())); | |
68 } | |
69 | |
70 LayoutText* layout_text = | |
71 toLayoutText(getLayoutObjectByElementId("text")->slowFirstChild()); | |
72 ASSERT(layout_text->hasTextBoxes()); | |
73 | |
74 ASSERT_EQ(4UL, text_fragments.size()); | |
75 | |
76 auto* text_fragment1 = text_fragments[0]; | |
77 // 40 = #left-float1' width 30 + #left-float2 10 | |
78 EXPECT_EQ(LayoutUnit(40), text_fragment1->LeftOffset()); | |
79 EXPECT_EQ("The quick ", text_fragment1->Text()); | |
80 InlineTextBox* inline_text_box1 = layout_text->firstTextBox(); | |
81 EXPECT_EQ(LayoutUnit(40), inline_text_box1->x()); | |
82 | |
83 auto* text_fragment2 = text_fragments[1]; | |
84 // 40 = #left-float1' width 30 | |
85 EXPECT_EQ(LayoutUnit(30), text_fragment2->LeftOffset()); | |
86 EXPECT_EQ("brown fox ", text_fragment2->Text()); | |
87 InlineTextBox* inline_text_box2 = inline_text_box1->nextTextBox(); | |
88 EXPECT_EQ(LayoutUnit(30), inline_text_box2->x()); | |
89 | |
90 auto* text_fragment3 = text_fragments[2]; | |
91 EXPECT_EQ(LayoutUnit(), text_fragment3->LeftOffset()); | |
92 EXPECT_EQ("jumps over the lazy ", text_fragment3->Text()); | |
93 InlineTextBox* inline_text_box3 = inline_text_box2->nextTextBox(); | |
94 EXPECT_EQ(LayoutUnit(), inline_text_box3->x()); | |
95 } | |
96 | |
97 // Verifies that text correctly flows around the inline float that fits on | |
98 // the same text line. | |
99 TEST_F(NGTextLayoutAlgorithmTest, TextFloatsAroundInlineFloatThatFitsOnLine) { | |
100 setBodyInnerHTML(R"HTML( | |
101 <!DOCTYPE html> | |
102 <style> | |
103 * { | |
104 font-family: "Arial", sans-serif; | |
105 font-size: 19px; | |
106 } | |
107 #container { | |
108 height: 200px; width: 200px; outline: solid orange; | |
109 } | |
110 #narrow-float { | |
111 float: left; width: 30px; height: 30px; background-color: blue; | |
112 } | |
113 </style> | |
114 <div id="container"> | |
115 <span id="text"> | |
116 The quick <div id="narrow-float"></div> brown fox jumps over the lazy | |
117 </span> | |
118 </div> | |
119 )HTML"); | |
120 LayoutText* layout_text = | |
121 toLayoutText(getLayoutObjectByElementId("text")->slowFirstChild()); | |
122 ASSERT(layout_text->hasTextBoxes()); | |
123 | |
124 InlineTextBox* inline_text_box1 = layout_text->firstTextBox(); | |
125 // 30 == narrow-float's width. | |
126 EXPECT_EQ(LayoutUnit(30), inline_text_box1->x()); | |
127 | |
128 Element* narrow_float = document().getElementById("narrow-float"); | |
129 // 8 == body's margin. | |
130 EXPECT_EQ(8, narrow_float->offsetLeft()); | |
131 EXPECT_EQ(8, narrow_float->offsetTop()); | |
132 } | |
133 | |
134 // Verifies that the inline float got pushed to the next line if it doesn't | |
135 // fit the current line. | |
136 TEST_F(NGTextLayoutAlgorithmTest, | |
137 TextFloatsAroundInlineFloatThatDoesNotFitOnLine) { | |
138 setBodyInnerHTML(R"HTML( | |
139 <!DOCTYPE html> | |
140 <style> | |
141 * { | |
142 font-family: "Arial", sans-serif; | |
143 font-size: 19px; | |
144 } | |
145 #container { | |
146 height: 200px; width: 200px; outline: solid orange; | |
147 } | |
148 #wide-float { | |
149 float: left; width: 160px; height: 30px; background-color: red; | |
150 } | |
151 </style> | |
152 <div id="container"> | |
153 <span id="text"> | |
154 The quick <div id="wide-float"></div> brown fox jumps over the lazy dog | |
155 </span> | |
156 </div> | |
157 )HTML"); | |
158 LayoutText* layout_text = | |
159 toLayoutText(getLayoutObjectByElementId("text")->slowFirstChild()); | |
160 ASSERT(layout_text->hasTextBoxes()); | |
161 | |
162 InlineTextBox* inline_text_box1 = layout_text->firstTextBox(); | |
163 EXPECT_EQ(LayoutUnit(), inline_text_box1->x()); | |
164 | |
165 Element* wide_float = document().getElementById("wide-float"); | |
166 // 8 == body's margin. | |
167 EXPECT_EQ(8, wide_float->offsetLeft()); | |
168 } | |
169 | |
170 // Verifies that if an inline float pushed to the next line then all others | |
171 // following inline floats positioned with respect to the float's top edge | |
172 // alignment rule. | |
173 TEST_F(NGTextLayoutAlgorithmTest, | |
174 FloatsArePositionedWithRespectToTopEdgeAlignmentRule) { | |
175 setBodyInnerHTML(R"HTML( | |
176 <!DOCTYPE html> | |
177 <style> | |
178 * { | |
179 font-family: "Arial", sans-serif; | |
180 font-size: 19px; | |
181 } | |
182 #container { | |
183 height: 200px; width: 200px; outline: solid orange; | |
184 } | |
185 #left-narrow { | |
186 float: left; width: 5px; height: 30px; background-color: blue; | |
187 } | |
188 #left-wide { | |
189 float: left; width: 160px; height: 30px; background-color: red; | |
190 } | |
191 </style> | |
192 <div id="container"> | |
193 <span id="text"> | |
194 The quick <div id="left-wide"></div> brown <div id="left-narrow"></div> | |
195 fox jumps over the lazy dog | |
196 </span> | |
197 </div> | |
198 )HTML"); | |
199 Element* wide_float = document().getElementById("left-wide"); | |
200 // 8 == body's margin. | |
201 EXPECT_EQ(8, wide_float->offsetLeft()); | |
202 | |
203 Element* narrow_float = document().getElementById("left-narrow"); | |
204 // 160 float-wide's width + 8 body's margin. | |
205 EXPECT_EQ(160 + 8, narrow_float->offsetLeft()); | |
206 | |
207 // On the same line. | |
208 EXPECT_EQ(wide_float->offsetTop(), narrow_float->offsetTop()); | |
209 } | |
210 | |
211 } // namespace | |
212 } // namespace blink | |
OLD | NEW |