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

Side by Side Diff: third_party/WebKit/Source/core/editing/VisibleUnits.cpp

Issue 2177433002: [Editing][Codehealth] Improve startOfParagraphAlgorithm readbility (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 5 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 | « no previous file | 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 Apple Inc. All rights reserv ed. 2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserv ed.
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 1458 matching lines...) Expand 10 before | Expand all | Expand 10 after
1469 return iterator->following(0); 1469 return iterator->following(0);
1470 } 1470 }
1471 1471
1472 VisiblePosition nextSentencePosition(const VisiblePosition& c) 1472 VisiblePosition nextSentencePosition(const VisiblePosition& c)
1473 { 1473 {
1474 VisiblePosition next = nextBoundary(c, nextSentencePositionBoundary); 1474 VisiblePosition next = nextBoundary(c, nextSentencePositionBoundary);
1475 return honorEditingBoundaryAtOrAfter(next, c.deepEquivalent()); 1475 return honorEditingBoundaryAtOrAfter(next, c.deepEquivalent());
1476 } 1476 }
1477 1477
1478 template <typename Strategy> 1478 template <typename Strategy>
1479 VisiblePositionTemplate<Strategy> startOfParagraphAlgorithm(const VisiblePositio nTemplate<Strategy>& c, EditingBoundaryCrossingRule boundaryCrossingRule) 1479 PositionTemplate<Strategy> startOfParagraphAlgorithm(const PositionTemplate<Stra tegy>& position, EditingBoundaryCrossingRule boundaryCrossingRule)
1480 { 1480 {
1481 const PositionTemplate<Strategy> p = c.deepEquivalent(); 1481 Node* const startNode = position.anchorNode();
1482 Node* startNode = p.anchorNode();
1483 1482
1484 if (!startNode) 1483 if (!startNode)
1485 return VisiblePositionTemplate<Strategy>(); 1484 return PositionTemplate<Strategy>();
1486 1485
1487 if (isRenderedAsNonInlineTableImageOrHR(startNode)) 1486 if (isRenderedAsNonInlineTableImageOrHR(startNode))
1488 return createVisiblePosition(PositionTemplate<Strategy>::beforeNode(star tNode)); 1487 return PositionTemplate<Strategy>::beforeNode(startNode);
1489 1488
1490 Element* startBlock = enclosingBlock(PositionTemplate<Strategy>::firstPositi onInOrBeforeNode(startNode), CannotCrossEditingBoundary); 1489 Element* const startBlock = enclosingBlock(PositionTemplate<Strategy>::first PositionInOrBeforeNode(startNode), CannotCrossEditingBoundary);
1490 ContainerNode* const highestRoot = highestEditableRoot(position);
1491 const bool startNodeIsEditable = hasEditableStyle(*startNode);
1491 1492
1492 Node* node = startNode; 1493 Node* candidateNode = startNode;
1493 ContainerNode* highestRoot = highestEditableRoot(p); 1494 PositionAnchorType candidateType = position.anchorType();
1494 int offset = p.computeEditingOffset(); 1495 int candidateOffset = position.computeEditingOffset();
1495 PositionAnchorType type = p.anchorType();
1496 1496
1497 Node* n = startNode; 1497 Node* prevousNodeIterator = startNode;
1498 bool startNodeIsEditable = hasEditableStyle(*startNode); 1498 while (prevousNodeIterator) {
1499 while (n) { 1499 if (boundaryCrossingRule == CannotCrossEditingBoundary && !nodeIsUserSel ectAll(prevousNodeIterator) && hasEditableStyle(*prevousNodeIterator) != startNo deIsEditable)
1500 if (boundaryCrossingRule == CannotCrossEditingBoundary && !nodeIsUserSel ectAll(n) && hasEditableStyle(*n) != startNodeIsEditable)
1501 break; 1500 break;
1502 if (boundaryCrossingRule == CanSkipOverEditingBoundary) { 1501 if (boundaryCrossingRule == CanSkipOverEditingBoundary) {
1503 while (n && hasEditableStyle(*n) != startNodeIsEditable) 1502 while (prevousNodeIterator && hasEditableStyle(*prevousNodeIterator) != startNodeIsEditable)
1504 n = Strategy::previousPostOrder(*n, startBlock); 1503 prevousNodeIterator = Strategy::previousPostOrder(*prevousNodeIt erator, startBlock);
1505 if (!n || !n->isDescendantOf(highestRoot)) 1504 if (!prevousNodeIterator || !prevousNodeIterator->isDescendantOf(hig hestRoot))
1506 break; 1505 break;
1507 } 1506 }
1508 LayoutObject* r = n->layoutObject(); 1507
1509 LayoutItem ri = LayoutItem(r); 1508 const LayoutItem layoutItem = LayoutItem(prevousNodeIterator->layoutObje ct());
1510 if (ri.isNull()) { 1509 if (layoutItem.isNull()) {
1511 n = Strategy::previousPostOrder(*n, startBlock); 1510 prevousNodeIterator = Strategy::previousPostOrder(*prevousNodeIterat or, startBlock);
1512 continue; 1511 continue;
1513 } 1512 }
1514 const ComputedStyle& style = ri.styleRef(); 1513 const ComputedStyle& style = layoutItem.styleRef();
1515 if (style.visibility() != VISIBLE) { 1514 if (style.visibility() != VISIBLE) {
1516 n = Strategy::previousPostOrder(*n, startBlock); 1515 prevousNodeIterator = Strategy::previousPostOrder(*prevousNodeIterat or, startBlock);
1517 continue; 1516 continue;
1518 } 1517 }
1519 1518
1520 if (ri.isBR() || isEnclosingBlock(n)) 1519 if (layoutItem.isBR() || isEnclosingBlock(prevousNodeIterator))
1521 break; 1520 break;
1522 1521
1523 if (ri.isText() && toLayoutText(r)->resolvedTextLength()) { 1522 if (layoutItem.isText() && toLayoutText(prevousNodeIterator->layoutObjec t())->resolvedTextLength()) {
1524 ASSERT_WITH_SECURITY_IMPLICATION(n->isTextNode()); 1523 SECURITY_DCHECK(prevousNodeIterator->isTextNode());
1525 type = PositionAnchorType::OffsetInAnchor;
1526 if (style.preserveNewline()) { 1524 if (style.preserveNewline()) {
1527 LayoutText* text = toLayoutText(r); 1525 LayoutText* text = toLayoutText(prevousNodeIterator->layoutObjec t());
1528 int i = text->textLength(); 1526 int index = text->textLength();
1529 int o = offset; 1527 if (prevousNodeIterator == startNode && candidateOffset < index)
1530 if (n == startNode && o < i) 1528 index = max(0, candidateOffset);
1531 i = max(0, o); 1529 while (--index >= 0) {
1532 while (--i >= 0) { 1530 if ((*text)[index] == '\n')
1533 if ((*text)[i] == '\n') 1531 return PositionTemplate<Strategy>(toText(prevousNodeIter ator), index + 1);
1534 return createVisiblePosition(PositionTemplate<Strategy>( toText(n), i + 1));
1535 } 1532 }
1536 } 1533 }
1537 node = n; 1534 candidateNode = prevousNodeIterator;
1538 offset = 0; 1535 candidateType = PositionAnchorType::OffsetInAnchor;
1539 n = Strategy::previousPostOrder(*n, startBlock); 1536 candidateOffset = 0;
1540 } else if (editingIgnoresContent(n) || isDisplayInsideTable(n)) { 1537 prevousNodeIterator = Strategy::previousPostOrder(*prevousNodeIterat or, startBlock);
1541 node = n; 1538 } else if (editingIgnoresContent(prevousNodeIterator) || isDisplayInside Table(prevousNodeIterator)) {
1542 type = PositionAnchorType::BeforeAnchor; 1539 candidateNode = prevousNodeIterator;
1543 n = n->previousSibling() ? n->previousSibling() : Strategy::previous PostOrder(*n, startBlock); 1540 candidateType = PositionAnchorType::BeforeAnchor;
1541 prevousNodeIterator = prevousNodeIterator->previousSibling() ? prevo usNodeIterator->previousSibling() : Strategy::previousPostOrder(*prevousNodeIter ator, startBlock);
1544 } else { 1542 } else {
1545 n = Strategy::previousPostOrder(*n, startBlock); 1543 prevousNodeIterator = Strategy::previousPostOrder(*prevousNodeIterat or, startBlock);
1546 } 1544 }
1547 } 1545 }
1548 1546
1549 if (type == PositionAnchorType::OffsetInAnchor) 1547 if (candidateType == PositionAnchorType::OffsetInAnchor)
1550 return createVisiblePosition(PositionTemplate<Strategy>(node, offset)); 1548 return PositionTemplate<Strategy>(candidateNode, candidateOffset);
1551 1549
1552 return createVisiblePosition(PositionTemplate<Strategy>(node, type)); 1550 return PositionTemplate<Strategy>(candidateNode, candidateType);
1551 }
1552
1553 template <typename Strategy>
1554 VisiblePositionTemplate<Strategy> startOfParagraphAlgorithm(const VisiblePositio nTemplate<Strategy>& visiblePosition, EditingBoundaryCrossingRule boundaryCrossi ngRule)
1555 {
1556 return createVisiblePosition(startOfParagraphAlgorithm(visiblePosition.deepE quivalent(), boundaryCrossingRule));
1553 } 1557 }
1554 1558
1555 VisiblePosition startOfParagraph(const VisiblePosition& c, EditingBoundaryCrossi ngRule boundaryCrossingRule) 1559 VisiblePosition startOfParagraph(const VisiblePosition& c, EditingBoundaryCrossi ngRule boundaryCrossingRule)
1556 { 1560 {
1557 return startOfParagraphAlgorithm<EditingStrategy>(c, boundaryCrossingRule); 1561 return startOfParagraphAlgorithm<EditingStrategy>(c, boundaryCrossingRule);
1558 } 1562 }
1559 1563
1560 VisiblePositionInFlatTree startOfParagraph(const VisiblePositionInFlatTree& c, E ditingBoundaryCrossingRule boundaryCrossingRule) 1564 VisiblePositionInFlatTree startOfParagraph(const VisiblePositionInFlatTree& c, E ditingBoundaryCrossingRule boundaryCrossingRule)
1561 { 1565 {
1562 return startOfParagraphAlgorithm<EditingInFlatTreeStrategy>(c, boundaryCross ingRule); 1566 return startOfParagraphAlgorithm<EditingInFlatTreeStrategy>(c, boundaryCross ingRule);
(...skipping 1689 matching lines...) Expand 10 before | Expand all | Expand 10 after
3252 { 3256 {
3253 return previousPositionOfAlgorithm<EditingStrategy>(visiblePosition, rule); 3257 return previousPositionOfAlgorithm<EditingStrategy>(visiblePosition, rule);
3254 } 3258 }
3255 3259
3256 VisiblePositionInFlatTree previousPositionOf(const VisiblePositionInFlatTree& vi siblePosition, EditingBoundaryCrossingRule rule) 3260 VisiblePositionInFlatTree previousPositionOf(const VisiblePositionInFlatTree& vi siblePosition, EditingBoundaryCrossingRule rule)
3257 { 3261 {
3258 return previousPositionOfAlgorithm<EditingInFlatTreeStrategy>(visiblePositio n, rule); 3262 return previousPositionOfAlgorithm<EditingInFlatTreeStrategy>(visiblePositio n, rule);
3259 } 3263 }
3260 3264
3261 } // namespace blink 3265 } // namespace blink
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698