Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(479)

Side by Side Diff: Source/core/editing/TextIterator.cpp

Issue 203973005: Add an overloaded version of findPlainText() accepting two Positions. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/core/editing/TextIterator.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012 Apple Inc. All r ights reserved. 2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012 Apple Inc. All r ights reserved.
3 * Copyright (C) 2005 Alexey Proskuryakov. 3 * Copyright (C) 2005 Alexey Proskuryakov.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
7 * are met: 7 * are met:
8 * 1. Redistributions of source code must retain the above copyright 8 * 1. 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 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
(...skipping 1459 matching lines...) Expand 10 before | Expand all | Expand 10 after
1470 PassRefPtr<Range> SimplifiedBackwardsTextIterator::range() const 1470 PassRefPtr<Range> SimplifiedBackwardsTextIterator::range() const
1471 { 1471 {
1472 if (m_positionNode) 1472 if (m_positionNode)
1473 return Range::create(m_positionNode->document(), m_positionNode, m_posit ionStartOffset, m_positionNode, m_positionEndOffset); 1473 return Range::create(m_positionNode->document(), m_positionNode, m_posit ionStartOffset, m_positionNode, m_positionEndOffset);
1474 1474
1475 return Range::create(m_startNode->document(), m_startNode, m_startOffset, m_ startNode, m_startOffset); 1475 return Range::create(m_startNode->document(), m_startNode, m_startOffset, m_ startNode, m_startOffset);
1476 } 1476 }
1477 1477
1478 // -------- 1478 // --------
1479 1479
1480 CharacterIterator::CharacterIterator(const Range* r, TextIteratorBehaviorFlags b ehavior) 1480 CharacterIterator::CharacterIterator(const Range* range, TextIteratorBehaviorFla gs behavior)
1481 : m_offset(0) 1481 : m_offset(0)
1482 , m_runOffset(0) 1482 , m_runOffset(0)
1483 , m_atBreak(true) 1483 , m_atBreak(true)
1484 , m_textIterator(r, behavior) 1484 , m_textIterator(range, behavior)
1485 {
1486 initialize();
1487 }
1488
1489 CharacterIterator::CharacterIterator(const Position& start, const Position& end, TextIteratorBehaviorFlags behavior)
1490 : m_offset(0)
1491 , m_runOffset(0)
1492 , m_atBreak(true)
1493 , m_textIterator(start, end, behavior)
1494 {
1495 initialize();
1496 }
1497
1498 void CharacterIterator::initialize()
1485 { 1499 {
1486 while (!atEnd() && !m_textIterator.length()) 1500 while (!atEnd() && !m_textIterator.length())
1487 m_textIterator.advance(); 1501 m_textIterator.advance();
1488 } 1502 }
1489 1503
1490 PassRefPtr<Range> CharacterIterator::range() const 1504 PassRefPtr<Range> CharacterIterator::range() const
1491 { 1505 {
1492 RefPtr<Range> r = m_textIterator.range(); 1506 RefPtr<Range> r = m_textIterator.range();
1493 if (!m_textIterator.atEnd()) { 1507 if (!m_textIterator.atEnd()) {
1494 if (m_textIterator.length() <= 1) { 1508 if (m_textIterator.length() <= 1) {
(...skipping 557 matching lines...) Expand 10 before | Expand all | Expand 10 after
2052 return builder.toString(); 2066 return builder.toString();
2053 } 2067 }
2054 2068
2055 static PassRefPtr<Range> collapsedToBoundary(const Range* range, bool forward) 2069 static PassRefPtr<Range> collapsedToBoundary(const Range* range, bool forward)
2056 { 2070 {
2057 RefPtr<Range> result = range->cloneRange(ASSERT_NO_EXCEPTION); 2071 RefPtr<Range> result = range->cloneRange(ASSERT_NO_EXCEPTION);
2058 result->collapse(!forward, ASSERT_NO_EXCEPTION); 2072 result->collapse(!forward, ASSERT_NO_EXCEPTION);
2059 return result.release(); 2073 return result.release();
2060 } 2074 }
2061 2075
2062 static size_t findPlainText(CharacterIterator& it, const String& target, FindOpt ions options, size_t& matchStart) 2076 static size_t findPlainTextInternal(CharacterIterator& it, const String& target, FindOptions options, size_t& matchStart)
2063 { 2077 {
2064 matchStart = 0; 2078 matchStart = 0;
2065 size_t matchLength = 0; 2079 size_t matchLength = 0;
2066 2080
2067 SearchBuffer buffer(target, options); 2081 SearchBuffer buffer(target, options);
2068 2082
2069 if (buffer.needsMoreContext()) { 2083 if (buffer.needsMoreContext()) {
2070 RefPtr<Range> startRange = it.range(); 2084 RefPtr<Range> startRange = it.range();
2071 RefPtr<Range> beforeStartRange = startRange->ownerDocument().createRange (); 2085 RefPtr<Range> beforeStartRange = startRange->ownerDocument().createRange ();
2072 beforeStartRange->setEnd(startRange->startContainer(), startRange->start Offset(), IGNORE_EXCEPTION); 2086 beforeStartRange->setEnd(startRange->startContainer(), startRange->start Offset(), IGNORE_EXCEPTION);
(...skipping 25 matching lines...) Expand all
2098 } 2112 }
2099 if (it.atBreak() && !buffer.atBreak()) { 2113 if (it.atBreak() && !buffer.atBreak()) {
2100 buffer.reachedBreak(); 2114 buffer.reachedBreak();
2101 goto tryAgain; 2115 goto tryAgain;
2102 } 2116 }
2103 } 2117 }
2104 2118
2105 return matchLength; 2119 return matchLength;
2106 } 2120 }
2107 2121
2122 static const TextIteratorBehaviorFlags iteratorFlagsForFindPlainText = TextItera torEntersTextControls | TextIteratorEntersAuthorShadowRoots;
2123
2108 PassRefPtr<Range> findPlainText(const Range* range, const String& target, FindOp tions options) 2124 PassRefPtr<Range> findPlainText(const Range* range, const String& target, FindOp tions options)
2109 { 2125 {
2110 // CharacterIterator requires renderers to be up-to-date 2126 // CharacterIterator requires renderers to be up-to-date
2111 range->ownerDocument().updateLayout(); 2127 range->ownerDocument().updateLayout();
2112 2128
2113 // First, find the text. 2129 // First, find the text.
2114 size_t matchStart; 2130 size_t matchStart;
2115 size_t matchLength; 2131 size_t matchLength;
2116 { 2132 {
2117 CharacterIterator findIterator(range, TextIteratorEntersTextControls | T extIteratorEntersAuthorShadowRoots); 2133 CharacterIterator findIterator(range, iteratorFlagsForFindPlainText);
2118 matchLength = findPlainText(findIterator, target, options, matchStart); 2134 matchLength = findPlainTextInternal(findIterator, target, options, match Start);
2119 if (!matchLength) 2135 if (!matchLength)
2120 return collapsedToBoundary(range, !(options & Backwards)); 2136 return collapsedToBoundary(range, !(options & Backwards));
2121 } 2137 }
2122 2138
2123 // Then, find the document position of the start and the end of the text. 2139 // Then, find the document position of the start and the end of the text.
2124 CharacterIterator computeRangeIterator(range, TextIteratorEntersTextControls | TextIteratorEntersAuthorShadowRoots); 2140 CharacterIterator computeRangeIterator(range, iteratorFlagsForFindPlainText) ;
2125 return characterSubrange(computeRangeIterator, matchStart, matchLength); 2141 return characterSubrange(computeRangeIterator, matchStart, matchLength);
2126 } 2142 }
2127 2143
2144 PassRefPtr<Range> findPlainText(const Position& start, const Position& end, cons t String& target, FindOptions options)
2145 {
2146 // CharacterIterator requires renderers to be up-to-date.
2147 if (!start.inDocument())
2148 return nullptr;
2149 ASSERT(start.document() == end.document());
2150 start.document()->updateLayout();
2151
2152 // FIXME: Reduce the code duplication with above (but how?).
2153 size_t matchStart;
2154 size_t matchLength;
2155 {
2156 CharacterIterator findIterator(start, end, iteratorFlagsForFindPlainText );
2157 matchLength = findPlainTextInternal(findIterator, target, options, match Start);
2158 if (!matchLength) {
2159 const Position& collapseTo = options & Backwards ? start : end;
2160 return Range::create(*start.document(), collapseTo, collapseTo);
2161 }
2162 }
2163
2164 CharacterIterator computeRangeIterator(start, end, iteratorFlagsForFindPlain Text);
2165 return characterSubrange(computeRangeIterator, matchStart, matchLength);
2166 }
2167
2128 } 2168 }
OLDNEW
« no previous file with comments | « Source/core/editing/TextIterator.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698