| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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_base_layout_algorithm_test.h" | 5 #include "core/layout/ng/ng_base_layout_algorithm_test.h" |
| 6 | 6 |
| 7 #include "core/layout/ng/inline/ng_inline_layout_algorithm.h" | 7 #include "core/layout/ng/inline/ng_inline_layout_algorithm.h" |
| 8 #include "core/layout/ng/inline/ng_inline_node.h" | 8 #include "core/layout/ng/inline/ng_inline_node.h" |
| 9 #include "core/layout/ng/inline/ng_line_breaker.h" | 9 #include "core/layout/ng/inline/ng_line_breaker.h" |
| 10 #include "core/layout/ng/layout_ng_block_flow.h" | 10 #include "core/layout/ng/layout_ng_block_flow.h" |
| 11 #include "core/layout/ng/ng_constraint_space_builder.h" | 11 #include "core/layout/ng/ng_constraint_space_builder.h" |
| 12 #include "platform/wtf/text/StringBuilder.h" | 12 #include "platform/wtf/text/StringBuilder.h" |
| 13 | 13 |
| 14 namespace blink { | 14 namespace blink { |
| 15 | 15 |
| 16 class NGLineBreakerTest : public NGBaseLayoutAlgorithmTest { | 16 class NGLineBreakerTest : public NGBaseLayoutAlgorithmTest { |
| 17 protected: | 17 protected: |
| 18 NGInlineNode* CreateInlineNode(const String& html_content) { | 18 NGInlineNode CreateInlineNode(const String& html_content) { |
| 19 SetBodyInnerHTML(html_content); | 19 SetBodyInnerHTML(html_content); |
| 20 | 20 |
| 21 LayoutNGBlockFlow* block_flow = | 21 LayoutNGBlockFlow* block_flow = |
| 22 ToLayoutNGBlockFlow(GetLayoutObjectByElementId("container")); | 22 ToLayoutNGBlockFlow(GetLayoutObjectByElementId("container")); |
| 23 NGInlineNode* inline_node = | 23 return NGInlineNode(block_flow, block_flow->FirstChild()); |
| 24 new NGInlineNode(block_flow->FirstChild(), block_flow); | |
| 25 | |
| 26 return inline_node; | |
| 27 } | 24 } |
| 28 | 25 |
| 29 // Break lines using the specified available width. | 26 // Break lines using the specified available width. |
| 30 Vector<NGInlineItemResults> BreakLines(NGInlineNode* node, | 27 Vector<NGInlineItemResults> BreakLines(NGInlineNode node, |
| 31 LayoutUnit available_width) { | 28 LayoutUnit available_width) { |
| 32 if (!node->IsPrepareLayoutFinished()) | 29 DCHECK(node); |
| 33 node->PrepareLayout(); | 30 |
| 31 if (!node.IsPrepareLayoutFinished()) |
| 32 node.PrepareLayout(); |
| 34 | 33 |
| 35 RefPtr<NGConstraintSpace> space = | 34 RefPtr<NGConstraintSpace> space = |
| 36 NGConstraintSpaceBuilder(NGWritingMode::kHorizontalTopBottom) | 35 NGConstraintSpaceBuilder(NGWritingMode::kHorizontalTopBottom) |
| 37 .SetAvailableSize({available_width, NGSizeIndefinite}) | 36 .SetAvailableSize({available_width, NGSizeIndefinite}) |
| 38 .ToConstraintSpace(NGWritingMode::kHorizontalTopBottom); | 37 .ToConstraintSpace(NGWritingMode::kHorizontalTopBottom); |
| 39 NGLineBreaker line_breaker(node, space.Get()); | 38 NGLineBreaker line_breaker(node, space.Get()); |
| 40 NGInlineLayoutAlgorithm algorithm(node, space.Get()); | 39 NGInlineLayoutAlgorithm algorithm(node, space.Get()); |
| 41 Vector<NGInlineItemResults> lines; | 40 Vector<NGInlineItemResults> lines; |
| 42 while (true) { | 41 while (true) { |
| 43 NGInlineItemResults item_results; | 42 NGInlineItemResults item_results; |
| 44 line_breaker.NextLine(&item_results, &algorithm); | 43 line_breaker.NextLine(&item_results, &algorithm); |
| 45 if (item_results.IsEmpty()) | 44 if (item_results.IsEmpty()) |
| 46 break; | 45 break; |
| 47 lines.push_back(item_results); | 46 lines.push_back(item_results); |
| 48 } | 47 } |
| 49 return lines; | 48 return lines; |
| 50 } | 49 } |
| 51 }; | 50 }; |
| 52 | 51 |
| 53 namespace { | 52 namespace { |
| 54 | 53 |
| 55 String ToString(NGInlineItemResults line, NGInlineNode* node) { | 54 String ToString(NGInlineItemResults line, NGInlineNode node) { |
| 56 StringBuilder builder; | 55 StringBuilder builder; |
| 57 for (const auto& item_result : line) { | 56 for (const auto& item_result : line) { |
| 58 builder.Append( | 57 builder.Append(node.Text(item_result.start_offset, item_result.end_offset)); |
| 59 node->Text(item_result.start_offset, item_result.end_offset)); | |
| 60 } | 58 } |
| 61 return builder.ToString(); | 59 return builder.ToString(); |
| 62 } | 60 } |
| 63 | 61 |
| 64 TEST_F(NGLineBreakerTest, SingleNode) { | 62 TEST_F(NGLineBreakerTest, SingleNode) { |
| 65 LoadAhem(); | 63 LoadAhem(); |
| 66 NGInlineNode* node = CreateInlineNode(R"HTML( | 64 NGInlineNode node = CreateInlineNode(R"HTML( |
| 67 <!DOCTYPE html> | 65 <!DOCTYPE html> |
| 68 <style> | 66 <style> |
| 69 #container { | 67 #container { |
| 70 font: 10px/1 Ahem; | 68 font: 10px/1 Ahem; |
| 71 } | 69 } |
| 72 </style> | 70 </style> |
| 73 <div id=container>123 456 789</div> | 71 <div id=container>123 456 789</div> |
| 74 )HTML"); | 72 )HTML"); |
| 75 | 73 |
| 76 Vector<NGInlineItemResults> lines; | 74 Vector<NGInlineItemResults> lines; |
| 77 lines = BreakLines(node, LayoutUnit(80)); | 75 lines = BreakLines(node, LayoutUnit(80)); |
| 78 EXPECT_EQ(2u, lines.size()); | 76 EXPECT_EQ(2u, lines.size()); |
| 79 EXPECT_EQ("123 456", ToString(lines[0], node)); | 77 EXPECT_EQ("123 456", ToString(lines[0], node)); |
| 80 EXPECT_EQ("789", ToString(lines[1], node)); | 78 EXPECT_EQ("789", ToString(lines[1], node)); |
| 81 | 79 |
| 82 lines = BreakLines(node, LayoutUnit(60)); | 80 lines = BreakLines(node, LayoutUnit(60)); |
| 83 EXPECT_EQ(3u, lines.size()); | 81 EXPECT_EQ(3u, lines.size()); |
| 84 EXPECT_EQ("123", ToString(lines[0], node)); | 82 EXPECT_EQ("123", ToString(lines[0], node)); |
| 85 EXPECT_EQ("456", ToString(lines[1], node)); | 83 EXPECT_EQ("456", ToString(lines[1], node)); |
| 86 EXPECT_EQ("789", ToString(lines[2], node)); | 84 EXPECT_EQ("789", ToString(lines[2], node)); |
| 87 } | 85 } |
| 88 | 86 |
| 89 TEST_F(NGLineBreakerTest, OverflowWord) { | 87 TEST_F(NGLineBreakerTest, OverflowWord) { |
| 90 LoadAhem(); | 88 LoadAhem(); |
| 91 NGInlineNode* node = CreateInlineNode(R"HTML( | 89 NGInlineNode node = CreateInlineNode(R"HTML( |
| 92 <!DOCTYPE html> | 90 <!DOCTYPE html> |
| 93 <style> | 91 <style> |
| 94 #container { | 92 #container { |
| 95 font: 10px/1 Ahem; | 93 font: 10px/1 Ahem; |
| 96 } | 94 } |
| 97 </style> | 95 </style> |
| 98 <div id=container>12345 678</div> | 96 <div id=container>12345 678</div> |
| 99 )HTML"); | 97 )HTML"); |
| 100 | 98 |
| 101 // The first line overflows, but the last line does not. | 99 // The first line overflows, but the last line does not. |
| 102 Vector<NGInlineItemResults> lines; | 100 Vector<NGInlineItemResults> lines; |
| 103 lines = BreakLines(node, LayoutUnit(40)); | 101 lines = BreakLines(node, LayoutUnit(40)); |
| 104 EXPECT_EQ(2u, lines.size()); | 102 EXPECT_EQ(2u, lines.size()); |
| 105 EXPECT_EQ("12345", ToString(lines[0], node)); | 103 EXPECT_EQ("12345", ToString(lines[0], node)); |
| 106 EXPECT_EQ("678", ToString(lines[1], node)); | 104 EXPECT_EQ("678", ToString(lines[1], node)); |
| 107 | 105 |
| 108 // Both lines overflow. | 106 // Both lines overflow. |
| 109 lines = BreakLines(node, LayoutUnit(20)); | 107 lines = BreakLines(node, LayoutUnit(20)); |
| 110 EXPECT_EQ(2u, lines.size()); | 108 EXPECT_EQ(2u, lines.size()); |
| 111 EXPECT_EQ("12345", ToString(lines[0], node)); | 109 EXPECT_EQ("12345", ToString(lines[0], node)); |
| 112 EXPECT_EQ("678", ToString(lines[1], node)); | 110 EXPECT_EQ("678", ToString(lines[1], node)); |
| 113 } | 111 } |
| 114 | 112 |
| 115 TEST_F(NGLineBreakerTest, OverflowAtomicInline) { | 113 TEST_F(NGLineBreakerTest, OverflowAtomicInline) { |
| 116 LoadAhem(); | 114 LoadAhem(); |
| 117 NGInlineNode* node = CreateInlineNode(R"HTML( | 115 NGInlineNode node = CreateInlineNode(R"HTML( |
| 118 <!DOCTYPE html> | 116 <!DOCTYPE html> |
| 119 <style> | 117 <style> |
| 120 #container { | 118 #container { |
| 121 font: 10px/1 Ahem; | 119 font: 10px/1 Ahem; |
| 122 } | 120 } |
| 123 span { | 121 span { |
| 124 display: inline-block; | 122 display: inline-block; |
| 125 width: 30px; | 123 width: 30px; |
| 126 height: 10px; | 124 height: 10px; |
| 127 } | 125 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 148 | 146 |
| 149 lines = BreakLines(node, LayoutUnit(20)); | 147 lines = BreakLines(node, LayoutUnit(20)); |
| 150 EXPECT_EQ(3u, lines.size()); | 148 EXPECT_EQ(3u, lines.size()); |
| 151 EXPECT_EQ("12345", ToString(lines[0], node)); | 149 EXPECT_EQ("12345", ToString(lines[0], node)); |
| 152 EXPECT_EQ(String(u"\uFFFC"), ToString(lines[1], node)); | 150 EXPECT_EQ(String(u"\uFFFC"), ToString(lines[1], node)); |
| 153 EXPECT_EQ("678", ToString(lines[2], node)); | 151 EXPECT_EQ("678", ToString(lines[2], node)); |
| 154 } | 152 } |
| 155 | 153 |
| 156 TEST_F(NGLineBreakerTest, OverflowMargin) { | 154 TEST_F(NGLineBreakerTest, OverflowMargin) { |
| 157 LoadAhem(); | 155 LoadAhem(); |
| 158 NGInlineNode* node = CreateInlineNode(R"HTML( | 156 NGInlineNode node = CreateInlineNode(R"HTML( |
| 159 <!DOCTYPE html> | 157 <!DOCTYPE html> |
| 160 <style> | 158 <style> |
| 161 #container { | 159 #container { |
| 162 font: 10px/1 Ahem; | 160 font: 10px/1 Ahem; |
| 163 } | 161 } |
| 164 span { | 162 span { |
| 165 margin-right: 4em; | 163 margin-right: 4em; |
| 166 } | 164 } |
| 167 </style> | 165 </style> |
| 168 <div id=container><span>123 456</span> 789</div> | 166 <div id=container><span>123 456</span> 789</div> |
| 169 )HTML"); | 167 )HTML"); |
| 170 const Vector<NGInlineItem>& items = node->Items(); | 168 const Vector<NGInlineItem>& items = node.Items(); |
| 171 | 169 |
| 172 // While "123 456" can fit in a line, "456" has a right margin that cannot | 170 // While "123 456" can fit in a line, "456" has a right margin that cannot |
| 173 // fit. Since "456" and its right margin is not breakable, "456" should be on | 171 // fit. Since "456" and its right margin is not breakable, "456" should be on |
| 174 // the next line. | 172 // the next line. |
| 175 Vector<NGInlineItemResults> lines; | 173 Vector<NGInlineItemResults> lines; |
| 176 lines = BreakLines(node, LayoutUnit(80)); | 174 lines = BreakLines(node, LayoutUnit(80)); |
| 177 EXPECT_EQ(3u, lines.size()); | 175 EXPECT_EQ(3u, lines.size()); |
| 178 EXPECT_EQ("123", ToString(lines[0], node)); | 176 EXPECT_EQ("123", ToString(lines[0], node)); |
| 179 EXPECT_EQ("456", ToString(lines[1], node)); | 177 EXPECT_EQ("456", ToString(lines[1], node)); |
| 180 DCHECK_EQ(NGInlineItem::kCloseTag, items[lines[1].back().item_index].Type()); | 178 DCHECK_EQ(NGInlineItem::kCloseTag, items[lines[1].back().item_index].Type()); |
| 181 EXPECT_EQ("789", ToString(lines[2], node)); | 179 EXPECT_EQ("789", ToString(lines[2], node)); |
| 182 | 180 |
| 183 // Same as above, but this time "456" overflows the line because it is 70px. | 181 // Same as above, but this time "456" overflows the line because it is 70px. |
| 184 lines = BreakLines(node, LayoutUnit(60)); | 182 lines = BreakLines(node, LayoutUnit(60)); |
| 185 EXPECT_EQ(3u, lines.size()); | 183 EXPECT_EQ(3u, lines.size()); |
| 186 EXPECT_EQ("123", ToString(lines[0], node)); | 184 EXPECT_EQ("123", ToString(lines[0], node)); |
| 187 EXPECT_EQ("456", ToString(lines[1], node)); | 185 EXPECT_EQ("456", ToString(lines[1], node)); |
| 188 DCHECK_EQ(NGInlineItem::kCloseTag, items[lines[1].back().item_index].Type()); | 186 DCHECK_EQ(NGInlineItem::kCloseTag, items[lines[1].back().item_index].Type()); |
| 189 EXPECT_EQ("789", ToString(lines[2], node)); | 187 EXPECT_EQ("789", ToString(lines[2], node)); |
| 190 } | 188 } |
| 191 | 189 |
| 192 TEST_F(NGLineBreakerTest, BoundaryInWord) { | 190 TEST_F(NGLineBreakerTest, BoundaryInWord) { |
| 193 LoadAhem(); | 191 LoadAhem(); |
| 194 NGInlineNode* node = CreateInlineNode(R"HTML( | 192 NGInlineNode node = CreateInlineNode(R"HTML( |
| 195 <!DOCTYPE html> | 193 <!DOCTYPE html> |
| 196 <style> | 194 <style> |
| 197 #container { | 195 #container { |
| 198 font: 10px/1 Ahem; | 196 font: 10px/1 Ahem; |
| 199 } | 197 } |
| 200 </style> | 198 </style> |
| 201 <div id=container><span>123 456</span>789 abc</div> | 199 <div id=container><span>123 456</span>789 abc</div> |
| 202 )HTML"); | 200 )HTML"); |
| 203 | 201 |
| 204 // The element boundary within "456789" should not cause a break. | 202 // The element boundary within "456789" should not cause a break. |
| 205 // Since "789" does not fit, it should go to the next line along with "456". | 203 // Since "789" does not fit, it should go to the next line along with "456". |
| 206 Vector<NGInlineItemResults> lines; | 204 Vector<NGInlineItemResults> lines; |
| 207 lines = BreakLines(node, LayoutUnit(80)); | 205 lines = BreakLines(node, LayoutUnit(80)); |
| 208 EXPECT_EQ(3u, lines.size()); | 206 EXPECT_EQ(3u, lines.size()); |
| 209 EXPECT_EQ("123", ToString(lines[0], node)); | 207 EXPECT_EQ("123", ToString(lines[0], node)); |
| 210 EXPECT_EQ("456789", ToString(lines[1], node)); | 208 EXPECT_EQ("456789", ToString(lines[1], node)); |
| 211 EXPECT_EQ("abc", ToString(lines[2], node)); | 209 EXPECT_EQ("abc", ToString(lines[2], node)); |
| 212 | 210 |
| 213 // Same as above, but this time "456789" overflows the line because it is | 211 // Same as above, but this time "456789" overflows the line because it is |
| 214 // 60px. | 212 // 60px. |
| 215 lines = BreakLines(node, LayoutUnit(50)); | 213 lines = BreakLines(node, LayoutUnit(50)); |
| 216 EXPECT_EQ(3u, lines.size()); | 214 EXPECT_EQ(3u, lines.size()); |
| 217 EXPECT_EQ("123", ToString(lines[0], node)); | 215 EXPECT_EQ("123", ToString(lines[0], node)); |
| 218 EXPECT_EQ("456789", ToString(lines[1], node)); | 216 EXPECT_EQ("456789", ToString(lines[1], node)); |
| 219 EXPECT_EQ("abc", ToString(lines[2], node)); | 217 EXPECT_EQ("abc", ToString(lines[2], node)); |
| 220 } | 218 } |
| 221 | 219 |
| 222 TEST_F(NGLineBreakerTest, BoundaryInFirstWord) { | 220 TEST_F(NGLineBreakerTest, BoundaryInFirstWord) { |
| 223 LoadAhem(); | 221 LoadAhem(); |
| 224 NGInlineNode* node = CreateInlineNode(R"HTML( | 222 NGInlineNode node = CreateInlineNode(R"HTML( |
| 225 <!DOCTYPE html> | 223 <!DOCTYPE html> |
| 226 <style> | 224 <style> |
| 227 #container { | 225 #container { |
| 228 font: 10px/1 Ahem; | 226 font: 10px/1 Ahem; |
| 229 } | 227 } |
| 230 </style> | 228 </style> |
| 231 <div id=container><span>123</span>456 789</div> | 229 <div id=container><span>123</span>456 789</div> |
| 232 )HTML"); | 230 )HTML"); |
| 233 | 231 |
| 234 Vector<NGInlineItemResults> lines; | 232 Vector<NGInlineItemResults> lines; |
| 235 lines = BreakLines(node, LayoutUnit(80)); | 233 lines = BreakLines(node, LayoutUnit(80)); |
| 236 EXPECT_EQ(2u, lines.size()); | 234 EXPECT_EQ(2u, lines.size()); |
| 237 EXPECT_EQ("123456", ToString(lines[0], node)); | 235 EXPECT_EQ("123456", ToString(lines[0], node)); |
| 238 EXPECT_EQ("789", ToString(lines[1], node)); | 236 EXPECT_EQ("789", ToString(lines[1], node)); |
| 239 | 237 |
| 240 lines = BreakLines(node, LayoutUnit(50)); | 238 lines = BreakLines(node, LayoutUnit(50)); |
| 241 EXPECT_EQ(2u, lines.size()); | 239 EXPECT_EQ(2u, lines.size()); |
| 242 EXPECT_EQ("123456", ToString(lines[0], node)); | 240 EXPECT_EQ("123456", ToString(lines[0], node)); |
| 243 EXPECT_EQ("789", ToString(lines[1], node)); | 241 EXPECT_EQ("789", ToString(lines[1], node)); |
| 244 | 242 |
| 245 lines = BreakLines(node, LayoutUnit(20)); | 243 lines = BreakLines(node, LayoutUnit(20)); |
| 246 EXPECT_EQ(2u, lines.size()); | 244 EXPECT_EQ(2u, lines.size()); |
| 247 EXPECT_EQ("123456", ToString(lines[0], node)); | 245 EXPECT_EQ("123456", ToString(lines[0], node)); |
| 248 EXPECT_EQ("789", ToString(lines[1], node)); | 246 EXPECT_EQ("789", ToString(lines[1], node)); |
| 249 } | 247 } |
| 250 | 248 |
| 251 } // namespace | 249 } // namespace |
| 252 } // namespace blink | 250 } // namespace blink |
| OLD | NEW |