| 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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 | 48 |
| 49 SurroundingText::SurroundingText(const Position& position, unsigned maxLength) | 49 SurroundingText::SurroundingText(const Position& position, unsigned maxLength) |
| 50 : m_startOffsetInContent(0) | 50 : m_startOffsetInContent(0) |
| 51 , m_endOffsetInContent(0) | 51 , m_endOffsetInContent(0) |
| 52 { | 52 { |
| 53 initialize(position, position, maxLength); | 53 initialize(position, position, maxLength); |
| 54 } | 54 } |
| 55 | 55 |
| 56 void SurroundingText::initialize(const Position& startPosition, const Position&
endPosition, unsigned maxLength) | 56 void SurroundingText::initialize(const Position& startPosition, const Position&
endPosition, unsigned maxLength) |
| 57 { | 57 { |
| 58 ASSERT(startPosition.document() == endPosition.document()); | 58 DCHECK_EQ(startPosition.document(), endPosition.document()); |
| 59 | 59 |
| 60 const unsigned halfMaxLength = maxLength / 2; | 60 const unsigned halfMaxLength = maxLength / 2; |
| 61 | 61 |
| 62 Document* document = startPosition.document(); | 62 Document* document = startPosition.document(); |
| 63 // 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). |
| 64 if (!document || !document->documentElement()) | 64 if (!document || !document->documentElement()) |
| 65 return; | 65 return; |
| 66 | 66 |
| 67 // 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 |
| 68 // 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 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 79 // Same as with the forward range but with the backward range. The range | 79 // Same as with the forward range but with the backward range. The range |
| 80 // starts at the document's start and ends at the selection start and will | 80 // starts at the document's start and ends at the selection start and will |
| 81 // be updated. | 81 // be updated. |
| 82 BackwardsCharacterIterator backwardsIterator(firstPositionInNode(document->d
ocumentElement()).parentAnchoredEquivalent(), startPosition, TextIteratorStopsOn
FormControls); | 82 BackwardsCharacterIterator backwardsIterator(firstPositionInNode(document->d
ocumentElement()).parentAnchoredEquivalent(), startPosition, TextIteratorStopsOn
FormControls); |
| 83 if (!backwardsIterator.atEnd()) | 83 if (!backwardsIterator.atEnd()) |
| 84 backwardsIterator.advance(halfMaxLength); | 84 backwardsIterator.advance(halfMaxLength); |
| 85 | 85 |
| 86 m_startOffsetInContent = Range::create(*document, backwardsIterator.endPosit
ion(), startPosition)->text().length(); | 86 m_startOffsetInContent = Range::create(*document, backwardsIterator.endPosit
ion(), startPosition)->text().length(); |
| 87 m_endOffsetInContent = Range::create(*document, backwardsIterator.endPositio
n(), endPosition)->text().length(); | 87 m_endOffsetInContent = Range::create(*document, backwardsIterator.endPositio
n(), endPosition)->text().length(); |
| 88 m_contentRange = Range::create(*document, backwardsIterator.endPosition(), f
orwardRange.startPosition()); | 88 m_contentRange = Range::create(*document, backwardsIterator.endPosition(), f
orwardRange.startPosition()); |
| 89 ASSERT(m_contentRange); | 89 DCHECK(m_contentRange); |
| 90 } | 90 } |
| 91 | 91 |
| 92 Range* SurroundingText::rangeFromContentOffsets(unsigned startOffsetInContent, u
nsigned endOffsetInContent) | 92 Range* SurroundingText::rangeFromContentOffsets(unsigned startOffsetInContent, u
nsigned endOffsetInContent) |
| 93 { | 93 { |
| 94 if (startOffsetInContent >= endOffsetInContent || endOffsetInContent > conte
nt().length()) | 94 if (startOffsetInContent >= endOffsetInContent || endOffsetInContent > conte
nt().length()) |
| 95 return nullptr; | 95 return nullptr; |
| 96 | 96 |
| 97 CharacterIterator iterator(m_contentRange->startPosition(), m_contentRange->
endPosition()); | 97 CharacterIterator iterator(m_contentRange->startPosition(), m_contentRange->
endPosition()); |
| 98 | 98 |
| 99 ASSERT(!iterator.atEnd()); | 99 DCHECK(!iterator.atEnd()); |
| 100 iterator.advance(startOffsetInContent); | 100 iterator.advance(startOffsetInContent); |
| 101 | 101 |
| 102 Position start = iterator.startPosition(); | 102 Position start = iterator.startPosition(); |
| 103 | 103 |
| 104 ASSERT(!iterator.atEnd()); | 104 DCHECK(!iterator.atEnd()); |
| 105 iterator.advance(endOffsetInContent - startOffsetInContent); | 105 iterator.advance(endOffsetInContent - startOffsetInContent); |
| 106 | 106 |
| 107 Position end = iterator.startPosition(); | 107 Position end = iterator.startPosition(); |
| 108 | 108 |
| 109 ASSERT(start.document()); | 109 DCHECK(start.document()); |
| 110 return Range::create(*start.document(), start, end); | 110 return Range::create(*start.document(), start, end); |
| 111 } | 111 } |
| 112 | 112 |
| 113 String SurroundingText::content() const | 113 String SurroundingText::content() const |
| 114 { | 114 { |
| 115 if (m_contentRange) | 115 if (m_contentRange) |
| 116 return m_contentRange->text(); | 116 return m_contentRange->text(); |
| 117 return String(); | 117 return String(); |
| 118 } | 118 } |
| 119 | 119 |
| 120 unsigned SurroundingText::startOffsetInContent() const | 120 unsigned SurroundingText::startOffsetInContent() const |
| 121 { | 121 { |
| 122 return m_startOffsetInContent; | 122 return m_startOffsetInContent; |
| 123 } | 123 } |
| 124 | 124 |
| 125 unsigned SurroundingText::endOffsetInContent() const | 125 unsigned SurroundingText::endOffsetInContent() const |
| 126 { | 126 { |
| 127 return m_endOffsetInContent; | 127 return m_endOffsetInContent; |
| 128 } | 128 } |
| 129 | 129 |
| 130 } // namespace blink | 130 } // namespace blink |
| OLD | NEW |