| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2004, 2008, 2009, 2010 Apple Inc. All rights reserved. | 2 * Copyright (C) 2004, 2008, 2009, 2010 Apple Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 #include "core/editing/Editor.h" | 29 #include "core/editing/Editor.h" |
| 30 #include "core/editing/SelectionAdjuster.h" | 30 #include "core/editing/SelectionAdjuster.h" |
| 31 #include "core/frame/LocalFrame.h" | 31 #include "core/frame/LocalFrame.h" |
| 32 | 32 |
| 33 namespace blink { | 33 namespace blink { |
| 34 | 34 |
| 35 SelectionEditor::SelectionEditor(FrameSelection& frameSelection) | 35 SelectionEditor::SelectionEditor(FrameSelection& frameSelection) |
| 36 : m_frameSelection(frameSelection) | 36 : m_frameSelection(frameSelection) |
| 37 , m_observingVisibleSelection(false) | 37 , m_observingVisibleSelection(false) |
| 38 { | 38 { |
| 39 clearVisibleSelection(); |
| 39 } | 40 } |
| 40 | 41 |
| 41 SelectionEditor::~SelectionEditor() | 42 SelectionEditor::~SelectionEditor() |
| 42 { | 43 { |
| 43 } | 44 } |
| 44 | 45 |
| 46 void SelectionEditor::clearVisibleSelection() |
| 47 { |
| 48 m_selection = VisibleSelection(); |
| 49 m_selectionInFlatTree = VisibleSelectionInFlatTree(); |
| 50 if (!shouldAlwaysUseDirectionalSelection()) |
| 51 return; |
| 52 m_selection.setIsDirectional(true); |
| 53 m_selectionInFlatTree.setIsDirectional(true); |
| 54 } |
| 55 |
| 45 void SelectionEditor::dispose() | 56 void SelectionEditor::dispose() |
| 46 { | 57 { |
| 47 resetLogicalRange(); | 58 resetLogicalRange(); |
| 59 clearVisibleSelection(); |
| 60 } |
| 61 |
| 62 const Document& SelectionEditor::document() const |
| 63 { |
| 64 DCHECK(m_document); |
| 65 return *m_document; |
| 48 } | 66 } |
| 49 | 67 |
| 50 LocalFrame* SelectionEditor::frame() const | 68 LocalFrame* SelectionEditor::frame() const |
| 51 { | 69 { |
| 52 return m_frameSelection->frame(); | 70 return m_frameSelection->frame(); |
| 53 } | 71 } |
| 54 | 72 |
| 55 template <> | 73 template <> |
| 56 const VisibleSelection& SelectionEditor::visibleSelection<EditingStrategy>() con
st | 74 const VisibleSelection& SelectionEditor::visibleSelection<EditingStrategy>() con
st |
| 57 { | 75 { |
| 76 DCHECK_EQ(frame()->document(), document()); |
| 77 DCHECK_EQ(frame(), document().frame()); |
| 78 if (m_selection.isNone()) |
| 79 return m_selection; |
| 80 DCHECK_EQ(m_selection.base().document(), document()); |
| 58 return m_selection; | 81 return m_selection; |
| 59 } | 82 } |
| 60 | 83 |
| 61 template <> | 84 template <> |
| 62 const VisibleSelectionInFlatTree& SelectionEditor::visibleSelection<EditingInFla
tTreeStrategy>() const | 85 const VisibleSelectionInFlatTree& SelectionEditor::visibleSelection<EditingInFla
tTreeStrategy>() const |
| 63 { | 86 { |
| 87 DCHECK_EQ(frame()->document(), document()); |
| 88 DCHECK_EQ(frame(), document().frame()); |
| 89 if (m_selectionInFlatTree.isNone()) |
| 90 return m_selectionInFlatTree; |
| 91 DCHECK_EQ(m_selectionInFlatTree.base().document(), document()); |
| 64 return m_selectionInFlatTree; | 92 return m_selectionInFlatTree; |
| 65 } | 93 } |
| 66 | 94 |
| 67 void SelectionEditor::setVisibleSelection(const VisibleSelection& newSelection,
FrameSelection::SetSelectionOptions options) | 95 void SelectionEditor::setVisibleSelection(const VisibleSelection& newSelection,
FrameSelection::SetSelectionOptions options) |
| 68 { | 96 { |
| 97 DCHECK(newSelection.isValidFor(document())) << newSelection; |
| 69 resetLogicalRange(); | 98 resetLogicalRange(); |
| 70 m_selection = newSelection; | 99 m_selection = newSelection; |
| 71 if (options & FrameSelection::DoNotAdjustInFlatTree) { | 100 if (options & FrameSelection::DoNotAdjustInFlatTree) { |
| 72 m_selectionInFlatTree.setWithoutValidation(toPositionInFlatTree(m_select
ion.base()), toPositionInFlatTree(m_selection.extent())); | 101 m_selectionInFlatTree.setWithoutValidation(toPositionInFlatTree(m_select
ion.base()), toPositionInFlatTree(m_selection.extent())); |
| 73 return; | 102 return; |
| 74 } | 103 } |
| 75 | 104 |
| 76 SelectionAdjuster::adjustSelectionInFlatTree(&m_selectionInFlatTree, m_selec
tion); | 105 SelectionAdjuster::adjustSelectionInFlatTree(&m_selectionInFlatTree, m_selec
tion); |
| 77 } | 106 } |
| 78 | 107 |
| 79 void SelectionEditor::setVisibleSelection(const VisibleSelectionInFlatTree& newS
election, FrameSelection::SetSelectionOptions options) | 108 void SelectionEditor::setVisibleSelection(const VisibleSelectionInFlatTree& newS
election, FrameSelection::SetSelectionOptions options) |
| 80 { | 109 { |
| 110 DCHECK(newSelection.isValidFor(document())) << newSelection; |
| 81 DCHECK(!(options & FrameSelection::DoNotAdjustInFlatTree)); | 111 DCHECK(!(options & FrameSelection::DoNotAdjustInFlatTree)); |
| 82 resetLogicalRange(); | 112 resetLogicalRange(); |
| 83 m_selectionInFlatTree = newSelection; | 113 m_selectionInFlatTree = newSelection; |
| 84 SelectionAdjuster::adjustSelectionInDOMTree(&m_selection, m_selectionInFlatT
ree); | 114 SelectionAdjuster::adjustSelectionInDOMTree(&m_selection, m_selectionInFlatT
ree); |
| 85 } | 115 } |
| 86 | 116 |
| 87 void SelectionEditor::setIsDirectional(bool isDirectional) | |
| 88 { | |
| 89 m_selection.setIsDirectional(isDirectional); | |
| 90 m_selectionInFlatTree.setIsDirectional(isDirectional); | |
| 91 } | |
| 92 | |
| 93 void SelectionEditor::setWithoutValidation(const Position& base, const Position&
extent) | 117 void SelectionEditor::setWithoutValidation(const Position& base, const Position&
extent) |
| 94 { | 118 { |
| 95 resetLogicalRange(); | 119 resetLogicalRange(); |
| 120 if (base.isNotNull()) |
| 121 DCHECK_EQ(base.document(), document()); |
| 122 if (extent.isNotNull()) |
| 123 DCHECK_EQ(extent.document(), document()); |
| 96 m_selection.setWithoutValidation(base, extent); | 124 m_selection.setWithoutValidation(base, extent); |
| 97 m_selectionInFlatTree.setWithoutValidation(toPositionInFlatTree(base), toPos
itionInFlatTree(extent)); | 125 m_selectionInFlatTree.setWithoutValidation(toPositionInFlatTree(base), toPos
itionInFlatTree(extent)); |
| 98 } | 126 } |
| 99 | 127 |
| 128 void SelectionEditor::documentAttached(Document* document) |
| 129 { |
| 130 DCHECK(document); |
| 131 DCHECK(!m_document) << m_document; |
| 132 m_document = document; |
| 133 } |
| 134 |
| 135 void SelectionEditor::documentDetached(const Document& document) |
| 136 { |
| 137 DCHECK_EQ(m_document, &document); |
| 138 dispose(); |
| 139 m_document = nullptr; |
| 140 } |
| 141 |
| 100 void SelectionEditor::resetLogicalRange() | 142 void SelectionEditor::resetLogicalRange() |
| 101 { | 143 { |
| 102 // Non-collapsed ranges are not allowed to start at the end of a line that | 144 // Non-collapsed ranges are not allowed to start at the end of a line that |
| 103 // is wrapped, they start at the beginning of the next line instead | 145 // is wrapped, they start at the beginning of the next line instead |
| 104 if (!m_logicalRange) | 146 if (!m_logicalRange) |
| 105 return; | 147 return; |
| 106 m_logicalRange->dispose(); | 148 m_logicalRange->dispose(); |
| 107 m_logicalRange = nullptr; | 149 m_logicalRange = nullptr; |
| 108 } | 150 } |
| 109 | 151 |
| 110 void SelectionEditor::setLogicalRange(Range* range) | 152 void SelectionEditor::setLogicalRange(Range* range) |
| 111 { | 153 { |
| 154 DCHECK_EQ(range->ownerDocument(), document()); |
| 112 DCHECK(!m_logicalRange) << "A logical range should be one."; | 155 DCHECK(!m_logicalRange) << "A logical range should be one."; |
| 113 m_logicalRange = range; | 156 m_logicalRange = range; |
| 114 } | 157 } |
| 115 | 158 |
| 116 Range* SelectionEditor::firstRange() const | 159 Range* SelectionEditor::firstRange() const |
| 117 { | 160 { |
| 118 if (m_logicalRange) | 161 if (m_logicalRange) |
| 119 return m_logicalRange->cloneRange(); | 162 return m_logicalRange->cloneRange(); |
| 120 return firstRangeOf(m_selection); | 163 return firstRangeOf(m_selection); |
| 121 } | 164 } |
| 122 | 165 |
| 166 bool SelectionEditor::shouldAlwaysUseDirectionalSelection() const |
| 167 { |
| 168 return frame()->editor().behavior().shouldConsiderSelectionAsDirectional(); |
| 169 } |
| 170 |
| 123 void SelectionEditor::updateIfNeeded() | 171 void SelectionEditor::updateIfNeeded() |
| 124 { | 172 { |
| 173 DCHECK(m_selection.isValidFor(document())) << m_selection; |
| 174 DCHECK(m_selectionInFlatTree.isValidFor(document())) << m_selection; |
| 125 m_selection.updateIfNeeded(); | 175 m_selection.updateIfNeeded(); |
| 126 m_selectionInFlatTree.updateIfNeeded(); | 176 m_selectionInFlatTree.updateIfNeeded(); |
| 127 } | 177 } |
| 128 | 178 |
| 129 DEFINE_TRACE(SelectionEditor) | 179 DEFINE_TRACE(SelectionEditor) |
| 130 { | 180 { |
| 181 visitor->trace(m_document); |
| 131 visitor->trace(m_frameSelection); | 182 visitor->trace(m_frameSelection); |
| 132 visitor->trace(m_selection); | 183 visitor->trace(m_selection); |
| 133 visitor->trace(m_selectionInFlatTree); | 184 visitor->trace(m_selectionInFlatTree); |
| 134 visitor->trace(m_logicalRange); | 185 visitor->trace(m_logicalRange); |
| 135 } | 186 } |
| 136 | 187 |
| 137 } // namespace blink | 188 } // namespace blink |
| OLD | NEW |