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 if (position.isNull()) |
46 return; | 45 return; |
47 | 46 |
48 const unsigned halfMaxLength = maxLength / 2; | 47 const unsigned halfMaxLength = maxLength / 2; |
49 CharacterIterator forwardIterator(makeRange(visiblePosition, endOfDocument(v isiblePosition)).get(), TextIteratorStopsOnFormControls); | 48 |
49 Document* document = position.document(); | |
50 ASSERT(document); | |
yosin_UTC9
2014/06/04 01:04:05
nit: We don't need to have this |ASSERT(document)|
yosin_UTC9
2014/06/04 01:13:10
How about unify |Position::isNull| and |Position::
mlamouri (slow - plz ping)
2014/06/04 09:52:12
Done.
| |
51 | |
52 // 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 | |
54 // right range around the selection. | |
55 RefPtrWillBeRawPtr<Range> forwardRange = rangeOfContents(document); | |
56 forwardRange->setStart(position); | |
57 CharacterIterator forwardIterator(forwardRange.get(), TextIteratorStopsOnFor mControls); | |
50 if (!forwardIterator.atEnd()) | 58 if (!forwardIterator.atEnd()) |
51 forwardIterator.advance(maxLength - halfMaxLength); | 59 forwardIterator.advance(maxLength - halfMaxLength); |
52 | 60 |
53 Position position = visiblePosition.deepEquivalent().parentAnchoredEquivalen t(); | 61 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()) { | 62 if (!forwardRange || !Range::create(*document, position, forwardRange->start Position())->text().length()) { |
58 ASSERT(forwardRange); | 63 ASSERT(forwardRange); |
59 return; | 64 return; |
60 } | 65 } |
61 | 66 |
62 BackwardsCharacterIterator backwardsIterator(makeRange(startOfDocument(visib lePosition), visiblePosition).get(), TextIteratorStopsOnFormControls); | 67 // Same as with the forward range but with the backward range. The range |
68 // starts at the document's start and ends at the selection start and will | |
69 // be updated. | |
70 RefPtrWillBeRawPtr<Range> backwardsRange = rangeOfContents(document); | |
71 backwardsRange->setEnd(position); | |
72 BackwardsCharacterIterator backwardsIterator(backwardsRange.get(), TextItera torStopsOnFormControls); | |
63 if (!backwardsIterator.atEnd()) | 73 if (!backwardsIterator.atEnd()) |
64 backwardsIterator.advance(halfMaxLength); | 74 backwardsIterator.advance(halfMaxLength); |
65 | 75 |
66 RefPtrWillBeRawPtr<Range> backwardsRange = backwardsIterator.range(); | 76 backwardsRange = backwardsIterator.range(); |
67 if (!backwardsRange) { | 77 if (!backwardsRange) { |
68 ASSERT(backwardsRange); | 78 ASSERT(backwardsRange); |
69 return; | 79 return; |
70 } | 80 } |
71 | 81 |
72 m_positionOffsetInContent = Range::create(*document, backwardsRange->endPosi tion(), position)->text().length(); | 82 m_positionOffsetInContent = Range::create(*document, backwardsRange->endPosi tion(), position)->text().length(); |
73 m_contentRange = Range::create(*document, backwardsRange->endPosition(), for wardRange->startPosition()); | 83 m_contentRange = Range::create(*document, backwardsRange->endPosition(), for wardRange->startPosition()); |
74 ASSERT(m_contentRange); | 84 ASSERT(m_contentRange); |
75 } | 85 } |
76 | 86 |
(...skipping 26 matching lines...) Expand all Loading... | |
103 return m_contentRange->text(); | 113 return m_contentRange->text(); |
104 return String(); | 114 return String(); |
105 } | 115 } |
106 | 116 |
107 unsigned SurroundingText::positionOffsetInContent() const | 117 unsigned SurroundingText::positionOffsetInContent() const |
108 { | 118 { |
109 return m_positionOffsetInContent; | 119 return m_positionOffsetInContent; |
110 } | 120 } |
111 | 121 |
112 } // namespace WebCore | 122 } // namespace WebCore |
OLD | NEW |