Commit ef53c41c authored by Xiaocheng Hu's avatar Xiaocheng Hu Committed by Commit Bot

[LayoutNG] Expose NGInlineFormattingContextOf() with unit tests

This patch exposes NGInlineFormattingContextOf() from ng_offset_mapping.cc
as a utility function, so that we can easily check if a given position is
laid out as layout ng inline, and if so, which context it is in. New unit
tests are added for the function.

This patch is split off from crrev.com/c/803015: Compute LocalCaretRect in NG

Bug: 699017
Cq-Include-Trybots: master.tryserver.chromium.linux:linux_layout_tests_layout_ng
Change-Id: I6ea8c8f77f7389ea7b68a58311900171883a582b
Reviewed-on: https://chromium-review.googlesource.com/809767
Commit-Queue: Xiaocheng Hu <xiaochengh@chromium.org>
Reviewed-by: default avataryosin (OOO Dec 11 to Jan 8) <yosin@chromium.org>
Reviewed-by: default avatarEmil A Eklund <eae@chromium.org>
Cr-Commit-Position: refs/heads/master@{#522471}
parent 3a4ca9f5
......@@ -40,8 +40,15 @@ std::pair<const Node&, unsigned> ToNodeOffsetPair(const Position& position) {
return {*position.AnchorNode(), 1};
}
// TODO(xiaochengh): Expose it properly as a utility function.
const LayoutObject* NGInlineFormattingContextOf(const Position& position) {
// TODO(xiaochengh): Introduce predicates for comparing Position and
// NGOffsetMappingUnit, to reduce position-offset conversion and ad-hoc
// predicates below.
} // namespace
const LayoutBlockFlow* NGInlineFormattingContextOf(const Position& position) {
if (!RuntimeEnabledFeatures::LayoutNGEnabled())
return nullptr;
if (!NGOffsetMapping::AcceptsPosition(position))
return nullptr;
const auto node_offset_pair = ToNodeOffsetPair(position);
......@@ -57,11 +64,10 @@ const LayoutObject* NGInlineFormattingContextOf(const Position& position) {
return layout_object->EnclosingNGBlockFlow();
}
// TODO(xiaochengh): Introduce predicates for comparing Position and
// NGOffsetMappingUnit, to reduce position-offset conversion and ad-hoc
// predicates below.
} // namespace
const LayoutBlockFlow* NGInlineFormattingContextOf(
const PositionInFlatTree& position) {
return NGInlineFormattingContextOf(ToPositionInDOMTree(position));
}
NGOffsetMappingUnit::NGOffsetMappingUnit(NGOffsetMappingUnitType type,
const Node& node,
......
......@@ -16,6 +16,7 @@
namespace blink {
class LayoutBlockFlow;
class LayoutObject;
class Node;
......@@ -185,6 +186,9 @@ class CORE_EXPORT NGOffsetMapping {
DISALLOW_COPY_AND_ASSIGN(NGOffsetMapping);
};
CORE_EXPORT const LayoutBlockFlow* NGInlineFormattingContextOf(const Position&);
const LayoutBlockFlow* NGInlineFormattingContextOf(const PositionInFlatTree&);
} // namespace blink
#endif // NGOffsetMapping_h
......@@ -121,6 +121,43 @@ TEST_P(ParameterizedNGOffsetMappingTest, StoredResult) {
EXPECT_TRUE(IsOffsetMappingStored());
}
TEST_P(ParameterizedNGOffsetMappingTest, NGInlineFormattingContextOf) {
SetBodyInnerHTML(
"<div id=container>"
" foo"
" <span id=inline-block style='display:inline-block'>blah</span>"
" <span id=inline-span>bar</span>"
"</div>");
const Element* container = GetElementById("container");
const Element* inline_block = GetElementById("inline-block");
const Element* inline_span = GetElementById("inline-span");
const Node* blah = inline_block->firstChild();
const Node* foo = inline_block->previousSibling();
const Node* bar = inline_span->firstChild();
EXPECT_EQ(nullptr,
NGInlineFormattingContextOf(Position::BeforeNode(*container)));
EXPECT_EQ(nullptr,
NGInlineFormattingContextOf(Position::AfterNode(*container)));
const LayoutObject* container_object = container->GetLayoutObject();
EXPECT_EQ(container_object, NGInlineFormattingContextOf(Position(foo, 0)));
EXPECT_EQ(container_object, NGInlineFormattingContextOf(Position(bar, 0)));
EXPECT_EQ(container_object,
NGInlineFormattingContextOf(Position::BeforeNode(*inline_block)));
EXPECT_EQ(container_object,
NGInlineFormattingContextOf(Position::AfterNode(*inline_block)));
EXPECT_EQ(container_object,
NGInlineFormattingContextOf(Position::BeforeNode(*inline_span)));
EXPECT_EQ(container_object,
NGInlineFormattingContextOf(Position::AfterNode(*inline_span)));
const LayoutObject* inline_block_object = inline_block->GetLayoutObject();
EXPECT_EQ(inline_block_object,
NGInlineFormattingContextOf(Position(blah, 0)));
}
TEST_P(ParameterizedNGOffsetMappingTest, OneTextNode) {
SetupHtml("t", "<div id=t>foo</div>");
const Node* foo_node = layout_object_->GetNode();
......
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