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

Side by Side Diff: Source/core/editing/spellcheck/TextCheckingHelper.cpp

Issue 1329253002: WIP: prefer using EphemeralRange for SpellChecker. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: introduce Range::dispose() Created 5 years, 3 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
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved. 2 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies) 3 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
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 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 misspelling.location = start + wordStart + misspellingLocation; 92 misspelling.location = start + wordStart + misspellingLocation;
93 misspelling.length = misspellingLength; 93 misspelling.length = misspellingLength;
94 misspelling.replacement = client.getAutoCorrectSuggestionForMisspell edWord(String(text + misspelling.location, misspelling.length)); 94 misspelling.replacement = client.getAutoCorrectSuggestionForMisspell edWord(String(text + misspelling.location, misspelling.length));
95 results.append(misspelling); 95 results.append(misspelling);
96 } 96 }
97 97
98 wordStart = wordEnd; 98 wordStart = wordEnd;
99 } 99 }
100 } 100 }
101 101
102 void expandRangeToSentenceBoundary(Range& range) 102 EphemeralRange expandRangeToSentenceBoundary(const EphemeralRange& range)
103 { 103 {
104 range.setStart(startOfSentence(createVisiblePosition(range.startPosition())) .deepEquivalent()); 104 return EphemeralRange(startOfSentence(createVisiblePosition(range.startPosit ion())).deepEquivalent(), endOfSentence(createVisiblePosition(range.endPosition( ))).deepEquivalent());
105 range.setEnd(endOfSentence(createVisiblePosition(range.endPosition())).deepE quivalent());
106 } 105 }
107 106
108 static PassRefPtrWillBeRawPtr<Range> expandToParagraphBoundary(PassRefPtrWillBeR awPtr<Range> range) 107 static EphemeralRange expandToParagraphBoundary(const EphemeralRange& range)
109 { 108 {
110 RefPtrWillBeRawPtr<Range> paragraphRange = range->cloneRange(); 109 return EphemeralRange(startOfParagraph(createVisiblePosition(range.startPosi tion())).deepEquivalent(), endOfParagraph(createVisiblePosition(range.endPositio n())).deepEquivalent());
111 paragraphRange->setStart(startOfParagraph(createVisiblePosition(range->start Position())).deepEquivalent());
112 paragraphRange->setEnd(endOfParagraph(createVisiblePosition(range->endPositi on())).deepEquivalent());
113 return paragraphRange;
114 } 110 }
115 111
116 TextCheckingParagraph::TextCheckingParagraph(PassRefPtrWillBeRawPtr<Range> check ingRange) 112 TextCheckingParagraph::TextCheckingParagraph(const EphemeralRange checkingRange)
117 : m_checkingRange(checkingRange) 113 : m_checkingRange(checkingRange)
118 , m_checkingStart(-1) 114 , m_checkingStart(-1)
119 , m_checkingEnd(-1) 115 , m_checkingEnd(-1)
120 , m_checkingLength(-1) 116 , m_checkingLength(-1)
121 { 117 {
122 } 118 }
123 119
124 TextCheckingParagraph::TextCheckingParagraph(PassRefPtrWillBeRawPtr<Range> check ingRange, PassRefPtrWillBeRawPtr<Range> paragraphRange) 120 TextCheckingParagraph::TextCheckingParagraph(const EphemeralRange checkingRange, const EphemeralRange paragraphRange)
125 : m_checkingRange(checkingRange) 121 : m_checkingRange(checkingRange)
126 , m_paragraphRange(paragraphRange) 122 , m_paragraphRange(paragraphRange)
127 , m_checkingStart(-1) 123 , m_checkingStart(-1)
128 , m_checkingEnd(-1) 124 , m_checkingEnd(-1)
129 , m_checkingLength(-1) 125 , m_checkingLength(-1)
130 { 126 {
131 } 127 }
132 128
129 TextCheckingParagraph::TextCheckingParagraph(PassRefPtrWillBeRawPtr<Range> check ingRange, PassRefPtrWillBeRawPtr<Range> paragraphRange)
130 : m_checkingRange(checkingRange.get())
131 , m_paragraphRange(paragraphRange.get())
132 , m_checkingStart(-1)
133 , m_checkingEnd(-1)
134 , m_checkingLength(-1)
135 {
136 }
137
133 TextCheckingParagraph::~TextCheckingParagraph() 138 TextCheckingParagraph::~TextCheckingParagraph()
134 { 139 {
135 } 140 }
136 141
137 void TextCheckingParagraph::expandRangeToNextEnd() 142 void TextCheckingParagraph::expandRangeToNextEnd()
138 { 143 {
139 ASSERT(m_checkingRange); 144 ASSERT(m_checkingRange.isNotNull());
140 paragraphRange()->setEnd(endOfParagraph(startOfNextParagraph(createVisiblePo sition(paragraphRange()->startPosition()))).deepEquivalent()); 145 setParagraphRange(EphemeralRange(paragraphRange().startPosition(), endOfPara graph(startOfNextParagraph(createVisiblePosition(paragraphRange().startPosition( )))).deepEquivalent()));
141 invalidateParagraphRangeValues(); 146 invalidateParagraphRangeValues();
142 } 147 }
143 148
144 void TextCheckingParagraph::invalidateParagraphRangeValues() 149 void TextCheckingParagraph::invalidateParagraphRangeValues()
145 { 150 {
146 m_checkingStart = m_checkingEnd = -1; 151 m_checkingStart = m_checkingEnd = -1;
147 m_offsetAsRange = nullptr; 152 m_offsetAsRange = EphemeralRange();
148 m_text = String(); 153 m_text = String();
149 } 154 }
150 155
151 int TextCheckingParagraph::rangeLength() const 156 int TextCheckingParagraph::rangeLength() const
152 { 157 {
153 ASSERT(m_checkingRange); 158 ASSERT(m_checkingRange.isNotNull());
154 return TextIterator::rangeLength(paragraphRange()->startPosition(), paragrap hRange()->endPosition()); 159 return TextIterator::rangeLength(paragraphRange().startPosition(), paragraph Range().endPosition());
155 } 160 }
156 161
157 PassRefPtrWillBeRawPtr<Range> TextCheckingParagraph::paragraphRange() const 162 EphemeralRange TextCheckingParagraph::paragraphRange() const
158 { 163 {
159 ASSERT(m_checkingRange); 164 ASSERT(m_checkingRange.isNotNull());
160 if (!m_paragraphRange) 165 if (m_paragraphRange.isNull())
161 m_paragraphRange = expandToParagraphBoundary(checkingRange()); 166 m_paragraphRange = expandToParagraphBoundary(checkingRange());
162 return m_paragraphRange; 167 return m_paragraphRange;
163 } 168 }
164 169
165 PassRefPtrWillBeRawPtr<Range> TextCheckingParagraph::subrange(int characterOffse t, int characterCount) const 170 void TextCheckingParagraph::setParagraphRange(const EphemeralRange& range)
166 { 171 {
167 ASSERT(m_checkingRange); 172 m_paragraphRange = range;
168 return createRange(calculateCharacterSubrange(EphemeralRange(paragraphRange( ).get()), characterOffset, characterCount)); 173 }
174
175 EphemeralRange TextCheckingParagraph::subrange(int characterOffset, int characte rCount) const
176 {
177 ASSERT(m_checkingRange.isNotNull());
178 return calculateCharacterSubrange(paragraphRange(), characterOffset, charact erCount);
169 } 179 }
170 180
171 int TextCheckingParagraph::offsetTo(const Position& position, ExceptionState& ex ceptionState) const 181 int TextCheckingParagraph::offsetTo(const Position& position, ExceptionState& ex ceptionState) const
172 { 182 {
173 ASSERT(m_checkingRange); 183 ASSERT(m_checkingRange.isNotNull());
174 RefPtrWillBeRawPtr<Range> range = offsetAsRange()->cloneRange(); 184 RefPtrWillBeRawPtr<Range> range = createRange(offsetAsRange());
yosin_UTC9 2015/09/10 01:58:39 Let's avoid to use |Range|. This could be: return
sof 2015/09/10 06:12:10 Don't we need to account for the exception-raising
yosin_UTC9 2015/09/10 06:37:03 Since caller is SpellChecker::markAndReplaceFor()
sof 2015/09/10 06:42:27 But this is a public function taking an ExceptionS
yosin_UTC9 2015/09/10 07:01:46 Yes, please. We don't want to have V8 thing in spe
sof 2015/09/10 07:16:27 Good, let's simplify that away.
175 range->setEnd(position.computeContainerNode(), position.computeOffsetInConta inerNode(), exceptionState); 185 range->setEnd(position.computeContainerNode(), position.computeOffsetInConta inerNode(), exceptionState);
176 if (exceptionState.hadException()) 186 if (exceptionState.hadException())
177 return 0; 187 return 0;
178 return TextIterator::rangeLength(range->startPosition(), range->endPosition( )); 188 int offset = TextIterator::rangeLength(range->startPosition(), range->endPos ition());
189 range->dispose();
190 return offset;
179 } 191 }
180 192
181 bool TextCheckingParagraph::isEmpty() const 193 bool TextCheckingParagraph::isEmpty() const
182 { 194 {
183 // Both predicates should have same result, but we check both just for sure. 195 // Both predicates should have same result, but we check both just for sure.
184 // We need to investigate to remove this redundancy. 196 // We need to investigate to remove this redundancy.
185 return isRangeEmpty() || isTextEmpty(); 197 return isRangeEmpty() || isTextEmpty();
186 } 198 }
187 199
188 PassRefPtrWillBeRawPtr<Range> TextCheckingParagraph::offsetAsRange() const 200 EphemeralRange TextCheckingParagraph::offsetAsRange() const
189 { 201 {
190 ASSERT(m_checkingRange); 202 ASSERT(m_checkingRange.isNotNull());
191 if (!m_offsetAsRange) 203 if (m_offsetAsRange.isNull())
192 m_offsetAsRange = Range::create(paragraphRange()->startContainer()->docu ment(), paragraphRange()->startPosition(), checkingRange()->startPosition()); 204 m_offsetAsRange = EphemeralRange(paragraphRange().startPosition(), check ingRange().startPosition());
193 205
194 return m_offsetAsRange; 206 return m_offsetAsRange;
195 } 207 }
196 208
197 const String& TextCheckingParagraph::text() const 209 const String& TextCheckingParagraph::text() const
198 { 210 {
199 ASSERT(m_checkingRange); 211 ASSERT(m_checkingRange.isNotNull());
200 if (m_text.isEmpty()) 212 if (m_text.isEmpty())
201 m_text = plainText(EphemeralRange(paragraphRange().get())); 213 m_text = plainText(paragraphRange());
202 return m_text; 214 return m_text;
203 } 215 }
204 216
205 int TextCheckingParagraph::checkingStart() const 217 int TextCheckingParagraph::checkingStart() const
206 { 218 {
207 ASSERT(m_checkingRange); 219 ASSERT(m_checkingRange.isNotNull());
208 if (m_checkingStart == -1) 220 if (m_checkingStart == -1)
209 m_checkingStart = TextIterator::rangeLength(offsetAsRange()->startPositi on(), offsetAsRange()->endPosition()); 221 m_checkingStart = TextIterator::rangeLength(offsetAsRange().startPositio n(), offsetAsRange().endPosition());
210 return m_checkingStart; 222 return m_checkingStart;
211 } 223 }
212 224
213 int TextCheckingParagraph::checkingEnd() const 225 int TextCheckingParagraph::checkingEnd() const
214 { 226 {
215 ASSERT(m_checkingRange); 227 ASSERT(m_checkingRange.isNotNull());
216 if (m_checkingEnd == -1) 228 if (m_checkingEnd == -1)
217 m_checkingEnd = checkingStart() + TextIterator::rangeLength(checkingRang e()->startPosition(), checkingRange()->endPosition()); 229 m_checkingEnd = checkingStart() + TextIterator::rangeLength(checkingRang e().startPosition(), checkingRange().endPosition());
218 return m_checkingEnd; 230 return m_checkingEnd;
219 } 231 }
220 232
221 int TextCheckingParagraph::checkingLength() const 233 int TextCheckingParagraph::checkingLength() const
222 { 234 {
223 ASSERT(m_checkingRange); 235 ASSERT(m_checkingRange.isNotNull());
224 if (-1 == m_checkingLength) 236 if (-1 == m_checkingLength)
225 m_checkingLength = TextIterator::rangeLength(checkingRange()->startPosit ion(), checkingRange()->endPosition()); 237 m_checkingLength = TextIterator::rangeLength(checkingRange().startPositi on(), checkingRange().endPosition());
226 return m_checkingLength; 238 return m_checkingLength;
227 } 239 }
228 240
229 TextCheckingHelper::TextCheckingHelper(SpellCheckerClient& client, const Positio n& start, const Position& end) 241 TextCheckingHelper::TextCheckingHelper(SpellCheckerClient& client, const Positio n& start, const Position& end)
230 : m_client(&client) 242 : m_client(&client)
231 , m_start(start) 243 , m_start(start)
232 , m_end(end) 244 , m_end(end)
233 { 245 {
234 } 246 }
235 247
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after
453 outGrammarDetail.length = 0; 465 outGrammarDetail.length = 0;
454 outGrammarDetail.guesses.clear(); 466 outGrammarDetail.guesses.clear();
455 outGrammarDetail.userDescription = ""; 467 outGrammarDetail.userDescription = "";
456 outGrammarPhraseOffset = 0; 468 outGrammarPhraseOffset = 0;
457 469
458 String firstBadGrammarPhrase; 470 String firstBadGrammarPhrase;
459 471
460 // Expand the search range to encompass entire paragraphs, since grammar che cking needs that much context. 472 // Expand the search range to encompass entire paragraphs, since grammar che cking needs that much context.
461 // Determine the character offset from the start of the paragraph to the sta rt of the original search range, 473 // Determine the character offset from the start of the paragraph to the sta rt of the original search range,
462 // since we will want to ignore results in this area. 474 // since we will want to ignore results in this area.
463 TextCheckingParagraph paragraph(Range::create(m_start.computeContainerNode() ->document(), m_start, m_end)); 475 TextCheckingParagraph paragraph(EphemeralRange(m_start, m_end));
464 476
465 // Start checking from beginning of paragraph, but skip past results that oc cur before the start of the original search range. 477 // Start checking from beginning of paragraph, but skip past results that oc cur before the start of the original search range.
466 int startOffset = 0; 478 int startOffset = 0;
467 while (startOffset < paragraph.checkingEnd()) { 479 while (startOffset < paragraph.checkingEnd()) {
468 Vector<GrammarDetail> grammarDetails; 480 Vector<GrammarDetail> grammarDetails;
469 int badGrammarPhraseLocation = -1; 481 int badGrammarPhraseLocation = -1;
470 int badGrammarPhraseLength = 0; 482 int badGrammarPhraseLength = 0;
471 m_client->textChecker().checkGrammarOfString(paragraph.textSubstring(sta rtOffset), grammarDetails, &badGrammarPhraseLocation, &badGrammarPhraseLength); 483 m_client->textChecker().checkGrammarOfString(paragraph.textSubstring(sta rtOffset), grammarDetails, &badGrammarPhraseLocation, &badGrammarPhraseLength);
472 484
473 if (!badGrammarPhraseLength) { 485 if (!badGrammarPhraseLength) {
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
568 return false; 580 return false;
569 581
570 const Settings* settings = frame->settings(); 582 const Settings* settings = frame->settings();
571 if (!settings) 583 if (!settings)
572 return false; 584 return false;
573 585
574 return settings->unifiedTextCheckerEnabled(); 586 return settings->unifiedTextCheckerEnabled();
575 } 587 }
576 588
577 } 589 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698