innerText-spec
innerText-spec copied to clipboard
suggested tweak for explicit newline
As rendered, an explicit newline at the end of a block collapses with a "required newline". Consider
<div>line1<span>
</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.
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.
There were some problems with my example - using <pre> (or <br> instead of 
) 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 
 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.
<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/