0
|
1 |
/*
|
|
2 |
* Copyright (C) 2007, 2009 Apple 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
|
|
6 |
* are met:
|
|
7 |
*
|
|
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 |
* 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
|
|
14 |
* its contributors may be used to endorse or promote products derived
|
|
15 |
* from this software without specific prior written permission.
|
|
16 |
*
|
|
17 |
* THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
|
|
18 |
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
19 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
20 |
* DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
|
|
21 |
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
22 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
23 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
24 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
25 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
26 |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
27 |
*/
|
|
28 |
|
|
29 |
|
|
30 |
#include "config.h"
|
|
31 |
#include "DOMSelection.h"
|
|
32 |
|
|
33 |
#include "ExceptionCode.h"
|
|
34 |
#include "Frame.h"
|
|
35 |
#include "Node.h"
|
|
36 |
#include "PlatformString.h"
|
|
37 |
#include "Range.h"
|
|
38 |
#include "SelectionController.h"
|
|
39 |
#include "TextIterator.h"
|
|
40 |
#include "htmlediting.h"
|
|
41 |
|
|
42 |
namespace WebCore {
|
|
43 |
|
|
44 |
static Node* selectionShadowAncestor(Frame* frame)
|
|
45 |
{
|
|
46 |
Node* node = frame->selection()->selection().base().anchorNode();
|
|
47 |
if (!node)
|
|
48 |
return 0;
|
|
49 |
Node* shadowAncestor = node->shadowAncestorNode();
|
|
50 |
if (shadowAncestor == node)
|
|
51 |
return 0;
|
|
52 |
return shadowAncestor;
|
|
53 |
}
|
|
54 |
|
|
55 |
DOMSelection::DOMSelection(Frame* frame)
|
|
56 |
: m_frame(frame)
|
|
57 |
{
|
|
58 |
}
|
|
59 |
|
|
60 |
Frame* DOMSelection::frame() const
|
|
61 |
{
|
|
62 |
return m_frame;
|
|
63 |
}
|
|
64 |
|
|
65 |
void DOMSelection::disconnectFrame()
|
|
66 |
{
|
|
67 |
m_frame = 0;
|
|
68 |
}
|
|
69 |
|
|
70 |
const VisibleSelection& DOMSelection::visibleSelection() const
|
|
71 |
{
|
|
72 |
ASSERT(m_frame);
|
|
73 |
return m_frame->selection()->selection();
|
|
74 |
}
|
|
75 |
|
|
76 |
static Position anchorPosition(const VisibleSelection& selection)
|
|
77 |
{
|
|
78 |
Position anchor = selection.isBaseFirst() ? selection.start() : selection.end();
|
|
79 |
return rangeCompliantEquivalent(anchor);
|
|
80 |
}
|
|
81 |
|
|
82 |
static Position focusPosition(const VisibleSelection& selection)
|
|
83 |
{
|
|
84 |
Position focus = selection.isBaseFirst() ? selection.end() : selection.start();
|
|
85 |
return rangeCompliantEquivalent(focus);
|
|
86 |
}
|
|
87 |
|
|
88 |
static Position basePosition(const VisibleSelection& selection)
|
|
89 |
{
|
|
90 |
return rangeCompliantEquivalent(selection.base());
|
|
91 |
}
|
|
92 |
|
|
93 |
static Position extentPosition(const VisibleSelection& selection)
|
|
94 |
{
|
|
95 |
return rangeCompliantEquivalent(selection.extent());
|
|
96 |
}
|
|
97 |
|
|
98 |
Node* DOMSelection::anchorNode() const
|
|
99 |
{
|
|
100 |
if (!m_frame)
|
|
101 |
return 0;
|
|
102 |
if (Node* shadowAncestor = selectionShadowAncestor(m_frame))
|
|
103 |
return shadowAncestor->parentNode();
|
|
104 |
return anchorPosition(visibleSelection()).node();
|
|
105 |
}
|
|
106 |
|
|
107 |
int DOMSelection::anchorOffset() const
|
|
108 |
{
|
|
109 |
if (!m_frame)
|
|
110 |
return 0;
|
|
111 |
if (Node* shadowAncestor = selectionShadowAncestor(m_frame))
|
|
112 |
return shadowAncestor->nodeIndex();
|
|
113 |
return anchorPosition(visibleSelection()).deprecatedEditingOffset();
|
|
114 |
}
|
|
115 |
|
|
116 |
Node* DOMSelection::focusNode() const
|
|
117 |
{
|
|
118 |
if (!m_frame)
|
|
119 |
return 0;
|
|
120 |
if (Node* shadowAncestor = selectionShadowAncestor(m_frame))
|
|
121 |
return shadowAncestor->parentNode();
|
|
122 |
return focusPosition(visibleSelection()).node();
|
|
123 |
}
|
|
124 |
|
|
125 |
int DOMSelection::focusOffset() const
|
|
126 |
{
|
|
127 |
if (!m_frame)
|
|
128 |
return 0;
|
|
129 |
if (Node* shadowAncestor = selectionShadowAncestor(m_frame))
|
|
130 |
return shadowAncestor->nodeIndex();
|
|
131 |
return focusPosition(visibleSelection()).deprecatedEditingOffset();
|
|
132 |
}
|
|
133 |
|
|
134 |
Node* DOMSelection::baseNode() const
|
|
135 |
{
|
|
136 |
if (!m_frame)
|
|
137 |
return 0;
|
|
138 |
if (Node* shadowAncestor = selectionShadowAncestor(m_frame))
|
|
139 |
return shadowAncestor->parentNode();
|
|
140 |
return basePosition(visibleSelection()).node();
|
|
141 |
}
|
|
142 |
|
|
143 |
int DOMSelection::baseOffset() const
|
|
144 |
{
|
|
145 |
if (!m_frame)
|
|
146 |
return 0;
|
|
147 |
if (Node* shadowAncestor = selectionShadowAncestor(m_frame))
|
|
148 |
return shadowAncestor->nodeIndex();
|
|
149 |
return basePosition(visibleSelection()).deprecatedEditingOffset();
|
|
150 |
}
|
|
151 |
|
|
152 |
Node* DOMSelection::extentNode() const
|
|
153 |
{
|
|
154 |
if (!m_frame)
|
|
155 |
return 0;
|
|
156 |
if (Node* shadowAncestor = selectionShadowAncestor(m_frame))
|
|
157 |
return shadowAncestor->parentNode();
|
|
158 |
return extentPosition(visibleSelection()).node();
|
|
159 |
}
|
|
160 |
|
|
161 |
int DOMSelection::extentOffset() const
|
|
162 |
{
|
|
163 |
if (!m_frame)
|
|
164 |
return 0;
|
|
165 |
if (Node* shadowAncestor = selectionShadowAncestor(m_frame))
|
|
166 |
return shadowAncestor->nodeIndex();
|
|
167 |
return extentPosition(visibleSelection()).deprecatedEditingOffset();
|
|
168 |
}
|
|
169 |
|
|
170 |
bool DOMSelection::isCollapsed() const
|
|
171 |
{
|
|
172 |
if (!m_frame || selectionShadowAncestor(m_frame))
|
|
173 |
return true;
|
|
174 |
return !m_frame->selection()->isRange();
|
|
175 |
}
|
|
176 |
|
|
177 |
String DOMSelection::type() const
|
|
178 |
{
|
|
179 |
if (!m_frame)
|
|
180 |
return String();
|
|
181 |
|
|
182 |
SelectionController* selection = m_frame->selection();
|
|
183 |
|
|
184 |
// This is a WebKit DOM extension, incompatible with an IE extension
|
|
185 |
// IE has this same attribute, but returns "none", "text" and "control"
|
|
186 |
// http://msdn.microsoft.com/en-us/library/ms534692(VS.85).aspx
|
|
187 |
if (selection->isNone())
|
|
188 |
return "None";
|
|
189 |
if (selection->isCaret())
|
|
190 |
return "Caret";
|
|
191 |
return "Range";
|
|
192 |
}
|
|
193 |
|
|
194 |
int DOMSelection::rangeCount() const
|
|
195 |
{
|
|
196 |
if (!m_frame)
|
|
197 |
return 0;
|
|
198 |
return m_frame->selection()->isNone() ? 0 : 1;
|
|
199 |
}
|
|
200 |
|
|
201 |
void DOMSelection::collapse(Node* node, int offset, ExceptionCode& ec)
|
|
202 |
{
|
|
203 |
if (!m_frame)
|
|
204 |
return;
|
|
205 |
|
|
206 |
if (offset < 0) {
|
|
207 |
ec = INDEX_SIZE_ERR;
|
|
208 |
return;
|
|
209 |
}
|
|
210 |
m_frame->selection()->moveTo(VisiblePosition(node, offset, DOWNSTREAM));
|
|
211 |
}
|
|
212 |
|
|
213 |
void DOMSelection::collapseToEnd()
|
|
214 |
{
|
|
215 |
if (!m_frame)
|
|
216 |
return;
|
|
217 |
|
|
218 |
const VisibleSelection& selection = m_frame->selection()->selection();
|
|
219 |
m_frame->selection()->moveTo(VisiblePosition(selection.end(), DOWNSTREAM));
|
|
220 |
}
|
|
221 |
|
|
222 |
void DOMSelection::collapseToStart()
|
|
223 |
{
|
|
224 |
if (!m_frame)
|
|
225 |
return;
|
|
226 |
|
|
227 |
const VisibleSelection& selection = m_frame->selection()->selection();
|
|
228 |
m_frame->selection()->moveTo(VisiblePosition(selection.start(), DOWNSTREAM));
|
|
229 |
}
|
|
230 |
|
|
231 |
void DOMSelection::empty()
|
|
232 |
{
|
|
233 |
if (!m_frame)
|
|
234 |
return;
|
|
235 |
m_frame->selection()->clear();
|
|
236 |
}
|
|
237 |
|
|
238 |
void DOMSelection::setBaseAndExtent(Node* baseNode, int baseOffset, Node* extentNode, int extentOffset, ExceptionCode& ec)
|
|
239 |
{
|
|
240 |
if (!m_frame)
|
|
241 |
return;
|
|
242 |
|
|
243 |
if (baseOffset < 0 || extentOffset < 0) {
|
|
244 |
ec = INDEX_SIZE_ERR;
|
|
245 |
return;
|
|
246 |
}
|
|
247 |
VisiblePosition visibleBase = VisiblePosition(baseNode, baseOffset, DOWNSTREAM);
|
|
248 |
VisiblePosition visibleExtent = VisiblePosition(extentNode, extentOffset, DOWNSTREAM);
|
|
249 |
|
|
250 |
m_frame->selection()->moveTo(visibleBase, visibleExtent);
|
|
251 |
}
|
|
252 |
|
|
253 |
void DOMSelection::setPosition(Node* node, int offset, ExceptionCode& ec)
|
|
254 |
{
|
|
255 |
if (!m_frame)
|
|
256 |
return;
|
|
257 |
if (offset < 0) {
|
|
258 |
ec = INDEX_SIZE_ERR;
|
|
259 |
return;
|
|
260 |
}
|
|
261 |
m_frame->selection()->moveTo(VisiblePosition(node, offset, DOWNSTREAM));
|
|
262 |
}
|
|
263 |
|
|
264 |
void DOMSelection::modify(const String& alterString, const String& directionString, const String& granularityString)
|
|
265 |
{
|
|
266 |
if (!m_frame)
|
|
267 |
return;
|
|
268 |
|
|
269 |
SelectionController::EAlteration alter;
|
|
270 |
if (equalIgnoringCase(alterString, "extend"))
|
|
271 |
alter = SelectionController::EXTEND;
|
|
272 |
else if (equalIgnoringCase(alterString, "move"))
|
|
273 |
alter = SelectionController::MOVE;
|
|
274 |
else
|
|
275 |
return;
|
|
276 |
|
|
277 |
SelectionController::EDirection direction;
|
|
278 |
if (equalIgnoringCase(directionString, "forward"))
|
|
279 |
direction = SelectionController::FORWARD;
|
|
280 |
else if (equalIgnoringCase(directionString, "backward"))
|
|
281 |
direction = SelectionController::BACKWARD;
|
|
282 |
else if (equalIgnoringCase(directionString, "left"))
|
|
283 |
direction = SelectionController::LEFT;
|
|
284 |
else if (equalIgnoringCase(directionString, "right"))
|
|
285 |
direction = SelectionController::RIGHT;
|
|
286 |
else
|
|
287 |
return;
|
|
288 |
|
|
289 |
TextGranularity granularity;
|
|
290 |
if (equalIgnoringCase(granularityString, "character"))
|
|
291 |
granularity = CharacterGranularity;
|
|
292 |
else if (equalIgnoringCase(granularityString, "word"))
|
|
293 |
granularity = WordGranularity;
|
|
294 |
else if (equalIgnoringCase(granularityString, "sentence"))
|
|
295 |
granularity = SentenceGranularity;
|
|
296 |
else if (equalIgnoringCase(granularityString, "line"))
|
|
297 |
granularity = LineGranularity;
|
|
298 |
else if (equalIgnoringCase(granularityString, "paragraph"))
|
|
299 |
granularity = ParagraphGranularity;
|
|
300 |
else if (equalIgnoringCase(granularityString, "lineboundary"))
|
|
301 |
granularity = LineBoundary;
|
|
302 |
else if (equalIgnoringCase(granularityString, "sentenceboundary"))
|
|
303 |
granularity = SentenceBoundary;
|
|
304 |
else if (equalIgnoringCase(granularityString, "paragraphboundary"))
|
|
305 |
granularity = ParagraphBoundary;
|
|
306 |
else if (equalIgnoringCase(granularityString, "documentboundary"))
|
|
307 |
granularity = DocumentBoundary;
|
|
308 |
else
|
|
309 |
return;
|
|
310 |
|
|
311 |
m_frame->selection()->modify(alter, direction, granularity, false);
|
|
312 |
}
|
|
313 |
|
|
314 |
void DOMSelection::extend(Node* node, int offset, ExceptionCode& ec)
|
|
315 |
{
|
|
316 |
if (!m_frame)
|
|
317 |
return;
|
|
318 |
|
|
319 |
if (!node) {
|
|
320 |
ec = TYPE_MISMATCH_ERR;
|
|
321 |
return;
|
|
322 |
}
|
|
323 |
if (offset < 0 || offset > (node->offsetInCharacters() ? caretMaxOffset(node) : (int)node->childNodeCount())) {
|
|
324 |
ec = INDEX_SIZE_ERR;
|
|
325 |
return;
|
|
326 |
}
|
|
327 |
|
|
328 |
SelectionController* selection = m_frame->selection();
|
|
329 |
selection->setExtent(VisiblePosition(node, offset, DOWNSTREAM));
|
|
330 |
}
|
|
331 |
|
|
332 |
PassRefPtr<Range> DOMSelection::getRangeAt(int index, ExceptionCode& ec)
|
|
333 |
{
|
|
334 |
if (!m_frame)
|
|
335 |
return 0;
|
|
336 |
|
|
337 |
if (index < 0 || index >= rangeCount()) {
|
|
338 |
ec = INDEX_SIZE_ERR;
|
|
339 |
return 0;
|
|
340 |
}
|
|
341 |
|
|
342 |
// If you're hitting this, you've added broken multi-range selection support
|
|
343 |
ASSERT(rangeCount() == 1);
|
|
344 |
|
|
345 |
if (Node* shadowAncestor = selectionShadowAncestor(m_frame)) {
|
|
346 |
Node* container = shadowAncestor->parentNode();
|
|
347 |
int offset = shadowAncestor->nodeIndex();
|
|
348 |
return Range::create(shadowAncestor->document(), container, offset, container, offset);
|
|
349 |
}
|
|
350 |
|
|
351 |
const VisibleSelection& selection = m_frame->selection()->selection();
|
|
352 |
return selection.firstRange();
|
|
353 |
}
|
|
354 |
|
|
355 |
void DOMSelection::removeAllRanges()
|
|
356 |
{
|
|
357 |
if (!m_frame)
|
|
358 |
return;
|
|
359 |
m_frame->selection()->clear();
|
|
360 |
}
|
|
361 |
|
|
362 |
void DOMSelection::addRange(Range* r)
|
|
363 |
{
|
|
364 |
if (!m_frame)
|
|
365 |
return;
|
|
366 |
if (!r)
|
|
367 |
return;
|
|
368 |
|
|
369 |
SelectionController* selection = m_frame->selection();
|
|
370 |
|
|
371 |
if (selection->isNone()) {
|
|
372 |
selection->setSelection(VisibleSelection(r));
|
|
373 |
return;
|
|
374 |
}
|
|
375 |
|
|
376 |
RefPtr<Range> range = selection->selection().toNormalizedRange();
|
|
377 |
ExceptionCode ec = 0;
|
|
378 |
if (r->compareBoundaryPoints(Range::START_TO_START, range.get(), ec) == -1) {
|
|
379 |
// We don't support discontiguous selection. We don't do anything if r and range don't intersect.
|
|
380 |
if (r->compareBoundaryPoints(Range::START_TO_END, range.get(), ec) > -1) {
|
|
381 |
if (r->compareBoundaryPoints(Range::END_TO_END, range.get(), ec) == -1)
|
|
382 |
// The original range and r intersect.
|
|
383 |
selection->setSelection(VisibleSelection(r->startPosition(), range->endPosition(), DOWNSTREAM));
|
|
384 |
else
|
|
385 |
// r contains the original range.
|
|
386 |
selection->setSelection(VisibleSelection(r));
|
|
387 |
}
|
|
388 |
} else {
|
|
389 |
// We don't support discontiguous selection. We don't do anything if r and range don't intersect.
|
|
390 |
if (r->compareBoundaryPoints(Range::END_TO_START, range.get(), ec) < 1) {
|
|
391 |
if (r->compareBoundaryPoints(Range::END_TO_END, range.get(), ec) == -1)
|
|
392 |
// The original range contains r.
|
|
393 |
selection->setSelection(VisibleSelection(range.get()));
|
|
394 |
else
|
|
395 |
// The original range and r intersect.
|
|
396 |
selection->setSelection(VisibleSelection(range->startPosition(), r->endPosition(), DOWNSTREAM));
|
|
397 |
}
|
|
398 |
}
|
|
399 |
}
|
|
400 |
|
|
401 |
void DOMSelection::deleteFromDocument()
|
|
402 |
{
|
|
403 |
if (!m_frame)
|
|
404 |
return;
|
|
405 |
|
|
406 |
SelectionController* selection = m_frame->selection();
|
|
407 |
|
|
408 |
if (selection->isNone())
|
|
409 |
return;
|
|
410 |
|
|
411 |
if (isCollapsed())
|
|
412 |
selection->modify(SelectionController::EXTEND, SelectionController::BACKWARD, CharacterGranularity);
|
|
413 |
|
|
414 |
RefPtr<Range> selectedRange = selection->selection().toNormalizedRange();
|
|
415 |
|
|
416 |
ExceptionCode ec = 0;
|
|
417 |
selectedRange->deleteContents(ec);
|
|
418 |
ASSERT(!ec);
|
|
419 |
|
|
420 |
setBaseAndExtent(selectedRange->startContainer(ec), selectedRange->startOffset(ec), selectedRange->startContainer(ec), selectedRange->startOffset(ec), ec);
|
|
421 |
ASSERT(!ec);
|
|
422 |
}
|
|
423 |
|
|
424 |
bool DOMSelection::containsNode(const Node* n, bool allowPartial) const
|
|
425 |
{
|
|
426 |
if (!m_frame)
|
|
427 |
return false;
|
|
428 |
|
|
429 |
SelectionController* selection = m_frame->selection();
|
|
430 |
|
|
431 |
if (!n || selection->isNone())
|
|
432 |
return false;
|
|
433 |
|
|
434 |
Node* parentNode = n->parentNode();
|
|
435 |
unsigned nodeIndex = n->nodeIndex();
|
|
436 |
RefPtr<Range> selectedRange = selection->selection().toNormalizedRange();
|
|
437 |
|
|
438 |
if (!parentNode)
|
|
439 |
return false;
|
|
440 |
|
|
441 |
ExceptionCode ec = 0;
|
|
442 |
bool nodeFullySelected = Range::compareBoundaryPoints(parentNode, nodeIndex, selectedRange->startContainer(ec), selectedRange->startOffset(ec)) >= 0
|
|
443 |
&& Range::compareBoundaryPoints(parentNode, nodeIndex + 1, selectedRange->endContainer(ec), selectedRange->endOffset(ec)) <= 0;
|
|
444 |
ASSERT(!ec);
|
|
445 |
if (nodeFullySelected)
|
|
446 |
return true;
|
|
447 |
|
|
448 |
bool nodeFullyUnselected = Range::compareBoundaryPoints(parentNode, nodeIndex, selectedRange->endContainer(ec), selectedRange->endOffset(ec)) > 0
|
|
449 |
|| Range::compareBoundaryPoints(parentNode, nodeIndex + 1, selectedRange->startContainer(ec), selectedRange->startOffset(ec)) < 0;
|
|
450 |
ASSERT(!ec);
|
|
451 |
if (nodeFullyUnselected)
|
|
452 |
return false;
|
|
453 |
|
|
454 |
return allowPartial || n->isTextNode();
|
|
455 |
}
|
|
456 |
|
|
457 |
void DOMSelection::selectAllChildren(Node* n, ExceptionCode& ec)
|
|
458 |
{
|
|
459 |
if (!n)
|
|
460 |
return;
|
|
461 |
|
|
462 |
// This doesn't (and shouldn't) select text node characters.
|
|
463 |
setBaseAndExtent(n, 0, n, n->childNodeCount(), ec);
|
|
464 |
}
|
|
465 |
|
|
466 |
String DOMSelection::toString()
|
|
467 |
{
|
|
468 |
if (!m_frame)
|
|
469 |
return String();
|
|
470 |
|
|
471 |
return plainText(m_frame->selection()->selection().toNormalizedRange().get());
|
|
472 |
}
|
|
473 |
|
|
474 |
} // namespace WebCore
|