innerText-spec icon indicating copy to clipboard operation
innerText-spec copied to clipboard

suggested tweak for explicit newline

Open PerBothner opened this issue 9 years ago • 3 comments
trafficstars

As rendered, an explicit newline at the end of a block collapses with a "required newline". Consider

<div>line1<span>&#xA;</span></div><div>line2</div>

This is rendered (in my experience) with no blank line in between.

If you want to model this, perhaps tweak the specification a bit. For example replace item 5 by:

For each remaining run of consecutive "required line break count" items calculate the maximum of the values in the "required line break count" items. Subtract one if the previous item is a string that ends in a newline. Replace the run with a string consisting of that many newline characters.

or by:

Replace each remaining run of consecutive "required line break count" items with a string consisting of as many newline characters as the maximum of the values in the "required line break count" items, subtracted by one if the previous item is a string that ends in a newline.

PerBothner avatar Jan 23 '16 00:01 PerBothner

What do browsers do for innerText in this example?

I'd suggest not adding any more complexity to innerText that isn't required for Web compatibility. There are endless tweaks that one could make to improve its fidelity to rendered text, but they're probably not worth making.

rocallahan avatar Aug 16 '16 13:08 rocallahan

There were some problems with my example - using <pre> (or <br> instead of &#xa;) would be better. Consider:

<pre>line1<br></pre><pre>line2</pre>

The innerText yields "line1\nline2" whether or not you include the <br>. Generally, if here are N <br> or &#xA; in a row at the end of a <pre> you get max(1,N) (equivalently 1+max(0,N-1)) newlines.

If you replace the <pre> by <p> you get 2+max(0,N-1) newlines.

So my suggestion seems to match browser behavior - at least better than the existing text.

PerBothner avatar Aug 16 '16 16:08 PerBothner

<br> in <div> also produces a newline even if rendered HTML doesn't have it.

<div>line1<br></div>line2

yields line1\n\nline2.

I also suggest that Element#innerText should not emit newline for <br> just before end tag of block element, in this case </div>.

See https://jsfiddle.net/3f504ery/

yosinch avatar Sep 25 '18 07:09 yosinch