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 21 matching lines...) Expand all Loading... |
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 Range& range, unsigned maxLength) |
| 43 : m_startOffsetInContent(0) |
| 44 , m_endOffsetInContent(0) |
| 45 { |
| 46 initialize(range.startPosition(), range.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 initialize(position, position, maxLength); |
| 54 } |
| 55 |
| 56 void SurroundingText::initialize(const Position& startPosition, const Position&
endPosition, unsigned maxLength) |
| 57 { |
| 58 ASSERT(startPosition.document() == endPosition.document()); |
| 59 |
45 const unsigned halfMaxLength = maxLength / 2; | 60 const unsigned halfMaxLength = maxLength / 2; |
46 | 61 |
47 Document* document = position.document(); | 62 Document* document = startPosition.document(); |
48 // The |position| will have no document if it is null (as in no position). | 63 // The position will have no document if it is null (as in no position). |
49 if (!document) | 64 if (!document) |
50 return; | 65 return; |
51 | 66 |
52 // The forward range starts at the selection end and ends at the document's | 67 // 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 | 68 // end. It will then be updated to only contain the text in the text in the |
54 // right range around the selection. | 69 // right range around the selection. |
55 RefPtrWillBeRawPtr<Range> forwardRange = Range::create(*document, position,
lastPositionInNode(document->documentElement()).parentAnchoredEquivalent()); | 70 RefPtrWillBeRawPtr<Range> forwardRange = Range::create(*document, endPositio
n, lastPositionInNode(document->documentElement()).parentAnchoredEquivalent()); |
56 CharacterIterator forwardIterator(forwardRange.get(), TextIteratorStopsOnFor
mControls); | 71 CharacterIterator forwardIterator(forwardRange.get(), TextIteratorStopsOnFor
mControls); |
| 72 // FIXME: why do we stop going trough the text if we were not able to select
something on the right? |
57 if (!forwardIterator.atEnd()) | 73 if (!forwardIterator.atEnd()) |
58 forwardIterator.advance(maxLength - halfMaxLength); | 74 forwardIterator.advance(maxLength - halfMaxLength); |
59 | 75 |
60 forwardRange = forwardIterator.range(); | 76 forwardRange = forwardIterator.range(); |
61 if (!forwardRange || !Range::create(*document, position, forwardRange->start
Position())->text().length()) { | 77 if (!forwardRange || !Range::create(*document, endPosition, forwardRange->st
artPosition())->text().length()) { |
62 ASSERT(forwardRange); | 78 ASSERT(forwardRange); |
63 return; | 79 return; |
64 } | 80 } |
65 | 81 |
66 // Same as with the forward range but with the backward range. The range | 82 // 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 | 83 // starts at the document's start and ends at the selection start and will |
68 // be updated. | 84 // be updated. |
69 RefPtrWillBeRawPtr<Range> backwardsRange = Range::create(*document, firstPos
itionInNode(document->documentElement()).parentAnchoredEquivalent(), position); | 85 RefPtrWillBeRawPtr<Range> backwardsRange = Range::create(*document, firstPos
itionInNode(document->documentElement()).parentAnchoredEquivalent(), startPositi
on); |
70 BackwardsCharacterIterator backwardsIterator(backwardsRange.get(), TextItera
torStopsOnFormControls); | 86 BackwardsCharacterIterator backwardsIterator(backwardsRange.get(), TextItera
torStopsOnFormControls); |
71 if (!backwardsIterator.atEnd()) | 87 if (!backwardsIterator.atEnd()) |
72 backwardsIterator.advance(halfMaxLength); | 88 backwardsIterator.advance(halfMaxLength); |
73 | 89 |
74 backwardsRange = backwardsIterator.range(); | 90 backwardsRange = backwardsIterator.range(); |
75 if (!backwardsRange) { | 91 if (!backwardsRange) { |
76 ASSERT(backwardsRange); | 92 ASSERT(backwardsRange); |
77 return; | 93 return; |
78 } | 94 } |
79 | 95 |
80 m_positionOffsetInContent = Range::create(*document, backwardsRange->endPosi
tion(), position)->text().length(); | 96 m_startOffsetInContent = Range::create(*document, backwardsRange->endPositio
n(), startPosition)->text().length(); |
| 97 m_endOffsetInContent = Range::create(*document, backwardsRange->endPosition(
), endPosition)->text().length(); |
81 m_contentRange = Range::create(*document, backwardsRange->endPosition(), for
wardRange->startPosition()); | 98 m_contentRange = Range::create(*document, backwardsRange->endPosition(), for
wardRange->startPosition()); |
82 ASSERT(m_contentRange); | 99 ASSERT(m_contentRange); |
83 } | 100 } |
84 | 101 |
85 PassRefPtrWillBeRawPtr<Range> SurroundingText::rangeFromContentOffsets(unsigned
startOffsetInContent, unsigned endOffsetInContent) | 102 PassRefPtrWillBeRawPtr<Range> SurroundingText::rangeFromContentOffsets(unsigned
startOffsetInContent, unsigned endOffsetInContent) |
86 { | 103 { |
87 if (startOffsetInContent >= endOffsetInContent || endOffsetInContent > conte
nt().length()) | 104 if (startOffsetInContent >= endOffsetInContent || endOffsetInContent > conte
nt().length()) |
88 return nullptr; | 105 return nullptr; |
89 | 106 |
90 CharacterIterator iterator(m_contentRange.get()); | 107 CharacterIterator iterator(m_contentRange.get()); |
(...skipping 14 matching lines...) Expand all Loading... |
105 return Range::create(*start.document(), start, end); | 122 return Range::create(*start.document(), start, end); |
106 } | 123 } |
107 | 124 |
108 String SurroundingText::content() const | 125 String SurroundingText::content() const |
109 { | 126 { |
110 if (m_contentRange) | 127 if (m_contentRange) |
111 return m_contentRange->text(); | 128 return m_contentRange->text(); |
112 return String(); | 129 return String(); |
113 } | 130 } |
114 | 131 |
115 unsigned SurroundingText::positionOffsetInContent() const | 132 unsigned SurroundingText::startOffsetInContent() const |
116 { | 133 { |
117 return m_positionOffsetInContent; | 134 return m_startOffsetInContent; |
| 135 } |
| 136 |
| 137 unsigned SurroundingText::endOffsetInContent() const |
| 138 { |
| 139 return m_endOffsetInContent; |
118 } | 140 } |
119 | 141 |
120 } // namespace WebCore | 142 } // namespace WebCore |
OLD | NEW |