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