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

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

Issue 323983006: Make SurroundingText work for a Range. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 6 years, 6 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/SurroundingText.h ('k') | Source/core/editing/SurroundingTextTest.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) 2012 Google Inc. All rights reserved. 2 * Copyright (C) 2012 Google 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 are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * 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 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 21 matching lines...) Expand all
32 #include "core/editing/SurroundingText.h" 32 #include "core/editing/SurroundingText.h"
33 33
34 #include "core/dom/Document.h" 34 #include "core/dom/Document.h"
35 #include "core/dom/Element.h" 35 #include "core/dom/Element.h"
36 #include "core/dom/Position.h" 36 #include "core/dom/Position.h"
37 #include "core/dom/Range.h" 37 #include "core/dom/Range.h"
38 #include "core/editing/TextIterator.h" 38 #include "core/editing/TextIterator.h"
39 39
40 namespace WebCore { 40 namespace WebCore {
41 41
42 SurroundingText::SurroundingText(const Range& range, unsigned maxLength)
43 : m_startOffsetInContent(0)
44 , m_endOffsetInContent(0)
45 {
46 process(range.startPosition(), range.endPosition(), maxLength);
47 }
48
42 SurroundingText::SurroundingText(const Position& position, unsigned maxLength) 49 SurroundingText::SurroundingText(const Position& position, unsigned maxLength)
43 : m_positionOffsetInContent(0) 50 : m_startOffsetInContent(0)
51 , m_endOffsetInContent(0)
44 { 52 {
53 process(position, position, maxLength);
54 }
55
56 void SurroundingText::process(const Position& startPosition, const Position& end Position, unsigned maxLength)
Yuta Kitamura 2014/06/11 09:22:57 The function name "process" is pretty much clueles
57 {
58 ASSERT(startPosition.document() == endPosition.document());
59
45 const unsigned halfMaxLength = maxLength / 2; 60 const unsigned halfMaxLength = maxLength / 2;
46 61
47 Document* document = position.document(); 62 Document* document = startPosition.document();
48 // The |position| will have no document if it is null (as in no position). 63 // The position will have no document if it is null (as in no position).
49 if (!document) 64 if (!document)
50 return; 65 return;
51 66
52 // The forward range starts at the selection end and ends at the document's 67 // The forward range starts at the selection end and ends at the document's
53 // end. It will then be updated to only contain the text in the text in the 68 // end. It will then be updated to only contain the text in the text in the
54 // right range around the selection. 69 // right range around the selection.
55 RefPtrWillBeRawPtr<Range> forwardRange = Range::create(*document, position, lastPositionInNode(document->documentElement()).parentAnchoredEquivalent()); 70 RefPtrWillBeRawPtr<Range> forwardRange = Range::create(*document, endPositio n, lastPositionInNode(document->documentElement()).parentAnchoredEquivalent());
56 CharacterIterator forwardIterator(forwardRange.get(), TextIteratorStopsOnFor mControls); 71 CharacterIterator forwardIterator(forwardRange.get(), TextIteratorStopsOnFor mControls);
72 // FIXME: why do we stop going trough the text if we were not able to select something on the right?
57 if (!forwardIterator.atEnd()) 73 if (!forwardIterator.atEnd())
58 forwardIterator.advance(maxLength - halfMaxLength); 74 forwardIterator.advance(maxLength - halfMaxLength);
59 75
60 forwardRange = forwardIterator.range(); 76 forwardRange = forwardIterator.range();
61 if (!forwardRange || !Range::create(*document, position, forwardRange->start Position())->text().length()) { 77 if (!forwardRange || !Range::create(*document, endPosition, forwardRange->st artPosition())->text().length()) {
62 ASSERT(forwardRange); 78 ASSERT(forwardRange);
63 return; 79 return;
64 } 80 }
65 81
66 // Same as with the forward range but with the backward range. The range 82 // Same as with the forward range but with the backward range. The range
67 // starts at the document's start and ends at the selection start and will 83 // starts at the document's start and ends at the selection start and will
68 // be updated. 84 // be updated.
69 RefPtrWillBeRawPtr<Range> backwardsRange = Range::create(*document, firstPos itionInNode(document->documentElement()).parentAnchoredEquivalent(), position); 85 RefPtrWillBeRawPtr<Range> backwardsRange = Range::create(*document, firstPos itionInNode(document->documentElement()).parentAnchoredEquivalent(), startPositi on);
70 BackwardsCharacterIterator backwardsIterator(backwardsRange.get(), TextItera torStopsOnFormControls); 86 BackwardsCharacterIterator backwardsIterator(backwardsRange.get(), TextItera torStopsOnFormControls);
71 if (!backwardsIterator.atEnd()) 87 if (!backwardsIterator.atEnd())
72 backwardsIterator.advance(halfMaxLength); 88 backwardsIterator.advance(halfMaxLength);
73 89
74 backwardsRange = backwardsIterator.range(); 90 backwardsRange = backwardsIterator.range();
75 if (!backwardsRange) { 91 if (!backwardsRange) {
76 ASSERT(backwardsRange); 92 ASSERT(backwardsRange);
77 return; 93 return;
78 } 94 }
79 95
80 m_positionOffsetInContent = Range::create(*document, backwardsRange->endPosi tion(), position)->text().length(); 96 m_startOffsetInContent = Range::create(*document, backwardsRange->endPositio n(), startPosition)->text().length();
yosin_UTC9 2014/06/11 01:04:28 nit: Can we defer calculating start/end offset in
mlamouri (slow - plz ping) 2014/06/11 09:19:54 This is as optional as m_positionOffsetInContent a
97 m_endOffsetInContent = Range::create(*document, backwardsRange->endPosition( ), endPosition)->text().length();
81 m_contentRange = Range::create(*document, backwardsRange->endPosition(), for wardRange->startPosition()); 98 m_contentRange = Range::create(*document, backwardsRange->endPosition(), for wardRange->startPosition());
82 ASSERT(m_contentRange); 99 ASSERT(m_contentRange);
83 } 100 }
84 101
85 PassRefPtrWillBeRawPtr<Range> SurroundingText::rangeFromContentOffsets(unsigned startOffsetInContent, unsigned endOffsetInContent) 102 PassRefPtrWillBeRawPtr<Range> SurroundingText::rangeFromContentOffsets(unsigned startOffsetInContent, unsigned endOffsetInContent)
86 { 103 {
87 if (startOffsetInContent >= endOffsetInContent || endOffsetInContent > conte nt().length()) 104 if (startOffsetInContent >= endOffsetInContent || endOffsetInContent > conte nt().length())
88 return nullptr; 105 return nullptr;
89 106
90 CharacterIterator iterator(m_contentRange.get()); 107 CharacterIterator iterator(m_contentRange.get());
(...skipping 14 matching lines...) Expand all
105 return Range::create(*start.document(), start, end); 122 return Range::create(*start.document(), start, end);
106 } 123 }
107 124
108 String SurroundingText::content() const 125 String SurroundingText::content() const
109 { 126 {
110 if (m_contentRange) 127 if (m_contentRange)
111 return m_contentRange->text(); 128 return m_contentRange->text();
112 return String(); 129 return String();
113 } 130 }
114 131
115 unsigned SurroundingText::positionOffsetInContent() const 132 unsigned SurroundingText::startOffsetInContent() const
116 { 133 {
117 return m_positionOffsetInContent; 134 return m_startOffsetInContent;
135 }
136
137 unsigned SurroundingText::endOffsetInContent() const
138 {
139 return m_endOffsetInContent;
118 } 140 }
119 141
120 } // namespace WebCore 142 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/core/editing/SurroundingText.h ('k') | Source/core/editing/SurroundingTextTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698