Bug 240566 - innerText from element with "white-space: pre-line" incorrectly removes line breaks
Summary: innerText from element with "white-space: pre-line" incorrectly removes line ...
Status: NEW
Alias: None
Product: WebKit
Classification: Unclassified
Component: DOM (show other bugs)
Version: Safari 15
Hardware: All Unspecified
: P2 Normal
Assignee: Chris Dumez
URL:
Keywords: BrowserCompat, InRadar, WPTImpact
Depends on:
Blocks:
 
Reported: 2022-05-18 01:41 PDT by ik
Modified: 2023-05-09 13:02 PDT (History)
3 users (show)

See Also:


Attachments
Testcase v101 (1.20 KB, application/x-zip-compressed)
2022-05-18 01:41 PDT, ik
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description ik 2022-05-18 01:41:29 PDT
Created attachment 459530 [details]
Testcase v101

When I retrieve the innerText value of an element that has "white-space: pre-line;", the resulting string does not contain any line breaks (\n).

The following changes/variations do preserve line breaks when retrieving :

* Retrieving textContent instead of innerText
* Using "pre-wrap"
* A combination of the above

... but these have side-effects, as it causes slightly different behavior.

According to MDN, newlines should be preserved for both pre-line and pre-wrap:
https://developer.mozilla.org/en-US/docs/Web/CSS/white-space#values

See attached testcase, also available here:
https://testcase.rejh.nl/webkit-pre-line-innertext/

* The first div has "white-space: pre-line"
* The second div has "pre-wrap"
* Use the buttons to print innerText or textContent to the console

Note: I've tested this in Safari, Chrome and Firefox. Only Chrome returns the same result for innerText and textContent when "pre-line" is set, preserving line breaks. Firefox seems to also strip the newline characters when retrieving innerText while preserving them for textContent. If the intended/spec'd behavior of innerText is to remove line breaks for "pre-line", I'll happily file a bug @ Chrome :)

Thanks!
Comment 1 Sam Sneddon [:gsnedders] 2022-05-18 10:06:43 PDT
https://html.spec.whatwg.org/multipage/dom.html#rendered-text-collection-steps says:

> If node is a Text node, then for each CSS text box produced by node, in content order, compute the text of the box after application of the CSS 'white-space' processing rules and 'text-transform' rules, set items to the list of the resulting strings, and return items. The CSS 'white-space' processing rules are slightly modified: collapsible spaces at the end of lines are always collapsed, but they are only removed if the line is the last line of the block, or it ends with a br element. Soft hyphens should be preserved. [CSSTEXT]

This implies that pre-line should be preserve the new lines.

Not looked closely at Firefox, but it seems Safari is the only browser failing the '\n preserved ("<div style='white-space:pre-line'>abc\ndef")' subtest at https://wpt.fyi/results/html/dom/elements/the-innertext-and-outertext-properties/getter.html?label=master&label=experimental&product=chrome&product=firefox&product=safari&aligned
Comment 2 Chris Dumez 2022-05-18 11:41:56 PDT
Pull request: https://github.com/WebKit/WebKit/pull/738
Comment 3 Radar WebKit Bug Importer 2022-05-25 01:42:14 PDT
<rdar://problem/93887979>
Comment 4 ik 2022-12-14 12:21:49 PST
Any news on this? A pull request was filed but nothing since?