| 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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 if (visiblePosition.isNull()) | 45 if (visiblePosition.isNull()) |
| 46 return; | 46 return; |
| 47 | 47 |
| 48 const unsigned halfMaxLength = maxLength / 2; | 48 const unsigned halfMaxLength = maxLength / 2; |
| 49 CharacterIterator forwardIterator(makeRange(visiblePosition, endOfDocument(v
isiblePosition)).get(), TextIteratorStopsOnFormControls); | 49 CharacterIterator forwardIterator(makeRange(visiblePosition, endOfDocument(v
isiblePosition)).get(), TextIteratorStopsOnFormControls); |
| 50 if (!forwardIterator.atEnd()) | 50 if (!forwardIterator.atEnd()) |
| 51 forwardIterator.advance(maxLength - halfMaxLength); | 51 forwardIterator.advance(maxLength - halfMaxLength); |
| 52 | 52 |
| 53 Position position = visiblePosition.deepEquivalent().parentAnchoredEquivalen
t(); | 53 Position position = visiblePosition.deepEquivalent().parentAnchoredEquivalen
t(); |
| 54 Document* document = position.document(); | 54 Document* document = position.document(); |
| 55 ASSERT(document); |
| 55 RefPtr<Range> forwardRange = forwardIterator.range(); | 56 RefPtr<Range> forwardRange = forwardIterator.range(); |
| 56 if (!forwardRange || !Range::create(document, position, forwardRange->startP
osition())->text().length()) { | 57 if (!forwardRange || !Range::create(*document, position, forwardRange->start
Position())->text().length()) { |
| 57 ASSERT(forwardRange); | 58 ASSERT(forwardRange); |
| 58 return; | 59 return; |
| 59 } | 60 } |
| 60 | 61 |
| 61 BackwardsCharacterIterator backwardsIterator(makeRange(startOfDocument(visib
lePosition), visiblePosition).get(), TextIteratorStopsOnFormControls); | 62 BackwardsCharacterIterator backwardsIterator(makeRange(startOfDocument(visib
lePosition), visiblePosition).get(), TextIteratorStopsOnFormControls); |
| 62 if (!backwardsIterator.atEnd()) | 63 if (!backwardsIterator.atEnd()) |
| 63 backwardsIterator.advance(halfMaxLength); | 64 backwardsIterator.advance(halfMaxLength); |
| 64 | 65 |
| 65 RefPtr<Range> backwardsRange = backwardsIterator.range(); | 66 RefPtr<Range> backwardsRange = backwardsIterator.range(); |
| 66 if (!backwardsRange) { | 67 if (!backwardsRange) { |
| 67 ASSERT(backwardsRange); | 68 ASSERT(backwardsRange); |
| 68 return; | 69 return; |
| 69 } | 70 } |
| 70 | 71 |
| 71 m_positionOffsetInContent = Range::create(document, backwardsRange->endPosit
ion(), position)->text().length(); | 72 m_positionOffsetInContent = Range::create(*document, backwardsRange->endPosi
tion(), position)->text().length(); |
| 72 m_contentRange = Range::create(document, backwardsRange->endPosition(), forw
ardRange->startPosition()); | 73 m_contentRange = Range::create(*document, backwardsRange->endPosition(), for
wardRange->startPosition()); |
| 73 ASSERT(m_contentRange); | 74 ASSERT(m_contentRange); |
| 74 } | 75 } |
| 75 | 76 |
| 76 PassRefPtr<Range> SurroundingText::rangeFromContentOffsets(unsigned startOffsetI
nContent, unsigned endOffsetInContent) | 77 PassRefPtr<Range> SurroundingText::rangeFromContentOffsets(unsigned startOffsetI
nContent, unsigned endOffsetInContent) |
| 77 { | 78 { |
| 78 if (startOffsetInContent >= endOffsetInContent || endOffsetInContent > conte
nt().length()) | 79 if (startOffsetInContent >= endOffsetInContent || endOffsetInContent > conte
nt().length()) |
| 79 return 0; | 80 return 0; |
| 80 | 81 |
| 81 CharacterIterator iterator(m_contentRange.get()); | 82 CharacterIterator iterator(m_contentRange.get()); |
| 82 | 83 |
| 83 ASSERT(!iterator.atEnd()); | 84 ASSERT(!iterator.atEnd()); |
| 84 iterator.advance(startOffsetInContent); | 85 iterator.advance(startOffsetInContent); |
| 85 | 86 |
| 86 ASSERT(iterator.range()); | 87 ASSERT(iterator.range()); |
| 87 Position start = iterator.range()->startPosition(); | 88 Position start = iterator.range()->startPosition(); |
| 88 | 89 |
| 89 ASSERT(!iterator.atEnd()); | 90 ASSERT(!iterator.atEnd()); |
| 90 iterator.advance(endOffsetInContent - startOffsetInContent); | 91 iterator.advance(endOffsetInContent - startOffsetInContent); |
| 91 | 92 |
| 92 ASSERT(iterator.range()); | 93 ASSERT(iterator.range()); |
| 93 Position end = iterator.range()->startPosition(); | 94 Position end = iterator.range()->startPosition(); |
| 94 | 95 |
| 95 return Range::create(start.document(), start, end); | 96 ASSERT(start.document()); |
| 97 return Range::create(*start.document(), start, end); |
| 96 } | 98 } |
| 97 | 99 |
| 98 String SurroundingText::content() const | 100 String SurroundingText::content() const |
| 99 { | 101 { |
| 100 if (m_contentRange) | 102 if (m_contentRange) |
| 101 return m_contentRange->text(); | 103 return m_contentRange->text(); |
| 102 return String(); | 104 return String(); |
| 103 } | 105 } |
| 104 | 106 |
| 105 unsigned SurroundingText::positionOffsetInContent() const | 107 unsigned SurroundingText::positionOffsetInContent() const |
| 106 { | 108 { |
| 107 return m_positionOffsetInContent; | 109 return m_positionOffsetInContent; |
| 108 } | 110 } |
| 109 | 111 |
| 110 } // namespace WebCore | 112 } // namespace WebCore |
| OLD | NEW |