Commit 5eeed90f authored by tanvir.rizvi's avatar tanvir.rizvi Committed by Commit Bot

Make ApplyStyleCommand::UpdateStartEnd to take EphemeralRange

UpdateStartEnd takes start and end position as parameters,
and then does a DCHECK_LE for start and end.
EphemeralRange constructor also does same DCHECK.
Since UpdateStartEnd is called from many places,
so to catch a bug at the call site, function
UpdateStartEnd is made to take EphemeralRange
instead.

Bug: 826106
Change-Id: I6e1cd23e9aac2846a2f083cd6f4d8110fa3a9085
Reviewed-on: https://chromium-review.googlesource.com/989852Reviewed-by: default avatarYoshifumi Inoue <yosin@chromium.org>
Commit-Queue: Tanvir Rizvi <tanvir.rizvi@samsung.com>
Cr-Commit-Position: refs/heads/master@{#547668}
parent 831eb55c
...@@ -176,16 +176,13 @@ ApplyStyleCommand::ApplyStyleCommand( ...@@ -176,16 +176,13 @@ ApplyStyleCommand::ApplyStyleCommand(
is_inline_element_to_remove_function_( is_inline_element_to_remove_function_(
is_inline_element_to_remove_function) {} is_inline_element_to_remove_function) {}
void ApplyStyleCommand::UpdateStartEnd(const Position& new_start, void ApplyStyleCommand::UpdateStartEnd(const EphemeralRange& range) {
const Position& new_end) { if (!use_ending_selection_ &&
DCHECK_GE(new_end, new_start); (range.StartPosition() != start_ || range.EndPosition() != end_))
if (!use_ending_selection_ && (new_start != start_ || new_end != end_))
use_ending_selection_ = true; use_ending_selection_ = true;
GetDocument().UpdateStyleAndLayoutIgnorePendingStylesheets(); GetDocument().UpdateStyleAndLayoutIgnorePendingStylesheets();
const bool was_base_first = const bool was_base_first =
StartingSelection().IsBaseFirst() || !SelectionIsDirectional(); StartingSelection().IsBaseFirst() || !SelectionIsDirectional();
const EphemeralRange range(new_start, new_end);
SelectionInDOMTree::Builder builder; SelectionInDOMTree::Builder builder;
if (was_base_first) if (was_base_first)
builder.SetAsForwardSelection(range); builder.SetAsForwardSelection(range);
...@@ -195,8 +192,8 @@ void ApplyStyleCommand::UpdateStartEnd(const Position& new_start, ...@@ -195,8 +192,8 @@ void ApplyStyleCommand::UpdateStartEnd(const Position& new_start,
CreateVisibleSelection(builder.Build()); CreateVisibleSelection(builder.Build());
SetEndingSelection( SetEndingSelection(
SelectionForUndoStep::From(visible_selection.AsSelection())); SelectionForUndoStep::From(visible_selection.AsSelection()));
start_ = new_start; start_ = range.StartPosition();
end_ = new_end; end_ = range.EndPosition();
} }
Position ApplyStyleCommand::StartPosition() { Position ApplyStyleCommand::StartPosition() {
...@@ -369,8 +366,8 @@ void ApplyStyleCommand::ApplyBlockStyle(EditingStyle* style, ...@@ -369,8 +366,8 @@ void ApplyStyleCommand::ApplyBlockStyle(EditingStyle* style,
PlainTextRange(end_index).CreateRangeForSelection(ToContainerNode(scope)); PlainTextRange(end_index).CreateRangeForSelection(ToContainerNode(scope));
if (end_ephemeral_range.IsNull()) if (end_ephemeral_range.IsNull())
return; return;
UpdateStartEnd(start_ephemeral_range.StartPosition(), UpdateStartEnd(EphemeralRange(start_ephemeral_range.StartPosition(),
end_ephemeral_range.StartPosition()); end_ephemeral_range.StartPosition()));
} }
static MutableCSSPropertyValueSet* CopyStyleOrCreateEmpty( static MutableCSSPropertyValueSet* CopyStyleOrCreateEmpty(
...@@ -1549,7 +1546,7 @@ void ApplyStyleCommand::RemoveInlineStyle(EditingStyle* style, ...@@ -1549,7 +1546,7 @@ void ApplyStyleCommand::RemoveInlineStyle(EditingStyle* style,
node = next; node = next;
} }
UpdateStartEnd(s, e); UpdateStartEnd(EphemeralRange(s, e));
} }
bool ApplyStyleCommand::ElementFullySelected(const HTMLElement& element, bool ApplyStyleCommand::ElementFullySelected(const HTMLElement& element,
...@@ -1580,7 +1577,7 @@ void ApplyStyleCommand::SplitTextAtStart(const Position& start, ...@@ -1580,7 +1577,7 @@ void ApplyStyleCommand::SplitTextAtStart(const Position& start,
Text* text = ToText(start.ComputeContainerNode()); Text* text = ToText(start.ComputeContainerNode());
SplitTextNode(text, start.OffsetInContainerNode()); SplitTextNode(text, start.OffsetInContainerNode());
UpdateStartEnd(Position::FirstPositionInNode(*text), new_end); UpdateStartEnd(EphemeralRange(Position::FirstPositionInNode(*text), new_end));
} }
void ApplyStyleCommand::SplitTextAtEnd(const Position& start, void ApplyStyleCommand::SplitTextAtEnd(const Position& start,
...@@ -1601,7 +1598,8 @@ void ApplyStyleCommand::SplitTextAtEnd(const Position& start, ...@@ -1601,7 +1598,8 @@ void ApplyStyleCommand::SplitTextAtEnd(const Position& start,
should_update_start should_update_start
? Position(ToText(prev_node), start.OffsetInContainerNode()) ? Position(ToText(prev_node), start.OffsetInContainerNode())
: start; : start;
UpdateStartEnd(new_start, Position::LastPositionInNode(*prev_node)); UpdateStartEnd(
EphemeralRange(new_start, Position::LastPositionInNode(*prev_node)));
} }
void ApplyStyleCommand::SplitTextElementAtStart(const Position& start, void ApplyStyleCommand::SplitTextElementAtStart(const Position& start,
...@@ -1618,7 +1616,8 @@ void ApplyStyleCommand::SplitTextElementAtStart(const Position& start, ...@@ -1618,7 +1616,8 @@ void ApplyStyleCommand::SplitTextElementAtStart(const Position& start,
SplitTextNodeContainingElement(ToText(start.ComputeContainerNode()), SplitTextNodeContainingElement(ToText(start.ComputeContainerNode()),
start.OffsetInContainerNode()); start.OffsetInContainerNode());
UpdateStartEnd(Position::BeforeNode(*start.ComputeContainerNode()), new_end); UpdateStartEnd(EphemeralRange(
Position::BeforeNode(*start.ComputeContainerNode()), new_end));
} }
void ApplyStyleCommand::SplitTextElementAtEnd(const Position& start, void ApplyStyleCommand::SplitTextElementAtEnd(const Position& start,
...@@ -1641,7 +1640,8 @@ void ApplyStyleCommand::SplitTextElementAtEnd(const Position& start, ...@@ -1641,7 +1640,8 @@ void ApplyStyleCommand::SplitTextElementAtEnd(const Position& start,
should_update_start should_update_start
? Position(ToText(first_text_node), start.OffsetInContainerNode()) ? Position(ToText(first_text_node), start.OffsetInContainerNode())
: start; : start;
UpdateStartEnd(new_start, Position::AfterNode(*first_text_node)); UpdateStartEnd(
EphemeralRange(new_start, Position::AfterNode(*first_text_node)));
} }
bool ApplyStyleCommand::ShouldSplitTextElement(Element* element, bool ApplyStyleCommand::ShouldSplitTextElement(Element* element,
...@@ -1700,9 +1700,10 @@ bool ApplyStyleCommand::MergeStartWithPreviousIfIdentical( ...@@ -1700,9 +1700,10 @@ bool ApplyStyleCommand::MergeStartWithPreviousIfIdentical(
int start_offset_adjustment = start_child->NodeIndex(); int start_offset_adjustment = start_child->NodeIndex();
int end_offset_adjustment = int end_offset_adjustment =
start_node == end.AnchorNode() ? start_offset_adjustment : 0; start_node == end.AnchorNode() ? start_offset_adjustment : 0;
UpdateStartEnd(Position(start_node, start_offset_adjustment), UpdateStartEnd(EphemeralRange(
Position(end.AnchorNode(), end.ComputeEditingOffset() + Position(start_node, start_offset_adjustment),
end_offset_adjustment)); Position(end.AnchorNode(),
end.ComputeEditingOffset() + end_offset_adjustment)));
return true; return true;
} }
...@@ -1742,10 +1743,11 @@ bool ApplyStyleCommand::MergeEndWithNextIfIdentical( ...@@ -1742,10 +1743,11 @@ bool ApplyStyleCommand::MergeEndWithNextIfIdentical(
bool should_update_start = start.ComputeContainerNode() == end_node; bool should_update_start = start.ComputeContainerNode() == end_node;
int end_offset = next_child ? next_child->NodeIndex() int end_offset = next_child ? next_child->NodeIndex()
: next_element->childNodes()->length(); : next_element->childNodes()->length();
UpdateStartEnd(should_update_start UpdateStartEnd(EphemeralRange(
? Position(next_element, start.OffsetInContainerNode()) should_update_start
: start, ? Position(next_element, start.OffsetInContainerNode())
Position(next_element, end_offset)); : start,
Position(next_element, end_offset)));
return true; return true;
} }
...@@ -2071,7 +2073,7 @@ void ApplyStyleCommand::JoinChildTextNodes(ContainerNode* node, ...@@ -2071,7 +2073,7 @@ void ApplyStyleCommand::JoinChildTextNodes(ContainerNode* node,
// don't move child node pointer. it may want to merge with more text nodes. // don't move child node pointer. it may want to merge with more text nodes.
} }
UpdateStartEnd(new_start, new_end); UpdateStartEnd(EphemeralRange(new_start, new_end));
} }
void ApplyStyleCommand::Trace(blink::Visitor* visitor) { void ApplyStyleCommand::Trace(blink::Visitor* visitor) {
......
...@@ -190,7 +190,7 @@ class CORE_EXPORT ApplyStyleCommand final : public CompositeEditCommand { ...@@ -190,7 +190,7 @@ class CORE_EXPORT ApplyStyleCommand final : public CompositeEditCommand {
HTMLElement* unsplit_ancestor, HTMLElement* unsplit_ancestor,
EditingState*); EditingState*);
void UpdateStartEnd(const Position& new_start, const Position& new_end); void UpdateStartEnd(const EphemeralRange&);
Position StartPosition(); Position StartPosition();
Position EndPosition(); Position EndPosition();
......
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