mirror of
https://github.com/servo/servo.git
synced 2025-08-06 06:00:15 +01:00
Turn white-space into a shorthand (#32146)
Bumps Stylo to servo/stylo#37 `white-space` is split into `white-space-collapse` and `text-wrap-mode`: | white-space | white-space-collapse | text-wrap-mode | | ----------- | -------------------- | -------------- | | normal | collapse | wrap | | nowrap | collapse | nowrap | | pre-wrap | preserve | wrap | | pre | preserve | nowrap | | pre-line | preserve-breaks | wrap | | - | preserve-breaks | nowrap | Note this introduces a combination that wasn't previously possible, but I think the existing logic can handle it well enough. The old `allow_wrap()` is replaced by checking whether `text-wrap-mode` is set to `wrap`. The old `preserve_newlines()` is replaced by checking whether `white-space-collapse` is *not* set to `collapse`. The old `preserve_spaces()` is replaced by checking whether `white-space-collapse` is set to `preserve`.
This commit is contained in:
parent
a1f8c19355
commit
d490fdf83c
23 changed files with 141 additions and 454 deletions
|
@ -41,8 +41,9 @@ use style::computed_values::overflow_wrap::T as OverflowWrap;
|
|||
use style::computed_values::overflow_x::T as StyleOverflow;
|
||||
use style::computed_values::position::T as Position;
|
||||
use style::computed_values::text_decoration_line::T as TextDecorationLine;
|
||||
use style::computed_values::text_wrap_mode::T as TextWrapMode;
|
||||
use style::computed_values::transform_style::T as TransformStyle;
|
||||
use style::computed_values::white_space::T as WhiteSpace;
|
||||
use style::computed_values::white_space_collapse::T as WhiteSpaceCollapse;
|
||||
use style::computed_values::word_break::T as WordBreak;
|
||||
use style::logical_geometry::{Direction, LogicalMargin, LogicalRect, LogicalSize, WritingMode};
|
||||
use style::properties::ComputedValues;
|
||||
|
@ -1539,8 +1540,12 @@ impl Fragment {
|
|||
&self.selected_style
|
||||
}
|
||||
|
||||
pub fn white_space(&self) -> WhiteSpace {
|
||||
self.style().get_inherited_text().white_space
|
||||
pub fn white_space_collapse(&self) -> WhiteSpaceCollapse {
|
||||
self.style().get_inherited_text().white_space_collapse
|
||||
}
|
||||
|
||||
pub fn text_wrap_mode(&self) -> TextWrapMode {
|
||||
self.style().get_inherited_text().text_wrap_mode
|
||||
}
|
||||
|
||||
pub fn color(&self) -> Color {
|
||||
|
@ -1586,7 +1591,7 @@ impl Fragment {
|
|||
/// Returns true if this element can be split. This is true for text fragments, unless
|
||||
/// `white-space: pre` or `white-space: nowrap` is set.
|
||||
pub fn can_split(&self) -> bool {
|
||||
self.is_scanned_text_fragment() && self.white_space().allow_wrap()
|
||||
self.is_scanned_text_fragment() && self.text_wrap_mode() == TextWrapMode::Wrap
|
||||
}
|
||||
|
||||
/// Returns true if and only if this fragment is a generated content fragment.
|
||||
|
@ -1703,7 +1708,7 @@ impl Fragment {
|
|||
.metrics_for_range(range)
|
||||
.advance_width;
|
||||
|
||||
let min_line_inline_size = if self_.white_space().allow_wrap() {
|
||||
let min_line_inline_size = if self_.text_wrap_mode() == TextWrapMode::Wrap {
|
||||
text_fragment_info.run.min_width_for_range(range)
|
||||
} else {
|
||||
max_line_inline_size
|
||||
|
@ -1968,7 +1973,7 @@ impl Fragment {
|
|||
// see if we're going to overflow the line. If so, perform a best-effort split.
|
||||
let mut remaining_range = slice.text_run_range();
|
||||
let split_is_empty = inline_start_range.is_empty() &&
|
||||
(self.white_space().allow_wrap() ||
|
||||
(self.text_wrap_mode() == TextWrapMode::Wrap ||
|
||||
!self.requires_line_break_afterward_if_wrapping_on_newlines());
|
||||
if split_is_empty {
|
||||
// We're going to overflow the line.
|
||||
|
@ -2520,7 +2525,8 @@ impl Fragment {
|
|||
// FIXME: Should probably use a whitelist of styles that can safely differ (#3165)
|
||||
if self.style().get_font() != other.style().get_font() ||
|
||||
self.text_decoration_line() != other.text_decoration_line() ||
|
||||
self.white_space() != other.white_space() ||
|
||||
self.white_space_collapse() != other.white_space_collapse() ||
|
||||
self.text_wrap_mode() != other.text_wrap_mode() ||
|
||||
self.color() != other.color()
|
||||
{
|
||||
return false;
|
||||
|
@ -2893,7 +2899,7 @@ impl Fragment {
|
|||
}
|
||||
|
||||
pub fn strip_leading_whitespace_if_necessary(&mut self) -> WhitespaceStrippingResult {
|
||||
if self.white_space().preserve_spaces() {
|
||||
if self.white_space_collapse() == WhiteSpaceCollapse::Preserve {
|
||||
return WhitespaceStrippingResult::RetainFragment;
|
||||
}
|
||||
|
||||
|
@ -2963,7 +2969,7 @@ impl Fragment {
|
|||
|
||||
/// Returns true if the entire fragment was stripped.
|
||||
pub fn strip_trailing_whitespace_if_necessary(&mut self) -> WhitespaceStrippingResult {
|
||||
if self.white_space().preserve_spaces() {
|
||||
if self.white_space_collapse() == WhiteSpaceCollapse::Preserve {
|
||||
return WhitespaceStrippingResult::RetainFragment;
|
||||
}
|
||||
|
||||
|
|
|
@ -22,7 +22,8 @@ use style::computed_values::overflow_x::T as StyleOverflow;
|
|||
use style::computed_values::position::T as Position;
|
||||
use style::computed_values::text_align::T as TextAlign;
|
||||
use style::computed_values::text_justify::T as TextJustify;
|
||||
use style::computed_values::white_space::T as WhiteSpace;
|
||||
use style::computed_values::text_wrap_mode::T as TextWrapMode;
|
||||
use style::computed_values::white_space_collapse::T as WhiteSpaceCollapse;
|
||||
use style::logical_geometry::{LogicalRect, LogicalSize, WritingMode};
|
||||
use style::properties::ComputedValues;
|
||||
use style::servo::restyle_damage::ServoRestyleDamage;
|
||||
|
@ -622,7 +623,7 @@ impl LineBreaker {
|
|||
if fragment.suppress_line_break_before() || !fragment.is_on_glyph_run_boundary() {
|
||||
false
|
||||
} else {
|
||||
fragment.white_space().allow_wrap()
|
||||
fragment.text_wrap_mode() == TextWrapMode::Wrap
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -663,7 +664,7 @@ impl LineBreaker {
|
|||
|
||||
// If we must flush the line after finishing this fragment due to `white-space: pre`,
|
||||
// detect that.
|
||||
let line_flush_mode = if fragment.white_space().preserve_newlines() {
|
||||
let line_flush_mode = if fragment.white_space_collapse() != WhiteSpaceCollapse::Collapse {
|
||||
if fragment.requires_line_break_afterward_if_wrapping_on_newlines() {
|
||||
LineFlushMode::Flush
|
||||
} else {
|
||||
|
@ -687,7 +688,7 @@ impl LineBreaker {
|
|||
|
||||
// If the wrapping mode prevents us from splitting, then back up and split at the last
|
||||
// known good split point.
|
||||
if !fragment.white_space().allow_wrap() {
|
||||
if fragment.text_wrap_mode() == TextWrapMode::Nowrap {
|
||||
debug!(
|
||||
"LineBreaker: fragment can't split; falling back to last known good split point"
|
||||
);
|
||||
|
@ -1494,10 +1495,16 @@ impl Flow for InlineFlow {
|
|||
let mut intrinsic_sizes_for_nonbroken_run = IntrinsicISizesContribution::new();
|
||||
for fragment in &mut self.fragments.fragments {
|
||||
let intrinsic_sizes_for_fragment = fragment.compute_intrinsic_inline_sizes().finish();
|
||||
match fragment.style.get_inherited_text().white_space {
|
||||
WhiteSpace::Nowrap => intrinsic_sizes_for_nonbroken_run
|
||||
.union_nonbreaking_inline(&intrinsic_sizes_for_fragment),
|
||||
WhiteSpace::Pre => {
|
||||
let style_text = fragment.style.get_inherited_text();
|
||||
match (style_text.white_space_collapse, style_text.text_wrap_mode) {
|
||||
(WhiteSpaceCollapse::Collapse, TextWrapMode::Nowrap) => {
|
||||
intrinsic_sizes_for_nonbroken_run
|
||||
.union_nonbreaking_inline(&intrinsic_sizes_for_fragment)
|
||||
},
|
||||
(
|
||||
WhiteSpaceCollapse::Preserve | WhiteSpaceCollapse::PreserveBreaks,
|
||||
TextWrapMode::Nowrap,
|
||||
) => {
|
||||
intrinsic_sizes_for_nonbroken_run
|
||||
.union_nonbreaking_inline(&intrinsic_sizes_for_fragment);
|
||||
|
||||
|
@ -1512,7 +1519,10 @@ impl Flow for InlineFlow {
|
|||
intrinsic_sizes_for_inline_run = IntrinsicISizesContribution::new();
|
||||
}
|
||||
},
|
||||
WhiteSpace::PreWrap | WhiteSpace::PreLine => {
|
||||
(
|
||||
WhiteSpaceCollapse::Preserve | WhiteSpaceCollapse::PreserveBreaks,
|
||||
TextWrapMode::Wrap,
|
||||
) => {
|
||||
// Flush the intrinsic sizes we were gathering up for the nonbroken run, if
|
||||
// necessary.
|
||||
intrinsic_sizes_for_inline_run
|
||||
|
@ -1532,7 +1542,7 @@ impl Flow for InlineFlow {
|
|||
intrinsic_sizes_for_inline_run = IntrinsicISizesContribution::new();
|
||||
}
|
||||
},
|
||||
WhiteSpace::Normal => {
|
||||
(WhiteSpaceCollapse::Collapse, TextWrapMode::Wrap) => {
|
||||
// Flush the intrinsic sizes we were gathering up for the nonbroken run, if
|
||||
// necessary.
|
||||
intrinsic_sizes_for_inline_run
|
||||
|
|
|
@ -17,7 +17,7 @@ use gfx::text::util::{self, CompressionMode};
|
|||
use log::{debug, warn};
|
||||
use range::Range;
|
||||
use style::computed_values::text_rendering::T as TextRendering;
|
||||
use style::computed_values::white_space::T as WhiteSpace;
|
||||
use style::computed_values::white_space_collapse::T as WhiteSpaceCollapse;
|
||||
use style::computed_values::word_break::T as WordBreak;
|
||||
use style::logical_geometry::{LogicalSize, WritingMode};
|
||||
use style::properties::style_structs::Font as FontStyleStruct;
|
||||
|
@ -46,7 +46,7 @@ fn text(fragments: &LinkedList<Fragment>) -> String {
|
|||
|
||||
for fragment in fragments {
|
||||
if let SpecificFragmentInfo::UnscannedText(ref info) = fragment.specific {
|
||||
if fragment.white_space().preserve_newlines() {
|
||||
if fragment.white_space_collapse() != WhiteSpaceCollapse::Collapse {
|
||||
text.push_str(&info.text);
|
||||
} else {
|
||||
text.push_str(&info.text.replace('\n', " "));
|
||||
|
@ -190,12 +190,10 @@ impl TextRunScanner {
|
|||
let font_style = in_fragment.style().clone_font();
|
||||
let inherited_text_style = in_fragment.style().get_inherited_text();
|
||||
font_group = font_context.font_group(font_style);
|
||||
compression = match in_fragment.white_space() {
|
||||
WhiteSpace::Normal | WhiteSpace::Nowrap => {
|
||||
CompressionMode::CompressWhitespaceNewline
|
||||
},
|
||||
WhiteSpace::Pre | WhiteSpace::PreWrap => CompressionMode::CompressNone,
|
||||
WhiteSpace::PreLine => CompressionMode::CompressWhitespace,
|
||||
compression = match in_fragment.white_space_collapse() {
|
||||
WhiteSpaceCollapse::Collapse => CompressionMode::CompressWhitespaceNewline,
|
||||
WhiteSpaceCollapse::Preserve => CompressionMode::CompressNone,
|
||||
WhiteSpaceCollapse::PreserveBreaks => CompressionMode::CompressWhitespace,
|
||||
};
|
||||
text_transform = inherited_text_style.text_transform;
|
||||
letter_spacing = inherited_text_style.letter_spacing;
|
||||
|
@ -568,7 +566,7 @@ fn split_first_fragment_at_newline_if_necessary(fragments: &mut LinkedList<Fragm
|
|||
let string_before;
|
||||
let selection_before;
|
||||
{
|
||||
if !first_fragment.white_space().preserve_newlines() {
|
||||
if first_fragment.white_space_collapse() == WhiteSpaceCollapse::Collapse {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -77,10 +77,12 @@ use gfx::font::FontMetrics;
|
|||
use gfx::text::glyph::GlyphStore;
|
||||
use serde::Serialize;
|
||||
use servo_arc::Arc;
|
||||
use style::computed_values::text_wrap_mode::T as TextWrapMode;
|
||||
use style::computed_values::vertical_align::T as VerticalAlign;
|
||||
use style::computed_values::white_space::T as WhiteSpace;
|
||||
use style::computed_values::white_space_collapse::T as WhiteSpaceCollapse;
|
||||
use style::context::QuirksMode;
|
||||
use style::logical_geometry::WritingMode;
|
||||
use style::properties::style_structs::InheritedText;
|
||||
use style::properties::ComputedValues;
|
||||
use style::values::computed::{Clear, Length};
|
||||
use style::values::generics::box_::VerticalAlignKeyword;
|
||||
|
@ -659,11 +661,17 @@ pub(super) struct InlineFormattingContextState<'a, 'b> {
|
|||
/// Whether or not this InlineFormattingContext has processed any in flow content at all.
|
||||
had_inflow_content: bool,
|
||||
|
||||
/// The currently white-space setting of this line. This is stored on the
|
||||
/// The currently white-space-collapse setting of this line. This is stored on the
|
||||
/// [`InlineFormattingContextState`] because when a soft wrap opportunity is defined
|
||||
/// by the boundary between two characters, the white-space property of their nearest
|
||||
/// by the boundary between two characters, the white-space-collapse property of their
|
||||
/// nearest common ancestor is used.
|
||||
white_space_collapse: WhiteSpaceCollapse,
|
||||
|
||||
/// The currently text-wrap-mode setting of this line. This is stored on the
|
||||
/// [`InlineFormattingContextState`] because when a soft wrap opportunity is defined
|
||||
/// by the boundary between two characters, the text-wrap-mode property of their nearest
|
||||
/// common ancestor is used.
|
||||
white_space: WhiteSpace,
|
||||
text_wrap_mode: TextWrapMode,
|
||||
|
||||
/// The offset of the first and last baselines in the inline formatting context that we
|
||||
/// are laying out. This is used to propagate baselines to the ancestors of
|
||||
|
@ -697,7 +705,9 @@ impl<'a, 'b> InlineFormattingContextState<'a, 'b> {
|
|||
Some(inline_box_state) => &inline_box_state.base.style,
|
||||
None => self.containing_block.style,
|
||||
};
|
||||
self.white_space = style.get_inherited_text().white_space;
|
||||
let style_text = style.get_inherited_text();
|
||||
self.white_space_collapse = style_text.white_space_collapse;
|
||||
self.text_wrap_mode = style_text.text_wrap_mode;
|
||||
}
|
||||
|
||||
fn processing_br_element(&self) -> bool {
|
||||
|
@ -1286,7 +1296,7 @@ impl<'a, 'b> InlineFormattingContextState<'a, 'b> {
|
|||
) {
|
||||
let inline_advance = Length::from(glyph_store.total_advance());
|
||||
let flags = if glyph_store.is_whitespace() {
|
||||
SegmentContentFlags::from(text_run.parent_style.get_inherited_text().white_space)
|
||||
SegmentContentFlags::from(text_run.parent_style.get_inherited_text())
|
||||
} else {
|
||||
SegmentContentFlags::empty()
|
||||
};
|
||||
|
@ -1394,7 +1404,7 @@ impl<'a, 'b> InlineFormattingContextState<'a, 'b> {
|
|||
if self.current_line_segment.line_items.is_empty() {
|
||||
return;
|
||||
}
|
||||
if !self.white_space.allow_wrap() {
|
||||
if self.text_wrap_mode == TextWrapMode::Nowrap {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -1498,13 +1508,13 @@ impl SegmentContentFlags {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<WhiteSpace> for SegmentContentFlags {
|
||||
fn from(white_space: WhiteSpace) -> Self {
|
||||
impl From<&InheritedText> for SegmentContentFlags {
|
||||
fn from(style_text: &InheritedText) -> Self {
|
||||
let mut flags = Self::empty();
|
||||
if !white_space.preserve_spaces() {
|
||||
if style_text.white_space_collapse != WhiteSpaceCollapse::Preserve {
|
||||
flags.insert(Self::COLLAPSIBLE_WHITESPACE);
|
||||
}
|
||||
if white_space.allow_wrap() {
|
||||
if style_text.text_wrap_mode == TextWrapMode::Wrap {
|
||||
flags.insert(Self::WRAPPABLE_WHITESPACE);
|
||||
}
|
||||
flags
|
||||
|
@ -1633,6 +1643,7 @@ impl InlineFormattingContext {
|
|||
.map(|font| font.borrow().metrics.clone())
|
||||
});
|
||||
|
||||
let style_text = containing_block.style.get_inherited_text();
|
||||
let mut ifc = InlineFormattingContextState {
|
||||
positioning_context,
|
||||
containing_block,
|
||||
|
@ -1658,7 +1669,8 @@ impl InlineFormattingContext {
|
|||
have_deferred_soft_wrap_opportunity: false,
|
||||
prevent_soft_wrap_opportunity_before_next_atomic: false,
|
||||
had_inflow_content: false,
|
||||
white_space: containing_block.style.get_inherited_text().white_space,
|
||||
white_space_collapse: style_text.white_space_collapse,
|
||||
text_wrap_mode: style_text.text_wrap_mode,
|
||||
baselines: Baselines::default(),
|
||||
};
|
||||
|
||||
|
@ -2143,7 +2155,7 @@ impl IndependentFormattingContext {
|
|||
&mut ifc.prevent_soft_wrap_opportunity_before_next_atomic,
|
||||
false,
|
||||
);
|
||||
if ifc.white_space.allow_wrap() && !soft_wrap_opportunity_prevented {
|
||||
if ifc.text_wrap_mode == TextWrapMode::Wrap && !soft_wrap_opportunity_prevented {
|
||||
ifc.process_soft_wrap_opportunity();
|
||||
}
|
||||
|
||||
|
@ -2403,10 +2415,9 @@ impl<'a> ContentSizesComputation<'a> {
|
|||
continue;
|
||||
}
|
||||
|
||||
let white_space =
|
||||
text_run.parent_style.get_inherited_text().white_space;
|
||||
if !white_space.preserve_spaces() {
|
||||
// TODO: need to handle !white_space.allow_wrap().
|
||||
let style_text = text_run.parent_style.get_inherited_text();
|
||||
if style_text.white_space_collapse != WhiteSpaceCollapse::Preserve {
|
||||
// TODO: need to handle TextWrapMode::Nowrap.
|
||||
self.line_break_opportunity();
|
||||
// Discard any leading whitespace in the line. This will always be trimmed.
|
||||
if self.had_content_yet {
|
||||
|
@ -2416,7 +2427,7 @@ impl<'a> ContentSizesComputation<'a> {
|
|||
}
|
||||
continue;
|
||||
}
|
||||
if white_space.allow_wrap() {
|
||||
if style_text.text_wrap_mode == TextWrapMode::Wrap {
|
||||
self.commit_pending_whitespace();
|
||||
self.line_break_opportunity();
|
||||
self.current_line.max_content += advance;
|
||||
|
|
|
@ -9,6 +9,7 @@ use atomic_refcell::AtomicRef;
|
|||
use gfx::font::FontMetrics;
|
||||
use gfx::text::glyph::GlyphStore;
|
||||
use servo_arc::Arc;
|
||||
use style::computed_values::white_space_collapse::T as WhiteSpaceCollapse;
|
||||
use style::properties::ComputedValues;
|
||||
use style::values::computed::Length;
|
||||
use style::values::generics::box_::{GenericVerticalAlign, VerticalAlignKeyword};
|
||||
|
@ -151,11 +152,8 @@ pub(super) struct TextRunLineItem {
|
|||
|
||||
impl TextRunLineItem {
|
||||
fn trim_whitespace_at_end(&mut self, whitespace_trimmed: &mut Length) -> bool {
|
||||
if self
|
||||
.parent_style
|
||||
.get_inherited_text()
|
||||
.white_space
|
||||
.preserve_spaces()
|
||||
if self.parent_style.get_inherited_text().white_space_collapse ==
|
||||
WhiteSpaceCollapse::Preserve
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
@ -179,11 +177,8 @@ impl TextRunLineItem {
|
|||
}
|
||||
|
||||
fn trim_whitespace_at_start(&mut self, whitespace_trimmed: &mut Length) -> bool {
|
||||
if self
|
||||
.parent_style
|
||||
.get_inherited_text()
|
||||
.white_space
|
||||
.preserve_spaces()
|
||||
if self.parent_style.get_inherited_text().white_space_collapse ==
|
||||
WhiteSpaceCollapse::Preserve
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@ use range::Range;
|
|||
use serde::Serialize;
|
||||
use servo_arc::Arc;
|
||||
use style::computed_values::text_rendering::T as TextRendering;
|
||||
use style::computed_values::white_space::T as WhiteSpace;
|
||||
use style::computed_values::white_space_collapse::T as WhiteSpaceCollapse;
|
||||
use style::computed_values::word_break::T as WordBreak;
|
||||
use style::properties::ComputedValues;
|
||||
use style::values::specified::text::TextTransformCase;
|
||||
|
@ -189,8 +189,8 @@ impl TextRun {
|
|||
/// Whether or not this [`TextRun`] has uncollapsible content. This is used
|
||||
/// to determine if an [`super::InlineFormattingContext`] is considered empty or not.
|
||||
pub(super) fn has_uncollapsible_content(&self) -> bool {
|
||||
let white_space = self.parent_style.clone_white_space();
|
||||
if white_space.preserve_spaces() && !self.text.is_empty() {
|
||||
let white_space_collapse = self.parent_style.clone_white_space_collapse();
|
||||
if white_space_collapse == WhiteSpaceCollapse::Preserve && !self.text.is_empty() {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -198,7 +198,7 @@ impl TextRun {
|
|||
if !character.is_ascii_whitespace() {
|
||||
return true;
|
||||
}
|
||||
if character == '\n' && white_space.preserve_newlines() {
|
||||
if character == '\n' && white_space_collapse != WhiteSpaceCollapse::Collapse {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -291,10 +291,10 @@ impl TextRun {
|
|||
|
||||
// TODO: Eventually the text should come directly from the Cow strings of the DOM nodes.
|
||||
let text = std::mem::take(&mut self.text);
|
||||
let white_space = self.parent_style.clone_white_space();
|
||||
let white_space_collapse = self.parent_style.clone_white_space_collapse();
|
||||
let collapsed = WhitespaceCollapse::new(
|
||||
text.as_str().chars(),
|
||||
white_space,
|
||||
white_space_collapse,
|
||||
*last_inline_box_ended_with_collapsible_white_space,
|
||||
);
|
||||
|
||||
|
@ -327,7 +327,7 @@ impl TextRun {
|
|||
|
||||
*on_word_boundary = character.is_whitespace();
|
||||
*last_inline_box_ended_with_collapsible_white_space =
|
||||
*on_word_boundary && !white_space.preserve_spaces();
|
||||
*on_word_boundary && white_space_collapse != WhiteSpaceCollapse::Preserve;
|
||||
|
||||
let prevents_soft_wrap_opportunity =
|
||||
char_prevents_soft_wrap_opportunity_when_before_or_after_atomic(character);
|
||||
|
@ -437,11 +437,8 @@ impl TextRun {
|
|||
if !run.glyph_store.is_whitespace() || run.range.length() != ByteIndex(1) {
|
||||
return false;
|
||||
}
|
||||
if !self
|
||||
.parent_style
|
||||
.get_inherited_text()
|
||||
.white_space
|
||||
.preserve_newlines()
|
||||
if self.parent_style.get_inherited_text().white_space_collapse ==
|
||||
WhiteSpaceCollapse::Collapse
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
@ -526,7 +523,7 @@ fn preserve_segment_break() -> bool {
|
|||
|
||||
pub struct WhitespaceCollapse<InputIterator> {
|
||||
char_iterator: InputIterator,
|
||||
white_space: WhiteSpace,
|
||||
white_space_collapse: WhiteSpaceCollapse,
|
||||
|
||||
/// Whether or not we should collapse white space completely at the start of the string.
|
||||
/// This is true when the last character handled in our owning [`super::InlineFormattingContext`]
|
||||
|
@ -555,12 +552,12 @@ pub struct WhitespaceCollapse<InputIterator> {
|
|||
impl<InputIterator> WhitespaceCollapse<InputIterator> {
|
||||
pub fn new(
|
||||
char_iterator: InputIterator,
|
||||
white_space: WhiteSpace,
|
||||
white_space_collapse: WhiteSpaceCollapse,
|
||||
trim_beginning_white_space: bool,
|
||||
) -> Self {
|
||||
Self {
|
||||
char_iterator,
|
||||
white_space,
|
||||
white_space_collapse,
|
||||
remove_collapsible_white_space_at_start: trim_beginning_white_space,
|
||||
inside_white_space: false,
|
||||
following_newline: false,
|
||||
|
@ -594,7 +591,7 @@ where
|
|||
// > characters are considered collapsible
|
||||
// If whitespace is not considered collapsible, it is preserved entirely, which
|
||||
// means that we can simply return the input string exactly.
|
||||
if self.white_space.preserve_spaces() {
|
||||
if self.white_space_collapse == WhiteSpaceCollapse::Preserve {
|
||||
return self.char_iterator.next();
|
||||
}
|
||||
|
||||
|
@ -623,7 +620,7 @@ where
|
|||
//
|
||||
// > When white-space is pre, pre-wrap, or pre-line, segment breaks are not
|
||||
// > collapsible and are instead transformed into a preserved line feed"
|
||||
if self.white_space == WhiteSpace::PreLine {
|
||||
if self.white_space_collapse != WhiteSpaceCollapse::Collapse {
|
||||
self.inside_white_space = false;
|
||||
self.following_newline = true;
|
||||
return Some(character);
|
||||
|
|
|
@ -4,52 +4,60 @@
|
|||
|
||||
mod text {
|
||||
use layout_2020::flow::text_run::WhitespaceCollapse;
|
||||
use style::computed_values::white_space::T as WhiteSpace;
|
||||
use style::computed_values::white_space_collapse::T as WhiteSpaceCollapse;
|
||||
|
||||
#[test]
|
||||
fn test_collapse_whitespace() {
|
||||
let collapse = |input: &str, white_space, trim_beginning_white_space| {
|
||||
WhitespaceCollapse::new(input.chars(), white_space, trim_beginning_white_space)
|
||||
.collect::<String>()
|
||||
let collapse = |input: &str, white_space_collapse, trim_beginning_white_space| {
|
||||
WhitespaceCollapse::new(
|
||||
input.chars(),
|
||||
white_space_collapse,
|
||||
trim_beginning_white_space,
|
||||
)
|
||||
.collect::<String>()
|
||||
};
|
||||
|
||||
let output = collapse("H ", WhiteSpace::Normal, false);
|
||||
let output = collapse("H ", WhiteSpaceCollapse::Collapse, false);
|
||||
assert_eq!(output, "H ");
|
||||
|
||||
let output = collapse(" W", WhiteSpace::Normal, true);
|
||||
let output = collapse(" W", WhiteSpaceCollapse::Collapse, true);
|
||||
assert_eq!(output, "W");
|
||||
|
||||
let output = collapse(" W", WhiteSpace::Normal, false);
|
||||
let output = collapse(" W", WhiteSpaceCollapse::Collapse, false);
|
||||
assert_eq!(output, " W");
|
||||
|
||||
let output = collapse(" H W", WhiteSpace::Normal, false);
|
||||
let output = collapse(" H W", WhiteSpaceCollapse::Collapse, false);
|
||||
assert_eq!(output, " H W");
|
||||
|
||||
let output = collapse("\n H \n \t W", WhiteSpace::Normal, false);
|
||||
let output = collapse("\n H \n \t W", WhiteSpaceCollapse::Collapse, false);
|
||||
assert_eq!(output, " H W");
|
||||
|
||||
let output = collapse("\n H \n \t W \n", WhiteSpace::Pre, false);
|
||||
let output = collapse("\n H \n \t W \n", WhiteSpaceCollapse::Preserve, false);
|
||||
assert_eq!(output, "\n H \n \t W \n");
|
||||
|
||||
let output = collapse("\n H \n \t W \n ", WhiteSpace::PreLine, false);
|
||||
let output = collapse(
|
||||
"\n H \n \t W \n ",
|
||||
WhiteSpaceCollapse::PreserveBreaks,
|
||||
false,
|
||||
);
|
||||
assert_eq!(output, "\nH\nW\n");
|
||||
|
||||
let output = collapse("Hello \n World", WhiteSpace::PreLine, true);
|
||||
let output = collapse("Hello \n World", WhiteSpaceCollapse::PreserveBreaks, true);
|
||||
assert_eq!(output, "Hello\nWorld");
|
||||
|
||||
let output = collapse(" \n World", WhiteSpace::PreLine, true);
|
||||
let output = collapse(" \n World", WhiteSpaceCollapse::PreserveBreaks, true);
|
||||
assert_eq!(output, "\nWorld");
|
||||
|
||||
let output = collapse(" ", WhiteSpace::Normal, true);
|
||||
let output = collapse(" ", WhiteSpaceCollapse::Collapse, true);
|
||||
assert_eq!(output, "");
|
||||
|
||||
let output = collapse(" ", WhiteSpace::Normal, false);
|
||||
let output = collapse(" ", WhiteSpaceCollapse::Collapse, false);
|
||||
assert_eq!(output, " ");
|
||||
|
||||
let output = collapse("\n ", WhiteSpace::Normal, true);
|
||||
let output = collapse("\n ", WhiteSpaceCollapse::Collapse, true);
|
||||
assert_eq!(output, "");
|
||||
|
||||
let output = collapse("\n ", WhiteSpace::Normal, false);
|
||||
let output = collapse("\n ", WhiteSpaceCollapse::Collapse, false);
|
||||
assert_eq!(output, " ");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@ use script_layout_interface::{
|
|||
use servo_arc::Arc;
|
||||
use servo_url::ServoUrl;
|
||||
use style;
|
||||
use style::computed_values::white_space_collapse::T as WhiteSpaceCollapse;
|
||||
use style::context::SharedStyleContext;
|
||||
use style::dom::{NodeInfo, TElement, TNode, TShadowRoot};
|
||||
use style::properties::ComputedValues;
|
||||
|
@ -337,11 +338,10 @@ impl<'dom> ThreadSafeLayoutNode<'dom> for ServoThreadSafeLayoutNode<'dom> {
|
|||
//
|
||||
// If you implement other values for this property, you will almost certainly
|
||||
// want to update this check.
|
||||
!self
|
||||
.style(context)
|
||||
self.style(context)
|
||||
.get_inherited_text()
|
||||
.white_space
|
||||
.preserve_newlines()
|
||||
.white_space_collapse ==
|
||||
WhiteSpaceCollapse::Collapse
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue