OLD | NEW |
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 14 matching lines...) Expand all Loading... |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #include "config.h" | 31 #include "config.h" |
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/Position.h" |
35 #include "core/dom/Range.h" | 36 #include "core/dom/Range.h" |
36 #include "core/editing/TextIterator.h" | 37 #include "core/editing/TextIterator.h" |
37 #include "core/editing/VisiblePosition.h" | |
38 #include "core/editing/VisibleUnits.h" | |
39 | 38 |
40 namespace WebCore { | 39 namespace WebCore { |
41 | 40 |
42 SurroundingText::SurroundingText(const VisiblePosition& visiblePosition, unsigne
d maxLength) | 41 SurroundingText::SurroundingText(const Position& position, unsigned maxLength) |
43 : m_positionOffsetInContent(0) | 42 : m_positionOffsetInContent(0) |
44 { | 43 { |
45 if (visiblePosition.isNull()) | 44 const unsigned halfMaxLength = maxLength / 2; |
| 45 |
| 46 Document* document = position.document(); |
| 47 // The |position| will have no document if it is null (as in no position). |
| 48 if (!document) |
46 return; | 49 return; |
47 | 50 |
48 const unsigned halfMaxLength = maxLength / 2; | 51 // The forward range starts at the selection end and ends at the document's |
49 CharacterIterator forwardIterator(makeRange(visiblePosition, endOfDocument(v
isiblePosition)).get(), TextIteratorStopsOnFormControls); | 52 // end. It will then be updated to only contain the text in the text in the |
| 53 // right range around the selection. |
| 54 RefPtrWillBeRawPtr<Range> forwardRange = rangeOfContents(document); |
| 55 forwardRange->setStart(position); |
| 56 CharacterIterator forwardIterator(forwardRange.get(), TextIteratorStopsOnFor
mControls); |
50 if (!forwardIterator.atEnd()) | 57 if (!forwardIterator.atEnd()) |
51 forwardIterator.advance(maxLength - halfMaxLength); | 58 forwardIterator.advance(maxLength - halfMaxLength); |
52 | 59 |
53 Position position = visiblePosition.deepEquivalent().parentAnchoredEquivalen
t(); | 60 forwardRange = forwardIterator.range(); |
54 Document* document = position.document(); | |
55 ASSERT(document); | |
56 RefPtrWillBeRawPtr<Range> forwardRange = forwardIterator.range(); | |
57 if (!forwardRange || !Range::create(*document, position, forwardRange->start
Position())->text().length()) { | 61 if (!forwardRange || !Range::create(*document, position, forwardRange->start
Position())->text().length()) { |
58 ASSERT(forwardRange); | 62 ASSERT(forwardRange); |
59 return; | 63 return; |
60 } | 64 } |
61 | 65 |
62 BackwardsCharacterIterator backwardsIterator(makeRange(startOfDocument(visib
lePosition), visiblePosition).get(), TextIteratorStopsOnFormControls); | 66 // 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 |
| 68 // be updated. |
| 69 RefPtrWillBeRawPtr<Range> backwardsRange = rangeOfContents(document); |
| 70 backwardsRange->setEnd(position); |
| 71 BackwardsCharacterIterator backwardsIterator(backwardsRange.get(), TextItera
torStopsOnFormControls); |
63 if (!backwardsIterator.atEnd()) | 72 if (!backwardsIterator.atEnd()) |
64 backwardsIterator.advance(halfMaxLength); | 73 backwardsIterator.advance(halfMaxLength); |
65 | 74 |
66 RefPtrWillBeRawPtr<Range> backwardsRange = backwardsIterator.range(); | 75 backwardsRange = backwardsIterator.range(); |
67 if (!backwardsRange) { | 76 if (!backwardsRange) { |
68 ASSERT(backwardsRange); | 77 ASSERT(backwardsRange); |
69 return; | 78 return; |
70 } | 79 } |
71 | 80 |
72 m_positionOffsetInContent = Range::create(*document, backwardsRange->endPosi
tion(), position)->text().length(); | 81 m_positionOffsetInContent = Range::create(*document, backwardsRange->endPosi
tion(), position)->text().length(); |
73 m_contentRange = Range::create(*document, backwardsRange->endPosition(), for
wardRange->startPosition()); | 82 m_contentRange = Range::create(*document, backwardsRange->endPosition(), for
wardRange->startPosition()); |
74 ASSERT(m_contentRange); | 83 ASSERT(m_contentRange); |
75 } | 84 } |
76 | 85 |
(...skipping 26 matching lines...) Expand all Loading... |
103 return m_contentRange->text(); | 112 return m_contentRange->text(); |
104 return String(); | 113 return String(); |
105 } | 114 } |
106 | 115 |
107 unsigned SurroundingText::positionOffsetInContent() const | 116 unsigned SurroundingText::positionOffsetInContent() const |
108 { | 117 { |
109 return m_positionOffsetInContent; | 118 return m_positionOffsetInContent; |
110 } | 119 } |
111 | 120 |
112 } // namespace WebCore | 121 } // namespace WebCore |
OLD | NEW |