Commit 6cb6854b authored by yosin@chromium.org's avatar yosin@chromium.org

Move static member function normalizeRange() out from VisibleSelection

This patch moves static member function |normalizeRange()| out from
|VisibleSelection| to "EditingUtilities.{cpp,h}" to make |VisibleSelection|
class not to use |PositionInComposedTree| directly for ease of templatazing
|VisibleSelection| to introduce composed tree version.

This patch is a preparation of making selection to handle granularity for web
component, http://crrev.com/1277863002

BUG=513568
TEST=n/a; no behavior changes

Review URL: https://codereview.chromium.org/1328493002

git-svn-id: svn://svn.chromium.org/blink/trunk@201538 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent cfff3341
...@@ -1531,6 +1531,33 @@ EphemeralRange makeRange(const VisiblePosition &start, const VisiblePosition &en ...@@ -1531,6 +1531,33 @@ EphemeralRange makeRange(const VisiblePosition &start, const VisiblePosition &en
return EphemeralRange(s, e); return EphemeralRange(s, e);
} }
template <typename Strategy>
static EphemeralRangeTemplate<Strategy> normalizeRangeAlgorithm(const EphemeralRangeTemplate<Strategy>& range)
{
ASSERT(range.isNotNull());
range.document().updateLayoutIgnorePendingStylesheets();
// TODO(yosin) We should not call |parentAnchoredEquivalent()|, it is
// redundant.
const PositionAlgorithm<Strategy> normalizedStart = mostForwardCaretPosition(range.startPosition()).parentAnchoredEquivalent();
const PositionAlgorithm<Strategy> normalizedEnd = mostBackwardCaretPosition(range.endPosition()).parentAnchoredEquivalent();
// The order of the positions of |start| and |end| can be swapped after
// upstream/downstream. e.g. editing/pasteboard/copy-display-none.html
if (normalizedStart.compareTo(normalizedEnd) > 0)
return EphemeralRangeTemplate<Strategy>(normalizedEnd, normalizedStart);
return EphemeralRangeTemplate<Strategy>(normalizedStart, normalizedEnd);
}
EphemeralRange normalizeRange(const EphemeralRange& range)
{
return normalizeRangeAlgorithm<EditingStrategy>(range);
}
EphemeralRangeInComposedTree normalizeRange(const EphemeralRangeInComposedTree& range)
{
return normalizeRangeAlgorithm<EditingInComposedTreeStrategy>(range);
}
VisiblePosition visiblePositionForIndex(int index, ContainerNode* scope) VisiblePosition visiblePositionForIndex(int index, ContainerNode* scope)
{ {
if (!scope) if (!scope)
......
...@@ -247,6 +247,8 @@ int comparePositions(const VisiblePosition&, const VisiblePosition&); ...@@ -247,6 +247,8 @@ int comparePositions(const VisiblePosition&, const VisiblePosition&);
int indexForVisiblePosition(const VisiblePosition&, RefPtrWillBeRawPtr<ContainerNode>& scope); int indexForVisiblePosition(const VisiblePosition&, RefPtrWillBeRawPtr<ContainerNode>& scope);
EphemeralRange makeRange(const VisiblePosition&, const VisiblePosition&); EphemeralRange makeRange(const VisiblePosition&, const VisiblePosition&);
EphemeralRange normalizeRange(const EphemeralRange&);
EphemeralRangeInComposedTree normalizeRange(const EphemeralRangeInComposedTree&);
VisiblePosition visiblePositionForIndex(int index, ContainerNode* scope); VisiblePosition visiblePositionForIndex(int index, ContainerNode* scope);
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
......
...@@ -1249,7 +1249,7 @@ static PassRefPtrWillBeRawPtr<Range> findRangeOfStringAlgorithm(Document& docume ...@@ -1249,7 +1249,7 @@ static PassRefPtrWillBeRawPtr<Range> findRangeOfStringAlgorithm(Document& docume
// the reference range, find again. Build a selection with the found range // the reference range, find again. Build a selection with the found range
// to remove collapsed whitespace. Compare ranges instead of selection // to remove collapsed whitespace. Compare ranges instead of selection
// objects to ignore the way that the current selection was made. // objects to ignore the way that the current selection was made.
if (resultRange && startInReferenceRange && VisibleSelection::normalizeRange(EphemeralRangeTemplate<Strategy>(resultRange.get())) == referenceRange) { if (resultRange && startInReferenceRange && normalizeRange(EphemeralRangeTemplate<Strategy>(resultRange.get())) == referenceRange) {
if (forward) if (forward)
searchRange = EphemeralRangeTemplate<Strategy>(fromPositionInDOMTree<Strategy>(resultRange->endPosition()), searchRange.endPosition()); searchRange = EphemeralRangeTemplate<Strategy>(fromPositionInDOMTree<Strategy>(resultRange->endPosition()), searchRange.endPosition());
else else
......
...@@ -1765,7 +1765,7 @@ template <typename SelectionType> ...@@ -1765,7 +1765,7 @@ template <typename SelectionType>
String extractSelectedTextAlgorithm(const FrameSelection& selection, TextIteratorBehavior behavior) String extractSelectedTextAlgorithm(const FrameSelection& selection, TextIteratorBehavior behavior)
{ {
VisibleSelection visibleSelection = selection.selection(); VisibleSelection visibleSelection = selection.selection();
EphemeralRangeTemplate<typename SelectionType::Strategy> range = VisibleSelection::normalizeRange(SelectionType::asRange(visibleSelection)); EphemeralRangeTemplate<typename SelectionType::Strategy> range = normalizeRange(SelectionType::asRange(visibleSelection));
// We remove '\0' characters because they are not visibly rendered to the user. // We remove '\0' characters because they are not visibly rendered to the user.
return plainText(range, behavior).replace(0, ""); return plainText(range, behavior).replace(0, "");
} }
...@@ -1781,7 +1781,7 @@ template <typename SelectionType> ...@@ -1781,7 +1781,7 @@ template <typename SelectionType>
static String extractSelectedHTMLAlgorithm(const FrameSelection& selection) static String extractSelectedHTMLAlgorithm(const FrameSelection& selection)
{ {
VisibleSelection visibleSelection = selection.selection(); VisibleSelection visibleSelection = selection.selection();
EphemeralRangeTemplate<typename SelectionType::Strategy> range = VisibleSelection::normalizeRange(SelectionType::asRange(visibleSelection)); EphemeralRangeTemplate<typename SelectionType::Strategy> range = normalizeRange(SelectionType::asRange(visibleSelection));
return createMarkup(range.startPosition(), range.endPosition(), AnnotateForInterchange, ConvertBlocksToInlines::NotConvert, ResolveNonLocalURLs); return createMarkup(range.startPosition(), range.endPosition(), AnnotateForInterchange, ConvertBlocksToInlines::NotConvert, ResolveNonLocalURLs);
} }
......
...@@ -230,33 +230,6 @@ PassRefPtrWillBeRawPtr<Range> firstRangeOf(const VisibleSelection& selection) ...@@ -230,33 +230,6 @@ PassRefPtrWillBeRawPtr<Range> firstRangeOf(const VisibleSelection& selection)
return Range::create(*start.document(), start, end); return Range::create(*start.document(), start, end);
} }
template <typename Strategy>
static EphemeralRangeTemplate<Strategy> normalizeRangeAlgorithm(const EphemeralRangeTemplate<Strategy>& range)
{
ASSERT(range.isNotNull());
range.document().updateLayoutIgnorePendingStylesheets();
// TODO(yosin) We should not call |parentAnchoredEquivalent()|, it is
// redundant.
const PositionAlgorithm<Strategy> normalizedStart = mostForwardCaretPosition(range.startPosition()).parentAnchoredEquivalent();
const PositionAlgorithm<Strategy> normalizedEnd = mostBackwardCaretPosition(range.endPosition()).parentAnchoredEquivalent();
// The order of the positions of |start| and |end| can be swapped after
// upstream/downstream. e.g. editing/pasteboard/copy-display-none.html
if (normalizedStart.compareTo(normalizedEnd) > 0)
return EphemeralRangeTemplate<Strategy>(normalizedEnd, normalizedStart);
return EphemeralRangeTemplate<Strategy>(normalizedStart, normalizedEnd);
}
EphemeralRange VisibleSelection::normalizeRange(const EphemeralRange& range)
{
return normalizeRangeAlgorithm<EditingStrategy>(range);
}
EphemeralRangeInComposedTree VisibleSelection::normalizeRange(const EphemeralRangeInComposedTree& range)
{
return normalizeRangeAlgorithm<EditingInComposedTreeStrategy>(range);
}
EphemeralRange VisibleSelection::toNormalizedEphemeralRange() const EphemeralRange VisibleSelection::toNormalizedEphemeralRange() const
{ {
if (isNone()) if (isNone())
......
...@@ -158,8 +158,6 @@ public: ...@@ -158,8 +158,6 @@ public:
// contracts the range around text, and moves the caret most backward // contracts the range around text, and moves the caret most backward
// visually equivalent position before returning the range/positions. // visually equivalent position before returning the range/positions.
EphemeralRange toNormalizedEphemeralRange() const; EphemeralRange toNormalizedEphemeralRange() const;
static EphemeralRange normalizeRange(const EphemeralRange&);
static EphemeralRangeInComposedTree normalizeRange(const EphemeralRangeInComposedTree&);
Element* rootEditableElement() const; Element* rootEditableElement() const;
bool isContentEditable() const; bool isContentEditable() const;
......
...@@ -195,7 +195,7 @@ static HTMLElement* highestAncestorToWrapMarkup(const PositionAlgorithm<Strategy ...@@ -195,7 +195,7 @@ static HTMLElement* highestAncestorToWrapMarkup(const PositionAlgorithm<Strategy
specialCommonAncestor = ancestorToRetainStructureAndAppearance(commonAncestor); specialCommonAncestor = ancestorToRetainStructureAndAppearance(commonAncestor);
if (Node* parentListNode = enclosingNodeOfType(firstPositionInOrBeforeNode(firstNode), isListItem)) { if (Node* parentListNode = enclosingNodeOfType(firstPositionInOrBeforeNode(firstNode), isListItem)) {
EphemeralRangeTemplate<Strategy> markupRange = EphemeralRangeTemplate<Strategy>(startPosition, endPosition); EphemeralRangeTemplate<Strategy> markupRange = EphemeralRangeTemplate<Strategy>(startPosition, endPosition);
EphemeralRangeTemplate<Strategy> nodeRange = VisibleSelection::normalizeRange(EphemeralRangeTemplate<Strategy>::rangeOfContents(*parentListNode)); EphemeralRangeTemplate<Strategy> nodeRange = normalizeRange(EphemeralRangeTemplate<Strategy>::rangeOfContents(*parentListNode));
if (nodeRange == markupRange) { if (nodeRange == markupRange) {
ContainerNode* ancestor = parentListNode->parentNode(); ContainerNode* ancestor = parentListNode->parentNode();
while (ancestor && !isHTMLListElement(ancestor)) while (ancestor && !isHTMLListElement(ancestor))
......
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