Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(312)

Side by Side Diff: Source/core/editing/SurroundingText.cpp

Issue 294073005: Extend WebSurroundingText to accept a WebRange. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: rebase and use int Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2012 Google Inc. All rights reserved. 2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 21 matching lines...) Expand all
32 #include "core/editing/SurroundingText.h" 32 #include "core/editing/SurroundingText.h"
33 33
34 #include "core/dom/Document.h" 34 #include "core/dom/Document.h"
35 #include "core/dom/Element.h" 35 #include "core/dom/Element.h"
36 #include "core/dom/Position.h" 36 #include "core/dom/Position.h"
37 #include "core/dom/Range.h" 37 #include "core/dom/Range.h"
38 #include "core/editing/TextIterator.h" 38 #include "core/editing/TextIterator.h"
39 39
40 namespace WebCore { 40 namespace WebCore {
41 41
42 SurroundingText::SurroundingText(const Position& startPosition, const Position& endPosition, unsigned maxLength)
43 : m_startOffsetInContent(0)
44 , m_endOffsetInContent(0)
45 {
46 process(startPosition, endPosition, maxLength);
47 }
48
42 SurroundingText::SurroundingText(const Position& position, unsigned maxLength) 49 SurroundingText::SurroundingText(const Position& position, unsigned maxLength)
43 : m_positionOffsetInContent(0) 50 : m_startOffsetInContent(0)
51 , m_endOffsetInContent(0)
44 { 52 {
53 process(position, position, maxLength);
54 }
55
56 void SurroundingText::process(const Position& startPosition, const Position& end Position, unsigned maxLength)
57 {
58 if (startPosition.isNull() || endPosition.isNull())
59 return;
60
45 const unsigned halfMaxLength = maxLength / 2; 61 const unsigned halfMaxLength = maxLength / 2;
46 62
47 Document* document = position.document(); 63 Document* document = startPosition.document();
48 // The |position| will have no document if it is null (as in no position). 64 ASSERT(document && startPosition.document() == endPosition.document());
49 if (!document)
50 return;
51 65
52 // The forward range starts at the selection end and ends at the document's 66 // The forward range starts at the selection end and ends at the document's
53 // end. It will then be updated to only contain the text in the text in the 67 // end. It will then be updated to only contain the text in the text in the
54 // right range around the selection. 68 // right range around the selection.
55 RefPtrWillBeRawPtr<Range> forwardRange = makeRange(position, lastPositionInN ode(document->documentElement())); 69 RefPtrWillBeRawPtr<Range> forwardRange = makeRange(endPosition, lastPosition InNode(document->documentElement()));
56 CharacterIterator forwardIterator(forwardRange.get(), TextIteratorStopsOnFor mControls); 70 CharacterIterator forwardIterator(forwardRange.get(), TextIteratorStopsOnFor mControls);
71 // FIXME: why do we stop going trough the text if we were not able to select something on the right?
57 if (!forwardIterator.atEnd()) 72 if (!forwardIterator.atEnd())
58 forwardIterator.advance(maxLength - halfMaxLength); 73 forwardIterator.advance(maxLength - halfMaxLength);
59 74
60 forwardRange = forwardIterator.range(); 75 forwardRange = forwardIterator.range();
61 if (!forwardRange || !Range::create(*document, position, forwardRange->start Position())->text().length()) { 76 if (!forwardRange || !Range::create(*document, endPosition, forwardRange->st artPosition())->text().length()) {
62 ASSERT(forwardRange); 77 ASSERT(forwardRange);
63 return; 78 return;
64 } 79 }
65 80
66 // Same as with the forward range but with the backward range. The range 81 // Same as with the forward range but with the backward range. The range
67 // starts at the document's start and ends at the selection start and will 82 // starts at the document's start and ends at the selection start and will
68 // be updated. 83 // be updated.
69 RefPtrWillBeRawPtr<Range> backwardsRange = makeRange(firstPositionInNode(doc ument->documentElement()), position); 84 RefPtrWillBeRawPtr<Range> backwardsRange = makeRange(firstPositionInNode(doc ument->documentElement()), startPosition);
70 BackwardsCharacterIterator backwardsIterator(backwardsRange.get(), TextItera torStopsOnFormControls); 85 BackwardsCharacterIterator backwardsIterator(backwardsRange.get(), TextItera torStopsOnFormControls);
71 if (!backwardsIterator.atEnd()) 86 if (!backwardsIterator.atEnd())
72 backwardsIterator.advance(halfMaxLength); 87 backwardsIterator.advance(halfMaxLength);
73 88
74 backwardsRange = backwardsIterator.range(); 89 backwardsRange = backwardsIterator.range();
75 if (!backwardsRange) { 90 if (!backwardsRange) {
76 ASSERT(backwardsRange); 91 ASSERT(backwardsRange);
77 return; 92 return;
78 } 93 }
79 94
80 m_positionOffsetInContent = Range::create(*document, backwardsRange->endPosi tion(), position)->text().length(); 95 m_startOffsetInContent = Range::create(*document, backwardsRange->endPositio n(), startPosition)->text().length();
96 m_endOffsetInContent = Range::create(*document, backwardsRange->endPosition( ), endPosition)->text().length();
81 m_contentRange = Range::create(*document, backwardsRange->endPosition(), for wardRange->startPosition()); 97 m_contentRange = Range::create(*document, backwardsRange->endPosition(), for wardRange->startPosition());
82 ASSERT(m_contentRange); 98 ASSERT(m_contentRange);
83 } 99 }
84 100
85 PassRefPtrWillBeRawPtr<Range> SurroundingText::rangeFromContentOffsets(unsigned startOffsetInContent, unsigned endOffsetInContent) 101 PassRefPtrWillBeRawPtr<Range> SurroundingText::rangeFromContentOffsets(unsigned startOffsetInContent, unsigned endOffsetInContent)
86 { 102 {
87 if (startOffsetInContent >= endOffsetInContent || endOffsetInContent > conte nt().length()) 103 if (startOffsetInContent >= endOffsetInContent || endOffsetInContent > conte nt().length())
88 return nullptr; 104 return nullptr;
89 105
90 CharacterIterator iterator(m_contentRange.get()); 106 CharacterIterator iterator(m_contentRange.get());
(...skipping 14 matching lines...) Expand all
105 return Range::create(*start.document(), start, end); 121 return Range::create(*start.document(), start, end);
106 } 122 }
107 123
108 String SurroundingText::content() const 124 String SurroundingText::content() const
109 { 125 {
110 if (m_contentRange) 126 if (m_contentRange)
111 return m_contentRange->text(); 127 return m_contentRange->text();
112 return String(); 128 return String();
113 } 129 }
114 130
115 unsigned SurroundingText::positionOffsetInContent() const 131 unsigned SurroundingText::startOffsetInContent() const
116 { 132 {
117 return m_positionOffsetInContent; 133 return m_startOffsetInContent;
134 }
135
136 unsigned SurroundingText::endOffsetInContent() const
137 {
138 return m_endOffsetInContent;
118 } 139 }
119 140
120 } // namespace WebCore 141 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698