diff options
Diffstat (limited to 'src/3rdparty/webkit/WebCore/page/DOMSelection.cpp')
-rw-r--r-- | src/3rdparty/webkit/WebCore/page/DOMSelection.cpp | 103 |
1 files changed, 61 insertions, 42 deletions
diff --git a/src/3rdparty/webkit/WebCore/page/DOMSelection.cpp b/src/3rdparty/webkit/WebCore/page/DOMSelection.cpp index 5dab325..3b54f02 100644 --- a/src/3rdparty/webkit/WebCore/page/DOMSelection.cpp +++ b/src/3rdparty/webkit/WebCore/page/DOMSelection.cpp @@ -56,76 +56,89 @@ void DOMSelection::disconnectFrame() m_frame = 0; } -Node* DOMSelection::anchorNode() const +const VisibleSelection& DOMSelection::visibleSelection() const { - if (!m_frame) - return 0; + ASSERT(m_frame); + return m_frame->selection()->selection(); +} - const Selection& selection = m_frame->selection()->selection(); +static Position anchorPosition(const VisibleSelection& selection) +{ Position anchor = selection.isBaseFirst() ? selection.start() : selection.end(); - anchor = rangeCompliantEquivalent(anchor); - return anchor.node(); + return rangeCompliantEquivalent(anchor); } -Node* DOMSelection::baseNode() const +static Position focusPosition(const VisibleSelection& selection) +{ + Position focus = selection.isBaseFirst() ? selection.end() : selection.start(); + return rangeCompliantEquivalent(focus); +} + +static Position basePosition(const VisibleSelection& selection) +{ + return rangeCompliantEquivalent(selection.base()); +} + +static Position extentPosition(const VisibleSelection& selection) +{ + return rangeCompliantEquivalent(selection.extent()); +} + +Node* DOMSelection::anchorNode() const { if (!m_frame) return 0; - return rangeCompliantEquivalent(m_frame->selection()->selection().base()).node(); + return anchorPosition(visibleSelection()).node(); } int DOMSelection::anchorOffset() const { if (!m_frame) return 0; - - const Selection& selection = m_frame->selection()->selection(); - Position anchor = selection.isBaseFirst() ? selection.start() : selection.end(); - anchor = rangeCompliantEquivalent(anchor); - return anchor.offset(); + return anchorPosition(visibleSelection()).deprecatedEditingOffset(); } -int DOMSelection::baseOffset() const +Node* DOMSelection::focusNode() const { if (!m_frame) return 0; - return rangeCompliantEquivalent(m_frame->selection()->selection().base()).offset(); + return focusPosition(visibleSelection()).node(); } -Node* DOMSelection::focusNode() const +int DOMSelection::focusOffset() const { if (!m_frame) return 0; - - const Selection& selection = m_frame->selection()->selection(); - Position focus = selection.isBaseFirst() ? selection.end() : selection.start(); - focus = rangeCompliantEquivalent(focus); - return focus.node(); + return focusPosition(visibleSelection()).deprecatedEditingOffset(); } -Node* DOMSelection::extentNode() const +Node* DOMSelection::baseNode() const { if (!m_frame) return 0; - return rangeCompliantEquivalent(m_frame->selection()->selection().extent()).node(); + return basePosition(visibleSelection()).node(); } -int DOMSelection::focusOffset() const +int DOMSelection::baseOffset() const { if (!m_frame) return 0; + return basePosition(visibleSelection()).deprecatedEditingOffset(); +} - const Selection& selection = m_frame->selection()->selection(); - Position focus = selection.isBaseFirst() ? selection.end() : selection.start(); - focus = rangeCompliantEquivalent(focus); - return focus.offset(); + +Node* DOMSelection::extentNode() const +{ + if (!m_frame) + return 0; + return extentPosition(visibleSelection()).node(); } int DOMSelection::extentOffset() const { if (!m_frame) return 0; - return rangeCompliantEquivalent(m_frame->selection()->selection().extent()).offset(); + return extentPosition(visibleSelection()).deprecatedEditingOffset(); } bool DOMSelection::isCollapsed() const @@ -142,6 +155,9 @@ String DOMSelection::type() const SelectionController* 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()) @@ -173,7 +189,7 @@ void DOMSelection::collapseToEnd() if (!m_frame) return; - const Selection& selection = m_frame->selection()->selection(); + const VisibleSelection& selection = m_frame->selection()->selection(); m_frame->selection()->moveTo(VisiblePosition(selection.end(), DOWNSTREAM)); } @@ -182,7 +198,7 @@ void DOMSelection::collapseToStart() if (!m_frame) return; - const Selection& selection = m_frame->selection()->selection(); + const VisibleSelection& selection = m_frame->selection()->selection(); m_frame->selection()->moveTo(VisiblePosition(selection.start(), DOWNSTREAM)); } @@ -298,8 +314,11 @@ PassRefPtr<Range> DOMSelection::getRangeAt(int index, ExceptionCode& ec) return 0; } - const Selection& selection = m_frame->selection()->selection(); - return selection.toRange(); + // If you're hitting this, you've added broken multi-range selection support + ASSERT(rangeCount() == 1); + + const VisibleSelection& selection = m_frame->selection()->selection(); + return selection.firstRange(); } void DOMSelection::removeAllRanges() @@ -319,31 +338,31 @@ void DOMSelection::addRange(Range* r) SelectionController* selection = m_frame->selection(); if (selection->isNone()) { - selection->setSelection(Selection(r)); + selection->setSelection(VisibleSelection(r)); return; } - RefPtr<Range> range = selection->selection().toRange(); + RefPtr<Range> range = selection->selection().toNormalizedRange(); ExceptionCode ec = 0; if (r->compareBoundaryPoints(Range::START_TO_START, range.get(), ec) == -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(), ec) > -1) { if (r->compareBoundaryPoints(Range::END_TO_END, range.get(), ec) == -1) // The original range and r intersect. - selection->setSelection(Selection(r->startPosition(), range->endPosition(), DOWNSTREAM)); + selection->setSelection(VisibleSelection(r->startPosition(), range->endPosition(), DOWNSTREAM)); else // r contains the original range. - selection->setSelection(Selection(r)); + selection->setSelection(VisibleSelection(r)); } } else { // We don't support discontiguous selection. We don't do anything if r and range don't intersect. if (r->compareBoundaryPoints(Range::END_TO_START, range.get(), ec) < 1) { if (r->compareBoundaryPoints(Range::END_TO_END, range.get(), ec) == -1) // The original range contains r. - selection->setSelection(Selection(range.get())); + selection->setSelection(VisibleSelection(range.get())); else // The original range and r intersect. - selection->setSelection(Selection(range->startPosition(), r->endPosition(), DOWNSTREAM)); + selection->setSelection(VisibleSelection(range->startPosition(), r->endPosition(), DOWNSTREAM)); } } } @@ -361,7 +380,7 @@ void DOMSelection::deleteFromDocument() if (isCollapsed()) selection->modify(SelectionController::EXTEND, SelectionController::BACKWARD, CharacterGranularity); - RefPtr<Range> selectedRange = selection->selection().toRange(); + RefPtr<Range> selectedRange = selection->selection().toNormalizedRange(); ExceptionCode ec = 0; selectedRange->deleteContents(ec); @@ -383,7 +402,7 @@ bool DOMSelection::containsNode(const Node* n, bool allowPartial) const Node* parentNode = n->parentNode(); unsigned nodeIndex = n->nodeIndex(); - RefPtr<Range> selectedRange = selection->selection().toRange(); + RefPtr<Range> selectedRange = selection->selection().toNormalizedRange(); if (!parentNode) return false; @@ -418,7 +437,7 @@ String DOMSelection::toString() if (!m_frame) return String(); - return plainText(m_frame->selection()->selection().toRange().get()); + return plainText(m_frame->selection()->selection().toNormalizedRange().get()); } } // namespace WebCore |