| 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 15 matching lines...) Expand all Loading... |
| 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 "core/editing/SurroundingText.h" | 31 #include "core/editing/SurroundingText.h" |
| 32 | 32 |
| 33 #include "core/dom/Document.h" | 33 #include "core/dom/Document.h" |
| 34 #include "core/dom/Element.h" | 34 #include "core/dom/Element.h" |
| 35 #include "core/dom/Range.h" | 35 #include "core/dom/Range.h" |
| 36 #include "core/editing/EditingUtilities.h" |
| 36 #include "core/editing/Position.h" | 37 #include "core/editing/Position.h" |
| 37 #include "core/editing/iterators/BackwardsCharacterIterator.h" | 38 #include "core/editing/iterators/BackwardsCharacterIterator.h" |
| 38 #include "core/editing/iterators/CharacterIterator.h" | 39 #include "core/editing/iterators/CharacterIterator.h" |
| 39 | 40 |
| 40 namespace blink { | 41 namespace blink { |
| 41 | 42 |
| 42 SurroundingText::SurroundingText(const Range& range, unsigned max_length) | 43 SurroundingText::SurroundingText(const Range& range, unsigned max_length) |
| 43 : start_offset_in_content_(0), end_offset_in_content_(0) { | 44 : start_offset_in_content_(0), end_offset_in_content_(0) { |
| 44 Initialize(range.StartPosition(), range.EndPosition(), max_length); | 45 Initialize(range.StartPosition(), range.EndPosition(), max_length); |
| 45 } | 46 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 56 | 57 |
| 57 const unsigned half_max_length = max_length / 2; | 58 const unsigned half_max_length = max_length / 2; |
| 58 | 59 |
| 59 Document* document = start_position.GetDocument(); | 60 Document* document = start_position.GetDocument(); |
| 60 // The position will have no document if it is null (as in no position). | 61 // The position will have no document if it is null (as in no position). |
| 61 if (!document || !document->documentElement()) | 62 if (!document || !document->documentElement()) |
| 62 return; | 63 return; |
| 63 DCHECK(!document->NeedsLayoutTreeUpdate()); | 64 DCHECK(!document->NeedsLayoutTreeUpdate()); |
| 64 | 65 |
| 65 // 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 |
| 66 // end. It will then be updated to only contain the text in the text in the | 67 // or the input element's end. It will then be updated to only contain the |
| 67 // right range around the selection. | 68 // text in the right range around the selection. |
| 69 DCHECK_EQ(RootEditableElementOf(start_position), |
| 70 RootEditableElementOf(end_position)); |
| 71 Element* const root_editable = RootEditableElementOf(start_position); |
| 72 Element* const root_element = |
| 73 root_editable ? root_editable : document->documentElement(); |
| 74 |
| 68 CharacterIterator forward_iterator( | 75 CharacterIterator forward_iterator( |
| 69 end_position, | 76 end_position, |
| 70 Position::LastPositionInNode(document->documentElement()) | 77 Position::LastPositionInNode(root_element).ParentAnchoredEquivalent(), |
| 71 .ParentAnchoredEquivalent(), | |
| 72 TextIteratorBehavior::Builder().SetStopsOnFormControls(true).Build()); | 78 TextIteratorBehavior::Builder().SetStopsOnFormControls(true).Build()); |
| 73 // FIXME: why do we stop going trough the text if we were not able to select | 79 // FIXME: why do we stop going trough the text if we were not able to select |
| 74 // something on the right? | 80 // something on the right? |
| 75 if (!forward_iterator.AtEnd()) | 81 if (!forward_iterator.AtEnd()) |
| 76 forward_iterator.Advance(max_length - half_max_length); | 82 forward_iterator.Advance(max_length - half_max_length); |
| 77 | 83 |
| 78 EphemeralRange forward_range = forward_iterator.Range(); | 84 EphemeralRange forward_range = forward_iterator.Range(); |
| 79 if (forward_range.IsNull() || | 85 if (forward_range.IsNull() || |
| 80 !Range::Create(*document, end_position, forward_range.StartPosition()) | 86 !Range::Create(*document, end_position, forward_range.StartPosition()) |
| 81 ->GetText() | 87 ->GetText() |
| 82 .length()) | 88 .length()) |
| 83 return; | 89 return; |
| 84 | 90 |
| 85 // Same as with the forward range but with the backward range. The range | 91 // Same as with the forward range but with the backward range. The range |
| 86 // starts at the document's start and ends at the selection start and will | 92 // starts at the document's or input element's start and ends at the selection |
| 87 // be updated. | 93 // start and will be updated. |
| 88 BackwardsCharacterIterator backwards_iterator( | 94 BackwardsCharacterIterator backwards_iterator( |
| 89 Position::FirstPositionInNode(document->documentElement()) | 95 Position::FirstPositionInNode(root_element).ParentAnchoredEquivalent(), |
| 90 .ParentAnchoredEquivalent(), | |
| 91 start_position, | 96 start_position, |
| 92 TextIteratorBehavior::Builder().SetStopsOnFormControls(true).Build()); | 97 TextIteratorBehavior::Builder().SetStopsOnFormControls(true).Build()); |
| 93 if (!backwards_iterator.AtEnd()) | 98 if (!backwards_iterator.AtEnd()) |
| 94 backwards_iterator.Advance(half_max_length); | 99 backwards_iterator.Advance(half_max_length); |
| 95 | 100 |
| 96 const TextIteratorBehavior behavior = | 101 const TextIteratorBehavior behavior = |
| 97 TextIteratorBehavior::NoTrailingSpaceRangeLengthBehavior(); | 102 TextIteratorBehavior::NoTrailingSpaceRangeLengthBehavior(); |
| 98 start_offset_in_content_ = TextIterator::RangeLength( | 103 start_offset_in_content_ = TextIterator::RangeLength( |
| 99 backwards_iterator.EndPosition(), start_position, behavior); | 104 backwards_iterator.EndPosition(), start_position, behavior); |
| 100 end_offset_in_content_ = TextIterator::RangeLength( | 105 end_offset_in_content_ = TextIterator::RangeLength( |
| (...skipping 15 matching lines...) Expand all Loading... |
| 116 | 121 |
| 117 unsigned SurroundingText::StartOffsetInContent() const { | 122 unsigned SurroundingText::StartOffsetInContent() const { |
| 118 return start_offset_in_content_; | 123 return start_offset_in_content_; |
| 119 } | 124 } |
| 120 | 125 |
| 121 unsigned SurroundingText::EndOffsetInContent() const { | 126 unsigned SurroundingText::EndOffsetInContent() const { |
| 122 return end_offset_in_content_; | 127 return end_offset_in_content_; |
| 123 } | 128 } |
| 124 | 129 |
| 125 } // namespace blink | 130 } // namespace blink |
| OLD | NEW |