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

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: Mon May 29 01:57:14 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
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 final {
56 STACK_ALLOCATED();
57
58 public:
59 TextIteratorTextNodeHandler(const TextIteratorBehavior&,
60 TextIteratorTextState*);
61
62 // Initializes the full iteration range of the TextIterator. This function
63 // should be called only once from TextIterator::Initialize.
64 // TODO(xiaochengh): TextNodeHandler doesn't need to know the full iteration
65 // range; The offset range in the current node suffices. Remove this function.
66 void Initialize(Node* start_container,
67 int start_offset,
68 Node* end_container,
69 int end_offset);
70
71 Text* GetNode() const { return text_node_; }
72
73 // Returns true if more text is emitted without traversing to the next node.
74 bool HandleRemainingTextRuns();
75
76 // Returns true if a leading white space is emitted before a replaced element.
77 bool FixLeadingWhiteSpaceForReplacedElement(Node*);
78
79 void ResetCollapsedWhiteSpaceFixup();
80
81 // TODO(xiaochengh): Make the return type |void|. The current return value is
82 // not very meaningful.
83 bool HandleTextNode(Text*);
84
85 private:
86 void HandlePreFormattedTextNode();
87 void HandleTextBox();
88 void HandleTextNodeFirstLetter(LayoutTextFragment*);
89 bool ShouldHandleFirstLetter(const LayoutText&) const;
90 bool ShouldProceedToRemainingText() const;
91 void ProceedToRemainingText();
92 size_t RestoreCollapsedTrailingSpace(InlineTextBox* next_text_box,
93 size_t subrun_end);
94
95 // Used when the visibility of the style should not affect text gathering.
96 bool IgnoresStyleVisibility() const {
97 return behavior_.IgnoresStyleVisibility();
98 }
99
100 void SpliceBuffer(UChar,
101 Node* text_node,
102 Node* offset_base_node,
103 int text_start_offset,
104 int text_end_offset);
105 void EmitText(Node* text_node,
106 LayoutText* layout_object,
107 int text_start_offset,
108 int text_end_offset);
109
110 // The range.
111 Member<Node> start_container_;
112 int start_offset_ = 0;
113 Member<Node> end_container_;
114 int end_offset_ = 0;
115
116 // The current text node and offset, from which text is being emitted.
117 Member<Text> text_node_;
118 int offset_ = 0;
119
120 InlineTextBox* text_box_ = nullptr;
121
122 // Remember if we are in the middle of handling a pre-formatted text node.
123 bool needs_handle_pre_formatted_text_node_ = false;
124 // Used when deciding text fragment created by :first-letter should be looked
125 // into.
126 bool handled_first_letter_ = false;
127 // Used when iteration over :first-letter text to save pointer to
128 // remaining text box.
129 InlineTextBox* remaining_text_box_ = nullptr;
130 // Used to point to LayoutText object for :first-letter.
131 LayoutText* first_letter_text_ = nullptr;
132
133 // Used to do the whitespace collapsing logic.
134 bool last_text_node_ended_with_collapsed_space_ = false;
135
136 // Used when text boxes are out of order (Hebrew/Arabic w/ embeded LTR text)
137 Vector<InlineTextBox*> sorted_text_boxes_;
138 size_t sorted_text_boxes_position_ = 0;
139
140 const TextIteratorBehavior behavior_;
141
142 // Contains state of emitted text.
143 TextIteratorTextState& text_state_;
144
145 DISALLOW_COPY_AND_ASSIGN(TextIteratorTextNodeHandler);
146 };
147
52 // Iterates through the DOM range, returning all the text, and 0-length 148 // 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 149 // 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. 150 // text comes back in chunks so as to optimize for performance of the iteration.
55 151
56 template <typename Strategy> 152 template <typename Strategy>
57 class CORE_TEMPLATE_CLASS_EXPORT TextIteratorAlgorithm { 153 class CORE_TEMPLATE_CLASS_EXPORT TextIteratorAlgorithm {
58 STACK_ALLOCATED(); 154 STACK_ALLOCATED();
59 155
60 public: 156 public:
61 // [start, end] indicates the document range that the iteration should take 157 // [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(); 230 bool ShouldRepresentNodeOffsetZero();
135 bool ShouldEmitSpaceBeforeAndAfterNode(Node*); 231 bool ShouldEmitSpaceBeforeAndAfterNode(Node*);
136 void RepresentNodeOffsetZero(); 232 void RepresentNodeOffsetZero();
137 233
138 // Return true if the iteration progress should advance to |kHandledNode| 234 // Return true if the iteration progress should advance to |kHandledNode|
139 // after calling a |HandleXXX| function. 235 // after calling a |HandleXXX| function.
140 // TODO(xiaochengh): The meaning of the return values is unclear, and they do 236 // TODO(xiaochengh): The meaning of the return values is unclear, and they do
141 // not always clearly control the iteration progress. Should consider removing 237 // not always clearly control the iteration progress. Should consider removing
142 // the return values and control the iteration in a cleaner way. 238 // the return values and control the iteration in a cleaner way.
143 bool HandleTextNode(); 239 bool HandleTextNode();
144 void HandlePreFormattedTextNode();
145 bool HandleReplacedElement(); 240 bool HandleReplacedElement();
146 bool HandleNonTextNode(); 241 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, 242 void SpliceBuffer(UChar,
162 Node* text_node, 243 Node* text_node,
163 Node* offset_base_node, 244 Node* offset_base_node,
164 int text_start_offset, 245 int text_start_offset,
165 int text_end_offset); 246 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 247
173 // Used by selection preservation code. There should be one character emitted 248 // Used by selection preservation code. There should be one character emitted
174 // between every VisiblePosition in the Range used to create the TextIterator. 249 // between every VisiblePosition in the Range used to create the TextIterator.
175 // FIXME <rdar://problem/6028818>: This functionality should eventually be 250 // FIXME <rdar://problem/6028818>: This functionality should eventually be
176 // phased out when we rewrite moveParagraphs to not clone/destroy moved 251 // phased out when we rewrite moveParagraphs to not clone/destroy moved
177 // content. 252 // content.
178 bool EmitsCharactersBetweenAllVisiblePositions() const { 253 bool EmitsCharactersBetweenAllVisiblePositions() const {
179 return behavior_.EmitsCharactersBetweenAllVisiblePositions(); 254 return behavior_.EmitsCharactersBetweenAllVisiblePositions();
180 } 255 }
181 256
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 291
217 // Append code units with offset range [position, position + copyLength) 292 // Append code units with offset range [position, position + copyLength)
218 // to the output buffer. 293 // to the output buffer.
219 void CopyCodeUnitsTo(ForwardsTextBuffer* output, 294 void CopyCodeUnitsTo(ForwardsTextBuffer* output,
220 int position, 295 int position,
221 int copy_length) const; 296 int copy_length) const;
222 297
223 // Current position, not necessarily of the text being returned, but position 298 // Current position, not necessarily of the text being returned, but position
224 // as we walk through the DOM tree. 299 // as we walk through the DOM tree.
225 Member<Node> node_; 300 Member<Node> node_;
226 int offset_;
227 IterationProgress iteration_progress_; 301 IterationProgress iteration_progress_;
228 FullyClippedStateStackAlgorithm<Strategy> fully_clipped_stack_; 302 FullyClippedStateStackAlgorithm<Strategy> fully_clipped_stack_;
229 int shadow_depth_; 303 int shadow_depth_;
230 304
231 // The range. 305 // The range.
232 Member<Node> start_container_; 306 Member<Node> start_container_;
233 int start_offset_; 307 int start_offset_;
234 Member<Node> end_container_; 308 Member<Node> end_container_;
235 int end_offset_; 309 int end_offset_;
236 // |m_endNode| stores |Strategy::childAt(*m_endContainer, m_endOffset - 1)|, 310 // |m_endNode| stores |Strategy::childAt(*m_endContainer, m_endOffset - 1)|,
237 // if it exists, or |nullptr| otherwise. 311 // if it exists, or |nullptr| otherwise.
238 Member<Node> end_node_; 312 Member<Node> end_node_;
239 Member<Node> past_end_node_; 313 Member<Node> past_end_node_;
240 314
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 315 // 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. 316 // these are false and 0, we go back to normal iterating.
246 bool needs_another_newline_; 317 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 318
254 // Used to do the whitespace collapsing logic.
255 Member<Text> last_text_node_; 319 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 320
262 const TextIteratorBehavior behavior_; 321 const TextIteratorBehavior behavior_;
263 322
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 323 // Used when stopsOnFormControls() is true to determine if the iterator should
270 // keep advancing. 324 // keep advancing.
271 bool should_stop_; 325 bool should_stop_;
272 // Used for use counter |InnerTextWithShadowTree| and 326 // Used for use counter |InnerTextWithShadowTree| and
273 // |SelectionToStringWithShadowTree|, we should not use other purpose. 327 // |SelectionToStringWithShadowTree|, we should not use other purpose.
274 bool handle_shadow_root_; 328 bool handle_shadow_root_;
275 329
276 // Contains state of emitted text. 330 // Contains state of emitted text.
277 TextIteratorTextState text_state_; 331 TextIteratorTextState text_state_;
332
333 // Helper for extracting text content from text nodes.
334 // TODO(xiaochengh): We should store a pointer here, so that we can use
335 // forward declaration and switch it to Layout NG easier.
336 TextIteratorTextNodeHandler text_node_handler_;
278 }; 337 };
279 338
280 extern template class CORE_EXTERN_TEMPLATE_EXPORT 339 extern template class CORE_EXTERN_TEMPLATE_EXPORT
281 TextIteratorAlgorithm<EditingStrategy>; 340 TextIteratorAlgorithm<EditingStrategy>;
282 extern template class CORE_EXTERN_TEMPLATE_EXPORT 341 extern template class CORE_EXTERN_TEMPLATE_EXPORT
283 TextIteratorAlgorithm<EditingInFlatTreeStrategy>; 342 TextIteratorAlgorithm<EditingInFlatTreeStrategy>;
284 343
285 using TextIterator = TextIteratorAlgorithm<EditingStrategy>; 344 using TextIterator = TextIteratorAlgorithm<EditingStrategy>;
286 using TextIteratorInFlatTree = TextIteratorAlgorithm<EditingInFlatTreeStrategy>; 345 using TextIteratorInFlatTree = TextIteratorAlgorithm<EditingInFlatTreeStrategy>;
287 346
288 } // namespace blink 347 } // namespace blink
289 348
290 #endif // TextIterator_h 349 #endif // TextIterator_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698