Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 2007, 2009 Apple Inc. All rights reserved. | 2 * Copyright (C) 2007, 2009 Apple Inc. All rights reserved. |
| 3 * Copyright (C) 2012 Google Inc. All rights reserved. | 3 * Copyright (C) 2012 Google Inc. All rights reserved. |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
| 7 * are met: | 7 * are met: |
| 8 * | 8 * |
| 9 * 1. Redistributions of source code must retain the above copyright | 9 * 1. Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 118 } | 118 } |
| 119 | 119 |
| 120 static Position basePosition(const VisibleSelection& selection) { | 120 static Position basePosition(const VisibleSelection& selection) { |
| 121 return selection.base().parentAnchoredEquivalent(); | 121 return selection.base().parentAnchoredEquivalent(); |
| 122 } | 122 } |
| 123 | 123 |
| 124 static Position extentPosition(const VisibleSelection& selection) { | 124 static Position extentPosition(const VisibleSelection& selection) { |
| 125 return selection.extent().parentAnchoredEquivalent(); | 125 return selection.extent().parentAnchoredEquivalent(); |
| 126 } | 126 } |
| 127 | 127 |
| 128 class ScopedRange { | |
|
yosin_UTC9
2017/03/30 08:58:17
Please introduce a function acts like primaryRange
| |
| 129 STACK_ALLOCATED(); | |
| 130 | |
| 131 public: | |
| 132 ScopedRange(Range* range, bool dispose) | |
| 133 : m_range(range), m_dispose(dispose) {} | |
| 134 ~ScopedRange() { | |
| 135 if (m_dispose) | |
| 136 m_range->dispose(); | |
| 137 } | |
| 138 | |
| 139 private: | |
| 140 Member<Range> m_range; | |
| 141 bool m_dispose; | |
| 142 }; | |
| 143 | |
| 128 Node* DOMSelection::anchorNode() const { | 144 Node* DOMSelection::anchorNode() const { |
| 129 if (Range* range = primaryRangeOrNull()) { | 145 if (Range* range = primaryRangeOrNull()) { |
| 146 ScopedRange scope(range, !isSelectionOfDocument()); | |
| 130 if (!frame() || isBaseFirstInSelection()) | 147 if (!frame() || isBaseFirstInSelection()) |
| 131 return range->startContainer(); | 148 return range->startContainer(); |
| 132 return range->endContainer(); | 149 return range->endContainer(); |
| 133 } | 150 } |
| 134 return nullptr; | 151 return nullptr; |
| 135 } | 152 } |
| 136 | 153 |
| 137 unsigned DOMSelection::anchorOffset() const { | 154 unsigned DOMSelection::anchorOffset() const { |
| 138 if (Range* range = primaryRangeOrNull()) { | 155 if (Range* range = primaryRangeOrNull()) { |
| 156 ScopedRange scope(range, !isSelectionOfDocument()); | |
| 139 if (!frame() || isBaseFirstInSelection()) | 157 if (!frame() || isBaseFirstInSelection()) |
| 140 return range->startOffset(); | 158 return range->startOffset(); |
| 141 return range->endOffset(); | 159 return range->endOffset(); |
| 142 } | 160 } |
| 143 return 0; | 161 return 0; |
| 144 } | 162 } |
| 145 | 163 |
| 146 Node* DOMSelection::focusNode() const { | 164 Node* DOMSelection::focusNode() const { |
| 147 if (Range* range = primaryRangeOrNull()) { | 165 if (Range* range = primaryRangeOrNull()) { |
| 166 ScopedRange scope(range, !isSelectionOfDocument()); | |
| 148 if (!frame() || isBaseFirstInSelection()) | 167 if (!frame() || isBaseFirstInSelection()) |
| 149 return range->endContainer(); | 168 return range->endContainer(); |
| 150 return range->startContainer(); | 169 return range->startContainer(); |
| 151 } | 170 } |
| 152 return nullptr; | 171 return nullptr; |
| 153 } | 172 } |
| 154 | 173 |
| 155 unsigned DOMSelection::focusOffset() const { | 174 unsigned DOMSelection::focusOffset() const { |
| 156 if (Range* range = primaryRangeOrNull()) { | 175 if (Range* range = primaryRangeOrNull()) { |
| 176 ScopedRange scope(range, !isSelectionOfDocument()); | |
| 157 if (!frame() || isBaseFirstInSelection()) | 177 if (!frame() || isBaseFirstInSelection()) |
| 158 return range->endOffset(); | 178 return range->endOffset(); |
| 159 return range->startOffset(); | 179 return range->startOffset(); |
| 160 } | 180 } |
| 161 return 0; | 181 return 0; |
| 162 } | 182 } |
| 163 | 183 |
| 164 Node* DOMSelection::baseNode() const { | 184 Node* DOMSelection::baseNode() const { |
| 165 if (!isAvailable()) | 185 if (!isAvailable()) |
| 166 return 0; | 186 return 0; |
| (...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 582 void DOMSelection::clearCachedRangeIfSelectionOfDocument() { | 602 void DOMSelection::clearCachedRangeIfSelectionOfDocument() { |
| 583 if (!isSelectionOfDocument()) | 603 if (!isSelectionOfDocument()) |
| 584 return; | 604 return; |
| 585 frame()->selection().clearDocumentCachedRange(); | 605 frame()->selection().clearDocumentCachedRange(); |
| 586 } | 606 } |
| 587 | 607 |
| 588 void DOMSelection::removeRange(Range* range) { | 608 void DOMSelection::removeRange(Range* range) { |
| 589 DCHECK(range); | 609 DCHECK(range); |
| 590 if (!isAvailable()) | 610 if (!isAvailable()) |
| 591 return; | 611 return; |
| 592 if (range == primaryRangeOrNull()) | 612 Range* originalRange = primaryRangeOrNull(); |
| 613 ScopedRange scope(originalRange, !isSelectionOfDocument()); | |
| 614 if (range == originalRange) | |
| 593 frame()->selection().clear(); | 615 frame()->selection().clear(); |
| 594 } | 616 } |
| 595 | 617 |
| 596 void DOMSelection::removeAllRanges() { | 618 void DOMSelection::removeAllRanges() { |
| 597 if (!isAvailable()) | 619 if (!isAvailable()) |
| 598 return; | 620 return; |
| 599 frame()->selection().clear(); | 621 frame()->selection().clear(); |
| 600 } | 622 } |
| 601 | 623 |
| 602 void DOMSelection::addRange(Range* newRange) { | 624 void DOMSelection::addRange(Range* newRange) { |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 624 updateFrameSelection(SelectionInDOMTree::Builder() | 646 updateFrameSelection(SelectionInDOMTree::Builder() |
| 625 .collapse(newRange->startPosition()) | 647 .collapse(newRange->startPosition()) |
| 626 .extend(newRange->endPosition()) | 648 .extend(newRange->endPosition()) |
| 627 .build(), | 649 .build(), |
| 628 newRange); | 650 newRange); |
| 629 return; | 651 return; |
| 630 } | 652 } |
| 631 | 653 |
| 632 Range* originalRange = primaryRangeOrNull(); | 654 Range* originalRange = primaryRangeOrNull(); |
| 633 DCHECK(originalRange); | 655 DCHECK(originalRange); |
| 656 ScopedRange scope(originalRange, !isSelectionOfDocument()); | |
| 634 | 657 |
| 635 if (originalRange->startContainer()->treeScope() != | 658 if (originalRange->startContainer()->treeScope() != |
| 636 newRange->startContainer()->treeScope()) { | 659 newRange->startContainer()->treeScope()) { |
| 637 return; | 660 return; |
| 638 } | 661 } |
| 639 | 662 |
| 640 if (originalRange->compareBoundaryPoints(Range::kStartToEnd, newRange, | 663 if (originalRange->compareBoundaryPoints(Range::kStartToEnd, newRange, |
| 641 ASSERT_NO_EXCEPTION) < 0 || | 664 ASSERT_NO_EXCEPTION) < 0 || |
| 642 newRange->compareBoundaryPoints(Range::kStartToEnd, originalRange, | 665 newRange->compareBoundaryPoints(Range::kStartToEnd, originalRange, |
| 643 ASSERT_NO_EXCEPTION) < 0) { | 666 ASSERT_NO_EXCEPTION) < 0) { |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 822 m_treeScope->document().addConsoleMessage( | 845 m_treeScope->document().addConsoleMessage( |
| 823 ConsoleMessage::create(JSMessageSource, ErrorMessageLevel, message)); | 846 ConsoleMessage::create(JSMessageSource, ErrorMessageLevel, message)); |
| 824 } | 847 } |
| 825 | 848 |
| 826 DEFINE_TRACE(DOMSelection) { | 849 DEFINE_TRACE(DOMSelection) { |
| 827 visitor->trace(m_treeScope); | 850 visitor->trace(m_treeScope); |
| 828 ContextClient::trace(visitor); | 851 ContextClient::trace(visitor); |
| 829 } | 852 } |
| 830 | 853 |
| 831 } // namespace blink | 854 } // namespace blink |
| OLD | NEW |