WebKit Bugzilla
New
Browse
Search+
Log In
×
Sign in with GitHub
or
Remember my login
Create Account
·
Forgot Password
Forgotten password account recovery
NEW
263302
[iOS] range.toString() ignores newlines
https://bugs.webkit.org/show_bug.cgi?id=263302
Summary
[iOS] range.toString() ignores newlines
Morgan Redding
Reported
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"
Attachments
Add attachment
proposed patch, testcase, etc.
Morgan Redding
Comment 1
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); } ```
Morgan Redding
Comment 2
2023-10-18 10:54:04 PDT
Oops, forgot to add ``` OptionSet<TextIteratorBehavior> options; ``` in the diff
Radar WebKit Bug Importer
Comment 3
2023-10-24 22:04:18 PDT
<
rdar://problem/117453464
>
Karl Dubost
Comment 4
2023-11-06 18:12:07 PST
Morgan, do you intend to send a PR? Or would you like someone else to do it?
Morgan Redding
Comment 5
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!
Note
You need to
log in
before you can comment on or make changes to this bug.
Top of Page
Format For Printing
XML
Clone This Bug