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

Side by Side Diff: Source/core/html/HTMLTextFormControlElement.cpp

Issue 1269263003: The start and end arguments should be unsigned long in setSelectionRange() function Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 5 years, 4 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) 1999 Lars Knoll (knoll@kde.org) 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org) 3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2001 Dirk Mueller (mueller@kde.org) 4 * (C) 2001 Dirk Mueller (mueller@kde.org)
5 * Copyright (C) 2004, 2005, 2006, 2007 Apple Inc. All rights reserved. 5 * Copyright (C) 2004, 2005, 2006, 2007 Apple Inc. All rights reserved.
6 * (C) 2006 Alexey Proskuryakov (ap@nypop.com) 6 * (C) 2006 Alexey Proskuryakov (ap@nypop.com)
7 * 7 *
8 * This library is free software; you can redistribute it and/or 8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public 9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either 10 * License as published by the Free Software Foundation; either
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 updatePlaceholderText(); 164 updatePlaceholderText();
165 HTMLElement* placeholder = placeholderElement(); 165 HTMLElement* placeholder = placeholderElement();
166 if (!placeholder) 166 if (!placeholder)
167 return; 167 return;
168 168
169 placeholder->setInlineStyleProperty(CSSPropertyDisplay, placeholderShouldBeV isible() ? CSSValueBlock : CSSValueNone); 169 placeholder->setInlineStyleProperty(CSSPropertyDisplay, placeholderShouldBeV isible() ? CSSValueBlock : CSSValueNone);
170 } 170 }
171 171
172 void HTMLTextFormControlElement::setSelectionStart(int start) 172 void HTMLTextFormControlElement::setSelectionStart(int start)
173 { 173 {
174 setSelectionRange(start, std::max(start, selectionEnd()), selectionDirection ()); 174 setSelectionRange(start, std::max<unsigned>(start, selectionEnd()), selectio nDirection());
175 } 175 }
176 176
177 void HTMLTextFormControlElement::setSelectionEnd(int end) 177 void HTMLTextFormControlElement::setSelectionEnd(int end)
178 { 178 {
179 setSelectionRange(std::min(end, selectionStart()), end, selectionDirection() ); 179 setSelectionRange(std::min<unsigned>(end, selectionStart()), end, selectionD irection());
180 } 180 }
181 181
182 void HTMLTextFormControlElement::setSelectionDirection(const String& direction) 182 void HTMLTextFormControlElement::setSelectionDirection(const String& direction)
183 { 183 {
184 setSelectionRange(selectionStart(), selectionEnd(), direction); 184 setSelectionRange(selectionStart(), selectionEnd(), direction);
185 } 185 }
186 186
187 void HTMLTextFormControlElement::select(NeedToDispatchSelectEvent eventBehaviour ) 187 void HTMLTextFormControlElement::select(NeedToDispatchSelectEvent eventBehaviour )
188 { 188 {
189 document().updateLayoutIgnorePendingStylesheets(); 189 document().updateLayoutIgnorePendingStylesheets();
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
259 259
260 if (newSelectionEnd > end) 260 if (newSelectionEnd > end)
261 newSelectionEnd += delta; 261 newSelectionEnd += delta;
262 else if (newSelectionEnd > start) 262 else if (newSelectionEnd > start)
263 newSelectionEnd = start + replacementLength; 263 newSelectionEnd = start + replacementLength;
264 } 264 }
265 265
266 setSelectionRange(newSelectionStart, newSelectionEnd, SelectionHasNoDirectio n); 266 setSelectionRange(newSelectionStart, newSelectionEnd, SelectionHasNoDirectio n);
267 } 267 }
268 268
269 void HTMLTextFormControlElement::setSelectionRange(int start, int end, const Str ing& directionString) 269 void HTMLTextFormControlElement::setSelectionRange(unsigned start, unsigned end, const String& directionString)
270 { 270 {
271 TextFieldSelectionDirection direction = SelectionHasNoDirection; 271 TextFieldSelectionDirection direction = SelectionHasNoDirection;
272 if (directionString == "forward") 272 if (directionString == "forward")
273 direction = SelectionHasForwardDirection; 273 direction = SelectionHasForwardDirection;
274 else if (directionString == "backward") 274 else if (directionString == "backward")
275 direction = SelectionHasBackwardDirection; 275 direction = SelectionHasBackwardDirection;
276 276
277 if (direction == SelectionHasNoDirection && document().frame() && document() .frame()->editor().behavior().shouldConsiderSelectionAsDirectional()) 277 if (direction == SelectionHasNoDirection && document().frame() && document() .frame()->editor().behavior().shouldConsiderSelectionAsDirectional())
278 direction = SelectionHasForwardDirection; 278 direction = SelectionHasForwardDirection;
279 279
280 return setSelectionRange(start, end, direction); 280 return setSelectionRange(start, end, direction);
281 } 281 }
282 282
283 static Position positionForIndex(HTMLElement* innerEditor, int index) 283 static Position positionForIndex(HTMLElement* innerEditor, unsigned index)
284 { 284 {
285 ASSERT(index >= 0);
286 if (index == 0) { 285 if (index == 0) {
287 Node* node = NodeTraversal::next(*innerEditor, innerEditor); 286 Node* node = NodeTraversal::next(*innerEditor, innerEditor);
288 if (node && node->isTextNode()) 287 if (node && node->isTextNode())
289 return Position(node, 0); 288 return Position(node, 0);
290 return Position(innerEditor, 0); 289 return Position(innerEditor, 0);
291 } 290 }
292 int remainingCharactersToMoveForward = index; 291 unsigned remainingCharactersToMoveForward = index;
293 Node* lastBrOrText = innerEditor; 292 Node* lastBrOrText = innerEditor;
294 for (Node& node : NodeTraversal::descendantsOf(*innerEditor)) { 293 for (Node& node : NodeTraversal::descendantsOf(*innerEditor)) {
295 ASSERT(remainingCharactersToMoveForward >= 0); 294 ASSERT(remainingCharactersToMoveForward >= 0);
296 if (node.hasTagName(brTag)) { 295 if (node.hasTagName(brTag)) {
297 if (remainingCharactersToMoveForward == 0) 296 if (remainingCharactersToMoveForward == 0)
298 return positionBeforeNode(&node); 297 return positionBeforeNode(&node);
299 --remainingCharactersToMoveForward; 298 --remainingCharactersToMoveForward;
300 lastBrOrText = &node; 299 lastBrOrText = &node;
301 continue; 300 continue;
302 } 301 }
303 302
304 if (node.isTextNode()) { 303 if (node.isTextNode()) {
305 Text& text = toText(node); 304 Text& text = toText(node);
306 if (remainingCharactersToMoveForward < static_cast<int>(text.length( ))) 305 if (remainingCharactersToMoveForward < text.length())
307 return Position(&text, remainingCharactersToMoveForward); 306 return Position(&text, remainingCharactersToMoveForward);
308 remainingCharactersToMoveForward -= text.length(); 307 remainingCharactersToMoveForward -= text.length();
309 lastBrOrText = &node; 308 lastBrOrText = &node;
310 continue; 309 continue;
311 } 310 }
312 311
313 ASSERT_NOT_REACHED(); 312 ASSERT_NOT_REACHED();
314 } 313 }
315 return lastPositionInOrAfterNode(lastBrOrText); 314 return lastPositionInOrAfterNode(lastBrOrText);
316 } 315 }
317 316
318 static int indexForPosition(HTMLElement* innerEditor, const Position& passedPosi tion) 317 static unsigned indexForPosition(HTMLElement* innerEditor, const Position& passe dPosition)
319 { 318 {
320 if (!innerEditor || !innerEditor->contains(passedPosition.anchorNode()) || p assedPosition.isNull()) 319 if (!innerEditor || !innerEditor->contains(passedPosition.anchorNode()) || p assedPosition.isNull())
321 return 0; 320 return 0;
322 321
323 if (positionBeforeNode(innerEditor) == passedPosition) 322 if (positionBeforeNode(innerEditor) == passedPosition)
324 return 0; 323 return 0;
325 324
326 int index = 0; 325 unsigned index = 0;
327 Node* startNode = passedPosition.computeNodeBeforePosition(); 326 Node* startNode = passedPosition.computeNodeBeforePosition();
328 if (!startNode) 327 if (!startNode)
329 startNode = passedPosition.computeContainerNode(); 328 startNode = passedPosition.computeContainerNode();
330 ASSERT(startNode); 329 ASSERT(startNode);
331 ASSERT(innerEditor->contains(startNode)); 330 ASSERT(innerEditor->contains(startNode));
332 331
333 for (Node* node = startNode; node; node = NodeTraversal::previous(*node, inn erEditor)) { 332 for (Node* node = startNode; node; node = NodeTraversal::previous(*node, inn erEditor)) {
334 if (node->isTextNode()) { 333 if (node->isTextNode()) {
335 int length = toText(*node).length(); 334 unsigned length = toText(*node).length();
336 if (node == passedPosition.computeContainerNode()) 335 if (node == passedPosition.computeContainerNode())
337 index += std::min(length, passedPosition.offsetInContainerNode() ); 336 index += std::min<unsigned>(length, passedPosition.offsetInConta inerNode());
philipj_slow 2015/08/14 07:45:52 offsetInContainerNode() returns int. Maybe assign
338 else 337 else
339 index += length; 338 index += length;
340 } else if (node->hasTagName(brTag)) { 339 } else if (node->hasTagName(brTag)) {
341 ++index; 340 ++index;
342 } 341 }
343 } 342 }
344 343
345 ASSERT(index >= 0);
346 return index; 344 return index;
347 } 345 }
348 346
349 void HTMLTextFormControlElement::setSelectionRange(int start, int end, TextField SelectionDirection direction, NeedToDispatchSelectEvent eventBehaviour, Selectio nOption selectionOption) 347 void HTMLTextFormControlElement::setSelectionRange(unsigned start, unsigned end, TextFieldSelectionDirection direction, NeedToDispatchSelectEvent eventBehaviour , SelectionOption selectionOption)
350 { 348 {
351 if (openShadowRoot() || !isTextFormControl() || !inDocument()) 349 if (openShadowRoot() || !isTextFormControl() || !inDocument())
352 return; 350 return;
353 351
354 const int editorValueLength = static_cast<int>(innerEditorValue().length()); 352 const unsigned editorValueLength = static_cast<unsigned>(innerEditorValue(). length());
philipj_slow 2015/08/14 07:45:52 String::length() is already unsigned.
tanay.c 2015/08/17 12:06:32 Done.
355 ASSERT(editorValueLength >= 0); 353 end = std::min<unsigned>(end, editorValueLength);
356 end = std::max(std::min(end, editorValueLength), 0); 354 start = std::min<unsigned>(start, end);
357 start = std::min(std::max(start, 0), end);
358 cacheSelection(start, end, direction); 355 cacheSelection(start, end, direction);
359 356
360 if (selectionOption == NotChangeSelection || (selectionOption == ChangeSelec tionIfFocused && document().focusedElement() != this)) { 357 if (selectionOption == NotChangeSelection || (selectionOption == ChangeSelec tionIfFocused && document().focusedElement() != this)) {
361 if (eventBehaviour == DispatchSelectEvent) 358 if (eventBehaviour == DispatchSelectEvent)
362 scheduleSelectEvent(); 359 scheduleSelectEvent();
363 return; 360 return;
364 } 361 }
365 362
366 LocalFrame* frame = document().frame(); 363 LocalFrame* frame = document().frame();
367 HTMLElement* innerEditor = innerEditorElement(); 364 HTMLElement* innerEditor = innerEditorElement();
368 if (!frame || !innerEditor) 365 if (!frame || !innerEditor)
369 return; 366 return;
370 367
371 Position startPosition = positionForIndex(innerEditor, start); 368 Position startPosition = positionForIndex(innerEditor, start);
372 Position endPosition = start == end ? startPosition : positionForIndex(inner Editor, end); 369 Position endPosition = start == end ? startPosition : positionForIndex(inner Editor, end);
373 370
374 ASSERT(start == indexForPosition(innerEditor, startPosition)); 371 ASSERT(start == (unsigned)indexForPosition(innerEditor, startPosition));
philipj_slow 2015/08/14 07:45:52 Don't use C-style casts.
tanay.c 2015/08/17 12:06:32 Done.
375 ASSERT(end == indexForPosition(innerEditor, endPosition)); 372 ASSERT(end == (unsigned)indexForPosition(innerEditor, endPosition));
376 373
377 // startPosition and endPosition can be null position for example when 374 // startPosition and endPosition can be null position for example when
378 // "-webkit-user-select: none" style attribute is specified. 375 // "-webkit-user-select: none" style attribute is specified.
379 if (startPosition.isNotNull() && endPosition.isNotNull()) { 376 if (startPosition.isNotNull() && endPosition.isNotNull()) {
380 ASSERT(startPosition.anchorNode()->shadowHost() == this 377 ASSERT(startPosition.anchorNode()->shadowHost() == this
381 && endPosition.anchorNode()->shadowHost() == this); 378 && endPosition.anchorNode()->shadowHost() == this);
382 } 379 }
383 VisibleSelection newSelection; 380 VisibleSelection newSelection;
384 if (direction == SelectionHasBackwardDirection) 381 if (direction == SelectionHasBackwardDirection)
385 newSelection.setWithoutValidation(endPosition, startPosition); 382 newSelection.setWithoutValidation(endPosition, startPosition);
(...skipping 24 matching lines...) Expand all
410 Position indexPosition = pos.deepEquivalent().parentAnchoredEquivalent(); 407 Position indexPosition = pos.deepEquivalent().parentAnchoredEquivalent();
411 if (enclosingTextFormControl(indexPosition) != this) 408 if (enclosingTextFormControl(indexPosition) != this)
412 return 0; 409 return 0;
413 ASSERT(indexPosition.document()); 410 ASSERT(indexPosition.document());
414 RefPtrWillBeRawPtr<Range> range = Range::create(*indexPosition.document()); 411 RefPtrWillBeRawPtr<Range> range = Range::create(*indexPosition.document());
415 range->setStart(innerEditorElement(), 0, ASSERT_NO_EXCEPTION); 412 range->setStart(innerEditorElement(), 0, ASSERT_NO_EXCEPTION);
416 range->setEnd(indexPosition.computeContainerNode(), indexPosition.offsetInCo ntainerNode(), ASSERT_NO_EXCEPTION); 413 range->setEnd(indexPosition.computeContainerNode(), indexPosition.offsetInCo ntainerNode(), ASSERT_NO_EXCEPTION);
417 return TextIterator::rangeLength(range->startPosition(), range->endPosition( )); 414 return TextIterator::rangeLength(range->startPosition(), range->endPosition( ));
418 } 415 }
419 416
420 int HTMLTextFormControlElement::selectionStart() const 417 unsigned HTMLTextFormControlElement::selectionStart() const
421 { 418 {
422 if (!isTextFormControl()) 419 if (!isTextFormControl())
423 return 0; 420 return 0;
424 if (document().focusedElement() != this) 421 if (document().focusedElement() != this)
425 return m_cachedSelectionStart; 422 return m_cachedSelectionStart;
426 423
427 return computeSelectionStart(); 424 return computeSelectionStart();
428 } 425 }
429 426
430 int HTMLTextFormControlElement::computeSelectionStart() const 427 unsigned HTMLTextFormControlElement::computeSelectionStart() const
431 { 428 {
432 ASSERT(isTextFormControl()); 429 ASSERT(isTextFormControl());
433 LocalFrame* frame = document().frame(); 430 LocalFrame* frame = document().frame();
434 if (!frame) 431 if (!frame)
435 return 0; 432 return 0;
436 433
437 return indexForPosition(innerEditorElement(), frame->selection().start()); 434 return indexForPosition(innerEditorElement(), frame->selection().start());
438 } 435 }
439 436
440 int HTMLTextFormControlElement::selectionEnd() const 437 unsigned HTMLTextFormControlElement::selectionEnd() const
441 { 438 {
442 if (!isTextFormControl()) 439 if (!isTextFormControl())
443 return 0; 440 return 0;
444 if (document().focusedElement() != this) 441 if (document().focusedElement() != this)
445 return m_cachedSelectionEnd; 442 return m_cachedSelectionEnd;
446 return computeSelectionEnd(); 443 return computeSelectionEnd();
447 } 444 }
448 445
449 int HTMLTextFormControlElement::computeSelectionEnd() const 446 unsigned HTMLTextFormControlElement::computeSelectionEnd() const
450 { 447 {
451 ASSERT(isTextFormControl()); 448 ASSERT(isTextFormControl());
452 LocalFrame* frame = document().frame(); 449 LocalFrame* frame = document().frame();
453 if (!frame) 450 if (!frame)
454 return 0; 451 return 0;
455 452
456 return indexForPosition(innerEditorElement(), frame->selection().end()); 453 return indexForPosition(innerEditorElement(), frame->selection().end());
457 } 454 }
458 455
459 static const AtomicString& directionString(TextFieldSelectionDirection direction ) 456 static const AtomicString& directionString(TextFieldSelectionDirection direction )
(...skipping 29 matching lines...) Expand all
489 { 486 {
490 ASSERT(isTextFormControl()); 487 ASSERT(isTextFormControl());
491 LocalFrame* frame = document().frame(); 488 LocalFrame* frame = document().frame();
492 if (!frame) 489 if (!frame)
493 return SelectionHasNoDirection; 490 return SelectionHasNoDirection;
494 491
495 const VisibleSelection& selection = frame->selection().selection(); 492 const VisibleSelection& selection = frame->selection().selection();
496 return selection.isDirectional() ? (selection.isBaseFirst() ? SelectionHasFo rwardDirection : SelectionHasBackwardDirection) : SelectionHasNoDirection; 493 return selection.isDirectional() ? (selection.isBaseFirst() ? SelectionHasFo rwardDirection : SelectionHasBackwardDirection) : SelectionHasNoDirection;
497 } 494 }
498 495
499 static inline void setContainerAndOffsetForRange(Node* node, int offset, Node*& containerNode, int& offsetInContainer) 496 static inline void setContainerAndOffsetForRange(Node* node, unsigned offset, No de*& containerNode, unsigned& offsetInContainer)
500 { 497 {
501 if (node->isTextNode()) { 498 if (node->isTextNode()) {
502 containerNode = node; 499 containerNode = node;
503 offsetInContainer = offset; 500 offsetInContainer = offset;
504 } else { 501 } else {
505 containerNode = node->parentNode(); 502 containerNode = node->parentNode();
506 offsetInContainer = node->nodeIndex() + offset; 503 offsetInContainer = node->nodeIndex() + offset;
507 } 504 }
508 } 505 }
509 506
510 PassRefPtrWillBeRawPtr<Range> HTMLTextFormControlElement::selection() const 507 PassRefPtrWillBeRawPtr<Range> HTMLTextFormControlElement::selection() const
511 { 508 {
512 if (!layoutObject() || !isTextFormControl()) 509 if (!layoutObject() || !isTextFormControl())
513 return nullptr; 510 return nullptr;
514 511
515 int start = m_cachedSelectionStart; 512 unsigned start = m_cachedSelectionStart;
516 int end = m_cachedSelectionEnd; 513 unsigned end = m_cachedSelectionEnd;
517 514
518 ASSERT(start <= end); 515 ASSERT(start <= end);
519 HTMLElement* innerText = innerEditorElement(); 516 HTMLElement* innerText = innerEditorElement();
520 if (!innerText) 517 if (!innerText)
521 return nullptr; 518 return nullptr;
522 519
523 if (!innerText->hasChildren()) 520 if (!innerText->hasChildren())
524 return Range::create(document(), innerText, 0, innerText, 0); 521 return Range::create(document(), innerText, 0, innerText, 0);
525 522
526 int offset = 0; 523 unsigned offset = 0;
527 Node* startNode = 0; 524 Node* startNode = 0;
528 Node* endNode = 0; 525 Node* endNode = 0;
529 for (Node& node : NodeTraversal::descendantsOf(*innerText)) { 526 for (Node& node : NodeTraversal::descendantsOf(*innerText)) {
530 ASSERT(!node.hasChildren()); 527 ASSERT(!node.hasChildren());
531 ASSERT(node.isTextNode() || isHTMLBRElement(node)); 528 ASSERT(node.isTextNode() || isHTMLBRElement(node));
532 int length = node.isTextNode() ? lastOffsetInNode(&node) : 1; 529 int length = node.isTextNode() ? lastOffsetInNode(&node) : 1;
533 530
534 if (offset <= start && start <= offset + length) 531 if (offset <= start && start <= offset + length)
535 setContainerAndOffsetForRange(&node, start - offset, startNode, star t); 532 setContainerAndOffsetForRange(&node, start - offset, startNode, star t);
536 533
(...skipping 466 matching lines...) Expand 10 before | Expand all | Expand 10 after
1003 } 1000 }
1004 1001
1005 void HTMLTextFormControlElement::copyNonAttributePropertiesFromElement(const Ele ment& source) 1002 void HTMLTextFormControlElement::copyNonAttributePropertiesFromElement(const Ele ment& source)
1006 { 1003 {
1007 const HTMLTextFormControlElement& sourceElement = static_cast<const HTMLText FormControlElement&>(source); 1004 const HTMLTextFormControlElement& sourceElement = static_cast<const HTMLText FormControlElement&>(source);
1008 m_lastChangeWasUserEdit = sourceElement.m_lastChangeWasUserEdit; 1005 m_lastChangeWasUserEdit = sourceElement.m_lastChangeWasUserEdit;
1009 HTMLFormControlElement::copyNonAttributePropertiesFromElement(source); 1006 HTMLFormControlElement::copyNonAttributePropertiesFromElement(source);
1010 } 1007 }
1011 1008
1012 } // namespace blink 1009 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698