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 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 100 } | 100 } |
| 101 | 101 |
| 102 Node* DOMSelection::anchorNode() const | 102 Node* DOMSelection::anchorNode() const |
| 103 { | 103 { |
| 104 if (!m_frame) | 104 if (!m_frame) |
| 105 return 0; | 105 return 0; |
| 106 | 106 |
| 107 return shadowAdjustedNode(anchorPosition(visibleSelection())); | 107 return shadowAdjustedNode(anchorPosition(visibleSelection())); |
| 108 } | 108 } |
| 109 | 109 |
| 110 int DOMSelection::anchorOffset() const | 110 unsigned DOMSelection::anchorOffset() const |
| 111 { | 111 { |
| 112 if (!m_frame) | 112 if (!m_frame) |
| 113 return 0; | 113 return 0; |
| 114 | 114 |
| 115 return shadowAdjustedOffset(anchorPosition(visibleSelection())); | 115 return shadowAdjustedOffset(anchorPosition(visibleSelection())); |
| 116 } | 116 } |
| 117 | 117 |
| 118 Node* DOMSelection::focusNode() const | 118 Node* DOMSelection::focusNode() const |
| 119 { | 119 { |
| 120 if (!m_frame) | 120 if (!m_frame) |
| 121 return 0; | 121 return 0; |
| 122 | 122 |
| 123 return shadowAdjustedNode(focusPosition(visibleSelection())); | 123 return shadowAdjustedNode(focusPosition(visibleSelection())); |
| 124 } | 124 } |
| 125 | 125 |
| 126 int DOMSelection::focusOffset() const | 126 unsigned DOMSelection::focusOffset() const |
| 127 { | 127 { |
| 128 if (!m_frame) | 128 if (!m_frame) |
| 129 return 0; | 129 return 0; |
| 130 | 130 |
| 131 return shadowAdjustedOffset(focusPosition(visibleSelection())); | 131 return shadowAdjustedOffset(focusPosition(visibleSelection())); |
| 132 } | 132 } |
| 133 | 133 |
| 134 Node* DOMSelection::baseNode() const | 134 Node* DOMSelection::baseNode() const |
| 135 { | 135 { |
| 136 if (!m_frame) | 136 if (!m_frame) |
| 137 return 0; | 137 return 0; |
| 138 | 138 |
| 139 return shadowAdjustedNode(basePosition(visibleSelection())); | 139 return shadowAdjustedNode(basePosition(visibleSelection())); |
| 140 } | 140 } |
| 141 | 141 |
| 142 int DOMSelection::baseOffset() const | 142 unsigned DOMSelection::baseOffset() const |
| 143 { | 143 { |
| 144 if (!m_frame) | 144 if (!m_frame) |
| 145 return 0; | 145 return 0; |
| 146 | 146 |
| 147 return shadowAdjustedOffset(basePosition(visibleSelection())); | 147 return shadowAdjustedOffset(basePosition(visibleSelection())); |
| 148 } | 148 } |
| 149 | 149 |
| 150 Node* DOMSelection::extentNode() const | 150 Node* DOMSelection::extentNode() const |
| 151 { | 151 { |
| 152 if (!m_frame) | 152 if (!m_frame) |
| 153 return 0; | 153 return 0; |
| 154 | 154 |
| 155 return shadowAdjustedNode(extentPosition(visibleSelection())); | 155 return shadowAdjustedNode(extentPosition(visibleSelection())); |
| 156 } | 156 } |
| 157 | 157 |
| 158 int DOMSelection::extentOffset() const | 158 unsigned DOMSelection::extentOffset() const |
| 159 { | 159 { |
| 160 if (!m_frame) | 160 if (!m_frame) |
| 161 return 0; | 161 return 0; |
| 162 | 162 |
| 163 return shadowAdjustedOffset(extentPosition(visibleSelection())); | 163 return shadowAdjustedOffset(extentPosition(visibleSelection())); |
| 164 } | 164 } |
| 165 | 165 |
| 166 bool DOMSelection::isCollapsed() const | 166 bool DOMSelection::isCollapsed() const |
| 167 { | 167 { |
| 168 if (!m_frame || selectionShadowAncestor(m_frame)) | 168 if (!m_frame || selectionShadowAncestor(m_frame)) |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 180 // This is a WebKit DOM extension, incompatible with an IE extension | 180 // This is a WebKit DOM extension, incompatible with an IE extension |
| 181 // IE has this same attribute, but returns "none", "text" and "control" | 181 // IE has this same attribute, but returns "none", "text" and "control" |
| 182 // http://msdn.microsoft.com/en-us/library/ms534692(VS.85).aspx | 182 // http://msdn.microsoft.com/en-us/library/ms534692(VS.85).aspx |
| 183 if (selection.isNone()) | 183 if (selection.isNone()) |
| 184 return "None"; | 184 return "None"; |
| 185 if (selection.isCaret()) | 185 if (selection.isCaret()) |
| 186 return "Caret"; | 186 return "Caret"; |
| 187 return "Range"; | 187 return "Range"; |
| 188 } | 188 } |
| 189 | 189 |
| 190 int DOMSelection::rangeCount() const | 190 unsigned DOMSelection::rangeCount() const |
| 191 { | 191 { |
| 192 if (!m_frame) | 192 if (!m_frame) |
| 193 return 0; | 193 return 0; |
| 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, unsigned offset, ExceptionState& excepti onState) |
| 198 { | 198 { |
| 199 if (!m_frame) | 199 if (!m_frame) |
| 200 return; | 200 return; |
| 201 | 201 |
| 202 if (!node) { | 202 if (!node) { |
| 203 m_frame->selection().clear(); | 203 m_frame->selection().clear(); |
| 204 return; | 204 return; |
| 205 } | 205 } |
| 206 | 206 |
| 207 if (offset < 0) { | 207 if (offset > node->lengthOfContents()) { |
| 208 exceptionState.throwDOMException(IndexSizeError, String::number(offset) + " is not a valid offset."); | 208 exceptionState.throwDOMException(IndexSizeError, String::number(offset) + " is larger than the given node's length."); |
| 209 return; | 209 return; |
| 210 } | 210 } |
| 211 | 211 |
| 212 if (!isValidForPosition(node)) | 212 if (!isValidForPosition(node)) |
| 213 return; | 213 return; |
| 214 RefPtrWillBeRawPtr<Range> range = Range::create(node->document()); | 214 RefPtrWillBeRawPtr<Range> range = Range::create(node->document()); |
| 215 range->setStart(node, offset, exceptionState); | 215 range->setStart(node, offset, exceptionState); |
| 216 if (exceptionState.hadException()) | 216 if (exceptionState.hadException()) |
| 217 return; | 217 return; |
| 218 range->setEnd(node, offset, exceptionState); | 218 range->setEnd(node, offset, exceptionState); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 251 m_frame->selection().moveTo(VisiblePosition(selection.start(), DOWNSTREAM)); | 251 m_frame->selection().moveTo(VisiblePosition(selection.start(), DOWNSTREAM)); |
| 252 } | 252 } |
| 253 | 253 |
| 254 void DOMSelection::empty() | 254 void DOMSelection::empty() |
| 255 { | 255 { |
| 256 if (!m_frame) | 256 if (!m_frame) |
| 257 return; | 257 return; |
| 258 m_frame->selection().clear(); | 258 m_frame->selection().clear(); |
| 259 } | 259 } |
| 260 | 260 |
| 261 void DOMSelection::setBaseAndExtent(Node* baseNode, int baseOffset, Node* extent Node, int extentOffset, ExceptionState& exceptionState) | 261 void DOMSelection::setBaseAndExtent(Node* baseNode, unsigned baseOffset, Node* e xtentNode, unsigned extentOffset, ExceptionState& exceptionState) |
| 262 { | 262 { |
| 263 if (!m_frame) | 263 if (!m_frame) |
| 264 return; | 264 return; |
| 265 | 265 |
| 266 if (baseOffset < 0) { | 266 if (baseNode && baseOffset > baseNode->lengthOfContents()) { |
| 267 exceptionState.throwDOMException(IndexSizeError, String::number(baseOffs et) + " is not a valid base offset."); | 267 exceptionState.throwDOMException(IndexSizeError, String::number(baseOffs et) + " is larger than the base node's length."); |
|
yoichio
2015/07/13 02:29:45
Raising exception when offset is over content leng
philipj_slow
2015/07/13 07:13:49
The spec has different variable names, but it actu
Habib Virji
2015/08/05 09:33:44
@philipj: As far as I can see no error. If it exce
Habib Virji
2015/08/05 09:33:44
Actually spec says to throw an error for both the
| |
| 268 return; | 268 return; |
| 269 } | 269 } |
| 270 | 270 |
| 271 if (extentOffset < 0) { | 271 if (extentNode && baseNode && extentOffset > (baseNode->lengthOfContents() + extentNode->lengthOfContents())) { |
| 272 exceptionState.throwDOMException(IndexSizeError, String::number(extentOf fset) + " is not a valid extent offset."); | 272 exceptionState.throwDOMException(IndexSizeError, String::number(extentOf fset) + " is larger than the extent node's length."); |
| 273 return; | 273 return; |
| 274 } | 274 } |
| 275 | 275 |
| 276 if (!isValidForPosition(baseNode) || !isValidForPosition(extentNode)) | 276 if (!isValidForPosition(baseNode) || !isValidForPosition(extentNode)) |
| 277 return; | 277 return; |
| 278 | 278 |
| 279 // FIXME: Eliminate legacy editing positions | 279 // FIXME: Eliminate legacy editing positions |
| 280 VisiblePosition visibleBase = VisiblePosition(createLegacyEditingPosition(ba seNode, baseOffset), DOWNSTREAM); | 280 VisiblePosition visibleBase = VisiblePosition(createLegacyEditingPosition(ba seNode, baseOffset), DOWNSTREAM); |
| 281 VisiblePosition visibleExtent = VisiblePosition(createLegacyEditingPosition( extentNode, extentOffset), DOWNSTREAM); | 281 VisiblePosition visibleExtent = VisiblePosition(createLegacyEditingPosition( extentNode, extentOffset), DOWNSTREAM); |
| 282 | 282 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 326 else if (equalIgnoringCase(granularityString, "paragraphboundary")) | 326 else if (equalIgnoringCase(granularityString, "paragraphboundary")) |
| 327 granularity = ParagraphBoundary; | 327 granularity = ParagraphBoundary; |
| 328 else if (equalIgnoringCase(granularityString, "documentboundary")) | 328 else if (equalIgnoringCase(granularityString, "documentboundary")) |
| 329 granularity = DocumentBoundary; | 329 granularity = DocumentBoundary; |
| 330 else | 330 else |
| 331 return; | 331 return; |
| 332 | 332 |
| 333 m_frame->selection().modify(alter, direction, granularity); | 333 m_frame->selection().modify(alter, direction, granularity); |
| 334 } | 334 } |
| 335 | 335 |
| 336 void DOMSelection::extend(Node* node, int offset, ExceptionState& exceptionState ) | 336 void DOMSelection::extend(Node* node, unsigned offset, ExceptionState& exception State) |
| 337 { | 337 { |
| 338 ASSERT(node); | 338 ASSERT(node); |
| 339 | 339 |
| 340 if (!m_frame) | 340 if (!m_frame) |
| 341 return; | 341 return; |
| 342 | 342 |
| 343 if (offset < 0) { | 343 if (offset > node->lengthOfContents()) { |
| 344 exceptionState.throwDOMException(IndexSizeError, String::number(offset) + " is not a valid offset."); | |
| 345 return; | |
| 346 } | |
| 347 if (static_cast<unsigned>(offset) > node->lengthOfContents()) { | |
| 348 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."); |
| 349 return; | 345 return; |
| 350 } | 346 } |
| 351 | 347 |
| 352 if (!isValidForPosition(node)) | 348 if (!isValidForPosition(node)) |
| 353 return; | 349 return; |
| 354 | 350 |
| 355 // FIXME: Eliminate legacy editing positions | 351 // FIXME: Eliminate legacy editing positions |
| 356 m_frame->selection().setExtent(VisiblePosition(createLegacyEditingPosition(n ode, offset), DOWNSTREAM)); | 352 m_frame->selection().setExtent(VisiblePosition(createLegacyEditingPosition(n ode, offset), DOWNSTREAM)); |
| 357 } | 353 } |
| 358 | 354 |
| 359 PassRefPtrWillBeRawPtr<Range> DOMSelection::getRangeAt(int index, ExceptionState & exceptionState) | 355 PassRefPtrWillBeRawPtr<Range> DOMSelection::getRangeAt(unsigned index, Exception State& exceptionState) |
| 360 { | 356 { |
| 361 if (!m_frame) | 357 if (!m_frame) |
| 362 return nullptr; | 358 return nullptr; |
| 363 | 359 |
| 364 if (index < 0 || index >= rangeCount()) { | 360 if (index >= rangeCount()) { |
| 365 exceptionState.throwDOMException(IndexSizeError, String::number(index) + " is not a valid index."); | 361 exceptionState.throwDOMException(IndexSizeError, String::number(index) + " is not a valid index."); |
| 366 return nullptr; | 362 return nullptr; |
| 367 } | 363 } |
| 368 | 364 |
| 369 // 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 |
| 370 ASSERT(rangeCount() == 1); | 366 ASSERT(rangeCount() == 1); |
| 371 | 367 |
| 372 Position anchor = anchorPosition(visibleSelection()); | 368 Position anchor = anchorPosition(visibleSelection()); |
| 373 if (!anchor.anchorNode()->isInShadowTree()) | 369 if (!anchor.anchorNode()->isInShadowTree()) |
| 374 return m_frame->selection().firstRange(); | 370 return m_frame->selection().firstRange(); |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 520 if (!adjustedNode) | 516 if (!adjustedNode) |
| 521 return 0; | 517 return 0; |
| 522 | 518 |
| 523 if (containerNode == adjustedNode) | 519 if (containerNode == adjustedNode) |
| 524 return containerNode; | 520 return containerNode; |
| 525 | 521 |
| 526 ASSERT(!adjustedNode->isShadowRoot()); | 522 ASSERT(!adjustedNode->isShadowRoot()); |
| 527 return adjustedNode->parentOrShadowHostNode(); | 523 return adjustedNode->parentOrShadowHostNode(); |
| 528 } | 524 } |
| 529 | 525 |
| 530 int DOMSelection::shadowAdjustedOffset(const Position& position) const | 526 unsigned DOMSelection::shadowAdjustedOffset(const Position& position) const |
| 531 { | 527 { |
| 532 if (position.isNull()) | 528 if (position.isNull()) |
| 533 return 0; | 529 return 0; |
| 534 | 530 |
| 535 Node* containerNode = position.containerNode(); | 531 Node* containerNode = position.containerNode(); |
| 536 Node* adjustedNode = m_treeScope->ancestorInThisScope(containerNode); | 532 Node* adjustedNode = m_treeScope->ancestorInThisScope(containerNode); |
| 537 | 533 |
| 538 if (!adjustedNode) | 534 if (!adjustedNode) |
| 539 return 0; | 535 return 0; |
| 540 | 536 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 558 m_treeScope->document().addConsoleMessage(ConsoleMessage::create(JSMessa geSource, ErrorMessageLevel, message)); | 554 m_treeScope->document().addConsoleMessage(ConsoleMessage::create(JSMessa geSource, ErrorMessageLevel, message)); |
| 559 } | 555 } |
| 560 | 556 |
| 561 DEFINE_TRACE(DOMSelection) | 557 DEFINE_TRACE(DOMSelection) |
| 562 { | 558 { |
| 563 visitor->trace(m_treeScope); | 559 visitor->trace(m_treeScope); |
| 564 DOMWindowProperty::trace(visitor); | 560 DOMWindowProperty::trace(visitor); |
| 565 } | 561 } |
| 566 | 562 |
| 567 } // namespace blink | 563 } // namespace blink |
| OLD | NEW |