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

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

Issue 1305963003: Introduce {next,previous}PositionOf() as replacement of {next,previous}() member functions in Positi (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: 2015-08-22T00:12:04 Rebase 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
« no previous file with comments | « Source/core/editing/Position.h ('k') | Source/core/editing/PositionTest.cpp » ('j') | 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, 2009 Apple Inc. All rights reserved. 2 * Copyright (C) 2004, 2005, 2006, 2009 Apple 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 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 int offsetB = positionB.computeOffsetInContainerNode(); 292 int offsetB = positionB.computeOffsetInContainerNode();
293 return comparePositionsInComposedTree(containerA, offsetA, containerB, offse tB); 293 return comparePositionsInComposedTree(containerA, offsetA, containerB, offse tB);
294 } 294 }
295 295
296 template <typename Strategy> 296 template <typename Strategy>
297 int PositionAlgorithm<Strategy>::compareTo(const PositionAlgorithm<Strategy>& ot her) const 297 int PositionAlgorithm<Strategy>::compareTo(const PositionAlgorithm<Strategy>& ot her) const
298 { 298 {
299 return comparePositions(*this, other); 299 return comparePositions(*this, other);
300 } 300 }
301 301
302 // TODO(yosin) We should move |uncheckedPreviousOffsetForBackwardDeletion()|
303 // to "EditingUtilities.cpp" with |previousPositionOf()|.
302 // TODO(yosin) To avoid forward declaration, we should move implementation of 304 // TODO(yosin) To avoid forward declaration, we should move implementation of
303 // |uncheckedPreviousOffsetForBackwardDeletion()| here. 305 // |uncheckedPreviousOffsetForBackwardDeletion()| here.
304 static int uncheckedPreviousOffsetForBackwardDeletion(const Node*, int current); 306 static int uncheckedPreviousOffsetForBackwardDeletion(const Node*, int current);
305 307
306 template <typename Strategy> 308 template <typename Strategy>
307 PositionAlgorithm<Strategy> PositionAlgorithm<Strategy>::previous(PositionMoveTy pe moveType) const 309 PositionAlgorithm<Strategy> previousPositionOfAlgorithm(const PositionAlgorithm< Strategy>& position, PositionMoveType moveType)
308 { 310 {
309 Node* node = anchorNode(); 311 Node* const node = position.anchorNode();
310 if (!node) 312 if (!node)
311 return PositionAlgorithm<Strategy>(*this); 313 return position;
312 314
313 int offset = computeEditingOffset(); 315 const int offset = position.computeEditingOffset();
314 316
315 if (offset > 0) { 317 if (offset > 0) {
316 if (editingIgnoresContent(node)) 318 if (editingIgnoresContent(node))
317 return beforeNode(node); 319 return PositionAlgorithm<Strategy>::beforeNode(node);
318 if (Node* child = Strategy::childAt(*node, offset - 1)) 320 if (Node* child = Strategy::childAt(*node, offset - 1))
319 return lastPositionInOrAfterNode(child); 321 return PositionAlgorithm<Strategy>::lastPositionInOrAfterNode(child) ;
320 322
321 // There are two reasons child might be 0: 323 // There are two reasons child might be 0:
322 // 1) The node is node like a text node that is not an element, and th erefore has no children. 324 // 1) The node is node like a text node that is not an element, and
323 // Going backward one character at a time is correct. 325 // therefore has no children. Going backward one character at a
324 // 2) The old offset was a bogus offset like (<br>, 1), and there is n o child. 326 // time is correct.
325 // Going from 1 to 0 is correct. 327 // 2) The old offset was a bogus offset like (<br>, 1), and there is
328 // no child. Going from 1 to 0 is correct.
326 switch (moveType) { 329 switch (moveType) {
327 case CodePoint: 330 case PositionMoveType::CodePoint:
328 return PositionAlgorithm<Strategy>(node, offset - 1); 331 return PositionAlgorithm<Strategy>(node, offset - 1);
329 case Character: 332 case PositionMoveType::Character:
330 return PositionAlgorithm<Strategy>(node, uncheckedPreviousOffset(nod e, offset)); 333 return PositionAlgorithm<Strategy>(node, uncheckedPreviousOffset(nod e, offset));
331 case BackwardDeletion: 334 case PositionMoveType::BackwardDeletion:
332 return PositionAlgorithm<Strategy>(node, uncheckedPreviousOffsetForB ackwardDeletion(node, offset)); 335 return PositionAlgorithm<Strategy>(node, uncheckedPreviousOffsetForB ackwardDeletion(node, offset));
333 } 336 }
334 } 337 }
335 338
336 if (ContainerNode* parent = Strategy::parent(*node)) { 339 if (ContainerNode* parent = Strategy::parent(*node)) {
337 if (editingIgnoresContent(parent)) 340 if (editingIgnoresContent(parent))
338 return beforeNode(parent); 341 return PositionAlgorithm<Strategy>::beforeNode(parent);
339 // TODO(yosin) We should use |Strategy::index(Node&)| instead of 342 // TODO(yosin) We should use |Strategy::index(Node&)| instead of
340 // |Node::nodeIndex()|. 343 // |Node::nodeIndex()|.
341 return PositionAlgorithm<Strategy>(parent, node->nodeIndex()); 344 return PositionAlgorithm<Strategy>(parent, node->nodeIndex());
342 } 345 }
343 return PositionAlgorithm<Strategy>(*this); 346 return position;
347 }
348
349 Position previousPositionOf(const Position& position, PositionMoveType moveType)
350 {
351 return previousPositionOfAlgorithm<EditingStrategy>(position, moveType);
352 }
353
354 PositionInComposedTree previousPositionOf(const PositionInComposedTree& position , PositionMoveType moveType)
355 {
356 return previousPositionOfAlgorithm<EditingInComposedTreeStrategy>(position, moveType);
344 } 357 }
345 358
346 template <typename Strategy> 359 template <typename Strategy>
347 PositionAlgorithm<Strategy> PositionAlgorithm<Strategy>::next(PositionMoveType m oveType) const 360 PositionAlgorithm<Strategy> nextPositionOfAlgorithm(const PositionAlgorithm<Stra tegy>& position, PositionMoveType moveType)
348 { 361 {
349 ASSERT(moveType != BackwardDeletion); 362 ASSERT(moveType != PositionMoveType::BackwardDeletion);
350 363
351 Node* node = anchorNode(); 364 Node* node = position.anchorNode();
352 if (!node) 365 if (!node)
353 return PositionAlgorithm<Strategy>(*this); 366 return position;
354 367
355 int offset = computeEditingOffset(); 368 const int offset = position.computeEditingOffset();
356 369
357 if (Node* child = Strategy::childAt(*node, offset)) 370 if (Node* child = Strategy::childAt(*node, offset))
358 return firstPositionInOrBeforeNode(child); 371 return PositionAlgorithm<Strategy>::firstPositionInOrBeforeNode(child);
359 372
360 // TODO(yosin) We should use |Strategy::lastOffsetForEditing()| instead of 373 // TODO(yosin) We should use |Strategy::lastOffsetForEditing()| instead of
361 // DOM tree version. 374 // DOM tree version.
362 if (!Strategy::hasChildren(*node) && offset < EditingStrategy::lastOffsetFor Editing(node)) { 375 if (!Strategy::hasChildren(*node) && offset < EditingStrategy::lastOffsetFor Editing(node)) {
363 // There are two reasons child might be 0: 376 // There are two reasons child might be 0:
364 // 1) The node is node like a text node that is not an element, and th erefore has no children. 377 // 1) The node is node like a text node that is not an element, and
365 // Going forward one character at a time is correct. 378 // therefore has no children. Going forward one character at a time
366 // 2) The new offset is a bogus offset like (<br>, 1), and there is no child. 379 // is correct.
367 // Going from 0 to 1 is correct. 380 // 2) The new offset is a bogus offset like (<br>, 1), and there is no
368 return editingPositionOf(node, (moveType == Character) ? uncheckedNextOf fset(node, offset) : offset + 1); 381 // child. Going from 0 to 1 is correct.
382 return PositionAlgorithm<Strategy>::editingPositionOf(node, (moveType == PositionMoveType::Character) ? uncheckedNextOffset(node, offset) : offset + 1);
369 } 383 }
370 384
371 if (ContainerNode* parent = Strategy::parent(*node)) 385 if (ContainerNode* parent = Strategy::parent(*node))
372 return editingPositionOf(parent, Strategy::index(*node) + 1); 386 return PositionAlgorithm<Strategy>::editingPositionOf(parent, Strategy:: index(*node) + 1);
373 return PositionAlgorithm<Strategy>(*this); 387 return position;
388 }
389
390 Position nextPositionOf(const Position& position, PositionMoveType moveType)
391 {
392 return nextPositionOfAlgorithm<EditingStrategy>(position, moveType);
393 }
394
395 PositionInComposedTree nextPositionOf(const PositionInComposedTree& position, Po sitionMoveType moveType)
396 {
397 return nextPositionOfAlgorithm<EditingInComposedTreeStrategy>(position, move Type);
374 } 398 }
375 399
376 int uncheckedPreviousOffset(const Node* n, int current) 400 int uncheckedPreviousOffset(const Node* n, int current)
377 { 401 {
378 return n->layoutObject() ? n->layoutObject()->previousOffset(current) : curr ent - 1; 402 return n->layoutObject() ? n->layoutObject()->previousOffset(current) : curr ent - 1;
379 } 403 }
380 404
381 static int uncheckedPreviousOffsetForBackwardDeletion(const Node* n, int current ) 405 static int uncheckedPreviousOffsetForBackwardDeletion(const Node* n, int current )
382 { 406 {
383 return n->layoutObject() ? n->layoutObject()->previousOffsetForBackwardDelet ion(current) : current - 1; 407 return n->layoutObject() ? n->layoutObject()->previousOffsetForBackwardDelet ion(current) : current - 1;
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
630 654
631 void showTree(const blink::Position* pos) 655 void showTree(const blink::Position* pos)
632 { 656 {
633 if (pos) 657 if (pos)
634 pos->showTreeForThis(); 658 pos->showTreeForThis();
635 else 659 else
636 fprintf(stderr, "Cannot showTree for (nil)\n"); 660 fprintf(stderr, "Cannot showTree for (nil)\n");
637 } 661 }
638 662
639 #endif 663 #endif
OLDNEW
« no previous file with comments | « Source/core/editing/Position.h ('k') | Source/core/editing/PositionTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698