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/Element.h" |
| 36 #include "core/dom/Position.h" |
35 #include "core/dom/Range.h" | 37 #include "core/dom/Range.h" |
36 #include "core/editing/TextIterator.h" | 38 #include "core/editing/TextIterator.h" |
37 #include "core/editing/VisiblePosition.h" | |
38 #include "core/editing/VisibleUnits.h" | |
39 | 39 |
40 namespace WebCore { | 40 namespace WebCore { |
41 | 41 |
42 SurroundingText::SurroundingText(const VisiblePosition& visiblePosition, unsigne
d maxLength) | 42 SurroundingText::SurroundingText(const Position& position, unsigned maxLength) |
43 : m_positionOffsetInContent(0) | 43 : m_positionOffsetInContent(0) |
44 { | 44 { |
45 if (visiblePosition.isNull()) | 45 const unsigned halfMaxLength = maxLength / 2; |
| 46 |
| 47 Document* document = position.document(); |
| 48 // The |position| will have no document if it is null (as in no position). |
| 49 if (!document) |
46 return; | 50 return; |
47 | 51 |
48 const unsigned halfMaxLength = maxLength / 2; | 52 // The forward range starts at the selection end and ends at the document's |
49 CharacterIterator forwardIterator(makeRange(visiblePosition, endOfDocument(v
isiblePosition)).get(), TextIteratorStopsOnFormControls); | 53 // end. It will then be updated to only contain the text in the text in the |
| 54 // right range around the selection. |
| 55 RefPtrWillBeRawPtr<Range> forwardRange = Range::create(*document, position,
lastPositionInNode(document->documentElement()).parentAnchoredEquivalent()); |
| 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 = Range::create(*document, firstPos
itionInNode(document->documentElement()).parentAnchoredEquivalent(), position); |
| 70 BackwardsCharacterIterator backwardsIterator(backwardsRange.get(), TextItera
torStopsOnFormControls); |
63 if (!backwardsIterator.atEnd()) | 71 if (!backwardsIterator.atEnd()) |
64 backwardsIterator.advance(halfMaxLength); | 72 backwardsIterator.advance(halfMaxLength); |
65 | 73 |
66 RefPtrWillBeRawPtr<Range> backwardsRange = backwardsIterator.range(); | 74 backwardsRange = backwardsIterator.range(); |
67 if (!backwardsRange) { | 75 if (!backwardsRange) { |
68 ASSERT(backwardsRange); | 76 ASSERT(backwardsRange); |
69 return; | 77 return; |
70 } | 78 } |
71 | 79 |
72 m_positionOffsetInContent = Range::create(*document, backwardsRange->endPosi
tion(), position)->text().length(); | 80 m_positionOffsetInContent = Range::create(*document, backwardsRange->endPosi
tion(), position)->text().length(); |
73 m_contentRange = Range::create(*document, backwardsRange->endPosition(), for
wardRange->startPosition()); | 81 m_contentRange = Range::create(*document, backwardsRange->endPosition(), for
wardRange->startPosition()); |
74 ASSERT(m_contentRange); | 82 ASSERT(m_contentRange); |
75 } | 83 } |
76 | 84 |
(...skipping 26 matching lines...) Expand all Loading... |
103 return m_contentRange->text(); | 111 return m_contentRange->text(); |
104 return String(); | 112 return String(); |
105 } | 113 } |
106 | 114 |
107 unsigned SurroundingText::positionOffsetInContent() const | 115 unsigned SurroundingText::positionOffsetInContent() const |
108 { | 116 { |
109 return m_positionOffsetInContent; | 117 return m_positionOffsetInContent; |
110 } | 118 } |
111 | 119 |
112 } // namespace WebCore | 120 } // namespace WebCore |
OLD | NEW |