Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(127)

Side by Side Diff: sky/engine/core/editing/DOMSelection.cpp

Issue 922893002: Merge the Sky Engine changes from the SkyDart branch (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 13 matching lines...) Expand all
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */ 28 */
29 29
30 30
31 #include "sky/engine/config.h" 31 #include "sky/engine/config.h"
32 #include "sky/engine/core/editing/DOMSelection.h" 32 #include "sky/engine/core/editing/DOMSelection.h"
33 33
34 #include "sky/engine/bindings/core/v8/ExceptionMessages.h" 34 #include "sky/engine/bindings2/exception_messages.h"
35 #include "sky/engine/bindings/core/v8/ExceptionState.h" 35 #include "sky/engine/bindings2/exception_state.h"
36 #include "sky/engine/bindings/core/v8/ExceptionStatePlaceholder.h" 36 #include "sky/engine/bindings2/exception_state_placeholder.h"
37 #include "sky/engine/core/dom/Document.h" 37 #include "sky/engine/core/dom/Document.h"
38 #include "sky/engine/core/dom/ExceptionCode.h" 38 #include "sky/engine/core/dom/ExceptionCode.h"
39 #include "sky/engine/core/dom/Node.h" 39 #include "sky/engine/core/dom/Node.h"
40 #include "sky/engine/core/dom/Range.h" 40 #include "sky/engine/core/dom/Range.h"
41 #include "sky/engine/core/dom/TreeScope.h" 41 #include "sky/engine/core/dom/TreeScope.h"
42 #include "sky/engine/core/editing/FrameSelection.h" 42 #include "sky/engine/core/editing/FrameSelection.h"
43 #include "sky/engine/core/editing/TextIterator.h" 43 #include "sky/engine/core/editing/TextIterator.h"
44 #include "sky/engine/core/editing/htmlediting.h" 44 #include "sky/engine/core/editing/htmlediting.h"
45 #include "sky/engine/core/frame/LocalFrame.h" 45 #include "sky/engine/core/frame/LocalFrame.h"
46 #include "sky/engine/core/inspector/ConsoleMessage.h" 46 #include "sky/engine/core/inspector/ConsoleMessage.h"
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 return m_frame->selection().isNone() ? 0 : 1; 194 return m_frame->selection().isNone() ? 0 : 1;
195 } 195 }
196 196
197 void DOMSelection::collapse(Node* node, int offset, ExceptionState& exceptionSta te) 197 void DOMSelection::collapse(Node* node, int offset, ExceptionState& exceptionSta te)
198 { 198 {
199 ASSERT(node); 199 ASSERT(node);
200 if (!m_frame) 200 if (!m_frame)
201 return; 201 return;
202 202
203 if (offset < 0) { 203 if (offset < 0) {
204 exceptionState.throwDOMException(IndexSizeError, String::number(offset) + " is not a valid offset."); 204 exceptionState.ThrowDOMException(IndexSizeError, String::number(offset) + " is not a valid offset.");
205 return; 205 return;
206 } 206 }
207 207
208 if (!isValidForPosition(node)) 208 if (!isValidForPosition(node))
209 return; 209 return;
210 RefPtr<Range> range = Range::create(node->document()); 210 RefPtr<Range> range = Range::create(node->document());
211 range->setStart(node, offset, exceptionState); 211 range->setStart(node, offset, exceptionState);
212 if (exceptionState.hadException()) 212 if (exceptionState.had_exception())
213 return; 213 return;
214 range->setEnd(node, offset, exceptionState); 214 range->setEnd(node, offset, exceptionState);
215 if (exceptionState.hadException()) 215 if (exceptionState.had_exception())
216 return; 216 return;
217 m_frame->selection().setSelectedRange(range.get(), DOWNSTREAM, m_frame->sele ction().isDirectional() ? FrameSelection::Directional : FrameSelection::NonDirec tional); 217 m_frame->selection().setSelectedRange(range.get(), DOWNSTREAM, m_frame->sele ction().isDirectional() ? FrameSelection::Directional : FrameSelection::NonDirec tional);
218 } 218 }
219 219
220 void DOMSelection::collapseToEnd(ExceptionState& exceptionState) 220 void DOMSelection::collapseToEnd(ExceptionState& exceptionState)
221 { 221 {
222 if (!m_frame) 222 if (!m_frame)
223 return; 223 return;
224 224
225 const VisibleSelection& selection = m_frame->selection().selection(); 225 const VisibleSelection& selection = m_frame->selection().selection();
226 226
227 if (selection.isNone()) { 227 if (selection.isNone()) {
228 exceptionState.throwDOMException(InvalidStateError, "there is no selecti on."); 228 exceptionState.ThrowDOMException(InvalidStateError, "there is no selecti on.");
229 return; 229 return;
230 } 230 }
231 231
232 m_frame->selection().moveTo(VisiblePosition(selection.end(), DOWNSTREAM)); 232 m_frame->selection().moveTo(VisiblePosition(selection.end(), DOWNSTREAM));
233 } 233 }
234 234
235 void DOMSelection::collapseToStart(ExceptionState& exceptionState) 235 void DOMSelection::collapseToStart(ExceptionState& exceptionState)
236 { 236 {
237 if (!m_frame) 237 if (!m_frame)
238 return; 238 return;
239 239
240 const VisibleSelection& selection = m_frame->selection().selection(); 240 const VisibleSelection& selection = m_frame->selection().selection();
241 241
242 if (selection.isNone()) { 242 if (selection.isNone()) {
243 exceptionState.throwDOMException(InvalidStateError, "there is no selecti on."); 243 exceptionState.ThrowDOMException(InvalidStateError, "there is no selecti on.");
244 return; 244 return;
245 } 245 }
246 246
247 m_frame->selection().moveTo(VisiblePosition(selection.start(), DOWNSTREAM)); 247 m_frame->selection().moveTo(VisiblePosition(selection.start(), DOWNSTREAM));
248 } 248 }
249 249
250 void DOMSelection::empty() 250 void DOMSelection::empty()
251 { 251 {
252 if (!m_frame) 252 if (!m_frame)
253 return; 253 return;
254 m_frame->selection().clear(); 254 m_frame->selection().clear();
255 } 255 }
256 256
257 void DOMSelection::setBaseAndExtent(Node* baseNode, int baseOffset, Node* extent Node, int extentOffset, ExceptionState& exceptionState) 257 void DOMSelection::setBaseAndExtent(Node* baseNode, int baseOffset, Node* extent Node, int extentOffset, ExceptionState& exceptionState)
258 { 258 {
259 if (!m_frame) 259 if (!m_frame)
260 return; 260 return;
261 261
262 if (baseOffset < 0) { 262 if (baseOffset < 0) {
263 exceptionState.throwDOMException(IndexSizeError, String::number(baseOffs et) + " is not a valid base offset."); 263 exceptionState.ThrowDOMException(IndexSizeError, String::number(baseOffs et) + " is not a valid base offset.");
264 return; 264 return;
265 } 265 }
266 266
267 if (extentOffset < 0) { 267 if (extentOffset < 0) {
268 exceptionState.throwDOMException(IndexSizeError, String::number(extentOf fset) + " is not a valid extent offset."); 268 exceptionState.ThrowDOMException(IndexSizeError, String::number(extentOf fset) + " is not a valid extent offset.");
269 return; 269 return;
270 } 270 }
271 271
272 if (!isValidForPosition(baseNode) || !isValidForPosition(extentNode)) 272 if (!isValidForPosition(baseNode) || !isValidForPosition(extentNode))
273 return; 273 return;
274 274
275 // FIXME: Eliminate legacy editing positions 275 // FIXME: Eliminate legacy editing positions
276 VisiblePosition visibleBase = VisiblePosition(createLegacyEditingPosition(ba seNode, baseOffset), DOWNSTREAM); 276 VisiblePosition visibleBase = VisiblePosition(createLegacyEditingPosition(ba seNode, baseOffset), DOWNSTREAM);
277 VisiblePosition visibleExtent = VisiblePosition(createLegacyEditingPosition( extentNode, extentOffset), DOWNSTREAM); 277 VisiblePosition visibleExtent = VisiblePosition(createLegacyEditingPosition( extentNode, extentOffset), DOWNSTREAM);
278 278
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 } 330 }
331 331
332 void DOMSelection::extend(Node* node, int offset, ExceptionState& exceptionState ) 332 void DOMSelection::extend(Node* node, int offset, ExceptionState& exceptionState )
333 { 333 {
334 ASSERT(node); 334 ASSERT(node);
335 335
336 if (!m_frame) 336 if (!m_frame)
337 return; 337 return;
338 338
339 if (offset < 0) { 339 if (offset < 0) {
340 exceptionState.throwDOMException(IndexSizeError, String::number(offset) + " is not a valid offset."); 340 exceptionState.ThrowDOMException(IndexSizeError, String::number(offset) + " is not a valid offset.");
341 return; 341 return;
342 } 342 }
343 if (offset > (node->offsetInCharacters() ? caretMaxOffset(node) : (int)node- >countChildren())) { 343 if (offset > (node->offsetInCharacters() ? caretMaxOffset(node) : (int)node- >countChildren())) {
344 exceptionState.throwDOMException(IndexSizeError, String::number(offset) + " is larger than the given node's length."); 344 exceptionState.ThrowDOMException(IndexSizeError, String::number(offset) + " is larger than the given node's length.");
345 return; 345 return;
346 } 346 }
347 347
348 if (!isValidForPosition(node)) 348 if (!isValidForPosition(node))
349 return; 349 return;
350 350
351 // FIXME: Eliminate legacy editing positions 351 // FIXME: Eliminate legacy editing positions
352 m_frame->selection().setExtent(VisiblePosition(createLegacyEditingPosition(n ode, offset), DOWNSTREAM)); 352 m_frame->selection().setExtent(VisiblePosition(createLegacyEditingPosition(n ode, offset), DOWNSTREAM));
353 } 353 }
354 354
355 PassRefPtr<Range> DOMSelection::getRangeAt(int index, ExceptionState& exceptionS tate) 355 PassRefPtr<Range> DOMSelection::getRangeAt(int index, ExceptionState& exceptionS tate)
356 { 356 {
357 if (!m_frame) 357 if (!m_frame)
358 return nullptr; 358 return nullptr;
359 359
360 if (index < 0 || index >= rangeCount()) { 360 if (index < 0 || index >= rangeCount()) {
361 exceptionState.throwDOMException(IndexSizeError, String::number(index) + " is not a valid index."); 361 exceptionState.ThrowDOMException(IndexSizeError, String::number(index) + " is not a valid index.");
362 return nullptr; 362 return nullptr;
363 } 363 }
364 364
365 // If you're hitting this, you've added broken multi-range selection support 365 // If you're hitting this, you've added broken multi-range selection support
366 ASSERT(rangeCount() == 1); 366 ASSERT(rangeCount() == 1);
367 367
368 if (Node* shadowAncestor = selectionShadowAncestor(m_frame)) { 368 if (Node* shadowAncestor = selectionShadowAncestor(m_frame)) {
369 ASSERT(!shadowAncestor->isShadowRoot()); 369 ASSERT(!shadowAncestor->isShadowRoot());
370 ContainerNode* container = shadowAncestor->parentOrShadowHostNode(); 370 ContainerNode* container = shadowAncestor->parentOrShadowHostNode();
371 int offset = shadowAncestor->nodeIndex(); 371 int offset = shadowAncestor->nodeIndex();
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
464 return false; 464 return false;
465 465
466 unsigned nodeIndex = n->nodeIndex(); 466 unsigned nodeIndex = n->nodeIndex();
467 RefPtr<Range> selectedRange = selection.selection().toNormalizedRange(); 467 RefPtr<Range> selectedRange = selection.selection().toNormalizedRange();
468 468
469 ContainerNode* parentNode = n->parentNode(); 469 ContainerNode* parentNode = n->parentNode();
470 if (!parentNode) 470 if (!parentNode)
471 return false; 471 return false;
472 472
473 TrackExceptionState exceptionState; 473 TrackExceptionState exceptionState;
474 bool nodeFullySelected = Range::compareBoundaryPoints(parentNode, nodeIndex, selectedRange->startContainer(), selectedRange->startOffset(), exceptionState) >= 0 && !exceptionState.hadException() 474 bool nodeFullySelected = Range::compareBoundaryPoints(parentNode, nodeIndex, selectedRange->startContainer(), selectedRange->startOffset(), exceptionState) >= 0 && !exceptionState.had_exception()
475 && Range::compareBoundaryPoints(parentNode, nodeIndex + 1, selectedRange ->endContainer(), selectedRange->endOffset(), exceptionState) <= 0 && !exception State.hadException(); 475 && Range::compareBoundaryPoints(parentNode, nodeIndex + 1, selectedRange ->endContainer(), selectedRange->endOffset(), exceptionState) <= 0 && !exception State.had_exception();
476 if (exceptionState.hadException()) 476 if (exceptionState.had_exception())
477 return false; 477 return false;
478 if (nodeFullySelected) 478 if (nodeFullySelected)
479 return true; 479 return true;
480 480
481 bool nodeFullyUnselected = (Range::compareBoundaryPoints(parentNode, nodeInd ex, selectedRange->endContainer(), selectedRange->endOffset(), exceptionState) > 0 && !exceptionState.hadException()) 481 bool nodeFullyUnselected = (Range::compareBoundaryPoints(parentNode, nodeInd ex, selectedRange->endContainer(), selectedRange->endOffset(), exceptionState) > 0 && !exceptionState.had_exception())
482 || (Range::compareBoundaryPoints(parentNode, nodeIndex + 1, selectedRang e->startContainer(), selectedRange->startOffset(), exceptionState) < 0 && !excep tionState.hadException()); 482 || (Range::compareBoundaryPoints(parentNode, nodeIndex + 1, selectedRang e->startContainer(), selectedRange->startOffset(), exceptionState) < 0 && !excep tionState.had_exception());
483 ASSERT(!exceptionState.hadException()); 483 ASSERT(!exceptionState.had_exception());
484 if (nodeFullyUnselected) 484 if (nodeFullyUnselected)
485 return false; 485 return false;
486 486
487 return allowPartial || n->isTextNode(); 487 return allowPartial || n->isTextNode();
488 } 488 }
489 489
490 void DOMSelection::selectAllChildren(Node* n, ExceptionState& exceptionState) 490 void DOMSelection::selectAllChildren(Node* n, ExceptionState& exceptionState)
491 { 491 {
492 if (!n) 492 if (!n)
493 return; 493 return;
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
547 return node->document() == m_frame->document(); 547 return node->document() == m_frame->document();
548 } 548 }
549 549
550 void DOMSelection::addConsoleError(const String& message) 550 void DOMSelection::addConsoleError(const String& message)
551 { 551 {
552 if (m_treeScope) 552 if (m_treeScope)
553 m_treeScope->document().addConsoleMessage(ConsoleMessage::create(JSMessa geSource, ErrorMessageLevel, message)); 553 m_treeScope->document().addConsoleMessage(ConsoleMessage::create(JSMessa geSource, ErrorMessageLevel, message));
554 } 554 }
555 555
556 } // namespace blink 556 } // namespace blink
OLDNEW
« no previous file with comments | « sky/engine/core/editing/DOMSelection.h ('k') | sky/engine/core/editing/DeleteFromTextNodeCommand.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698