Jelajahi Sumber

LibWeb: Make text newlines in "pre" mode emit a ForcedBreak item

Instead of emitting a Text item with the "should_force_break" flag set
to true, newlines in newline-preserving text content now timply turn
into ForcedBreak items. This makes the <pre> element work again.
Andreas Kling 3 tahun lalu
induk
melakukan
aefe1727fc

+ 1 - 1
Userland/Libraries/LibWeb/Layout/BlockFormattingContext.cpp

@@ -593,7 +593,7 @@ void BlockFormattingContext::layout_floating_box(Box const& box, BlockContainer
     // If we have a LineBuilder, we're in the middle of inline layout, otherwise this is block layout.
     if (line_builder) {
         float y_offset = box_state.margin_box_top() + box_state.inset_top;
-        line_builder->break_if_needed(layout_mode, box_state.border_box_width(), false);
+        line_builder->break_if_needed(layout_mode, box_state.border_box_width());
         box_state.offset.set_y(line_builder->current_y() + y_offset);
         line_builder->adjust_last_line_after_inserting_floating_box({}, box.computed_values().float_(), box_state.border_box_width());
     } else {

+ 2 - 2
Userland/Libraries/LibWeb/Layout/InlineFormattingContext.cpp

@@ -241,7 +241,7 @@ void InlineFormattingContext::generate_line_boxes(LayoutMode layout_mode)
             break;
         case InlineLevelIterator::Item::Type::Element: {
             auto& box = verify_cast<Layout::Box>(*item.node);
-            line_builder.break_if_needed(layout_mode, item.border_box_width(), item.should_force_break);
+            line_builder.break_if_needed(layout_mode, item.border_box_width());
             line_builder.append_box(box, item.border_start + item.padding_start, item.padding_end + item.border_end, item.margin_start, item.margin_end);
             break;
         }
@@ -257,7 +257,7 @@ void InlineFormattingContext::generate_line_boxes(LayoutMode layout_mode)
 
         case InlineLevelIterator::Item::Type::Text: {
             auto& text_node = verify_cast<Layout::TextNode>(*item.node);
-            if (line_builder.break_if_needed(layout_mode, item.border_box_width(), item.should_force_break)) {
+            if (line_builder.break_if_needed(layout_mode, item.border_box_width())) {
                 if (item.is_collapsible_whitespace)
                     break;
             }

+ 7 - 1
Userland/Libraries/LibWeb/Layout/InlineLevelIterator.cpp

@@ -142,13 +142,19 @@ Optional<InlineLevelIterator::Item> InlineLevelIterator::next(float available_wi
 
         auto& chunk = chunk_opt.value();
         float chunk_width = text_node.font().width(chunk.view) + text_node.font().glyph_spacing();
+
+        if (m_text_node_context->do_respect_linebreaks && chunk.has_breaking_newline) {
+            return Item {
+                .type = Item::Type::ForcedBreak,
+            };
+        }
+
         Item item {
             .type = Item::Type::Text,
             .node = &text_node,
             .offset_in_node = chunk.start,
             .length_in_node = chunk.length,
             .width = chunk_width,
-            .should_force_break = m_text_node_context->do_respect_linebreaks && chunk.has_breaking_newline,
             .is_collapsible_whitespace = m_text_node_context->do_collapse && chunk.is_all_whitespace,
         };
 

+ 0 - 1
Userland/Libraries/LibWeb/Layout/InlineLevelIterator.h

@@ -40,7 +40,6 @@ public:
         float border_end { 0.0f };
         float margin_start { 0.0f };
         float margin_end { 0.0f };
-        bool should_force_break { false };
         bool is_collapsible_whitespace { false };
 
         float border_box_width() const

+ 1 - 3
Userland/Libraries/LibWeb/Layout/LineBuilder.cpp

@@ -79,12 +79,10 @@ void LineBuilder::append_text_chunk(TextNode const& text_node, size_t offset_in_
     m_max_height_on_current_line = max(m_max_height_on_current_line, content_height);
 }
 
-bool LineBuilder::should_break(LayoutMode layout_mode, float next_item_width, bool should_force_break)
+bool LineBuilder::should_break(LayoutMode layout_mode, float next_item_width)
 {
     if (layout_mode == LayoutMode::MinContent)
         return true;
-    if (should_force_break)
-        return true;
     if (layout_mode == LayoutMode::MaxContent)
         return false;
     auto const& line_boxes = m_containing_block_state.line_boxes;

+ 3 - 3
Userland/Libraries/LibWeb/Layout/LineBuilder.h

@@ -23,9 +23,9 @@ public:
     void append_text_chunk(TextNode const&, size_t offset_in_node, size_t length_in_node, float leading_size, float trailing_size, float leading_margin, float trailing_margin, float content_width, float content_height);
 
     // Returns whether a line break occurred.
-    bool break_if_needed(LayoutMode layout_mode, float next_item_width, bool should_force_break)
+    bool break_if_needed(LayoutMode layout_mode, float next_item_width)
     {
-        if (should_break(layout_mode, next_item_width, should_force_break)) {
+        if (should_break(layout_mode, next_item_width)) {
             break_line();
             return true;
         }
@@ -45,7 +45,7 @@ public:
 private:
     void begin_new_line(bool increment_y);
 
-    bool should_break(LayoutMode, float next_item_width, bool should_force_break);
+    bool should_break(LayoutMode, float next_item_width);
 
     LineBox& ensure_last_line_box();