| 1 | /* |
| 2 | * Copyright (C) 2011 Google Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * |
| 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 |
| 13 | * distribution. |
| 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. |
| 17 | * |
| 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. |
| 29 | */ |
| 30 | |
| 31 | #include "config.h" |
| 32 | #include "RenderedPosition.h" |
| 33 | |
| 34 | #include "InlineTextBox.h" |
| 35 | #include "VisiblePosition.h" |
| 36 | |
| 37 | namespace WebCore { |
| 38 | |
| 39 | static inline RenderObject* rendererFromPosition(const Position& position) |
| 40 | { |
| 41 | ASSERT(position.isNotNull()); |
| 42 | Node* rendererNode = nullptr; |
| 43 | switch (position.anchorType()) { |
| 44 | case Position::PositionIsOffsetInAnchor: |
| 45 | rendererNode = position.computeNodeAfterPosition(); |
| 46 | if (!rendererNode || !rendererNode->renderer()) |
| 47 | rendererNode = position.anchorNode()->lastChild(); |
| 48 | break; |
| 49 | |
| 50 | case Position::PositionIsBeforeAnchor: |
| 51 | case Position::PositionIsAfterAnchor: |
| 52 | break; |
| 53 | |
| 54 | case Position::PositionIsBeforeChildren: |
| 55 | rendererNode = position.anchorNode()->firstChild(); |
| 56 | break; |
| 57 | case Position::PositionIsAfterChildren: |
| 58 | rendererNode = position.anchorNode()->lastChild(); |
| 59 | break; |
| 60 | } |
| 61 | if (!rendererNode || !rendererNode->renderer()) |
| 62 | rendererNode = position.anchorNode(); |
| 63 | return rendererNode->renderer(); |
| 64 | } |
| 65 | |
| 66 | RenderedPosition::RenderedPosition(const VisiblePosition& position) |
| 67 | : m_offset(0) |
| 68 | , m_prevLeafChild(uncachedInlineBox()) |
| 69 | , m_nextLeafChild(uncachedInlineBox()) |
| 70 | { |
| 71 | if (position.isNull()) |
| 72 | return; |
| 73 | position.getInlineBoxAndOffset(m_inlineBox, m_offset); |
| 74 | if (m_inlineBox) |
| 75 | m_renderer = &m_inlineBox->renderer(); |
| 76 | else |
| 77 | m_renderer = rendererFromPosition(position.deepEquivalent()); |
| 78 | } |
| 79 | |
| 80 | RenderedPosition::RenderedPosition(const Position& position, EAffinity affinity) |
| 81 | : m_offset(0) |
| 82 | , m_prevLeafChild(uncachedInlineBox()) |
| 83 | , m_nextLeafChild(uncachedInlineBox()) |
| 84 | { |
| 85 | if (position.isNull()) |
| 86 | return; |
| 87 | position.getInlineBoxAndOffset(affinity, m_inlineBox, m_offset); |
| 88 | if (m_inlineBox) |
| 89 | m_renderer = &m_inlineBox->renderer(); |
| 90 | else |
| 91 | m_renderer = rendererFromPosition(position); |
| 92 | } |
| 93 | |
| 94 | InlineBox* RenderedPosition::prevLeafChild() const |
| 95 | { |
| 96 | if (m_prevLeafChild == uncachedInlineBox()) |
| 97 | m_prevLeafChild = m_inlineBox->prevLeafChildIgnoringLineBreak(); |
| 98 | return m_prevLeafChild; |
| 99 | } |
| 100 | |
| 101 | InlineBox* RenderedPosition::nextLeafChild() const |
| 102 | { |
| 103 | if (m_nextLeafChild == uncachedInlineBox()) |
| 104 | m_nextLeafChild = m_inlineBox->nextLeafChildIgnoringLineBreak(); |
| 105 | return m_nextLeafChild; |
| 106 | } |
| 107 | |
| 108 | bool RenderedPosition::isEquivalent(const RenderedPosition& other) const |
| 109 | { |
| 110 | return (m_renderer == other.m_renderer && m_inlineBox == other.m_inlineBox && m_offset == other.m_offset) |
| 111 | || (atLeftmostOffsetInBox() && other.atRightmostOffsetInBox() && prevLeafChild() == other.m_inlineBox) |
| 112 | || (atRightmostOffsetInBox() && other.atLeftmostOffsetInBox() && nextLeafChild() == other.m_inlineBox); |
| 113 | } |
| 114 | |
| 115 | unsigned char RenderedPosition::bidiLevelOnLeft() const |
| 116 | { |
| 117 | InlineBox* box = atLeftmostOffsetInBox() ? prevLeafChild() : m_inlineBox; |
| 118 | return box ? box->bidiLevel() : 0; |
| 119 | } |
| 120 | |
| 121 | unsigned char RenderedPosition::bidiLevelOnRight() const |
| 122 | { |
| 123 | InlineBox* box = atRightmostOffsetInBox() ? nextLeafChild() : m_inlineBox; |
| 124 | return box ? box->bidiLevel() : 0; |
| 125 | } |
| 126 | |
| 127 | RenderedPosition RenderedPosition::leftBoundaryOfBidiRun(unsigned char bidiLevelOfRun) |
| 128 | { |
| 129 | if (!m_inlineBox || bidiLevelOfRun > m_inlineBox->bidiLevel()) |
| 130 | return RenderedPosition(); |
| 131 | |
| 132 | InlineBox* box = m_inlineBox; |
| 133 | do { |
| 134 | InlineBox* prev = box->prevLeafChildIgnoringLineBreak(); |
| 135 | if (!prev || prev->bidiLevel() < bidiLevelOfRun) |
| 136 | return RenderedPosition(&box->renderer(), box, box->caretLeftmostOffset()); |
| 137 | box = prev; |
| 138 | } while (box); |
| 139 | |
| 140 | ASSERT_NOT_REACHED(); |
| 141 | return RenderedPosition(); |
| 142 | } |
| 143 | |
| 144 | RenderedPosition RenderedPosition::rightBoundaryOfBidiRun(unsigned char bidiLevelOfRun) |
| 145 | { |
| 146 | if (!m_inlineBox || bidiLevelOfRun > m_inlineBox->bidiLevel()) |
| 147 | return RenderedPosition(); |
| 148 | |
| 149 | InlineBox* box = m_inlineBox; |
| 150 | do { |
| 151 | InlineBox* next = box->nextLeafChildIgnoringLineBreak(); |
| 152 | if (!next || next->bidiLevel() < bidiLevelOfRun) |
| 153 | return RenderedPosition(&box->renderer(), box, box->caretRightmostOffset()); |
| 154 | box = next; |
| 155 | } while (box); |
| 156 | |
| 157 | ASSERT_NOT_REACHED(); |
| 158 | return RenderedPosition(); |
| 159 | } |
| 160 | |
| 161 | bool RenderedPosition::atLeftBoundaryOfBidiRun(ShouldMatchBidiLevel shouldMatchBidiLevel, unsigned char bidiLevelOfRun) const |
| 162 | { |
| 163 | if (!m_inlineBox) |
| 164 | return false; |
| 165 | |
| 166 | if (atLeftmostOffsetInBox()) { |
| 167 | if (shouldMatchBidiLevel == IgnoreBidiLevel) |
| 168 | return !prevLeafChild() || prevLeafChild()->bidiLevel() < m_inlineBox->bidiLevel(); |
| 169 | return m_inlineBox->bidiLevel() >= bidiLevelOfRun && (!prevLeafChild() || prevLeafChild()->bidiLevel() < bidiLevelOfRun); |
| 170 | } |
| 171 | |
| 172 | if (atRightmostOffsetInBox()) { |
| 173 | if (shouldMatchBidiLevel == IgnoreBidiLevel) |
| 174 | return nextLeafChild() && m_inlineBox->bidiLevel() < nextLeafChild()->bidiLevel(); |
| 175 | return nextLeafChild() && m_inlineBox->bidiLevel() < bidiLevelOfRun && nextLeafChild()->bidiLevel() >= bidiLevelOfRun; |
| 176 | } |
| 177 | |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | bool RenderedPosition::atRightBoundaryOfBidiRun(ShouldMatchBidiLevel shouldMatchBidiLevel, unsigned char bidiLevelOfRun) const |
| 182 | { |
| 183 | if (!m_inlineBox) |
| 184 | return false; |
| 185 | |
| 186 | if (atRightmostOffsetInBox()) { |
| 187 | if (shouldMatchBidiLevel == IgnoreBidiLevel) |
| 188 | return !nextLeafChild() || nextLeafChild()->bidiLevel() < m_inlineBox->bidiLevel(); |
| 189 | return m_inlineBox->bidiLevel() >= bidiLevelOfRun && (!nextLeafChild() || nextLeafChild()->bidiLevel() < bidiLevelOfRun); |
| 190 | } |
| 191 | |
| 192 | if (atLeftmostOffsetInBox()) { |
| 193 | if (shouldMatchBidiLevel == IgnoreBidiLevel) |
| 194 | return prevLeafChild() && m_inlineBox->bidiLevel() < prevLeafChild()->bidiLevel(); |
| 195 | return prevLeafChild() && m_inlineBox->bidiLevel() < bidiLevelOfRun && prevLeafChild()->bidiLevel() >= bidiLevelOfRun; |
| 196 | } |
| 197 | |
| 198 | return false; |
| 199 | } |
| 200 | |
| 201 | Position RenderedPosition::positionAtLeftBoundaryOfBiDiRun() const |
| 202 | { |
| 203 | ASSERT(atLeftBoundaryOfBidiRun()); |
| 204 | |
| 205 | if (atLeftmostOffsetInBox()) |
| 206 | return createLegacyEditingPosition(m_renderer->node(), m_offset); |
| 207 | |
| 208 | return createLegacyEditingPosition(nextLeafChild()->renderer().node(), nextLeafChild()->caretLeftmostOffset()); |
| 209 | } |
| 210 | |
| 211 | Position RenderedPosition::positionAtRightBoundaryOfBiDiRun() const |
| 212 | { |
| 213 | ASSERT(atRightBoundaryOfBidiRun()); |
| 214 | |
| 215 | if (atRightmostOffsetInBox()) |
| 216 | return createLegacyEditingPosition(m_renderer->node(), m_offset); |
| 217 | |
| 218 | return createLegacyEditingPosition(prevLeafChild()->renderer().node(), prevLeafChild()->caretRightmostOffset()); |
| 219 | } |
| 220 | |
| 221 | IntRect RenderedPosition::absoluteRect(LayoutUnit* ) const |
| 222 | { |
| 223 | if (isNull()) |
| 224 | return IntRect(); |
| 225 | |
| 226 | IntRect localRect = snappedIntRect(m_renderer->localCaretRect(m_inlineBox, m_offset, extraWidthToEndOfLine)); |
| 227 | return localRect == IntRect() ? IntRect() : m_renderer->localToAbsoluteQuad(FloatRect(localRect)).enclosingBoundingBox(); |
| 228 | } |
| 229 | |
| 230 | bool renderObjectContainsPosition(RenderObject* target, const Position& position) |
| 231 | { |
| 232 | for (RenderObject* renderer = rendererFromPosition(position); renderer && renderer->node(); renderer = renderer->parent()) { |
| 233 | if (renderer == target) |
| 234 | return true; |
| 235 | } |
| 236 | return false; |
| 237 | } |
| 238 | |
| 239 | }; |
| 240 | |