Bug 263302
| Summary: | [iOS] range.toString() ignores newlines | ||
|---|---|---|---|
| Product: | WebKit | Reporter: | Morgan Redding <morganfredding> |
| Component: | HTML Editing | Assignee: | Nobody <webkit-unassigned> |
| Status: | NEW | ||
| Severity: | Normal | CC: | karlcow, megan_gardner, rniwa, webkit-bug-importer, wenson_hsieh |
| Priority: | P2 | Keywords: | BrowserCompat, GoodFirstBug, InRadar |
| Version: | Other | ||
| Hardware: | iPhone / iPad | ||
| OS: | iOS 17 | ||
Morgan Redding
When a range includes newlines, range.toString() does not include them.
This bug occurs 100% of the time and does *not* occur on Macbook Safari (or Chrome).
Also "window.getSelection().toString()" *does* include newlines (i.e. there's probably already a bug-free implementation you can use in the webkit code base).
Bug probably occurs on all iPhones/iPads, but I've only specifically reproduced it in the simulator for "iPhone 15 Pro Max" and "iPhone 14 Pro Max".
navigator.userAgent: "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148"
Repro steps:
```
myDiv.innerHTML = '<div>a</div><div>b</div><br><div>c</div>';
let range = document.createRange();
range.setStartBefore(myDiv);
range.setEndAfter(myDiv);
console.log(range.toString());
```
Expected:
"a
b
c"
Actual:
"abc"
| Attachments | ||
|---|---|---|
| Add attachment proposed patch, testcase, etc. |
Morgan Redding
Probably something like this should work (I cannot seem to run the tests on my machine):
1) add `#include "../editing/TextIterator.h"` to "Source/WebCore/dom/Range.cpp"
2) change "Range::toString() const" to
```
String Range::toString() const
{
auto range = makeSimpleRange(*this);
return plainText(range, options);
}
```
For your convenience, here is the output of `git diff`:
```
diff --git a/Source/WebCore/dom/Range.cpp b/Source/WebCore/dom/Range.cpp
index 5ea2924c3ad3..890888b9022b 100644
--- a/Source/WebCore/dom/Range.cpp
+++ b/Source/WebCore/dom/Range.cpp
@@ -25,6 +25,7 @@
#include "config.h"
#include "Range.h"
+#include "../editing/TextIterator.h"
#include "Comment.h"
#include "CustomElementReactionQueue.h"
#include "DOMRect.h"
@@ -720,14 +721,7 @@ ExceptionOr<void> Range::insertNode(Ref<Node>&& node)
String Range::toString() const
{
auto range = makeSimpleRange(*this);
- StringBuilder builder;
- for (auto& node : intersectingNodes(range)) {
- if (is<Text>(node)) {
- auto offsetRange = characterDataOffsetRange(range, node);
- builder.appendSubstring(downcast<Text>(node).data(), offsetRange.start, offsetRange.end - offsetRange.start);
- }
- }
- return builder.toString();
+ return plainText(range, options);
}
```
Morgan Redding
Oops, forgot to add
```
OptionSet<TextIteratorBehavior> options;
```
in the diff
Radar WebKit Bug Importer
<rdar://problem/117453464>
Karl Dubost
Morgan,
do you intend to send a PR?
Or would you like someone else to do it?
Morgan Redding
Oh, sorry for the ambiguity. I’ve never submitted a PR for WebKit and can’t seem to get the tests running so I’d really appreciate if someone else could submit a PR!