Bug 1921963 part 1 - Use JS::ReportUncatchableException more outside the JS engine...
[gecko.git] / dom / events / ContentEventHandler.h
blob4216063c61d236d08f1c38c139bcc8dd223b426e
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_ContentEventHandler_h_
8 #define mozilla_ContentEventHandler_h_
10 #include "js/GCAPI.h"
11 #include "mozilla/Assertions.h"
12 #include "mozilla/EventForwards.h"
13 #include "mozilla/RangeBoundary.h"
14 #include "mozilla/dom/Selection.h"
15 #include "mozilla/dom/Text.h"
16 #include "nsCOMPtr.h"
17 #include "nsIFrame.h"
18 #include "nsINode.h"
20 class nsPresContext;
21 class nsRange;
23 struct nsRect;
25 namespace mozilla {
27 namespace dom {
28 class Element;
29 } // namespace dom
31 enum LineBreakType { LINE_BREAK_TYPE_NATIVE, LINE_BREAK_TYPE_XP };
34 * Query Content Event Handler
35 * ContentEventHandler is a helper class for EventStateManager.
36 * The platforms request some content informations, e.g., the selected text,
37 * the offset of the selected text and the text for specified range.
38 * This class answers to NS_QUERY_* events from actual contents.
41 class MOZ_STACK_CLASS ContentEventHandler {
42 private:
43 /**
44 * SimpleRangeBase is a helper template class of ContentEventHandler class
45 * that stores 2 DOM points as a range without observing the mutation. I.e.,
46 * similar to dom::StaticRange, but can only be on the stack and does not have
47 * unnecessary features for ContentEventHandler so it is fast.
48 * Therefore, initializers are responsible for making sure the start/end nodes
49 * are in document order. This is enforced by assertions in DEBUG builds.
51 template <typename NodeType, typename RangeBoundaryType>
52 class MOZ_STACK_CLASS SimpleRangeBase final {
53 public:
54 SimpleRangeBase();
55 SimpleRangeBase(SimpleRangeBase<NodeType, RangeBoundaryType>&&) noexcept;
56 template <typename OtherNodeType, typename OtherRangeBoundaryType>
57 explicit SimpleRangeBase(
58 const SimpleRangeBase<OtherNodeType, OtherRangeBoundaryType>& aOther);
59 #ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
60 ~SimpleRangeBase();
61 #endif
63 void Clear() {
64 mRoot = nullptr;
65 mStart = {};
66 mEnd = {};
69 bool IsPositioned() const { return mStart.IsSet() && mEnd.IsSet(); }
70 bool Collapsed() const { return mStart == mEnd && IsPositioned(); }
71 nsINode* GetStartContainer() const { return mStart.Container(); }
72 nsINode* GetEndContainer() const { return mEnd.Container(); }
73 uint32_t StartOffset() const {
74 return *mStart.Offset(
75 RangeBoundaryType::OffsetFilter::kValidOrInvalidOffsets);
77 uint32_t EndOffset() const {
78 return *mEnd.Offset(
79 RangeBoundaryType::OffsetFilter::kValidOrInvalidOffsets);
81 nsIContent* StartRef() const { return mStart.Ref(); }
82 nsIContent* EndRef() const { return mEnd.Ref(); }
84 const RangeBoundaryType& Start() const { return mStart; }
85 const RangeBoundaryType& End() const { return mEnd; }
87 nsINode* GetRoot() const { return mRoot; }
89 // XXX: Make these use RangeBoundaries...
90 nsresult CollapseTo(const RawRangeBoundary& aBoundary) {
91 return SetStartAndEnd(aBoundary, aBoundary);
93 nsresult SetStart(const RawRangeBoundary& aStart);
94 nsresult SetEnd(const RawRangeBoundary& aEnd);
96 // NOTE: These helpers can hide performance problems, as they perform a
97 // search to find aStartOffset in aStartContainer.
98 nsresult SetStart(nsINode* aStartContainer, uint32_t aStartOffset) {
99 return SetStart(RawRangeBoundary(aStartContainer, aStartOffset));
101 nsresult SetEnd(nsINode* aEndContainer, uint32_t aEndOffset) {
102 return SetEnd(RawRangeBoundary(aEndContainer, aEndOffset));
105 nsresult SetEndAfter(nsINode* aEndContainer);
106 void SetStartAndEnd(const nsRange* aRange);
107 nsresult SetStartAndEnd(const RawRangeBoundary& aStart,
108 const RawRangeBoundary& aEnd);
110 nsresult SelectNodeContents(const nsINode* aNodeToSelectContents);
112 private:
113 inline void AssertStartIsBeforeOrEqualToEnd();
115 NodeType mRoot;
117 RangeBoundaryType mStart;
118 RangeBoundaryType mEnd;
120 #ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
121 nsMutationGuard mMutationGuard;
122 Maybe<JS::AutoAssertNoGC> mAssertNoGC;
123 #endif
126 using SimpleRange = SimpleRangeBase<RefPtr<nsINode>, RangeBoundary>;
127 using UnsafeSimpleRange = SimpleRangeBase<nsINode*, RawRangeBoundary>;
129 public:
130 using Element = dom::Element;
131 using Selection = dom::Selection;
133 explicit ContentEventHandler(nsPresContext* aPresContext);
135 // Handle aEvent in the current process.
136 MOZ_CAN_RUN_SCRIPT nsresult
137 HandleQueryContentEvent(WidgetQueryContentEvent* aEvent);
139 // eQuerySelectedText event handler
140 MOZ_CAN_RUN_SCRIPT nsresult
141 OnQuerySelectedText(WidgetQueryContentEvent* aEvent);
142 // eQueryTextContent event handler
143 MOZ_CAN_RUN_SCRIPT nsresult
144 OnQueryTextContent(WidgetQueryContentEvent* aEvent);
145 // eQueryCaretRect event handler
146 MOZ_CAN_RUN_SCRIPT nsresult OnQueryCaretRect(WidgetQueryContentEvent* aEvent);
147 // eQueryTextRect event handler
148 MOZ_CAN_RUN_SCRIPT nsresult OnQueryTextRect(WidgetQueryContentEvent* aEvent);
149 // eQueryTextRectArray event handler
150 MOZ_CAN_RUN_SCRIPT nsresult
151 OnQueryTextRectArray(WidgetQueryContentEvent* aEvent);
152 // eQueryEditorRect event handler
153 MOZ_CAN_RUN_SCRIPT nsresult
154 OnQueryEditorRect(WidgetQueryContentEvent* aEvent);
155 // eQueryContentState event handler
156 MOZ_CAN_RUN_SCRIPT nsresult
157 OnQueryContentState(WidgetQueryContentEvent* aEvent);
158 // eQuerySelectionAsTransferable event handler
159 MOZ_CAN_RUN_SCRIPT nsresult
160 OnQuerySelectionAsTransferable(WidgetQueryContentEvent* aEvent);
161 // eQueryCharacterAtPoint event handler
162 MOZ_CAN_RUN_SCRIPT nsresult
163 OnQueryCharacterAtPoint(WidgetQueryContentEvent* aEvent);
164 // eQueryDOMWidgetHittest event handler
165 MOZ_CAN_RUN_SCRIPT nsresult
166 OnQueryDOMWidgetHittest(WidgetQueryContentEvent* aEvent);
167 // eQueryDropTargetHittest event handler
168 MOZ_CAN_RUN_SCRIPT nsresult
169 OnQueryDropTargetHittest(WidgetQueryContentEvent* aEvent);
171 // NS_SELECTION_* event
172 MOZ_CAN_RUN_SCRIPT nsresult OnSelectionEvent(WidgetSelectionEvent* aEvent);
174 protected:
175 RefPtr<dom::Document> mDocument;
176 // mSelection is typically normal selection but if OnQuerySelectedText()
177 // is called, i.e., handling eQuerySelectedText, it's the specified selection
178 // by WidgetQueryContentEvent::mInput::mSelectionType.
179 RefPtr<Selection> mSelection;
180 // mFirstSelectedSimpleRange is initialized from the first range of
181 // mSelection, if it exists. Otherwise, it is reset by Clear().
182 SimpleRange mFirstSelectedSimpleRange;
183 RefPtr<Element> mRootElement;
185 MOZ_CAN_RUN_SCRIPT nsresult Init(WidgetQueryContentEvent* aEvent);
186 MOZ_CAN_RUN_SCRIPT nsresult Init(WidgetSelectionEvent* aEvent);
188 nsresult InitBasic(bool aRequireFlush = true);
189 MOZ_CAN_RUN_SCRIPT nsresult
190 InitCommon(EventMessage aEventMessage,
191 SelectionType aSelectionType = SelectionType::eNormal,
192 bool aRequireFlush = true);
194 * InitRootContent() computes the root content of current focused editor.
196 * @param aNormalSelection This must be a Selection instance whose type is
197 * SelectionType::eNormal.
199 MOZ_CAN_RUN_SCRIPT nsresult
200 InitRootContent(const Selection& aNormalSelection);
202 public:
203 // FlatText means the text that is generated from DOM tree. The BR elements
204 // are replaced to native linefeeds. Other elements are ignored.
206 // RawNodePosition stores a pair of node and offset in the node.
207 // When mNode is an element and mOffset is 0, the start position means after
208 // the open tag of mNode.
209 // This is useful to receive one or more sets of them instead of nsRange.
210 // This type is intended to be used for short-lived operations, and is thus
211 // marked MOZ_STACK_CLASS.
212 struct MOZ_STACK_CLASS RawNodePosition : public RawRangeBoundary {
213 // Only when mNode is an element node and mOffset is 0, mAfterOpenTag is
214 // referred.
215 bool mAfterOpenTag = true;
217 RawNodePosition() = default;
218 MOZ_IMPLICIT RawNodePosition(const RawNodePosition& aOther)
219 : RawRangeBoundary(aOther),
220 mAfterOpenTag(aOther.mAfterOpenTag)
221 // Don't use the copy constructor of mAssertNoGC.
225 * Factory method returning a RawNodePosition object which points start of
226 * first content of aContainer (first child or first character in the data).
227 * I.e., if aContainer is an element node, the result points before the
228 * first child but after the open tag, e.g., <div>{}abc</div> if aContainer
229 * is the <div>. This is important to understand the difference with the
230 * result of Before().
232 static RawNodePosition BeforeFirstContentOf(const nsINode& aContainer) {
233 return RawNodePosition(const_cast<nsINode*>(&aContainer), 0u);
237 * Factory method returning a RawNodePosition object which points after
238 * aContent. I.e., if aContent is an element node, the result points after
239 * its close tag, e.g., `<div>abc</div>{}` if aContent is the <div>.
241 static RawNodePosition After(const nsIContent& aContent) {
242 RawNodePosition it(aContent.GetParentNode(),
243 const_cast<nsIContent*>(&aContent));
244 it.mAfterOpenTag = false;
245 return it;
249 * Factory method returning a RawNodePosition object which points end of
250 * aContainer. If aContainer is an element node, the result points before
251 * its close tag, e.g., `<div>abc{}</div>` if aContainer is the <div>.
253 static RawNodePosition AtEndOf(const nsINode& aContainer) {
254 return RawNodePosition(const_cast<nsINode*>(&aContainer),
255 aContainer.IsText()
256 ? aContainer.AsText()->TextDataLength()
257 : aContainer.GetChildCount());
261 * Factory method returning a RawNodePosition object which points before
262 * aContent. I.e., if aContent is an element node, the result points
263 * before its open tag, e.g., `{}<div>abc</div>` if aContent is the <div>.
264 * Note that this sets different containers whether aContent is being
265 * removed or not. If aContent is being removed, i.e., this is used in
266 * nsIMutationObserver::ContentRemoved(), aContent is already not a child
267 * of its ex-parent. Therefore, the container becomes aContent, but
268 * indicates that it points before the container with mAfterOpenTag.
269 * On the other hand, if it's not being removed, the container is set to
270 * the parent node of aContent. So, in this case, it points after the
271 * previous sibling of aContent actually.
273 static RawNodePosition Before(const nsIContent& aContent) {
274 if (!aContent.IsBeingRemoved()) {
275 return RawNodePosition(aContent.GetParentNode(),
276 aContent.GetPreviousSibling());
278 RawNodePosition ret(const_cast<nsIContent*>(&aContent), 0u);
279 ret.mAfterOpenTag = false;
280 return ret;
283 RawNodePosition(nsINode* aContainer, uint32_t aOffset)
284 : RawRangeBoundary(aContainer, aOffset) {}
286 RawNodePosition(nsINode* aContainer, nsIContent* aRef)
287 : RawRangeBoundary(aContainer, aRef) {}
289 explicit RawNodePosition(const nsIFrame::ContentOffsets& aContentOffsets)
290 : RawRangeBoundary(aContentOffsets.content, aContentOffsets.offset) {}
292 #ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
293 ~RawNodePosition() { MOZ_DIAGNOSTIC_ASSERT(!mMutationGuard.Mutated(0)); }
294 #endif // #ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
296 public:
297 const RawNodePosition& operator=(const RawNodePosition& aOther) {
298 if (this != &aOther) {
299 RawRangeBoundary::operator=(aOther);
300 mAfterOpenTag = aOther.mAfterOpenTag;
302 return *this;
305 bool operator==(const RawNodePosition& aOther) const {
306 return RawRangeBoundary::operator==(aOther) &&
307 mAfterOpenTag == aOther.mAfterOpenTag;
310 bool IsBeforeOpenTag() const {
311 return IsSet() && Container()->IsElement() && !Ref() && !mAfterOpenTag;
313 bool IsImmediatelyAfterOpenTag() const {
314 return IsSet() && Container()->IsElement() && !Ref() && mAfterOpenTag;
317 #ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
318 private:
319 nsMutationGuard mMutationGuard;
320 JS::AutoAssertNoGC mAssertNoGC;
321 #endif // #ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
325 * Get the flatten text length in the range.
326 * @param aStartPosition Start node and offset in the node of the range.
327 * If the container is an element node, it's
328 * important to start from before or after its open
329 * tag because open tag of some elements causes a
330 * line break in the result. If you need the line
331 * break, you need to use
332 * RawNodePosition::Before().
333 * @param aEndPosition End node and offset in the node of the range.
334 * If you don't want to include line break which is
335 * caused by the open tag of the container when
336 * you specify start of an element node, you need
337 * to use RawNodePosition::Before().
338 * @param aRootElement The root element of the editor or document.
339 * aRootElement won't cause any text including
340 * line breaks.
341 * @param aLength The result of the flatten text length of the
342 * range.
343 * @param aLineBreakType Whether this computes flatten text length with
344 * native line breakers on the platform or
345 * with XP line breaker (\n).
346 * @param aIsRemovingNode Should be true only when this is called from
347 * nsIMutationObserver::ContentRemoved().
348 * When this is true, the container of
349 * aStartPosition should be the removing node and
350 * points start of it and the container of
351 * aEndPosition must be same as the container of
352 * aStartPosition and points end of the container.
354 static nsresult GetFlatTextLengthInRange(
355 const RawNodePosition& aStartPosition,
356 const RawNodePosition& aEndPosition, const Element* aRootElement,
357 uint32_t* aLength, LineBreakType aLineBreakType,
358 bool aIsRemovingNode = false);
360 // Computes the native text length between aStartOffset and aEndOffset of
361 // aTextNode.
362 static uint32_t GetNativeTextLength(const dom::Text& aTextNode,
363 uint32_t aStartOffset,
364 uint32_t aEndOffset);
365 // Get the native text length of aTextNode.
366 static uint32_t GetNativeTextLength(const dom::Text& aTextNode,
367 uint32_t aMaxLength = UINT32_MAX);
369 static uint32_t GetNativeTextLength(const nsAString& aText);
371 // Get the range between start offset and end offset
372 MOZ_CAN_RUN_SCRIPT
373 already_AddRefed<nsRange> GetRangeFromFlatTextOffset(
374 WidgetContentCommandEvent* aEvent, uint32_t aOffset, uint32_t aLength);
376 // Get the contents of aRange as plain text.
377 nsresult GenerateFlatTextContent(const nsRange* aRange, nsString& aString);
379 protected:
380 // Get the text length of aTextNode.
381 static uint32_t GetTextLength(const dom::Text& aTextNode,
382 LineBreakType aLineBreakType,
383 uint32_t aMaxLength = UINT32_MAX);
384 // Get the text length of a given range of a content node in
385 // the given line break type.
386 static uint32_t GetTextLengthInRange(const dom::Text& aTextNode,
387 uint32_t aXPStartOffset,
388 uint32_t aXPEndOffset,
389 LineBreakType aLineBreakType);
390 // Get the contents in aElement (meaning all children of aElement) as plain
391 // text. E.g., specifying mRootElement gets whole text in it.
392 // Note that the result is not same as .textContent. The result is
393 // optimized for native IMEs. For example, <br> element and some block
394 // elements causes "\n" (or "\r\n"), see also ShouldBreakLineBefore().
395 nsresult GenerateFlatTextContent(const Element* aElement, nsString& aString,
396 LineBreakType aLineBreakType);
397 // Get the contents of aRange as plain text.
398 template <typename NodeType, typename RangeBoundaryType>
399 nsresult GenerateFlatTextContent(
400 const SimpleRangeBase<NodeType, RangeBoundaryType>& aSimpleRange,
401 nsString& aString, LineBreakType aLineBreakType);
402 // Get offset of start of aRange. Note that the result includes the length
403 // of line breaker caused by the start of aContent because aRange never
404 // includes the line breaker caused by its start node.
405 template <typename SimpleRangeType>
406 nsresult GetStartOffset(const SimpleRangeType& aSimpleRange,
407 uint32_t* aOffset, LineBreakType aLineBreakType);
408 // Check if we should insert a line break before aContent.
409 // This should return false only when aContent is an html element which
410 // is typically used in a paragraph like <em>.
411 static bool ShouldBreakLineBefore(const nsIContent& aContent,
412 const Element* aRootElement);
413 // Get the line breaker length.
414 static inline uint32_t GetBRLength(LineBreakType aLineBreakType);
415 static LineBreakType GetLineBreakType(WidgetQueryContentEvent* aEvent);
416 static LineBreakType GetLineBreakType(WidgetSelectionEvent* aEvent);
417 static LineBreakType GetLineBreakType(bool aUseNativeLineBreak);
418 // Returns focused content (including its descendant documents).
419 nsIContent* GetFocusedContent();
420 // QueryContentRect() sets the rect of aContent's frame(s) to aEvent.
421 nsresult QueryContentRect(nsIContent* aContent,
422 WidgetQueryContentEvent* aEvent);
424 template <typename RangeType, typename TextNodeType>
425 struct MOZ_STACK_CLASS DOMRangeAndAdjustedOffsetInFlattenedTextBase {
426 bool RangeStartsFromLastTextNode() const {
427 return mLastTextNode && mRange.GetStartContainer() == mLastTextNode;
429 bool RangeStartsFromEndOfContainer() const {
430 return mRange.GetStartContainer() &&
431 mRange.GetStartContainer()->Length() == mRange.StartOffset();
433 bool RangeStartsFromContent() const {
434 return mRange.GetStartContainer() &&
435 mRange.GetStartContainer()->IsContent();
438 // The range in the DOM tree.
439 RangeType mRange;
440 // Actual start offset of the range in the flattened text. If aOffset
441 // of ConvertFlatTextOffsetToDOMRange() is middle of a surrogate pair,
442 // a CRLF or a complex character of some languages, this may be set to
443 // different offset.
444 uint32_t mAdjustedOffset = 0;
445 // The last text node which is found while walking the tree.
446 // If the range ends in a text node, this is the text node. Otherwise,
447 // the last found text node before the end container of mRange.
448 TextNodeType mLastTextNode = nullptr;
450 using DOMRangeAndAdjustedOffsetInFlattenedText =
451 DOMRangeAndAdjustedOffsetInFlattenedTextBase<SimpleRange,
452 RefPtr<dom::Text>>;
453 using UnsafeDOMRangeAndAdjustedOffsetInFlattenedText =
454 DOMRangeAndAdjustedOffsetInFlattenedTextBase<UnsafeSimpleRange,
455 dom::Text*>;
458 * Scans the DOM tree and set mRange as same as from aOffset to aOffset +
459 * aLength in the flattened text.
460 * NOTE: Use ConvertFlatTextOffsetToDOMRange() or
461 * ConvertFlatTextOffsetToUnsafeDOMRange() instead of
462 * ConvertFlatTextOffsetToDOMRangeBase<RangeType, TextNodeType>().
464 template <typename RangeType, typename TextNodeType>
465 Result<DOMRangeAndAdjustedOffsetInFlattenedTextBase<RangeType, TextNodeType>,
466 nsresult>
467 ConvertFlatTextOffsetToDOMRangeBase(uint32_t aOffset, uint32_t aLength,
468 LineBreakType aLineBreakType,
469 bool aExpandToClusterBoundaries);
470 MOZ_ALWAYS_INLINE Result<DOMRangeAndAdjustedOffsetInFlattenedText, nsresult>
471 ConvertFlatTextOffsetToDOMRange(uint32_t aOffset, uint32_t aLength,
472 LineBreakType aLineBreakType,
473 bool aExpandToClusterBoundaries) {
474 return ConvertFlatTextOffsetToDOMRangeBase<SimpleRange, RefPtr<dom::Text>>(
475 aOffset, aLength, aLineBreakType, aExpandToClusterBoundaries);
477 MOZ_ALWAYS_INLINE
478 Result<UnsafeDOMRangeAndAdjustedOffsetInFlattenedText, nsresult>
479 ConvertFlatTextOffsetToUnsafeDOMRange(uint32_t aOffset, uint32_t aLength,
480 LineBreakType aLineBreakType,
481 bool aExpandToClusterBoundaries) {
482 return ConvertFlatTextOffsetToDOMRangeBase<UnsafeSimpleRange, dom::Text*>(
483 aOffset, aLength, aLineBreakType, aExpandToClusterBoundaries);
486 // If the aSimpleRange isn't in text node but next to a text node,
487 // this method modifies it in the text node. Otherwise, not modified.
488 // Note that aSimpleRange must be collapsed.
489 nsresult AdjustCollapsedRangeMaybeIntoTextNode(SimpleRange& aSimpleRange);
490 // Convert the frame relative offset to be relative to the root frame of the
491 // root presContext (but still measured in appUnits of aFrame's presContext).
492 nsresult ConvertToRootRelativeOffset(nsIFrame* aFrame, nsRect& aRect);
493 // Expand aXPOffset to the nearest offset in cluster boundary. aForward is
494 // true, it is expanded to forward.
495 // FYI: Due to `nsFrameSelection::GetFrameForNodeOffset()`, this cannot
496 // take `const dom::Text&`.
497 nsresult ExpandToClusterBoundary(dom::Text& aTextNode, bool aForward,
498 uint32_t* aXPOffset) const;
500 using FontRangeArray = nsTArray<mozilla::FontRange>;
501 static void AppendFontRanges(FontRangeArray& aFontRanges,
502 const dom::Text& aTextNode, uint32_t aBaseOffset,
503 uint32_t aXPStartOffset, uint32_t aXPEndOffset,
504 LineBreakType aLineBreakType);
505 nsresult GenerateFlatFontRanges(const UnsafeSimpleRange& aSimpleRange,
506 FontRangeArray& aFontRanges,
507 uint32_t& aLength,
508 LineBreakType aLineBreakType);
509 nsresult QueryTextRectByRange(const SimpleRange& aSimpleRange,
510 LayoutDeviceIntRect& aRect,
511 WritingMode& aWritingMode);
513 struct MOZ_STACK_CLASS FrameAndNodeOffset final {
514 // mFrame is safe since this can live in only stack class and
515 // ContentEventHandler doesn't modify layout after
516 // ContentEventHandler::Init() flushes pending layout. In other words,
517 // this struct shouldn't be used before calling
518 // ContentEventHandler::Init().
519 nsIFrame* mFrame;
520 // offset in the node of mFrame
521 int32_t mOffsetInNode;
523 FrameAndNodeOffset() : mFrame(nullptr), mOffsetInNode(-1) {}
525 FrameAndNodeOffset(nsIFrame* aFrame, int32_t aStartOffsetInNode)
526 : mFrame(aFrame), mOffsetInNode(aStartOffsetInNode) {}
528 nsIFrame* operator->() { return mFrame; }
529 const nsIFrame* operator->() const { return mFrame; }
530 operator nsIFrame*() { return mFrame; }
531 operator const nsIFrame*() const { return mFrame; }
532 bool IsValid() const { return mFrame && mOffsetInNode >= 0; }
534 // Get first frame after the start of the given range for computing text rect.
535 // This returns invalid FrameAndNodeOffset if there is no content which
536 // should affect to computing text rect in the range. mOffsetInNode is start
537 // offset in the frame.
538 template <typename NodeType, typename RangeBoundaryType>
539 FrameAndNodeOffset GetFirstFrameInRangeForTextRect(
540 const SimpleRangeBase<NodeType, RangeBoundaryType>& aSimpleRange);
542 // Get last frame before the end of the given range for computing text rect.
543 // This returns invalid FrameAndNodeOffset if there is no content which
544 // should affect to computing text rect in the range. mOffsetInNode is end
545 // offset in the frame.
546 template <typename NodeType, typename RangeBoundaryType>
547 FrameAndNodeOffset GetLastFrameInRangeForTextRect(
548 const SimpleRangeBase<NodeType, RangeBoundaryType>& aSimpleRange);
550 struct MOZ_STACK_CLASS FrameRelativeRect final {
551 // mRect is relative to the mBaseFrame's position.
552 nsRect mRect;
553 nsIFrame* mBaseFrame;
555 FrameRelativeRect() : mBaseFrame(nullptr) {}
557 explicit FrameRelativeRect(nsIFrame* aBaseFrame) : mBaseFrame(aBaseFrame) {}
559 FrameRelativeRect(const nsRect& aRect, nsIFrame* aBaseFrame)
560 : mRect(aRect), mBaseFrame(aBaseFrame) {}
562 bool IsValid() const { return mBaseFrame != nullptr; }
564 // Returns an nsRect relative to aBaseFrame instead of mBaseFrame.
565 nsRect RectRelativeTo(nsIFrame* aBaseFrame) const;
568 // Returns a rect for line breaker before the node of aFrame (If aFrame is
569 // a <br> frame or a block level frame, it causes a line break at its
570 // element's open tag, see also ShouldBreakLineBefore()). Note that this
571 // doesn't check if aFrame should cause line break in non-debug build.
572 FrameRelativeRect GetLineBreakerRectBefore(nsIFrame* aFrame);
574 // Returns a line breaker rect after aTextNode as there is a line breaker
575 // immediately after aTextNode. This is useful when following block
576 // element causes a line break before it and it needs to compute the line
577 // breaker's rect. For example, if there is |<p>abc</p><p>def</p>|, the
578 // rect of 2nd <p>'s line breaker should be at right of "c" in the first
579 // <p>, not the start of 2nd <p>. The result is relative to the last text
580 // frame which represents the last character of aTextNode.
581 FrameRelativeRect GuessLineBreakerRectAfter(const dom::Text& aTextNode);
583 // Returns a guessed first rect. I.e., it may be different from actual
584 // caret when selection is collapsed at start of aFrame. For example, this
585 // guess the caret rect only with the content box of aFrame and its font
586 // height like:
587 // +-aFrame----------------- (border box)
588 // |
589 // | +--------------------- (content box)
590 // | | I
591 // ^ guessed caret rect
592 // However, actual caret is computed with more information like line-height,
593 // child frames of aFrame etc. But this does not emulate actual caret
594 // behavior exactly for simpler and faster code because it's difficult and
595 // we're not sure it's worthwhile to do it with complicated implementation.
596 FrameRelativeRect GuessFirstCaretRectIn(nsIFrame* aFrame);
598 // Make aRect non-empty. If width and/or height is 0, these methods set them
599 // to 1. Note that it doesn't set nsRect's width nor height to one device
600 // pixel because using nsRect::ToOutsidePixels() makes actual width or height
601 // to 2 pixels because x and y may not be aligned to device pixels.
602 void EnsureNonEmptyRect(nsRect& aRect) const;
603 void EnsureNonEmptyRect(LayoutDeviceIntRect& aRect) const;
606 * Compute caret rect before or after a character rect.
608 static LayoutDeviceIntRect GetCaretRectBefore(
609 const LayoutDeviceIntRect& aCharRect, const WritingMode& aWritingMode);
610 static LayoutDeviceIntRect GetCaretRectAfter(
611 const LayoutDeviceIntRect& aCharRect, const WritingMode& aWritingMode);
612 static nsRect GetCaretRectBefore(const nsRect& aCharRect,
613 const WritingMode& aWritingMode);
614 static nsRect GetCaretRectAfter(nsPresContext& aPresContext,
615 const nsRect& aCharRect,
616 const WritingMode& aWritingMode);
618 nsresult QueryHittestImpl(WidgetQueryContentEvent* aEvent, bool aFlushLayout,
619 Element** aContentUnderMouse);
622 } // namespace mozilla
624 #endif // mozilla_ContentEventHandler_h_