Truly independent web browser
LibWeb: Make wbr elements soft wrap opportunities
sideshowbarker17d026a0bcde
diff --git a/Libraries/LibWeb/CSS/ElementBoxKind.h b/Libraries/LibWeb/CSS/ElementBoxKind.hindex a61656ffe62..d28a0cfc07d 100644--- a/Libraries/LibWeb/CSS/ElementBoxKind.h+++ b/Libraries/LibWeb/CSS/ElementBoxKind.h@@ -45,7 +45,9 @@ enum class ElementBoxKind : u8 {// A frame element whose parent is a frameset.Frame,FrameSet,+ // A wbr element: a line break opportunity, whatever its display says.+ WordBreakOpportunity,};-static_assert(to_underlying(ElementBoxKind::FrameSet) == 24, "Rust's ElementBoxKind lists the same kinds in the same order");+static_assert(to_underlying(ElementBoxKind::WordBreakOpportunity) == 25, "Rust's ElementBoxKind lists the same kinds in the same order");}diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cppindex 4c669421578..b16cd8a9899 100644--- a/Libraries/LibWeb/DOM/Document.cpp+++ b/Libraries/LibWeb/DOM/Document.cpp@@ -907,6 +907,7 @@ Layout::NodeArena& Document::layout_node_arena()switch (kind) {case Layout::RustFFI::NodeKind::InlineNode:case Layout::RustFFI::NodeKind::BreakNode:+ case Layout::RustFFI::NodeKind::WordBreakOpportunityNode:Layout::allocate_layout_node<Layout::NodeWithStyle>(document, Layout::BindToPreparedArenaSlot::Yes, slot, kind);return;case Layout::RustFFI::NodeKind::TextNode:diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cppindex b8b18ae19a3..29b11316b06 100644--- a/Libraries/LibWeb/DOM/Element.cpp+++ b/Libraries/LibWeb/DOM/Element.cpp@@ -1382,6 +1382,10 @@ CSS::ElementBoxKind Element::box_kind() const{if (local_name() == u"noscript"sv && document().is_scripting_enabled())return CSS::ElementBoxKind::NoBox;+ // https://html.spec.whatwg.org/multipage/text-level-semantics.html#the-wbr-element+ // "The wbr element represents a line break opportunity."+ if (is_html_element() && local_name() == HTML::TagNames::wbr)+ return CSS::ElementBoxKind::WordBreakOpportunity;return CSS::ElementBoxKind::FromDisplay;}diff --git a/Libraries/LibWeb/Layout/Node.cpp b/Libraries/LibWeb/Layout/Node.cppindex 8d0f7172023..97b684ce276 100644--- a/Libraries/LibWeb/Layout/Node.cpp+++ b/Libraries/LibWeb/Layout/Node.cpp@@ -140,6 +140,7 @@ StringView Node::class_name() constLAYOUT_NODE_KIND_NAME_CASE(TextNode)LAYOUT_NODE_KIND_NAME_CASE(VideoBox)LAYOUT_NODE_KIND_NAME_CASE(Viewport)+ LAYOUT_NODE_KIND_NAME_CASE(WordBreakOpportunityNode)case RustFFI::NodeKind::Unset:break;}diff --git a/Libraries/LibWeb/Rust/src/css/style/bridge.rs b/Libraries/LibWeb/Rust/src/css/style/bridge.rsindex e9d1370b873..d68bca51efb 100644--- a/Libraries/LibWeb/Rust/src/css/style/bridge.rs+++ b/Libraries/LibWeb/Rust/src/css/style/bridge.rs@@ -828,6 +828,8 @@ pub enum ElementBoxKind {/// A `<frame>` whose parent is a `<frameset>`.Frame = 23,FrameSet = 24,+ /// A `<wbr>`: a line break opportunity, whatever its display says.+ WordBreakOpportunity = 25,}impl ElementBoxKind {diff --git a/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs b/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rsindex 34844d57618..35070f30e37 100644--- a/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs+++ b/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs@@ -1224,7 +1224,24 @@ impl<'pass> InlineContentPreparer<'pass> {&& !facts.produces_line_box_fragment_when_empty()&& self.pass.text_content(self.current_node).text.is_empty())};- let event = InlineBoxEvent::leaving_innermost_box(self.inline_box_event_position(), before_item);+ // NB: A wbr has no item, so a box that ends right after one ends with the item before the wbr instead, and its+ // end edges stay on the line of that item when the line breaks at the wbr.+ let mut position = self.inline_box_event_position();+ if !self.current_node.is_invalid()+ && self.facts(self.current_node).is_word_break_opportunity_node()+ && let Some(previous_item_index) = self.buffers.items.len().checked_sub(1)+ {+ let previous_item_position = InlineBoxEvent::position_at(previous_item_index, false);+ if self+ .buffers+ .box_events+ .last()+ .is_none_or(|event| event.position() <= previous_item_position)+ {+ position = previous_item_position;+ }+ }+ let event = InlineBoxEvent::leaving_innermost_box(position, before_item);self.buffers.box_events.push(event);}@@ -1287,6 +1304,7 @@ impl<'pass> InlineContentPreparer<'pass> {if facts.is_inline()&& facts.has_box_model_metrics()&& !facts.is_break_node()+ && !facts.is_word_break_opportunity_node()&& self.style(self.next_node).display().is_flow_inside()&& !self.is_out_of_flow(self.next_node)&& !facts.is_atomic_inline()@@ -1665,7 +1683,10 @@ impl<'pass> InlineContentPreparer<'pass> {&& let Some(previous_text) = self.previous_text.take()&& wraps_at_boundary{- if previous_node.is_some_and(|node| !self.facts(node).is_text_node()) {+ if previous_node.is_some_and(|node| {+ let facts = self.facts(node);+ !facts.is_text_node() && !facts.is_word_break_opportunity_node()+ }) {can_break_before = text_chunker::soft_wrap_opportunity_after_atomic_inline(&full_text[chunk.start..]);} else {let style = text_context.style;@@ -1766,6 +1787,19 @@ impl<'pass> InlineContentPreparer<'pass> {continue;}}+ if facts.is_word_break_opportunity_node() {+ // https://html.spec.whatwg.org/multipage/text-level-semantics.html#the-wbr-element+ // "The wbr element represents a line break opportunity."+ // NB: The wbr stands in the text before the text after it as U+200B ZERO WIDTH SPACE, which the line+ // breaker allows a break after, if the nearest common ancestor of the wbr and that text wraps.+ self.previous_text = Some(&ZERO_WIDTH_SPACE);+ self.previous_node = Some(node);+ self.previous_text_breaks_all = false;+ self.previous_text_regional_indicators = 0;+ self.code_unit_before_previous_text = None;+ self.skip_to_next();+ continue;+ }if !facts.is_box() {self.skip_to_next();continue;@@ -2996,3 +3030,6 @@ fn inline_extent_without_removed_whitespace<'item>(/// What an atomic inline stands for in the text around it when looking for soft wrap opportunities.static OBJECT_REPLACEMENT_CHARACTER: [u16; 1] = [0xFFFC];++/// What a `<wbr>` stands for in the text before the text after it when looking for soft wrap opportunities.+static ZERO_WIDTH_SPACE: [u16; 1] = [0x200B];diff --git a/Libraries/LibWeb/Rust/src/layout/node_data.rs b/Libraries/LibWeb/Rust/src/layout/node_data.rsindex f3465d51fe9..ab99b2d394e 100644--- a/Libraries/LibWeb/Rust/src/layout/node_data.rs+++ b/Libraries/LibWeb/Rust/src/layout/node_data.rs@@ -168,6 +168,8 @@ pub enum NodeKind {VideoBox = 37,Viewport = 38,FrameSetBox = 39,+ /// A `<wbr>`, which gives the line a soft wrap opportunity.+ WordBreakOpportunityNode = 40,}#[derive(Clone, Copy, Debug, Eq, PartialEq)]diff --git a/Libraries/LibWeb/Rust/src/layout/node_facts.rs b/Libraries/LibWeb/Rust/src/layout/node_facts.rsindex c2aaaabf74b..a8e2e39cc86 100644--- a/Libraries/LibWeb/Rust/src/layout/node_facts.rs+++ b/Libraries/LibWeb/Rust/src/layout/node_facts.rs@@ -443,7 +443,7 @@ pub(crate) fn node_display(style: Option<ComputedValuesView<'_>>) -> crate::css:pub(crate) fn node_can_have_children(data: &impl NodeShape) -> bool {match data.kind() {- NodeKind::BreakNode => false,+ NodeKind::BreakNode | NodeKind::WordBreakOpportunityNode => false,NodeKind::AudioBox | NodeKind::VideoBox => has_flag(data, NodeFlag::ReplacedBoxCanHaveChildren),NodeKind::SVGSVGBox => true,kind if kind_is_replaced_box(kind) => false,@@ -768,6 +768,7 @@ pub(crate) fn kind_is_box(kind: NodeKind) -> bool {kind,NodeKind::Unset| NodeKind::BreakNode+ | NodeKind::WordBreakOpportunityNode| NodeKind::InlineNode| NodeKind::Node| NodeKind::GeneratedTextNode@@ -1104,6 +1105,10 @@ impl<'pass> NodeFacts<'pass> {self.data().kind.get() == NodeKind::BreakNode}+ pub(crate) fn is_word_break_opportunity_node(&self) -> bool {+ self.data().kind.get() == NodeKind::WordBreakOpportunityNode+ }+pub(crate) fn is_box(&self) -> bool {kind_is_box(self.data().kind.get())}diff --git a/Libraries/LibWeb/Rust/src/layout/tree_builder.rs b/Libraries/LibWeb/Rust/src/layout/tree_builder.rsindex 83c577c52a8..8bc03375180 100644--- a/Libraries/LibWeb/Rust/src/layout/tree_builder.rs+++ b/Libraries/LibWeb/Rust/src/layout/tree_builder.rs@@ -191,6 +191,7 @@ pub(crate) fn node_kind_for_element_box_kind(ElementBoxKind::NoBox => return None,ElementBoxKind::FromDisplay => return node_kind_for_display(display),ElementBoxKind::Break => NodeKind::BreakNode,+ ElementBoxKind::WordBreakOpportunity => NodeKind::WordBreakOpportunityNode,ElementBoxKind::FieldSet => NodeKind::FieldSetBox,ElementBoxKind::Legend => NodeKind::LegendBox,ElementBoxKind::Audio => NodeKind::AudioBox,diff --git a/Libraries/LibWeb/Rust/src/layout/tree_plan/planner.rs b/Libraries/LibWeb/Rust/src/layout/tree_plan/planner.rsindex b85ac9bc4d4..80a39fb5220 100644--- a/Libraries/LibWeb/Rust/src/layout/tree_plan/planner.rs+++ b/Libraries/LibWeb/Rust/src/layout/tree_plan/planner.rs@@ -4237,7 +4237,7 @@ fn prepare_element(/// Whether a box of `kind`, with `flags`, holds the boxes of its children. See `node_facts::node_can_have_children`.fn kind_can_have_children(kind: NodeKind, flags: u32) -> bool {match kind {- NodeKind::BreakNode => false,+ NodeKind::BreakNode | NodeKind::WordBreakOpportunityNode => false,NodeKind::AudioBox | NodeKind::VideoBox => flags & NodeFlag::ReplacedBoxCanHaveChildren as u32 != 0,NodeKind::SVGSVGBox => true,kind if node_facts::kind_is_replaced_box(kind) => false,diff --git a/Libraries/LibWeb/Rust/src/painting/node_painting.rs b/Libraries/LibWeb/Rust/src/painting/node_painting.rsindex 1b3a55c3375..e861ceec836 100644--- a/Libraries/LibWeb/Rust/src/painting/node_painting.rs+++ b/Libraries/LibWeb/Rust/src/painting/node_painting.rs@@ -10,7 +10,12 @@ use crate::painting::paint_read::{GeometryRead, PaintRow};pub(crate) const fn has_paintable(kind: NodeKind) -> bool {!matches!(kind,- NodeKind::Unset | NodeKind::BreakNode | NodeKind::GeneratedTextNode | NodeKind::Node | NodeKind::TextNode+ NodeKind::Unset+ | NodeKind::BreakNode+ | NodeKind::WordBreakOpportunityNode+ | NodeKind::GeneratedTextNode+ | NodeKind::Node+ | NodeKind::TextNode)}diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/mod.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/mod.rsindex 2b978db34c3..21dbd2256f4 100644--- a/Libraries/LibWeb/Rust/src/painting/record/paint/mod.rs+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/mod.rs@@ -195,7 +195,12 @@ fn paint_box_phase<O: Observer>(recorder: &mut PaintRecorder<'_, O>, paintable:svg::paint_path(recorder, paintable, phase);}NodeKind::SVGImageBox => svg::paint_image_element(recorder, paintable, phase),- NodeKind::Unset | NodeKind::BreakNode | NodeKind::GeneratedTextNode | NodeKind::Node | NodeKind::TextNode => {}+ NodeKind::Unset+ | NodeKind::BreakNode+ | NodeKind::WordBreakOpportunityNode+ | NodeKind::GeneratedTextNode+ | NodeKind::Node+ | NodeKind::TextNode => {}NodeKind::Viewport| NodeKind::BlockContainer| NodeKind::LegendBoxdiff --git a/Tests/LibWeb/Text/expected/HTML/wbr-soft-wrap-opportunity.txt b/Tests/LibWeb/Text/expected/HTML/wbr-soft-wrap-opportunity.txtnew file mode 100644index 00000000000..7c12679dd8b--- /dev/null+++ b/Tests/LibWeb/Text/expected/HTML/wbr-soft-wrap-opportunity.txt@@ -0,0 +1,17 @@+plain: 36x40+in-span: 36x40+after-span: 36x40+before-space: 36x40+after-space: 36x40+keep-all: 36x40+pre-wrap: 36x40+display-block: 36x40+end-padding: 36x40+nowrap: 36x20+pre: 36x20+nobr: 36x20+display-none: 36x20+wbr-only: 36x0+max-content: 42.1875x20+min-content: 36.375x40+end-padding span: 5.75..36diff --git a/Tests/LibWeb/Text/input/HTML/wbr-soft-wrap-opportunity.html b/Tests/LibWeb/Text/input/HTML/wbr-soft-wrap-opportunity.htmlnew file mode 100644index 00000000000..d911a2db5e3--- /dev/null+++ b/Tests/LibWeb/Text/input/HTML/wbr-soft-wrap-opportunity.html@@ -0,0 +1,40 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<style>+ div { width: 3ch; font: 20px/20px monospace; }+</style>+<!-- Each div is too narrow for "xxyy", and wide enough for "xx" and for "yy". So a div whose wbr is a soft wrap+ opportunity is two lines tall. -->+<div id="plain">xx<wbr>yy</div>+<div id="in-span">xx<span><wbr></span>yy</div>+<div id="after-span"><span>xx</span><wbr>yy</div>+<div id="before-space">xx<wbr> yy</div>+<div id="after-space">xx <wbr>yy</div>+<div id="keep-all" style="word-break: keep-all">xx<wbr>yy</div>+<div id="pre-wrap" style="white-space: pre-wrap">xx<wbr>yy</div>+<div id="display-block">xx<wbr style="display: block">yy</div>+<!-- An inline box that ends right after a wbr ends on the line before the break, so its end padding counts toward the+ width that line is aligned by. -->+<div id="end-padding" style="text-align: right"><span style="padding-right: 6px">xx<wbr></span>yy</div>+<!-- Text that doesn't wrap has no soft wrap opportunities, so these stay on one line. -->+<div id="nowrap" style="white-space: nowrap">xx<wbr>yy</div>+<div id="pre" style="white-space: pre">xx<wbr>yy</div>+<div id="nobr"><nobr>xx<wbr>yy</nobr></div>+<div id="display-none">xx<wbr style="display: none">yy</div>+<!-- A wbr is no content of a line, so a line holding nothing else takes no height. -->+<div id="wbr-only"><wbr></div>+<div id="max-content" style="width: max-content">xx<wbr>yy</div>+<!-- The min-content width is that of the longer side of the wbr. -->+<div id="min-content" style="width: min-content">xxx<wbr>yy</div>+<script>+ test(() => {+ for (const div of document.querySelectorAll("div")) {+ const rect = div.getBoundingClientRect();+ println(`${div.id}: ${rect.width}x${rect.height}`);+ }+ const endPadding = document.getElementById("end-padding");+ const left = endPadding.getBoundingClientRect().left;+ for (const rect of endPadding.firstElementChild.getClientRects())+ println(`end-padding span: ${rect.left - left}..${rect.right - left}`);+ });+</script>