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

Side by Side Diff: third_party/WebKit/Source/core/editing/iterators/TextIterator.h

Issue 2903693005: Introduce a wrapper class to handle text node in TextIterator (Closed)
Patch Set: Thu May 25 17:36:28 PDT 2017 Created 3 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 | « no previous file | third_party/WebKit/Source/core/editing/iterators/TextIterator.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, 2006, 2009 Apple Inc. All rights reserved. 2 * Copyright (C) 2004, 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 class LayoutText; 42 class LayoutText;
43 class LayoutTextFragment; 43 class LayoutTextFragment;
44 44
45 CORE_EXPORT String 45 CORE_EXPORT String
46 PlainText(const EphemeralRange&, 46 PlainText(const EphemeralRange&,
47 const TextIteratorBehavior& = TextIteratorBehavior()); 47 const TextIteratorBehavior& = TextIteratorBehavior());
48 48
49 String PlainText(const EphemeralRangeInFlatTree&, 49 String PlainText(const EphemeralRangeInFlatTree&,
50 const TextIteratorBehavior& = TextIteratorBehavior()); 50 const TextIteratorBehavior& = TextIteratorBehavior());
51 51
52 // TODO(xiaochengh): Move the class to dedicated files.
53 // TextIteratorTextNodeHandler extracts plain text from a text node by calling
54 // HandleTextNode() function. It should be used only by TextIterator.
55 class TextIteratorTextNodeHandler {
56 STACK_ALLOCATED();
57
58 public:
59 TextIteratorTextNodeHandler(const TextIteratorBehavior&,
60 TextIteratorTextState&);
yosin_UTC9 2017/05/26 01:47:25 nit: s/TextIteratorState&/TextIterator*/
Xiaocheng 2017/05/26 21:56:39 Done.
61
62 void SetBoundaries(Node* start_container,
63 int start_offset,
64 Node* end_container,
65 int end_offset);
66
67 Text* GetNode() const { return text_node_; }
68
69 // Returns true if more text is emitted without traversing to the next node.
70 bool HandleRemainingTextRuns();
71
72 // Returns true if a leading white space is emitted before a replaced element.
73 bool FixLeadingWhiteSpaceForReplacedElement(Node*);
74
75 void ResetCollapsedWhiteSpaceFixup();
76
77 // TODO(xiaochengh): Make the return type |void|. The current return value is
78 // not very meaningful.
79 bool HandleTextNode(Text*);
80
81 private:
82 void HandlePreFormattedTextNode();
83 void HandleTextBox();
84 void HandleTextNodeFirstLetter(LayoutTextFragment*);
85 bool ShouldHandleFirstLetter(const LayoutText&) const;
86 bool ShouldProceedToRemainingText() const;
87 void ProceedToRemainingText();
88 size_t RestoreCollapsedTrailingSpace(InlineTextBox* next_text_box,
89 size_t subrun_end);
90
91 // Used when the visibility of the style should not affect text gathering.
92 bool IgnoresStyleVisibility() const {
93 return behavior_.IgnoresStyleVisibility();
94 }
95
96 void SpliceBuffer(UChar,
97 Node* text_node,
98 Node* offset_base_node,
99 int text_start_offset,
100 int text_end_offset);
101 void EmitText(Node* text_node,
102 LayoutText* layout_object,
103 int text_start_offset,
104 int text_end_offset);
105
106 // The range.
107 Member<Node> start_container_;
108 int start_offset_ = 0;
109 Member<Node> end_container_;
110 int end_offset_ = 0;
111
112 // The current text node and offset, from which text is being emitted.
113 Member<Text> text_node_;
114 int offset_ = 0;
115
116 InlineTextBox* text_box_ = nullptr;
117
118 // Remember if we are in the middle of handling a pre-formatted text node.
119 bool needs_handle_pre_formatted_text_node_ = false;
120 // Used when deciding text fragment created by :first-letter should be looked
121 // into.
122 bool handled_first_letter_ = false;
123 // Used when iteration over :first-letter text to save pointer to
124 // remaining text box.
125 InlineTextBox* remaining_text_box_ = nullptr;
126 // Used to point to LayoutText object for :first-letter.
127 LayoutText* first_letter_text_ = nullptr;
128
129 // Used to do the whitespace collapsing logic.
130 bool last_text_node_ended_with_collapsed_space_ = false;
131
132 // Used when text boxes are out of order (Hebrew/Arabic w/ embeded LTR text)
133 Vector<InlineTextBox*> sorted_text_boxes_;
134 size_t sorted_text_boxes_position_ = 0;
135
136 const TextIteratorBehavior behavior_;
137
138 // Contains state of emitted text.
139 TextIteratorTextState& text_state_;
140 };
yosin_UTC9 2017/05/26 01:47:25 nit: We should have |DISALLOW_COPY_AND_ASSIGN(...)
Xiaocheng 2017/05/26 21:56:39 Done.
141
52 // Iterates through the DOM range, returning all the text, and 0-length 142 // Iterates through the DOM range, returning all the text, and 0-length
53 // boundaries at points where replaced elements break up the text flow. The 143 // boundaries at points where replaced elements break up the text flow. The
54 // text comes back in chunks so as to optimize for performance of the iteration. 144 // text comes back in chunks so as to optimize for performance of the iteration.
55 145
56 template <typename Strategy> 146 template <typename Strategy>
57 class CORE_TEMPLATE_CLASS_EXPORT TextIteratorAlgorithm { 147 class CORE_TEMPLATE_CLASS_EXPORT TextIteratorAlgorithm {
58 STACK_ALLOCATED(); 148 STACK_ALLOCATED();
59 149
60 public: 150 public:
61 // [start, end] indicates the document range that the iteration should take 151 // [start, end] indicates the document range that the iteration should take
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 bool ShouldRepresentNodeOffsetZero(); 224 bool ShouldRepresentNodeOffsetZero();
135 bool ShouldEmitSpaceBeforeAndAfterNode(Node*); 225 bool ShouldEmitSpaceBeforeAndAfterNode(Node*);
136 void RepresentNodeOffsetZero(); 226 void RepresentNodeOffsetZero();
137 227
138 // Return true if the iteration progress should advance to |kHandledNode| 228 // Return true if the iteration progress should advance to |kHandledNode|
139 // after calling a |HandleXXX| function. 229 // after calling a |HandleXXX| function.
140 // TODO(xiaochengh): The meaning of the return values is unclear, and they do 230 // TODO(xiaochengh): The meaning of the return values is unclear, and they do
141 // not always clearly control the iteration progress. Should consider removing 231 // not always clearly control the iteration progress. Should consider removing
142 // the return values and control the iteration in a cleaner way. 232 // the return values and control the iteration in a cleaner way.
143 bool HandleTextNode(); 233 bool HandleTextNode();
144 void HandlePreFormattedTextNode();
145 bool HandleReplacedElement(); 234 bool HandleReplacedElement();
146 bool HandleNonTextNode(); 235 bool HandleNonTextNode();
147
148 void HandleTextBox();
149 void HandleTextNodeFirstLetter(LayoutTextFragment*);
150 bool ShouldHandleFirstLetter(const LayoutText&) const;
151 bool ShouldProceedToRemainingText() const;
152 void ProceedToRemainingText();
153 void ResetCollapsedWhiteSpaceFixup();
154
155 // Returns true if more text is emitted without traversing to the next node.
156 bool HandleRemainingTextRuns();
157
158 // Returns true if a leading white space is emitted before a replaced element.
159 bool FixLeadingWhiteSpaceForReplacedElement(Node* parent);
160
161 void SpliceBuffer(UChar, 236 void SpliceBuffer(UChar,
162 Node* text_node, 237 Node* text_node,
163 Node* offset_base_node, 238 Node* offset_base_node,
164 int text_start_offset, 239 int text_start_offset,
165 int text_end_offset); 240 int text_end_offset);
166 void EmitText(Node* text_node,
167 LayoutText* layout_object,
168 int text_start_offset,
169 int text_end_offset);
170 size_t RestoreCollapsedTrailingSpace(InlineTextBox* next_text_box,
171 size_t subrun_end);
172 241
173 // Used by selection preservation code. There should be one character emitted 242 // Used by selection preservation code. There should be one character emitted
174 // between every VisiblePosition in the Range used to create the TextIterator. 243 // between every VisiblePosition in the Range used to create the TextIterator.
175 // FIXME <rdar://problem/6028818>: This functionality should eventually be 244 // FIXME <rdar://problem/6028818>: This functionality should eventually be
176 // phased out when we rewrite moveParagraphs to not clone/destroy moved 245 // phased out when we rewrite moveParagraphs to not clone/destroy moved
177 // content. 246 // content.
178 bool EmitsCharactersBetweenAllVisiblePositions() const { 247 bool EmitsCharactersBetweenAllVisiblePositions() const {
179 return behavior_.EmitsCharactersBetweenAllVisiblePositions(); 248 return behavior_.EmitsCharactersBetweenAllVisiblePositions();
180 } 249 }
181 250
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 285
217 // Append code units with offset range [position, position + copyLength) 286 // Append code units with offset range [position, position + copyLength)
218 // to the output buffer. 287 // to the output buffer.
219 void CopyCodeUnitsTo(ForwardsTextBuffer* output, 288 void CopyCodeUnitsTo(ForwardsTextBuffer* output,
220 int position, 289 int position,
221 int copy_length) const; 290 int copy_length) const;
222 291
223 // Current position, not necessarily of the text being returned, but position 292 // Current position, not necessarily of the text being returned, but position
224 // as we walk through the DOM tree. 293 // as we walk through the DOM tree.
225 Member<Node> node_; 294 Member<Node> node_;
226 int offset_;
227 IterationProgress iteration_progress_; 295 IterationProgress iteration_progress_;
228 FullyClippedStateStackAlgorithm<Strategy> fully_clipped_stack_; 296 FullyClippedStateStackAlgorithm<Strategy> fully_clipped_stack_;
229 int shadow_depth_; 297 int shadow_depth_;
230 298
231 // The range. 299 // The range.
232 Member<Node> start_container_; 300 Member<Node> start_container_;
233 int start_offset_; 301 int start_offset_;
234 Member<Node> end_container_; 302 Member<Node> end_container_;
235 int end_offset_; 303 int end_offset_;
236 // |m_endNode| stores |Strategy::childAt(*m_endContainer, m_endOffset - 1)|, 304 // |m_endNode| stores |Strategy::childAt(*m_endContainer, m_endOffset - 1)|,
237 // if it exists, or |nullptr| otherwise. 305 // if it exists, or |nullptr| otherwise.
238 Member<Node> end_node_; 306 Member<Node> end_node_;
239 Member<Node> past_end_node_; 307 Member<Node> past_end_node_;
240 308
241 // The current text node, from which text is being emitted.
242 Member<Text> text_node_;
243
244 // Used when there is still some pending text from the current node; when 309 // Used when there is still some pending text from the current node; when
245 // these are false and 0, we go back to normal iterating. 310 // these are false and 0, we go back to normal iterating.
246 bool needs_another_newline_; 311 bool needs_another_newline_;
247 InlineTextBox* text_box_;
248 // Used when iteration over :first-letter text to save pointer to
249 // remaining text box.
250 InlineTextBox* remaining_text_box_;
251 // Used to point to LayoutText object for :first-letter.
252 LayoutText* first_letter_text_;
253 312
254 // Used to do the whitespace collapsing logic.
255 Member<Text> last_text_node_; 313 Member<Text> last_text_node_;
256 bool last_text_node_ended_with_collapsed_space_;
257
258 // Used when text boxes are out of order (Hebrew/Arabic w/ embeded LTR text)
259 Vector<InlineTextBox*> sorted_text_boxes_;
260 size_t sorted_text_boxes_position_;
261 314
262 const TextIteratorBehavior behavior_; 315 const TextIteratorBehavior behavior_;
263 316
264 // Remember if we are in the middle of handling a pre-formatted text node.
265 bool needs_handle_pre_formatted_text_node_;
266 // Used when deciding text fragment created by :first-letter should be looked
267 // into.
268 bool handled_first_letter_;
269 // Used when stopsOnFormControls() is true to determine if the iterator should 317 // Used when stopsOnFormControls() is true to determine if the iterator should
270 // keep advancing. 318 // keep advancing.
271 bool should_stop_; 319 bool should_stop_;
272 // Used for use counter |InnerTextWithShadowTree| and 320 // Used for use counter |InnerTextWithShadowTree| and
273 // |SelectionToStringWithShadowTree|, we should not use other purpose. 321 // |SelectionToStringWithShadowTree|, we should not use other purpose.
274 bool handle_shadow_root_; 322 bool handle_shadow_root_;
275 323
276 // Contains state of emitted text. 324 // Contains state of emitted text.
277 TextIteratorTextState text_state_; 325 TextIteratorTextState text_state_;
326
327 // Helper for extracting text content from text nodes.
328 TextIteratorTextNodeHandler text_node_handler_;
yosin_UTC9 2017/05/26 01:47:25 It is better to use |TextIteratorTextNodeHandler*|
Xiaocheng 2017/05/26 21:56:39 Added a TODO for it. I don't want to handle memor
278 }; 329 };
279 330
280 extern template class CORE_EXTERN_TEMPLATE_EXPORT 331 extern template class CORE_EXTERN_TEMPLATE_EXPORT
281 TextIteratorAlgorithm<EditingStrategy>; 332 TextIteratorAlgorithm<EditingStrategy>;
282 extern template class CORE_EXTERN_TEMPLATE_EXPORT 333 extern template class CORE_EXTERN_TEMPLATE_EXPORT
283 TextIteratorAlgorithm<EditingInFlatTreeStrategy>; 334 TextIteratorAlgorithm<EditingInFlatTreeStrategy>;
284 335
285 using TextIterator = TextIteratorAlgorithm<EditingStrategy>; 336 using TextIterator = TextIteratorAlgorithm<EditingStrategy>;
286 using TextIteratorInFlatTree = TextIteratorAlgorithm<EditingInFlatTreeStrategy>; 337 using TextIteratorInFlatTree = TextIteratorAlgorithm<EditingInFlatTreeStrategy>;
287 338
288 } // namespace blink 339 } // namespace blink
289 340
290 #endif // TextIterator_h 341 #endif // TextIterator_h
OLDNEW
« no previous file with comments | « no previous file | third_party/WebKit/Source/core/editing/iterators/TextIterator.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698