Skip to content

Commit

Permalink
Revert "[editing] adjust selection to avoid select user-select:none
Browse files Browse the repository at this point in the history
… element when triple-click"

This reverts commit 31a47fc.

Reason for revert: Caused crbug.com/1410448

Original change's description:
> [editing] adjust selection to avoid select `user-select:none` element when triple-click
>
> Consider this testcase:
>   <div style="user-select: none;">
>     <div style="user-select: text;">line1</div>
>     <input type="text" value="input" />
>   </div>
>   <div>line2</div>
>
> renderered as:
> line1
> InputElement: input
> line2
>
> Before this patch, when triple-clicking at 1st line,
> 'line1' of 1st line and the input element will be selected.
>
> In this case, the extent value of the selection returned by
> ExpandWithGranularity is "DIV#two offset[0]".
> When `anchor_node` is TextControlElement,
> current situation is that the return value of
> IsVisuallyEquivalentCandidate depends on
> whether the parent node is selectable.
>
> `VisibleSelection` affect every thing, but `user-select`
> should affect only for user interaction, so we should
> shrink selection in `SelectionController`.
>
> This patch introduce new function `AdjustSelectionByUserSelect`.
> `AdjustSelectionByUserSelect` will do:
> 1. expand selection to to respect `user-select: all`
> 2. scan backward from `anchor_node` to the base of
>    `expanded_selection`,
>    when position is not selectable, shrink the seleciton.
> 3. scan forward from `anchor_node` to the extent of
>    `expanded_selection`,
>    when position is not selectable, shrink the seleciton.
>
> Bug: 1372847
>
> Test: SelectionControllerTest.AdjustSelectionByUserSelect
>
> Change-Id: Ic3d014c5b7334947b4185afdf3a81256df3f1b22
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3954728
> Reviewed-by: Xiaocheng Hu <xiaochengh@chromium.org>
> Commit-Queue: Xiaocheng Hu <xiaochengh@chromium.org>
> Cr-Commit-Position: refs/heads/main@{#1064183}

Bug: 1372847,1410448
Change-Id: I004b0ec38c099a81129ba7c2c34b03ee131b992f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4205687
Commit-Queue: Prudhvikumar Bommana <pbommana@google.com>
Reviewed-by: Srinivas Sista <srinivassista@chromium.org>
Owners-Override: Prudhvikumar Bommana <pbommana@google.com>
Cr-Commit-Position: refs/branch-heads/5569@{#3}
Cr-Branched-From: 48c4dbe-refs/heads/main@{#1098540}
  • Loading branch information
xiaochengh authored and Chromium LUCI CQ committed Jan 30, 2023
1 parent e114032 commit b2d1864
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 90 deletions.
66 changes: 3 additions & 63 deletions third_party/blink/renderer/core/editing/selection_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,6 @@ void MarkSelectionEndpointsForRepaint(const SelectionInFlatTree& selection) {
}
}

bool IsNonSelectable(const Node* node) {
LayoutObject* layout_object = node ? node->GetLayoutObject() : nullptr;
return !layout_object || !layout_object->IsSelectable();
}

} // namespace

SelectionInFlatTree AdjustSelectionWithTrailingWhitespace(
Expand All @@ -184,61 +179,6 @@ SelectionInFlatTree AdjustSelectionWithTrailingWhitespace(
.Build();
}

SelectionInFlatTree AdjustSelectionByUserSelect(
Node* anchor_node,
const SelectionInFlatTree& selection) {
DCHECK(anchor_node);

if (selection.IsNone())
return SelectionInFlatTree();

SelectionInFlatTree expanded_selection =
ExpandSelectionToRespectUserSelectAll(anchor_node, selection);

PositionInFlatTree base = expanded_selection.Base();
PositionInFlatTree new_start_pos =
PositionInFlatTree::FirstPositionInNode(*anchor_node);
for (PositionIteratorInFlatTree iter =
PositionIteratorInFlatTree(new_start_pos);
!iter.AtStart(); iter.Decrement()) {
PositionInFlatTree current_pos = iter.ComputePosition();
if (current_pos <= base) {
new_start_pos = base;
break;
}

if (IsNonSelectable(iter.GetNode())) {
new_start_pos = current_pos;
break;
}
}

PositionInFlatTree extent = expanded_selection.Extent();
PositionInFlatTree new_end_pos =
PositionInFlatTree::LastPositionInNode(*anchor_node);
for (PositionIteratorInFlatTree iter =
PositionIteratorInFlatTree(new_end_pos);
!iter.AtEnd(); iter.Increment()) {
PositionInFlatTree current_pos = iter.ComputePosition();
if (current_pos >= extent) {
new_end_pos = extent;
break;
}

if (IsNonSelectable(iter.GetNode())) {
new_end_pos = current_pos;
break;
}
}

return SelectionInFlatTree::Builder()
.Collapse(
MostBackwardCaretPosition(new_start_pos, kCannotCrossEditingBoundary))
.Extend(
MostForwardCaretPosition(new_end_pos, kCannotCrossEditingBoundary))
.Build();
}

SelectionController::~SelectionController() = default;

Document& SelectionController::GetDocument() const {
Expand Down Expand Up @@ -1039,14 +979,14 @@ bool SelectionController::HandleTripleClick(
SelectionInFlatTree::Builder().Collapse(pos).Build(),
TextGranularity::kParagraph)
: SelectionInFlatTree();
const SelectionInFlatTree adjusted_selection =
AdjustSelectionByUserSelect(inner_node, new_selection);

const bool is_handle_visible =
event.Event().FromTouch() && new_selection.IsRange();

const bool did_select = UpdateSelectionForMouseDownDispatchingSelectStart(
inner_node, adjusted_selection,
inner_node,
ExpandSelectionToRespectUserSelectAll(inner_node, new_selection),

SetSelectionOptions::Builder()
.SetGranularity(TextGranularity::kParagraph)
.SetShouldShowHandle(is_handle_visible)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,6 @@ bool IsSelectionOverLink(const MouseEventWithHitTestResults&);
bool IsExtendingSelection(const MouseEventWithHitTestResults&);
CORE_EXPORT SelectionInFlatTree
AdjustSelectionWithTrailingWhitespace(const SelectionInFlatTree&);
CORE_EXPORT SelectionInFlatTree
AdjustSelectionByUserSelect(Node*, const SelectionInFlatTree&);

} // namespace blink

#endif // THIRD_PARTY_BLINK_RENDERER_CORE_EDITING_SELECTION_CONTROLLER_H_
Original file line number Diff line number Diff line change
Expand Up @@ -520,28 +520,4 @@ TEST_F(SelectionControllerTest, Scroll) {
EXPECT_EQ(line8_node->textContent(), "x");
}

// http://crbug.com/1372847
TEST_F(SelectionControllerTest, AdjustSelectionByUserSelect) {
SetBodyContent(R"HTML(
<div style="user-select: none;">
<div id="one" style="user-select: text;">11</div>
<input type="text" value="input"/>
</div>
<div id="two">22</div>)HTML");

Element* one = GetDocument().getElementById("one");
Element* input = GetDocument().QuerySelector("input");

const SelectionInFlatTree& selection =
ExpandWithGranularity(SelectionInFlatTree::Builder()
.Collapse(PositionInFlatTree(one, 0))
.Build(),
TextGranularity::kParagraph);
SelectionInFlatTree adjust_selection =
AdjustSelectionByUserSelect(one, selection);
EXPECT_EQ(adjust_selection.Base(),
PositionInFlatTree::FirstPositionInNode(*one));
EXPECT_EQ(adjust_selection.Extent(), PositionInFlatTree::BeforeNode(*input));
}

} // namespace blink

0 comments on commit b2d1864

Please sign in to comment.