// Updates |selectionInFlatTree| to match with |selection|. void SelectionAdjuster::adjustSelectionInFlatTree( VisibleSelectionInFlatTree* selectionInFlatTree, const VisibleSelection& selection) { if (selection.isNone()) { *selectionInFlatTree = VisibleSelectionInFlatTree(); return; } const PositionInFlatTree& base = toPositionInFlatTree(selection.base()); const PositionInFlatTree& extent = toPositionInFlatTree(selection.extent()); const PositionInFlatTree& position1 = toPositionInFlatTree(selection.start()); const PositionInFlatTree& position2 = toPositionInFlatTree(selection.end()); position1.anchorNode()->updateDistribution(); position2.anchorNode()->updateDistribution(); selectionInFlatTree->m_base = base; selectionInFlatTree->m_extent = extent; selectionInFlatTree->m_affinity = selection.m_affinity; selectionInFlatTree->m_isDirectional = selection.m_isDirectional; selectionInFlatTree->m_granularity = selection.m_granularity; selectionInFlatTree->m_hasTrailingWhitespace = selection.m_hasTrailingWhitespace; selectionInFlatTree->m_baseIsFirst = base.isNull() || base.compareTo(extent) <= 0; if (position1.compareTo(position2) <= 0) { selectionInFlatTree->m_start = position1; selectionInFlatTree->m_end = position2; } else { selectionInFlatTree->m_start = position2; selectionInFlatTree->m_end = position1; } selectionInFlatTree->updateSelectionType(); }
TEST_F(PositionTest, ToPositionInFlatTreeWithInactiveInsertionPoint) { const char* bodyContent = "<p id='p'><content></content></p>"; setBodyContent(bodyContent); RefPtrWillBeRawPtr<Element> anchor = document().getElementById("p"); EXPECT_EQ(PositionInFlatTree(anchor.get(), 0), toPositionInFlatTree(Position(anchor.get(), 0))); EXPECT_EQ(PositionInFlatTree(anchor, PositionAnchorType::AfterChildren), toPositionInFlatTree(Position(anchor.get(), 1))); }
TEST_F(PositionTest, ToPositionInFlatTreeWithShadowRootContainingSingleContent) { const char* bodyContent = "<p id='host'>00<b id='one'>11</b>22</p>"; const char* shadowContent = "<content select=#one></content>"; setBodyContent(bodyContent); RefPtrWillBeRawPtr<ShadowRoot> shadowRoot = setShadowContent(shadowContent, "host"); RefPtrWillBeRawPtr<Element> host = document().getElementById("host"); EXPECT_EQ(PositionInFlatTree(host.get(), 0), toPositionInFlatTree(Position(shadowRoot.get(), 0))); EXPECT_EQ(PositionInFlatTree(host.get(), PositionAnchorType::AfterChildren), toPositionInFlatTree(Position(shadowRoot.get(), 1))); }
TEST_F(PositionTest, ToPositionInFlatTreeWithShadowRoot) { const char* bodyContent = "<p id='host'>00<b id='one'>11</b>22</p>"; const char* shadowContent = "<a><content select=#one></content></a>"; setBodyContent(bodyContent); ShadowRoot* shadowRoot = setShadowContent(shadowContent, "host"); Element* host = document().getElementById("host"); EXPECT_EQ(PositionInFlatTree(host, 0), toPositionInFlatTree(Position(shadowRoot, 0))); EXPECT_EQ(PositionInFlatTree(host, PositionAnchorType::AfterChildren), toPositionInFlatTree(Position(shadowRoot, 1))); }
TEST_F(PositionTest, ToPositionInFlatTreeWithActiveInsertionPoint) { const char* bodyContent = "<p id='host'>00<b id='one'>11</b>22</p>"; const char* shadowContent = "<a id='a'><content select=#one id='content'></content><content></content></a>"; setBodyContent(bodyContent); RefPtrWillBeRawPtr<ShadowRoot> shadowRoot = setShadowContent(shadowContent, "host"); RefPtrWillBeRawPtr<Element> anchor = shadowRoot->getElementById("a"); EXPECT_EQ(PositionInFlatTree(anchor.get(), 0), toPositionInFlatTree(Position(anchor.get(), 0))); EXPECT_EQ(PositionInFlatTree(anchor.get(), 1), toPositionInFlatTree(Position(anchor.get(), 1))); EXPECT_EQ(PositionInFlatTree(anchor, PositionAnchorType::AfterChildren), toPositionInFlatTree(Position(anchor.get(), 2))); }
// This test comes from "editing/style/block-style-progress-crash.html". TEST_F(PositionTest, ToPositionInFlatTreeWithNotDistributed) { setBodyContent("<progress id=sample>foo</progress>"); Element* sample = document().getElementById("sample"); EXPECT_EQ(PositionInFlatTree(sample, PositionAnchorType::AfterChildren), toPositionInFlatTree(Position(sample, 0))); }