Skip to content

script: Stringify selections according to the flat tree / user-select - #48242

Merged
mrobinson merged 1 commit into
servo:mainfrom
mrobinson:selection-flat-tree-stringification
Sep 22, 2026
Merged

mrobinson merged 1 commit into
servo:mainfrom
mrobinson:selection-flat-tree-stringification

Conversation

@mrobinson

@mrobinson mrobinson commented Sep 21, 2026 •

Copy link
Copy Markdown
Member

This change makes it so that Servo produces selection text according to
the flat tree and respecting the box tree and user-select values.
There are still some issues around white space handling at the end of
lines and also block elements are no inserting a newline as they should,
but this is an improvement.

Testing: This causes a WPT subtest to pass (more waiting on improvements
to handling whitespace trimming in the rendered text query). In addition,
since stringification behavior is not well specified, two Servo-specific
tests are added for flat tree / display: none behavior.

Fixes: #48242

@servo-highfive servo-highfive added the S-awaiting-review There is new code that needs to be reviewed. label Sep 21, 2026
Comment thread components/layout/flow/inline/mod.rs Outdated
}

pub(crate) fn text_content(&self) -> &str {
self.text_content.get().unwrap_or_default()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

untested fix for the compile error:

Suggested change
self.text_content.get().unwrap_or_default()
self.text_content.get().as_deref().unwrap_or_default()


/// A slot used to share the full text string of the [`InlineFormattingContext`] this
/// builder will ultimately build.
pub text_content_slot: Arc<OnceLock<String>>,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s an Arc<OnceLock<_>> because it’s shared with text fragments created before the full string is available, right?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that's exactly right.

Comment thread components/layout/dom.rs
(range_in_text_run.start + start_in_ifc).to_utf8_code_units_in(AssumeUnder4GB, string);
let end =
(range_in_text_run.end + start_in_ifc).to_utf8_code_units_in(AssumeUnder4GB, string);
Some(string[start.0 as usize..end.0 as usize].to_owned())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn’t we add a helper for this? (slicing &str with a Range<Utf8CodeUnits>) Or do I only wish we did?

At some point I tried adding an Index impl but the orphan rule didn’t allow it. (std::ops::Range<Utf8CodeUnits> is not considered "local" even if Utf8CodeUnits is.) So I’m not sure where a good place for it would be. An associated function on Utf8CodeUnits sounds verbose

So unless we come up with something good I’m ok with leaving this as-is

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took another look and couldn't find a helper unfortunately.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I didn’t just imagine it! Utf8CodeUnits::to_usize_range(&Range<Self>) -> Range<usize> added in #47742

Though it’s almost as verbose as not using it, so I don’t mind if we don’t use it

Comment on lines +1406 to +1407
// Flush all layout before stringifying so that rendered text is up-to-date.
self.document.window().layout_reflow(QueryMsg::StyleQuery);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does StyleQuery include box construction?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, at least for the moment StyleQuery does a box / fragment tree rebuild. Once we split style and layout into two passes this probably won't be the case any longer.

Comment on lines +121 to +126
pub(crate) fn rendered_text(&self, range: RangeAny<Utf32CodeUnits>) -> Option<String> {
self.upcast::<Node>()
.layout_data()
.borrow()
.as_ref()
.and_then(|layout_data| layout_data.rendered_text(range))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess not having rendered text for display: none comes indirectly from not having layout_data?

Is layout_data actively removed if display used to be something else and later changed to none?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, setting a box have have display: none should clear its layout data and clear the style and layout data of all descendants.

@servo-highfive servo-highfive removed the S-awaiting-review There is new code that needs to be reviewed. label Sep 21, 2026

@mrobinson mrobinson left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the review!


/// A slot used to share the full text string of the [`InlineFormattingContext`] this
/// builder will ultimately build.
pub text_content_slot: Arc<OnceLock<String>>,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that's exactly right.

Comment thread components/layout/dom.rs
(range_in_text_run.start + start_in_ifc).to_utf8_code_units_in(AssumeUnder4GB, string);
let end =
(range_in_text_run.end + start_in_ifc).to_utf8_code_units_in(AssumeUnder4GB, string);
Some(string[start.0 as usize..end.0 as usize].to_owned())

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took another look and couldn't find a helper unfortunately.

Comment on lines +121 to +126
pub(crate) fn rendered_text(&self, range: RangeAny<Utf32CodeUnits>) -> Option<String> {
self.upcast::<Node>()
.layout_data()
.borrow()
.as_ref()
.and_then(|layout_data| layout_data.rendered_text(range))

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, setting a box have have display: none should clear its layout data and clear the style and layout data of all descendants.

Comment on lines +1406 to +1407
// Flush all layout before stringifying so that rendered text is up-to-date.
self.document.window().layout_reflow(QueryMsg::StyleQuery);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, at least for the moment StyleQuery does a box / fragment tree rebuild. Once we split style and layout into two passes this probably won't be the case any longer.

@mrobinson
mrobinson force-pushed the selection-flat-tree-stringification branch from c0a5c82 to 5129932 Compare September 22, 2026 08:48
@servo-highfive servo-highfive added the S-awaiting-review There is new code that needs to be reviewed. label Sep 22, 2026
@mrobinson
mrobinson enabled auto-merge September 22, 2026 08:49
@mrobinson mrobinson mentioned this pull request Sep 22, 2026
25 of 40 tasks
@mrobinson
mrobinson added this pull request to the merge queue Sep 22, 2026
@servo-highfive servo-highfive added the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Sep 22, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Sep 22, 2026
@servo-highfive servo-highfive added S-tests-failed The changes caused existing tests to fail. and removed S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. labels Sep 22, 2026
This change makes it so that Servo produces selection text according to
the flat tree and respecting the box tree and `user-select` values.
There are still some issues around white space handling at the end of
lines and also block elements are no inserting a newline as they should,
but this is an improvement.

Signed-off-by: Martin Robinson <[email protected]>
@mrobinson
mrobinson force-pushed the selection-flat-tree-stringification branch from 5129932 to adc49ea Compare September 22, 2026 14:42
@servo-highfive servo-highfive removed the S-tests-failed The changes caused existing tests to fail. label Sep 22, 2026
@mrobinson
mrobinson enabled auto-merge September 22, 2026 14:42
@mrobinson
mrobinson added this pull request to the merge queue Sep 22, 2026
@servo-highfive servo-highfive added the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Sep 22, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Sep 22, 2026
@servo-highfive servo-highfive added S-tests-failed The changes caused existing tests to fail. and removed S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. labels Sep 22, 2026
@mrobinson
mrobinson added this pull request to the merge queue Sep 22, 2026
@servo-highfive servo-highfive added S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. and removed S-tests-failed The changes caused existing tests to fail. labels Sep 22, 2026
Merged via the queue into servo:main with commit 19faf5a Sep 22, 2026
39 checks passed
@mrobinson
mrobinson deleted the selection-flat-tree-stringification branch September 22, 2026 23:05
@servo-highfive servo-highfive removed the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Sep 22, 2026
RichardTjokroutomo pushed a commit to RichardTjokroutomo/servo that referenced this pull request Oct 6, 2026
…t` (servo#48242)

This change makes it so that Servo produces selection text according to
the flat tree and respecting the box tree and `user-select` values.
There are still some issues around white space handling at the end of
lines and also block elements are no inserting a newline as they should,
but this is an improvement.

Testing: This causes a WPT subtest to pass (more waiting on improvements
to handling whitespace trimming in the rendered text query). In
addition,
since stringification behavior is not well specified, two Servo-specific
tests are added for flat tree / `display: none` behavior.

Fixes: servo#48242

Signed-off-by: Martin Robinson <[email protected]>
hhhwsqhhh pushed a commit to hhhwsqhhh/servo that referenced this pull request Oct 12, 2026
…t` (servo#48242)

This change makes it so that Servo produces selection text according to
the flat tree and respecting the box tree and `user-select` values.
There are still some issues around white space handling at the end of
lines and also block elements are no inserting a newline as they should,
but this is an improvement.

Testing: This causes a WPT subtest to pass (more waiting on improvements
to handling whitespace trimming in the rendered text query). In
addition,
since stringification behavior is not well specified, two Servo-specific
tests are added for flat tree / `display: none` behavior.

Fixes: servo#48242

Signed-off-by: Martin Robinson <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-awaiting-review There is new code that needs to be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants