| 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 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 } | 194 } |
| 195 | 195 |
| 196 int DOMSelection::rangeCount() const { | 196 int DOMSelection::rangeCount() const { |
| 197 if (!isAvailable()) | 197 if (!isAvailable()) |
| 198 return 0; | 198 return 0; |
| 199 if (documentCachedRange()) | 199 if (documentCachedRange()) |
| 200 return 1; | 200 return 1; |
| 201 return frame()->selection().isNone() ? 0 : 1; | 201 return frame()->selection().isNone() ? 0 : 1; |
| 202 } | 202 } |
| 203 | 203 |
| 204 // https://www.w3.org/TR/selection-api/#dom-selection-collapse |
| 204 void DOMSelection::collapse(Node* node, | 205 void DOMSelection::collapse(Node* node, |
| 205 int offset, | 206 int offset, |
| 206 ExceptionState& exceptionState) { | 207 ExceptionState& exceptionState) { |
| 207 if (!isAvailable()) | 208 if (!isAvailable()) |
| 208 return; | 209 return; |
| 209 | 210 |
| 211 // 1. If node is null, this method must behave identically as |
| 212 // removeAllRanges() and abort these steps. |
| 210 if (!node) { | 213 if (!node) { |
| 211 UseCounter::count(frame(), UseCounter::SelectionCollapseNull); | 214 UseCounter::count(frame(), UseCounter::SelectionCollapseNull); |
| 212 frame()->selection().clear(); | 215 frame()->selection().clear(); |
| 213 return; | 216 return; |
| 214 } | 217 } |
| 215 | 218 |
| 219 // 2. The method must throw an IndexSizeError exception if offset is longer |
| 220 // than node's length ([DOM4]) and abort these steps. |
| 216 if (offset < 0) { | 221 if (offset < 0) { |
| 217 exceptionState.throwDOMException( | 222 exceptionState.throwDOMException( |
| 218 IndexSizeError, String::number(offset) + " is not a valid offset."); | 223 IndexSizeError, String::number(offset) + " is not a valid offset."); |
| 219 return; | 224 return; |
| 220 } | 225 } |
| 221 Range::checkNodeWOffset(node, offset, exceptionState); | 226 Range::checkNodeWOffset(node, offset, exceptionState); |
| 222 if (exceptionState.hadException()) | 227 if (exceptionState.hadException()) |
| 223 return; | 228 return; |
| 224 | 229 |
| 230 // 3. If node's root is not the document associated with the context object, |
| 231 // abort these steps. |
| 225 if (!isValidForPosition(node)) | 232 if (!isValidForPosition(node)) |
| 226 return; | 233 return; |
| 227 | 234 |
| 235 // 4. Otherwise, let newRange be a new range. |
| 236 Range* newRange = Range::create(*frame()->document()); |
| 237 |
| 238 // 5. Set ([DOM4]) the start and the end of newRange to (node, offset). |
| 239 newRange->setStart(node, offset, exceptionState); |
| 240 if (exceptionState.hadException()) |
| 241 return; |
| 242 newRange->setEnd(node, offset, exceptionState); |
| 243 if (exceptionState.hadException()) |
| 244 return; |
| 245 |
| 246 // 6. Set the context object's range to newRange. |
| 228 frame()->selection().setSelection( | 247 frame()->selection().setSelection( |
| 229 SelectionInDOMTree::Builder() | 248 SelectionInDOMTree::Builder() |
| 230 .collapse(Position(node, offset)) | 249 .collapse(Position(node, offset)) |
| 231 .setIsDirectional(frame()->selection().isDirectional()) | 250 .setIsDirectional(frame()->selection().isDirectional()) |
| 232 .build()); | 251 .build()); |
| 252 cacheRangeIfSelectionOfDocument(newRange); |
| 233 } | 253 } |
| 234 | 254 |
| 235 void DOMSelection::collapseToEnd(ExceptionState& exceptionState) { | 255 void DOMSelection::collapseToEnd(ExceptionState& exceptionState) { |
| 236 if (!isAvailable()) | 256 if (!isAvailable()) |
| 237 return; | 257 return; |
| 238 | 258 |
| 239 const VisibleSelection& selection = frame()->selection().selection(); | 259 const VisibleSelection& selection = frame()->selection().selection(); |
| 240 | 260 |
| 241 if (selection.isNone()) { | 261 if (selection.isNone()) { |
| 242 exceptionState.throwDOMException(InvalidStateError, | 262 exceptionState.throwDOMException(InvalidStateError, |
| (...skipping 546 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 789 m_treeScope->document().addConsoleMessage( | 809 m_treeScope->document().addConsoleMessage( |
| 790 ConsoleMessage::create(JSMessageSource, ErrorMessageLevel, message)); | 810 ConsoleMessage::create(JSMessageSource, ErrorMessageLevel, message)); |
| 791 } | 811 } |
| 792 | 812 |
| 793 DEFINE_TRACE(DOMSelection) { | 813 DEFINE_TRACE(DOMSelection) { |
| 794 visitor->trace(m_treeScope); | 814 visitor->trace(m_treeScope); |
| 795 ContextClient::trace(visitor); | 815 ContextClient::trace(visitor); |
| 796 } | 816 } |
| 797 | 817 |
| 798 } // namespace blink | 818 } // namespace blink |
| OLD | NEW |