| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved. | |
| 3 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies) | |
| 4 * | |
| 5 * Redistribution and use in source and binary forms, with or without | |
| 6 * modification, are permitted provided that the following conditions | |
| 7 * are met: | |
| 8 * 1. Redistributions of source code must retain the above copyright | |
| 9 * notice, this list of conditions and the following disclaimer. | |
| 10 * 2. Redistributions in binary form must reproduce the above copyright | |
| 11 * notice, this list of conditions and the following disclaimer in the | |
| 12 * documentation and/or other materials provided with the distribution. | |
| 13 * | |
| 14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | |
| 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | |
| 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
| 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 25 */ | |
| 26 | |
| 27 #include "core/editing/spellcheck/TextCheckingHelper.h" | |
| 28 | |
| 29 #include "core/dom/Range.h" | |
| 30 #include "core/editing/VisiblePosition.h" | |
| 31 #include "core/editing/VisibleUnits.h" | |
| 32 #include "core/editing/iterators/CharacterIterator.h" | |
| 33 | |
| 34 namespace blink { | |
| 35 | |
| 36 static EphemeralRange expandToParagraphBoundary(const EphemeralRange& range) | |
| 37 { | |
| 38 const VisiblePosition& start = createVisiblePosition(range.startPosition()); | |
| 39 DCHECK(start.isNotNull()) << range.startPosition(); | |
| 40 const VisiblePosition& paragraphStart = startOfParagraph(start); | |
| 41 DCHECK(paragraphStart.isNotNull()) << range.startPosition(); | |
| 42 | |
| 43 const VisiblePosition& end = createVisiblePosition(range.endPosition()); | |
| 44 DCHECK(end.isNotNull()) << range.endPosition(); | |
| 45 const VisiblePosition& paragraphEnd = endOfParagraph(end); | |
| 46 DCHECK(paragraphEnd.isNotNull()) << range.endPosition(); | |
| 47 | |
| 48 return EphemeralRange(paragraphStart.deepEquivalent(), paragraphEnd.deepEqui
valent()); | |
| 49 } | |
| 50 | |
| 51 TextCheckingParagraph::TextCheckingParagraph(const EphemeralRange& checkingRange
) | |
| 52 : m_checkingRange(checkingRange) | |
| 53 , m_checkingStart(-1) | |
| 54 , m_checkingEnd(-1) | |
| 55 , m_checkingLength(-1) | |
| 56 { | |
| 57 } | |
| 58 | |
| 59 TextCheckingParagraph::TextCheckingParagraph(const EphemeralRange& checkingRange
, const EphemeralRange& paragraphRange) | |
| 60 : m_checkingRange(checkingRange) | |
| 61 , m_paragraphRange(paragraphRange) | |
| 62 , m_checkingStart(-1) | |
| 63 , m_checkingEnd(-1) | |
| 64 , m_checkingLength(-1) | |
| 65 { | |
| 66 } | |
| 67 | |
| 68 TextCheckingParagraph::TextCheckingParagraph(Range* checkingRange, Range* paragr
aphRange) | |
| 69 : m_checkingRange(checkingRange) | |
| 70 , m_paragraphRange(paragraphRange) | |
| 71 , m_checkingStart(-1) | |
| 72 , m_checkingEnd(-1) | |
| 73 , m_checkingLength(-1) | |
| 74 { | |
| 75 } | |
| 76 | |
| 77 TextCheckingParagraph::~TextCheckingParagraph() | |
| 78 { | |
| 79 } | |
| 80 | |
| 81 void TextCheckingParagraph::expandRangeToNextEnd() | |
| 82 { | |
| 83 DCHECK(m_checkingRange.isNotNull()); | |
| 84 setParagraphRange(EphemeralRange(paragraphRange().startPosition(), endOfPara
graph(startOfNextParagraph(createVisiblePosition(paragraphRange().startPosition(
)))).deepEquivalent())); | |
| 85 invalidateParagraphRangeValues(); | |
| 86 } | |
| 87 | |
| 88 void TextCheckingParagraph::invalidateParagraphRangeValues() | |
| 89 { | |
| 90 m_checkingStart = m_checkingEnd = -1; | |
| 91 m_offsetAsRange = EphemeralRange(); | |
| 92 m_text = String(); | |
| 93 } | |
| 94 | |
| 95 int TextCheckingParagraph::rangeLength() const | |
| 96 { | |
| 97 DCHECK(m_checkingRange.isNotNull()); | |
| 98 return TextIterator::rangeLength(paragraphRange().startPosition(), paragraph
Range().endPosition()); | |
| 99 } | |
| 100 | |
| 101 EphemeralRange TextCheckingParagraph::paragraphRange() const | |
| 102 { | |
| 103 DCHECK(m_checkingRange.isNotNull()); | |
| 104 if (m_paragraphRange.isNull()) | |
| 105 m_paragraphRange = expandToParagraphBoundary(checkingRange()); | |
| 106 return m_paragraphRange; | |
| 107 } | |
| 108 | |
| 109 void TextCheckingParagraph::setParagraphRange(const EphemeralRange& range) | |
| 110 { | |
| 111 m_paragraphRange = range; | |
| 112 } | |
| 113 | |
| 114 EphemeralRange TextCheckingParagraph::subrange(int characterOffset, int characte
rCount) const | |
| 115 { | |
| 116 DCHECK(m_checkingRange.isNotNull()); | |
| 117 return calculateCharacterSubrange(paragraphRange(), characterOffset, charact
erCount); | |
| 118 } | |
| 119 | |
| 120 int TextCheckingParagraph::offsetTo(const Position& position) const | |
| 121 { | |
| 122 DCHECK(m_checkingRange.isNotNull()); | |
| 123 return TextIterator::rangeLength(offsetAsRange().startPosition(), position); | |
| 124 } | |
| 125 | |
| 126 bool TextCheckingParagraph::isEmpty() const | |
| 127 { | |
| 128 // Both predicates should have same result, but we check both just to be sur
e. | |
| 129 // We need to investigate to remove this redundancy. | |
| 130 return isRangeEmpty() || isTextEmpty(); | |
| 131 } | |
| 132 | |
| 133 EphemeralRange TextCheckingParagraph::offsetAsRange() const | |
| 134 { | |
| 135 DCHECK(m_checkingRange.isNotNull()); | |
| 136 if (m_offsetAsRange.isNotNull()) | |
| 137 return m_offsetAsRange; | |
| 138 const Position& paragraphStart = paragraphRange().startPosition(); | |
| 139 const Position& checkingStart = checkingRange().startPosition(); | |
| 140 if (paragraphStart <= checkingStart) { | |
| 141 m_offsetAsRange = EphemeralRange(paragraphStart, checkingStart); | |
| 142 return m_offsetAsRange; | |
| 143 } | |
| 144 // editing/pasteboard/paste-table-001.html and more reach here. | |
| 145 m_offsetAsRange = EphemeralRange(checkingStart, paragraphStart); | |
| 146 return m_offsetAsRange; | |
| 147 } | |
| 148 | |
| 149 const String& TextCheckingParagraph::text() const | |
| 150 { | |
| 151 DCHECK(m_checkingRange.isNotNull()); | |
| 152 if (m_text.isEmpty()) | |
| 153 m_text = plainText(paragraphRange()); | |
| 154 return m_text; | |
| 155 } | |
| 156 | |
| 157 int TextCheckingParagraph::checkingStart() const | |
| 158 { | |
| 159 DCHECK(m_checkingRange.isNotNull()); | |
| 160 if (m_checkingStart == -1) | |
| 161 m_checkingStart = TextIterator::rangeLength(offsetAsRange().startPositio
n(), offsetAsRange().endPosition()); | |
| 162 return m_checkingStart; | |
| 163 } | |
| 164 | |
| 165 int TextCheckingParagraph::checkingEnd() const | |
| 166 { | |
| 167 DCHECK(m_checkingRange.isNotNull()); | |
| 168 if (m_checkingEnd == -1) | |
| 169 m_checkingEnd = checkingStart() + TextIterator::rangeLength(checkingRang
e().startPosition(), checkingRange().endPosition()); | |
| 170 return m_checkingEnd; | |
| 171 } | |
| 172 | |
| 173 int TextCheckingParagraph::checkingLength() const | |
| 174 { | |
| 175 DCHECK(m_checkingRange.isNotNull()); | |
| 176 if (-1 == m_checkingLength) | |
| 177 m_checkingLength = TextIterator::rangeLength(checkingRange().startPositi
on(), checkingRange().endPosition()); | |
| 178 return m_checkingLength; | |
| 179 } | |
| 180 | |
| 181 } // namespace blink | |
| OLD | NEW |