| 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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 DCHECK_EQ(RootEditableElementOf(start_position), | 69 DCHECK_EQ(RootEditableElementOf(start_position), |
| 70 RootEditableElementOf(end_position)); | 70 RootEditableElementOf(end_position)); |
| 71 Element* const root_editable = RootEditableElementOf(start_position); | 71 Element* const root_editable = RootEditableElementOf(start_position); |
| 72 Element* const root_element = | 72 Element* const root_element = |
| 73 root_editable ? root_editable : document->documentElement(); | 73 root_editable ? root_editable : document->documentElement(); |
| 74 | 74 |
| 75 CharacterIterator forward_iterator( | 75 CharacterIterator forward_iterator( |
| 76 end_position, | 76 end_position, |
| 77 Position::LastPositionInNode(root_element).ParentAnchoredEquivalent(), | 77 Position::LastPositionInNode(root_element).ParentAnchoredEquivalent(), |
| 78 TextIteratorBehavior::Builder().SetStopsOnFormControls(true).Build()); | 78 TextIteratorBehavior::Builder().SetStopsOnFormControls(true).Build()); |
| 79 // FIXME: why do we stop going trough the text if we were not able to select |
| 80 // something on the right? |
| 79 if (!forward_iterator.AtEnd()) | 81 if (!forward_iterator.AtEnd()) |
| 80 forward_iterator.Advance(max_length - half_max_length); | 82 forward_iterator.Advance(max_length - half_max_length); |
| 81 | 83 |
| 84 EphemeralRange forward_range = forward_iterator.Range(); |
| 85 if (forward_range.IsNull() || |
| 86 !Range::Create(*document, end_position, forward_range.StartPosition()) |
| 87 ->GetText() |
| 88 .length()) |
| 89 return; |
| 90 |
| 82 // 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 |
| 83 // starts at the document's or input element's start and ends at the selection | 92 // starts at the document's or input element's start and ends at the selection |
| 84 // start and will be updated. | 93 // start and will be updated. |
| 85 BackwardsCharacterIterator backwards_iterator( | 94 BackwardsCharacterIterator backwards_iterator( |
| 86 Position::FirstPositionInNode(root_element).ParentAnchoredEquivalent(), | 95 Position::FirstPositionInNode(root_element).ParentAnchoredEquivalent(), |
| 87 start_position, | 96 start_position, |
| 88 TextIteratorBehavior::Builder().SetStopsOnFormControls(true).Build()); | 97 TextIteratorBehavior::Builder().SetStopsOnFormControls(true).Build()); |
| 89 if (!backwards_iterator.AtEnd()) | 98 if (!backwards_iterator.AtEnd()) |
| 90 backwards_iterator.Advance(half_max_length); | 99 backwards_iterator.Advance(half_max_length); |
| 91 | 100 |
| 92 const TextIteratorBehavior behavior = | 101 const TextIteratorBehavior behavior = |
| 93 TextIteratorBehavior::NoTrailingSpaceRangeLengthBehavior(); | 102 TextIteratorBehavior::NoTrailingSpaceRangeLengthBehavior(); |
| 94 start_offset_in_content_ = TextIterator::RangeLength( | 103 start_offset_in_content_ = TextIterator::RangeLength( |
| 95 backwards_iterator.EndPosition(), start_position, behavior); | 104 backwards_iterator.EndPosition(), start_position, behavior); |
| 96 end_offset_in_content_ = TextIterator::RangeLength( | 105 end_offset_in_content_ = TextIterator::RangeLength( |
| 97 backwards_iterator.EndPosition(), end_position, behavior); | 106 backwards_iterator.EndPosition(), end_position, behavior); |
| 98 content_range_ = Range::Create(*document, backwards_iterator.EndPosition(), | 107 content_range_ = Range::Create(*document, backwards_iterator.EndPosition(), |
| 99 forward_iterator.StartPosition()); | 108 forward_range.StartPosition()); |
| 100 DCHECK(content_range_); | 109 DCHECK(content_range_); |
| 101 } | 110 } |
| 102 | 111 |
| 103 String SurroundingText::Content() const { | 112 String SurroundingText::Content() const { |
| 104 if (content_range_) { | 113 if (content_range_) { |
| 105 // SurroundingText is created with clean layout and must not be stored | 114 // SurroundingText is created with clean layout and must not be stored |
| 106 // through DOM or style changes, so layout must still be clean here. | 115 // through DOM or style changes, so layout must still be clean here. |
| 107 DCHECK(!content_range_->OwnerDocument().NeedsLayoutTreeUpdate()); | 116 DCHECK(!content_range_->OwnerDocument().NeedsLayoutTreeUpdate()); |
| 108 return content_range_->GetText(); | 117 return content_range_->GetText(); |
| 109 } | 118 } |
| 110 return String(); | 119 return String(); |
| 111 } | 120 } |
| 112 | 121 |
| 113 unsigned SurroundingText::StartOffsetInContent() const { | 122 unsigned SurroundingText::StartOffsetInContent() const { |
| 114 return start_offset_in_content_; | 123 return start_offset_in_content_; |
| 115 } | 124 } |
| 116 | 125 |
| 117 unsigned SurroundingText::EndOffsetInContent() const { | 126 unsigned SurroundingText::EndOffsetInContent() const { |
| 118 return end_offset_in_content_; | 127 return end_offset_in_content_; |
| 119 } | 128 } |
| 120 | 129 |
| 121 } // namespace blink | 130 } // namespace blink |
| OLD | NEW |