-
-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add failing test case for new-lines #3
Conversation
This comment has been minimized.
This comment has been minimized.
ah, it makes more sense to add it to the dom, which does use the space: const $div = document.createElement('div')
const $span = document.createElement('span')
$div.appendChild(document.createTextNode('alpha\n'))
$div.appendChild($span)
$span.appendChild(document.createTextNode('bravo'))
document.body.innerHTML = ''
document.body.appendChild($div)
document.body.innerText
|
I could have also added a fiddle, for completeness sake, here is one: https://jsfiddle.net/L5y31gxc/ (though I see you also reproduced it already) |
This comment has been minimized.
This comment has been minimized.
Hmm, I looked into it for a while. Here are some more tests: + t.equal(
+ toText(h('p', ['A\n', h('span', 'b')])),
+ 'A b',
+ 'should support line endings around element breaks (1)'
+ )
+
+ t.equal(
+ toText(h('p', ['A\nb', h('span', 'c')])),
+ 'A bc',
+ 'should support line endings around element breaks (2)'
+ )
+
+ t.equal(
+ toText(h('p', ['A', h('span', '\nb')])),
+ 'A b',
+ 'should support line endings around element breaks (3)'
+ )
+
+ t.equal(
+ toText(h('p', ['A\n', h('span', '\nb')])),
+ 'A b',
+ 'should support line endings around element breaks (4)'
+ )
+
+ t.equal(
+ toText(h('p', [h('span', 'A\n'), h('span', 'b')])),
+ 'A b',
+ 'should support line endings around element breaks (5)'
+ )
+
+ t.equal(
+ toText(h('p', [h('span', 'A'), h('span', '\nb')])),
+ 'A b',
+ 'should support line endings around element breaks (6)'
+ )
+
+ t.equal(
+ toText(h('p', [h('span', 'A\n'), h('span', '\nb')])),
+ 'A b',
+ 'should support line endings around element breaks (7)'
+ )
+
+ t.equal(
+ toText(h('p', [h('span', 'A\n'), 'b'])),
+ 'A b',
+ 'should support line endings around element breaks (8)'
+ )
+
+ t.equal(
+ toText(h('p', [h('span', 'A'), '\nb'])),
+ 'A b',
+ 'should support line endings around element breaks (9)'
+ )
+
+ t.equal(
+ toText(h('p', [h('span', 'A\n'), '\nb'])),
+ 'A b',
+ 'should support line endings around element breaks (10)'
+ )
+
+ t.equal(
+ toText(h('div', [h('p', [h('span', 'A\n'), '\nb'])])),
+ 'A b',
+ 'should support line endings around element breaks (11)'
+ )
+ For a solution, there are two parts. The first loop in - while (start < value.length) {
+ while (start <= value.length) {
searchLineFeeds.lastIndex = start
match = searchLineFeeds.exec(value)
// @ts-expect-error: `index` is set.
end = match ? match.index : value.length
-
- lines.push(
- // Any sequence of collapsible spaces and tabs immediately preceding or
- // following a segment break is removed.
- trimAndCollapseSpacesAndTabs(
- // [...] ignoring bidi formatting characters (characters with the
- // Bidi_Control property [UAX9]: ALM, LTR, RTL, LRE-RLO, LRI-PDI) as if
- // they were not there.
- value
- .slice(start, end)
- .replace(/[\u061C\u200E\u200F\u202A-\u202E\u2066-\u2069]/g, ''),
- options.breakBefore,
- options.breakAfter
- )
- )
-
+ lines.push(value.slice(start, end))
start = end + 1
}
+ lines = lines.map((line, i) =>
+ // Any sequence of collapsible spaces and tabs immediately preceding or
+ // following a segment break is removed.
+ trimAndCollapseSpacesAndTabs(
+ // […] ignoring bidi formatting characters (characters with the
+ // Bidi_Control property [UAX9]: ALM, LTR, RTL, LRE-RLO, LRI-PDI) as if
+ // they were not there.
+ line.replace(/[\u061C\u200E\u200F\u202A-\u202E\u2066-\u2069]/g, ''),
+ i === 0 ? options.breakBefore : true,
+ i === lines.length - 1 ? options.breakAfter : true
+ )
+ )
+ Second, is add a space at the start/end, if a line ending is found, and there is no break before/after. |
This comment has been minimized.
This comment has been minimized.
Found it! :) |
Initial checklist
Description of changes
Hi there,
thanks for putting all the work into this. I found a case where this lib diverges from
innerText
and added a test case for it. If you agree that it should behave differently, I'd be happy to also work on a fix.