2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "core/editing/RenderedPosition.h"
34 #include "core/dom/Position.h"
35 #include "core/editing/VisiblePosition.h"
36 #include "core/rendering/RenderLayer.h"
37 #include "core/rendering/compositing/CompositedSelectionBound.h"
41 static inline RenderObject* rendererFromPosition(const Position& position)
43 ASSERT(position.isNotNull());
44 Node* rendererNode = 0;
45 switch (position.anchorType()) {
46 case Position::PositionIsOffsetInAnchor:
47 rendererNode = position.computeNodeAfterPosition();
48 if (!rendererNode || !rendererNode->renderer())
49 rendererNode = position.anchorNode()->lastChild();
52 case Position::PositionIsBeforeAnchor:
53 case Position::PositionIsAfterAnchor:
56 case Position::PositionIsBeforeChildren:
57 rendererNode = position.anchorNode()->firstChild();
59 case Position::PositionIsAfterChildren:
60 rendererNode = position.anchorNode()->lastChild();
63 if (!rendererNode || !rendererNode->renderer())
64 rendererNode = position.anchorNode();
65 return rendererNode->renderer();
68 RenderedPosition::RenderedPosition(const VisiblePosition& position)
72 , m_prevLeafChild(uncachedInlineBox())
73 , m_nextLeafChild(uncachedInlineBox())
75 if (position.isNull())
77 position.getInlineBoxAndOffset(m_inlineBox, m_offset);
79 m_renderer = &m_inlineBox->renderer();
81 m_renderer = rendererFromPosition(position.deepEquivalent());
84 RenderedPosition::RenderedPosition(const Position& position, EAffinity affinity)
88 , m_prevLeafChild(uncachedInlineBox())
89 , m_nextLeafChild(uncachedInlineBox())
91 if (position.isNull())
93 position.getInlineBoxAndOffset(affinity, m_inlineBox, m_offset);
95 m_renderer = &m_inlineBox->renderer();
97 m_renderer = rendererFromPosition(position);
100 InlineBox* RenderedPosition::prevLeafChild() const
102 if (m_prevLeafChild == uncachedInlineBox())
103 m_prevLeafChild = m_inlineBox->prevLeafChildIgnoringLineBreak();
104 return m_prevLeafChild;
107 InlineBox* RenderedPosition::nextLeafChild() const
109 if (m_nextLeafChild == uncachedInlineBox())
110 m_nextLeafChild = m_inlineBox->nextLeafChildIgnoringLineBreak();
111 return m_nextLeafChild;
114 bool RenderedPosition::isEquivalent(const RenderedPosition& other) const
116 return (m_renderer == other.m_renderer && m_inlineBox == other.m_inlineBox && m_offset == other.m_offset)
117 || (atLeftmostOffsetInBox() && other.atRightmostOffsetInBox() && prevLeafChild() == other.m_inlineBox)
118 || (atRightmostOffsetInBox() && other.atLeftmostOffsetInBox() && nextLeafChild() == other.m_inlineBox);
121 unsigned char RenderedPosition::bidiLevelOnLeft() const
123 InlineBox* box = atLeftmostOffsetInBox() ? prevLeafChild() : m_inlineBox;
124 return box ? box->bidiLevel() : 0;
127 unsigned char RenderedPosition::bidiLevelOnRight() const
129 InlineBox* box = atRightmostOffsetInBox() ? nextLeafChild() : m_inlineBox;
130 return box ? box->bidiLevel() : 0;
133 RenderedPosition RenderedPosition::leftBoundaryOfBidiRun(unsigned char bidiLevelOfRun)
135 if (!m_inlineBox || bidiLevelOfRun > m_inlineBox->bidiLevel())
136 return RenderedPosition();
138 InlineBox* box = m_inlineBox;
140 InlineBox* prev = box->prevLeafChildIgnoringLineBreak();
141 if (!prev || prev->bidiLevel() < bidiLevelOfRun)
142 return RenderedPosition(&box->renderer(), box, box->caretLeftmostOffset());
146 ASSERT_NOT_REACHED();
147 return RenderedPosition();
150 RenderedPosition RenderedPosition::rightBoundaryOfBidiRun(unsigned char bidiLevelOfRun)
152 if (!m_inlineBox || bidiLevelOfRun > m_inlineBox->bidiLevel())
153 return RenderedPosition();
155 InlineBox* box = m_inlineBox;
157 InlineBox* next = box->nextLeafChildIgnoringLineBreak();
158 if (!next || next->bidiLevel() < bidiLevelOfRun)
159 return RenderedPosition(&box->renderer(), box, box->caretRightmostOffset());
163 ASSERT_NOT_REACHED();
164 return RenderedPosition();
167 bool RenderedPosition::atLeftBoundaryOfBidiRun(ShouldMatchBidiLevel shouldMatchBidiLevel, unsigned char bidiLevelOfRun) const
172 if (atLeftmostOffsetInBox()) {
173 if (shouldMatchBidiLevel == IgnoreBidiLevel)
174 return !prevLeafChild() || prevLeafChild()->bidiLevel() < m_inlineBox->bidiLevel();
175 return m_inlineBox->bidiLevel() >= bidiLevelOfRun && (!prevLeafChild() || prevLeafChild()->bidiLevel() < bidiLevelOfRun);
178 if (atRightmostOffsetInBox()) {
179 if (shouldMatchBidiLevel == IgnoreBidiLevel)
180 return nextLeafChild() && m_inlineBox->bidiLevel() < nextLeafChild()->bidiLevel();
181 return nextLeafChild() && m_inlineBox->bidiLevel() < bidiLevelOfRun && nextLeafChild()->bidiLevel() >= bidiLevelOfRun;
187 bool RenderedPosition::atRightBoundaryOfBidiRun(ShouldMatchBidiLevel shouldMatchBidiLevel, unsigned char bidiLevelOfRun) const
192 if (atRightmostOffsetInBox()) {
193 if (shouldMatchBidiLevel == IgnoreBidiLevel)
194 return !nextLeafChild() || nextLeafChild()->bidiLevel() < m_inlineBox->bidiLevel();
195 return m_inlineBox->bidiLevel() >= bidiLevelOfRun && (!nextLeafChild() || nextLeafChild()->bidiLevel() < bidiLevelOfRun);
198 if (atLeftmostOffsetInBox()) {
199 if (shouldMatchBidiLevel == IgnoreBidiLevel)
200 return prevLeafChild() && m_inlineBox->bidiLevel() < prevLeafChild()->bidiLevel();
201 return prevLeafChild() && m_inlineBox->bidiLevel() < bidiLevelOfRun && prevLeafChild()->bidiLevel() >= bidiLevelOfRun;
207 Position RenderedPosition::positionAtLeftBoundaryOfBiDiRun() const
209 ASSERT(atLeftBoundaryOfBidiRun());
211 if (atLeftmostOffsetInBox())
212 return createLegacyEditingPosition(m_renderer->node(), m_offset);
214 return createLegacyEditingPosition(nextLeafChild()->renderer().node(), nextLeafChild()->caretLeftmostOffset());
217 Position RenderedPosition::positionAtRightBoundaryOfBiDiRun() const
219 ASSERT(atRightBoundaryOfBidiRun());
221 if (atRightmostOffsetInBox())
222 return createLegacyEditingPosition(m_renderer->node(), m_offset);
224 return createLegacyEditingPosition(prevLeafChild()->renderer().node(), prevLeafChild()->caretRightmostOffset());
227 IntRect RenderedPosition::absoluteRect(LayoutUnit* extraWidthToEndOfLine) const
232 IntRect localRect = pixelSnappedIntRect(m_renderer->localCaretRect(m_inlineBox, m_offset, extraWidthToEndOfLine));
233 return localRect == IntRect() ? IntRect() : m_renderer->localToAbsoluteQuad(FloatRect(localRect)).enclosingBoundingBox();
236 void RenderedPosition::positionInGraphicsLayerBacking(CompositedSelectionBound& bound) const
238 bound.layer = nullptr;
239 bound.edgeTopInLayer = bound.edgeBottomInLayer = FloatPoint();
244 LayoutRect rect = m_renderer->localCaretRect(m_inlineBox, m_offset);
245 if (rect == LayoutRect())
249 bound.edgeTopInLayer = m_renderer->localToInvalidationBackingPoint(rect.minXMinYCorner(), &layer);
250 bound.edgeBottomInLayer = m_renderer->localToInvalidationBackingPoint(rect.minXMaxYCorner(), nullptr);
251 bound.layer = layer->graphicsLayerBacking();
254 bool renderObjectContainsPosition(RenderObject* target, const Position& position)
256 for (RenderObject* renderer = rendererFromPosition(position); renderer && renderer->node(); renderer = renderer->parent()) {
257 if (renderer == target)