Commit 50cdaa5f authored by Xiaocheng Hu's avatar Xiaocheng Hu Committed by Commit Bot

[LayoutNG] Extract LayoutBR::PositionForCaretOffset/CaretOffsetFromPosition from LayoutText code

LayoutText has two NG utility functions PositionForCaretOffset() and
OffsetForCaretPosition() with type-based branching code.

This patch extracts the LayoutBR handling into LayoutBR overrides for
better code health.

Change-Id: Ieca60f177c68944a74cfc30fe6bdc444012eca6e
Reviewed-on: https://chromium-review.googlesource.com/1011629Reviewed-by: default avatarAleks Totic <atotic@chromium.org>
Reviewed-by: default avatarEmil A Eklund <eae@chromium.org>
Commit-Queue: Xiaocheng Hu <xiaochengh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#551048}
parent b51e5467
......@@ -60,4 +60,19 @@ PositionWithAffinity LayoutBR::PositionForPoint(const LayoutPoint&) const {
return CreatePositionWithAffinity(0);
}
Position LayoutBR::PositionForCaretOffset(unsigned offset) const {
DCHECK_LE(offset, 1u);
DCHECK(GetNode());
return offset ? Position::AfterNode(*GetNode())
: Position::BeforeNode(*GetNode());
}
Optional<unsigned> LayoutBR::CaretOffsetForPosition(
const Position& position) const {
if (position.IsNull() || position.AnchorNode() != GetNode())
return WTF::nullopt;
DCHECK(position.IsBeforeAnchor() || position.IsAfterAnchor()) << position;
return position.IsBeforeAnchor() ? 0 : 1;
}
} // namespace blink
......@@ -70,6 +70,9 @@ class LayoutBR final : public LayoutText {
PositionWithAffinity PositionForPoint(const LayoutPoint&) const final;
Position PositionForCaretOffset(unsigned) const final;
Optional<unsigned> CaretOffsetForPosition(const Position&) const final;
protected:
void StyleDidChange(StyleDifference, const ComputedStyle* old_style) override;
};
......
......@@ -2053,52 +2053,39 @@ const NGOffsetMapping* LayoutText::GetNGOffsetMapping() const {
Position LayoutText::PositionForCaretOffset(unsigned offset) const {
// ::first-letter handling should be done by LayoutTextFragment override.
DCHECK(!IsTextFragment());
// BR handling should be done by LayoutBR override.
DCHECK(!IsBR());
// WBR handling should be done by LayoutWordBreak override.
DCHECK(!IsWordBreak());
DCHECK_LE(offset, TextLength());
const Node* node = GetNode();
if (!node)
return Position();
if (node->IsTextNode()) {
// TODO(layout-dev): Support offset change due to text-transform.
return Position(node, offset);
}
// TODO(xiaochengh): This should be done in LayoutBR override.
if (IsBR()) {
DCHECK(IsHTMLBRElement(node));
DCHECK_LE(offset, 1u);
return offset ? Position::AfterNode(*node) : Position::BeforeNode(*node);
}
NOTREACHED();
return Position();
DCHECK(node->IsTextNode());
// TODO(layout-dev): Support offset change due to text-transform.
return Position(node, offset);
}
Optional<unsigned> LayoutText::CaretOffsetForPosition(
const Position& position) const {
// ::first-letter handling should be done by LayoutTextFragment override.
DCHECK(!IsTextFragment());
// BR handling should be done by LayoutBR override.
DCHECK(!IsBR());
// WBR handling should be done by LayoutWordBreak override.
DCHECK(!IsWordBreak());
if (position.IsNull() || position.AnchorNode() != GetNode())
return WTF::nullopt;
if (GetNode()->IsTextNode()) {
if (position.IsBeforeAnchor())
return 0;
// TODO(layout-dev): Support offset change due to text-transform.
if (position.IsAfterAnchor())
return TextLength();
DCHECK(position.IsOffsetInAnchor()) << position;
DCHECK_LE(position.OffsetInContainerNode(), static_cast<int>(TextLength()))
<< position;
return position.OffsetInContainerNode();
}
// TODO(xiaochengh): This should be done by LayoutBR override.
if (IsBR()) {
DCHECK(position.IsBeforeAnchor() || position.IsAfterAnchor()) << position;
return position.IsBeforeAnchor() ? 0 : 1;
}
NOTREACHED();
return WTF::nullopt;
DCHECK(GetNode()->IsTextNode());
if (position.IsBeforeAnchor())
return 0;
// TODO(layout-dev): Support offset change due to text-transform.
if (position.IsAfterAnchor())
return TextLength();
DCHECK(position.IsOffsetInAnchor()) << position;
DCHECK_LE(position.OffsetInContainerNode(), static_cast<int>(TextLength()))
<< position;
return position.OffsetInContainerNode();
}
int LayoutText::CaretMinOffset() const {
......
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