Bug 263302 - [iOS] range.toString() ignores newlines
Summary: [iOS] range.toString() ignores newlines
Status: NEW
Alias: None
Product: WebKit
Classification: Unclassified
Component: HTML Editing (show other bugs)
Version: Other
Hardware: iPhone / iPad iOS 17
: P2 Normal
Assignee: Nobody
URL:
Keywords: BrowserCompat, GoodFirstBug, InRadar
Depends on:
Blocks:
 
Reported: 2023-10-17 22:03 PDT by Morgan Redding
Modified: 2023-11-06 18:14 PST (History)
5 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Morgan Redding 2023-10-17 22:03:08 PDT
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"
Comment 1 Morgan Redding 2023-10-17 23:56:43 PDT
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);
 }
```
Comment 2 Morgan Redding 2023-10-18 10:54:04 PDT
Oops, forgot to add

```
OptionSet<TextIteratorBehavior> options;
```

in the diff
Comment 3 Radar WebKit Bug Importer 2023-10-24 22:04:18 PDT
<rdar://problem/117453464>
Comment 4 Karl Dubost 2023-11-06 18:12:07 PST
Morgan,

do you intend to send a PR?
Or would you like someone else to do it?
Comment 5 Morgan Redding 2023-11-06 18:14:40 PST
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!