Auto merge of #26447 - jdm:white-space-2020, r=nox

Layout 2020: Implement basic white-space: pre support

With these changes `<pre>` and `<br>` preserve spaces and force line breaks appropriately.

---
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix #26440
- [x] There are tests for these changes
This commit is contained in:
bors-servo 2020-07-29 10:29:30 -04:00 committed by GitHub
commit 6a3c3a4e18
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
53 changed files with 128 additions and 135 deletions

View file

@ -20,6 +20,7 @@ use rayon_croissant::ParallelIteratorExt;
use servo_arc::Arc;
use std::borrow::Cow;
use std::convert::{TryFrom, TryInto};
use style::computed_values::white_space::T as WhiteSpace;
use style::properties::longhands::list_style_position::computed_value::T as ListStylePosition;
use style::properties::ComputedValues;
use style::selector_parser::PseudoElement;
@ -293,60 +294,96 @@ where
}
fn handle_text(&mut self, info: &NodeAndStyleInfo<Node>, input: Cow<'dom, str>) {
let (leading_whitespace, mut input) = self.handle_leading_whitespace(&input);
if leading_whitespace || !input.is_empty() {
// This text node should be pushed either to the next ongoing
// inline level box with the parent style of that inline level box
// that will be ended, or directly to the ongoing inline formatting
// context with the parent style of that builder.
let inlines = self.current_inline_level_boxes();
// Skip any leading whitespace as dictated by the node's style.
let white_space = info.style.get_inherited_text().white_space;
let (preserved_leading_whitespace, mut input) =
self.handle_leading_whitespace(&input, white_space);
let mut new_text_run_contents;
let output;
if !preserved_leading_whitespace && input.is_empty() {
return;
}
{
let mut last_box = inlines.last_mut().map(|last| last.borrow_mut());
let last_text = last_box.as_mut().and_then(|last| match &mut **last {
InlineLevelBox::TextRun(last) => Some(&mut last.text),
_ => None,
});
// This text node should be pushed either to the next ongoing
// inline level box with the parent style of that inline level box
// that will be ended, or directly to the ongoing inline formatting
// context with the parent style of that builder.
let inlines = self.current_inline_level_boxes();
if let Some(text) = last_text {
// Append to the existing text run
new_text_run_contents = None;
output = text;
} else {
new_text_run_contents = Some(String::new());
output = new_text_run_contents.as_mut().unwrap();
}
let mut new_text_run_contents;
let output;
if leading_whitespace {
output.push(' ')
}
loop {
if let Some(i) = input.bytes().position(|b| b.is_ascii_whitespace()) {
{
let mut last_box = inlines.last_mut().map(|last| last.borrow_mut());
let last_text = last_box.as_mut().and_then(|last| match &mut **last {
InlineLevelBox::TextRun(last) => Some(&mut last.text),
_ => None,
});
if let Some(text) = last_text {
// Append to the existing text run
new_text_run_contents = None;
output = text;
} else {
new_text_run_contents = Some(String::new());
output = new_text_run_contents.as_mut().unwrap();
}
if preserved_leading_whitespace {
output.push(' ')
}
match (
white_space.preserve_spaces(),
white_space.preserve_newlines(),
) {
// All whitespace is significant, so we don't need to transform
// the input at all.
(true, true) => {
output.push_str(input);
},
// There are no cases in CSS where where need to preserve spaces
// but not newlines.
(true, false) => unreachable!(),
// Spaces are not significant, but newlines might be. We need
// to collapse non-significant whitespace as appropriate.
(false, preserve_newlines) => loop {
// If there are any spaces that need preserving, split the string
// that precedes them, collapse them into a single whitespace,
// then process the remainder of the string independently.
if let Some(i) = input
.bytes()
.position(|b| b.is_ascii_whitespace() && (!preserve_newlines || b != b'\n'))
{
let (non_whitespace, rest) = input.split_at(i);
output.push_str(non_whitespace);
output.push(' ');
if let Some(i) = rest.bytes().position(|b| !b.is_ascii_whitespace()) {
// Find the first byte that is either significant whitespace or
// non-whitespace to continue processing it.
if let Some(i) = rest.bytes().position(|b| {
!b.is_ascii_whitespace() || (preserve_newlines && b == b'\n')
}) {
input = &rest[i..];
} else {
break;
}
} else {
// No whitespace found, so no transformation is required.
output.push_str(input);
break;
}
}
},
}
}
if let Some(text) = new_text_run_contents {
inlines.push(ArcRefCell::new(InlineLevelBox::TextRun(TextRun {
tag: Tag::from_node_and_style_info(info),
parent_style: Arc::clone(&info.style),
text,
})))
}
if let Some(text) = new_text_run_contents {
inlines.push(ArcRefCell::new(InlineLevelBox::TextRun(TextRun {
tag: Tag::from_node_and_style_info(info),
parent_style: Arc::clone(&info.style),
text,
})))
}
}
}
@ -359,10 +396,14 @@ where
///
/// * Whether this text run has preserved (non-collapsible) leading whitespace
/// * The contents starting at the first non-whitespace character (or the empty string)
fn handle_leading_whitespace<'text>(&mut self, text: &'text str) -> (bool, &'text str) {
fn handle_leading_whitespace<'text>(
&mut self,
text: &'text str,
white_space: WhiteSpace,
) -> (bool, &'text str) {
// FIXME: this is only an approximation of
// https://drafts.csswg.org/css2/text.html#white-space-model
if !text.starts_with(|c: char| c.is_ascii_whitespace()) {
if !text.starts_with(|c: char| c.is_ascii_whitespace()) || white_space.preserve_spaces() {
return (false, text);
}

View file

@ -756,13 +756,21 @@ impl TextRun {
let mut glyphs = vec![];
let mut advance_width = Length::zero();
let mut last_break_opportunity = None;
let mut force_line_break = false;
// Fit as many glyphs within a single line as possible.
loop {
let next = runs.next();
// If there are no more text runs we still need to check if the last
// run was a forced line break
if next
.as_ref()
.map_or(true, |run| run.glyph_store.is_whitespace())
{
// If this run exceeds the bounds of the containing block, then
// we need to attempt to break the line.
if advance_width > ifc.containing_block.inline_size - ifc.inline_position {
// Reset the text run iterator to the last whitespace if possible,
// to attempt to re-layout the most recent glyphs on a new line.
if let Some((len, width, iter)) = last_break_opportunity.take() {
glyphs.truncate(len);
advance_width = width;
@ -774,10 +782,24 @@ impl TextRun {
if let Some(run) = next {
if run.glyph_store.is_whitespace() {
last_break_opportunity = Some((glyphs.len(), advance_width, runs.clone()));
// If this whitespace ends with a newline, we need to check if
// it's meaningful within the current style. If so, we force
// a line break immediately.
let last_byte = self.text.as_bytes().get(run.range.end().to_usize() - 1);
if last_byte == Some(&b'\n') &&
self.parent_style
.get_inherited_text()
.white_space
.preserve_newlines()
{
force_line_break = true;
break;
}
}
glyphs.push(run.glyph_store.clone());
advance_width += Length::from(run.glyph_store.total_advance());
} else {
// No more runs, so we can end the line.
break;
}
}
@ -812,7 +834,8 @@ impl TextRun {
glyphs,
text_decoration_line: ifc.current_nesting_level.text_decoration_line,
}));
if runs.as_slice().is_empty() {
// If this line is being broken because of a trailing newline, we can't ignore it.
if runs.as_slice().is_empty() && !force_line_break {
break;
} else {
// New line