Commit 3f46a79e authored by Yoshifumi Inoue's avatar Yoshifumi Inoue Committed by Commit Bot

Make FrameSelection::Selected{Text,HTML}ForClipboard() not to use VisibleSelection

This patch changes |FrameSelection::Selected{Text,HTML}ForClipboard()| not to
use |VisibleSelection| to avoid editing boundary adjustment. Because of
selection editing boundary adjustment should be applied for selection set by
scripts.

This patch is a follow-up of CL[1] which does editing boundary adjustment
correctly.

Note: This patch changes |selection_test()| to access test window to use
selection in test window in <iframe>.


[1] http://crrev.com/c/1102157 Avoid crossing editing boundaries selection.

Bug: 928726
Change-Id: I7b9716679764f2b8821289c202e8c2c56d94ce06
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1647651
Commit-Queue: Yoshifumi Inoue <yosin@chromium.org>
Auto-Submit: Yoshifumi Inoue <yosin@chromium.org>
Reviewed-by: default avatarYoichi Osato <yoichio@chromium.org>
Cr-Commit-Position: refs/heads/master@{#669171}
parent 84e53e8a
...@@ -918,22 +918,31 @@ void FrameSelection::SetFocusedNodeIfNeeded() { ...@@ -918,22 +918,31 @@ void FrameSelection::SetFocusedNodeIfNeeded() {
} }
} }
static EphemeralRangeInFlatTree ComputeRangeForSerialization(
const SelectionInDOMTree& selection) {
const EphemeralRangeInFlatTree& range =
ConvertToSelectionInFlatTree(selection).ComputeRange();
const PositionInFlatTree& start =
CreateVisiblePosition(range.StartPosition()).DeepEquivalent();
const PositionInFlatTree& end =
CreateVisiblePosition(range.EndPosition()).DeepEquivalent();
if (start.IsNull() || end.IsNull() || start >= end)
return EphemeralRangeInFlatTree();
return NormalizeRange(EphemeralRangeInFlatTree(start, end));
}
static String ExtractSelectedText(const FrameSelection& selection, static String ExtractSelectedText(const FrameSelection& selection,
TextIteratorBehavior behavior) { TextIteratorBehavior behavior) {
const VisibleSelectionInFlatTree& visible_selection =
selection.ComputeVisibleSelectionInFlatTree();
const EphemeralRangeInFlatTree& range = const EphemeralRangeInFlatTree& range =
visible_selection.ToNormalizedEphemeralRange(); ComputeRangeForSerialization(selection.GetSelectionInDOMTree());
// We remove '\0' characters because they are not visibly rendered to the // We remove '\0' characters because they are not visibly rendered to the
// user. // user.
return PlainText(range, behavior).Replace(0, ""); return PlainText(range, behavior).Replace(0, "");
} }
String FrameSelection::SelectedHTMLForClipboard() const { String FrameSelection::SelectedHTMLForClipboard() const {
const VisibleSelectionInFlatTree& visible_selection =
ComputeVisibleSelectionInFlatTree();
const EphemeralRangeInFlatTree& range = const EphemeralRangeInFlatTree& range =
visible_selection.ToNormalizedEphemeralRange(); ComputeRangeForSerialization(GetSelectionInDOMTree());
return CreateMarkup( return CreateMarkup(
range.StartPosition(), range.EndPosition(), kAnnotateForInterchange, range.StartPosition(), range.EndPosition(), kAnnotateForInterchange,
ConvertBlocksToInlines::kNotConvert, kResolveNonLocalURLs); ConvertBlocksToInlines::kNotConvert, kResolveNonLocalURLs);
......
...@@ -754,6 +754,7 @@ class Sample { ...@@ -754,6 +754,7 @@ class Sample {
this.selection_.computeRight = computeRight; this.selection_.computeRight = computeRight;
this.selection_.computeTop = computeTop; this.selection_.computeTop = computeTop;
this.selection_.computeBottom = computeBottom; this.selection_.computeBottom = computeBottom;
this.selection_.window = this.iframe_.contentWindow;
this.load(sampleText); this.load(sampleText);
} }
......
<!doctype html>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script src="../assert_selection.js"></script>
<script>
selection_test(
'<div contenteditable>^foo</div>bar|',
selection => {
assert_own_property(window, 'internals',
'This test requires window.internals');
assert_equals(selection.window.internals.selectedTextForClipboard(), 'foo\nbar');
assert_equals(selection.window.internals.selectedHTMLForClipboard()
.replace(/ style=".+?"/g, ' style="..."'),
'<div contenteditable="" style="...">foo</div><span style="...">bar</span>');
},
'<div contenteditable>^foo</div>bar|',
'Selection by script should not be adjusted.');
</script>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment