Thu, 15 Jan 2015 21:03:48 +0100
Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | /* |
michael@0 | 7 | * Implementation of the DOM nsIDOMRange object. |
michael@0 | 8 | */ |
michael@0 | 9 | |
michael@0 | 10 | #include "nscore.h" |
michael@0 | 11 | #include "nsRange.h" |
michael@0 | 12 | |
michael@0 | 13 | #include "nsString.h" |
michael@0 | 14 | #include "nsReadableUtils.h" |
michael@0 | 15 | #include "nsIDOMNode.h" |
michael@0 | 16 | #include "nsIDOMDocumentFragment.h" |
michael@0 | 17 | #include "nsIContent.h" |
michael@0 | 18 | #include "nsIDocument.h" |
michael@0 | 19 | #include "nsIDOMText.h" |
michael@0 | 20 | #include "nsError.h" |
michael@0 | 21 | #include "nsIContentIterator.h" |
michael@0 | 22 | #include "nsIDOMNodeList.h" |
michael@0 | 23 | #include "nsGkAtoms.h" |
michael@0 | 24 | #include "nsContentUtils.h" |
michael@0 | 25 | #include "nsGenericDOMDataNode.h" |
michael@0 | 26 | #include "nsLayoutUtils.h" |
michael@0 | 27 | #include "nsTextFrame.h" |
michael@0 | 28 | #include "nsFontFaceList.h" |
michael@0 | 29 | #include "mozilla/dom/DocumentFragment.h" |
michael@0 | 30 | #include "mozilla/dom/DocumentType.h" |
michael@0 | 31 | #include "mozilla/dom/RangeBinding.h" |
michael@0 | 32 | #include "mozilla/dom/DOMRect.h" |
michael@0 | 33 | #include "mozilla/dom/ShadowRoot.h" |
michael@0 | 34 | #include "mozilla/Telemetry.h" |
michael@0 | 35 | #include "mozilla/Likely.h" |
michael@0 | 36 | #include "nsCSSFrameConstructor.h" |
michael@0 | 37 | |
michael@0 | 38 | using namespace mozilla; |
michael@0 | 39 | using namespace mozilla::dom; |
michael@0 | 40 | |
michael@0 | 41 | JSObject* |
michael@0 | 42 | nsRange::WrapObject(JSContext* aCx) |
michael@0 | 43 | { |
michael@0 | 44 | return RangeBinding::Wrap(aCx, this); |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | /****************************************************** |
michael@0 | 48 | * stack based utilty class for managing monitor |
michael@0 | 49 | ******************************************************/ |
michael@0 | 50 | |
michael@0 | 51 | static void InvalidateAllFrames(nsINode* aNode) |
michael@0 | 52 | { |
michael@0 | 53 | NS_PRECONDITION(aNode, "bad arg"); |
michael@0 | 54 | |
michael@0 | 55 | nsIFrame* frame = nullptr; |
michael@0 | 56 | switch (aNode->NodeType()) { |
michael@0 | 57 | case nsIDOMNode::TEXT_NODE: |
michael@0 | 58 | case nsIDOMNode::ELEMENT_NODE: |
michael@0 | 59 | { |
michael@0 | 60 | nsIContent* content = static_cast<nsIContent*>(aNode); |
michael@0 | 61 | frame = content->GetPrimaryFrame(); |
michael@0 | 62 | break; |
michael@0 | 63 | } |
michael@0 | 64 | case nsIDOMNode::DOCUMENT_NODE: |
michael@0 | 65 | { |
michael@0 | 66 | nsIDocument* doc = static_cast<nsIDocument*>(aNode); |
michael@0 | 67 | nsIPresShell* shell = doc ? doc->GetShell() : nullptr; |
michael@0 | 68 | frame = shell ? shell->GetRootFrame() : nullptr; |
michael@0 | 69 | break; |
michael@0 | 70 | } |
michael@0 | 71 | } |
michael@0 | 72 | for (nsIFrame* f = frame; f; f = f->GetNextContinuation()) { |
michael@0 | 73 | f->InvalidateFrameSubtree(); |
michael@0 | 74 | } |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | // Utility routine to detect if a content node is completely contained in a range |
michael@0 | 78 | // If outNodeBefore is returned true, then the node starts before the range does. |
michael@0 | 79 | // If outNodeAfter is returned true, then the node ends after the range does. |
michael@0 | 80 | // Note that both of the above might be true. |
michael@0 | 81 | // If neither are true, the node is contained inside of the range. |
michael@0 | 82 | // XXX - callers responsibility to ensure node in same doc as range! |
michael@0 | 83 | |
michael@0 | 84 | // static |
michael@0 | 85 | nsresult |
michael@0 | 86 | nsRange::CompareNodeToRange(nsINode* aNode, nsRange* aRange, |
michael@0 | 87 | bool *outNodeBefore, bool *outNodeAfter) |
michael@0 | 88 | { |
michael@0 | 89 | NS_ENSURE_STATE(aNode); |
michael@0 | 90 | // create a pair of dom points that expresses location of node: |
michael@0 | 91 | // NODE(start), NODE(end) |
michael@0 | 92 | // Let incoming range be: |
michael@0 | 93 | // {RANGE(start), RANGE(end)} |
michael@0 | 94 | // if (RANGE(start) <= NODE(start)) and (RANGE(end) => NODE(end)) |
michael@0 | 95 | // then the Node is contained (completely) by the Range. |
michael@0 | 96 | |
michael@0 | 97 | if (!aRange || !aRange->IsPositioned()) |
michael@0 | 98 | return NS_ERROR_UNEXPECTED; |
michael@0 | 99 | |
michael@0 | 100 | // gather up the dom point info |
michael@0 | 101 | int32_t nodeStart, nodeEnd; |
michael@0 | 102 | nsINode* parent = aNode->GetParentNode(); |
michael@0 | 103 | if (!parent) { |
michael@0 | 104 | // can't make a parent/offset pair to represent start or |
michael@0 | 105 | // end of the root node, because it has no parent. |
michael@0 | 106 | // so instead represent it by (node,0) and (node,numChildren) |
michael@0 | 107 | parent = aNode; |
michael@0 | 108 | nodeStart = 0; |
michael@0 | 109 | nodeEnd = aNode->GetChildCount(); |
michael@0 | 110 | } |
michael@0 | 111 | else { |
michael@0 | 112 | nodeStart = parent->IndexOf(aNode); |
michael@0 | 113 | nodeEnd = nodeStart + 1; |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | nsINode* rangeStartParent = aRange->GetStartParent(); |
michael@0 | 117 | nsINode* rangeEndParent = aRange->GetEndParent(); |
michael@0 | 118 | int32_t rangeStartOffset = aRange->StartOffset(); |
michael@0 | 119 | int32_t rangeEndOffset = aRange->EndOffset(); |
michael@0 | 120 | |
michael@0 | 121 | // is RANGE(start) <= NODE(start) ? |
michael@0 | 122 | bool disconnected = false; |
michael@0 | 123 | *outNodeBefore = nsContentUtils::ComparePoints(rangeStartParent, |
michael@0 | 124 | rangeStartOffset, |
michael@0 | 125 | parent, nodeStart, |
michael@0 | 126 | &disconnected) > 0; |
michael@0 | 127 | NS_ENSURE_TRUE(!disconnected, NS_ERROR_DOM_WRONG_DOCUMENT_ERR); |
michael@0 | 128 | |
michael@0 | 129 | // is RANGE(end) >= NODE(end) ? |
michael@0 | 130 | *outNodeAfter = nsContentUtils::ComparePoints(rangeEndParent, |
michael@0 | 131 | rangeEndOffset, |
michael@0 | 132 | parent, nodeEnd, |
michael@0 | 133 | &disconnected) < 0; |
michael@0 | 134 | NS_ENSURE_TRUE(!disconnected, NS_ERROR_DOM_WRONG_DOCUMENT_ERR); |
michael@0 | 135 | return NS_OK; |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | struct FindSelectedRangeData |
michael@0 | 139 | { |
michael@0 | 140 | nsINode* mNode; |
michael@0 | 141 | nsRange* mResult; |
michael@0 | 142 | uint32_t mStartOffset; |
michael@0 | 143 | uint32_t mEndOffset; |
michael@0 | 144 | }; |
michael@0 | 145 | |
michael@0 | 146 | static PLDHashOperator |
michael@0 | 147 | FindSelectedRange(nsPtrHashKey<nsRange>* aEntry, void* userArg) |
michael@0 | 148 | { |
michael@0 | 149 | nsRange* range = aEntry->GetKey(); |
michael@0 | 150 | if (range->IsInSelection() && !range->Collapsed()) { |
michael@0 | 151 | FindSelectedRangeData* data = static_cast<FindSelectedRangeData*>(userArg); |
michael@0 | 152 | int32_t cmp = nsContentUtils::ComparePoints(data->mNode, data->mEndOffset, |
michael@0 | 153 | range->GetStartParent(), |
michael@0 | 154 | range->StartOffset()); |
michael@0 | 155 | if (cmp == 1) { |
michael@0 | 156 | cmp = nsContentUtils::ComparePoints(data->mNode, data->mStartOffset, |
michael@0 | 157 | range->GetEndParent(), |
michael@0 | 158 | range->EndOffset()); |
michael@0 | 159 | if (cmp == -1) { |
michael@0 | 160 | data->mResult = range; |
michael@0 | 161 | return PL_DHASH_STOP; |
michael@0 | 162 | } |
michael@0 | 163 | } |
michael@0 | 164 | } |
michael@0 | 165 | return PL_DHASH_NEXT; |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | static nsINode* |
michael@0 | 169 | GetNextRangeCommonAncestor(nsINode* aNode) |
michael@0 | 170 | { |
michael@0 | 171 | while (aNode && !aNode->IsCommonAncestorForRangeInSelection()) { |
michael@0 | 172 | if (!aNode->IsDescendantOfCommonAncestorForRangeInSelection()) { |
michael@0 | 173 | return nullptr; |
michael@0 | 174 | } |
michael@0 | 175 | aNode = aNode->GetParentNode(); |
michael@0 | 176 | } |
michael@0 | 177 | return aNode; |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | /* static */ bool |
michael@0 | 181 | nsRange::IsNodeSelected(nsINode* aNode, uint32_t aStartOffset, |
michael@0 | 182 | uint32_t aEndOffset) |
michael@0 | 183 | { |
michael@0 | 184 | NS_PRECONDITION(aNode, "bad arg"); |
michael@0 | 185 | |
michael@0 | 186 | FindSelectedRangeData data = { aNode, nullptr, aStartOffset, aEndOffset }; |
michael@0 | 187 | nsINode* n = GetNextRangeCommonAncestor(aNode); |
michael@0 | 188 | NS_ASSERTION(n || !aNode->IsSelectionDescendant(), |
michael@0 | 189 | "orphan selection descendant"); |
michael@0 | 190 | for (; n; n = GetNextRangeCommonAncestor(n->GetParentNode())) { |
michael@0 | 191 | RangeHashTable* ranges = |
michael@0 | 192 | static_cast<RangeHashTable*>(n->GetProperty(nsGkAtoms::range)); |
michael@0 | 193 | ranges->EnumerateEntries(FindSelectedRange, &data); |
michael@0 | 194 | if (data.mResult) { |
michael@0 | 195 | return true; |
michael@0 | 196 | } |
michael@0 | 197 | } |
michael@0 | 198 | return false; |
michael@0 | 199 | } |
michael@0 | 200 | |
michael@0 | 201 | /****************************************************** |
michael@0 | 202 | * constructor/destructor |
michael@0 | 203 | ******************************************************/ |
michael@0 | 204 | |
michael@0 | 205 | nsRange::~nsRange() |
michael@0 | 206 | { |
michael@0 | 207 | NS_ASSERTION(!IsInSelection(), "deleting nsRange that is in use"); |
michael@0 | 208 | |
michael@0 | 209 | // Maybe we can remove Detach() -- bug 702948. |
michael@0 | 210 | Telemetry::Accumulate(Telemetry::DOM_RANGE_DETACHED, mIsDetached); |
michael@0 | 211 | |
michael@0 | 212 | // we want the side effects (releases and list removals) |
michael@0 | 213 | DoSetRange(nullptr, 0, nullptr, 0, nullptr); |
michael@0 | 214 | } |
michael@0 | 215 | |
michael@0 | 216 | /* static */ |
michael@0 | 217 | nsresult |
michael@0 | 218 | nsRange::CreateRange(nsINode* aStartParent, int32_t aStartOffset, |
michael@0 | 219 | nsINode* aEndParent, int32_t aEndOffset, |
michael@0 | 220 | nsRange** aRange) |
michael@0 | 221 | { |
michael@0 | 222 | nsCOMPtr<nsIDOMNode> startDomNode = do_QueryInterface(aStartParent); |
michael@0 | 223 | nsCOMPtr<nsIDOMNode> endDomNode = do_QueryInterface(aEndParent); |
michael@0 | 224 | |
michael@0 | 225 | nsresult rv = CreateRange(startDomNode, aStartOffset, endDomNode, aEndOffset, |
michael@0 | 226 | aRange); |
michael@0 | 227 | |
michael@0 | 228 | return rv; |
michael@0 | 229 | |
michael@0 | 230 | } |
michael@0 | 231 | |
michael@0 | 232 | /* static */ |
michael@0 | 233 | nsresult |
michael@0 | 234 | nsRange::CreateRange(nsIDOMNode* aStartParent, int32_t aStartOffset, |
michael@0 | 235 | nsIDOMNode* aEndParent, int32_t aEndOffset, |
michael@0 | 236 | nsRange** aRange) |
michael@0 | 237 | { |
michael@0 | 238 | MOZ_ASSERT(aRange); |
michael@0 | 239 | *aRange = nullptr; |
michael@0 | 240 | |
michael@0 | 241 | nsCOMPtr<nsINode> startParent = do_QueryInterface(aStartParent); |
michael@0 | 242 | NS_ENSURE_ARG_POINTER(startParent); |
michael@0 | 243 | |
michael@0 | 244 | nsRefPtr<nsRange> range = new nsRange(startParent); |
michael@0 | 245 | |
michael@0 | 246 | nsresult rv = range->SetStart(startParent, aStartOffset); |
michael@0 | 247 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 248 | |
michael@0 | 249 | rv = range->SetEnd(aEndParent, aEndOffset); |
michael@0 | 250 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 251 | |
michael@0 | 252 | range.forget(aRange); |
michael@0 | 253 | return NS_OK; |
michael@0 | 254 | } |
michael@0 | 255 | |
michael@0 | 256 | /* static */ |
michael@0 | 257 | nsresult |
michael@0 | 258 | nsRange::CreateRange(nsIDOMNode* aStartParent, int32_t aStartOffset, |
michael@0 | 259 | nsIDOMNode* aEndParent, int32_t aEndOffset, |
michael@0 | 260 | nsIDOMRange** aRange) |
michael@0 | 261 | { |
michael@0 | 262 | nsRefPtr<nsRange> range; |
michael@0 | 263 | nsresult rv = nsRange::CreateRange(aStartParent, aStartOffset, aEndParent, |
michael@0 | 264 | aEndOffset, getter_AddRefs(range)); |
michael@0 | 265 | range.forget(aRange); |
michael@0 | 266 | return rv; |
michael@0 | 267 | } |
michael@0 | 268 | |
michael@0 | 269 | /****************************************************** |
michael@0 | 270 | * nsISupports |
michael@0 | 271 | ******************************************************/ |
michael@0 | 272 | |
michael@0 | 273 | NS_IMPL_CYCLE_COLLECTING_ADDREF(nsRange) |
michael@0 | 274 | NS_IMPL_CYCLE_COLLECTING_RELEASE_WITH_LAST_RELEASE(nsRange, |
michael@0 | 275 | DoSetRange(nullptr, 0, nullptr, 0, nullptr)) |
michael@0 | 276 | |
michael@0 | 277 | // QueryInterface implementation for nsRange |
michael@0 | 278 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsRange) |
michael@0 | 279 | NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY |
michael@0 | 280 | NS_INTERFACE_MAP_ENTRY(nsIDOMRange) |
michael@0 | 281 | NS_INTERFACE_MAP_ENTRY(nsIMutationObserver) |
michael@0 | 282 | NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMRange) |
michael@0 | 283 | NS_INTERFACE_MAP_END |
michael@0 | 284 | |
michael@0 | 285 | NS_IMPL_CYCLE_COLLECTION_CLASS(nsRange) |
michael@0 | 286 | |
michael@0 | 287 | NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsRange) |
michael@0 | 288 | NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER |
michael@0 | 289 | NS_IMPL_CYCLE_COLLECTION_UNLINK(mOwner); |
michael@0 | 290 | tmp->Reset(); |
michael@0 | 291 | NS_IMPL_CYCLE_COLLECTION_UNLINK_END |
michael@0 | 292 | |
michael@0 | 293 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsRange) |
michael@0 | 294 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mOwner) |
michael@0 | 295 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mStartParent) |
michael@0 | 296 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mEndParent) |
michael@0 | 297 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRoot) |
michael@0 | 298 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS |
michael@0 | 299 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END |
michael@0 | 300 | |
michael@0 | 301 | NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(nsRange) |
michael@0 | 302 | NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER |
michael@0 | 303 | NS_IMPL_CYCLE_COLLECTION_TRACE_END |
michael@0 | 304 | |
michael@0 | 305 | static void MarkDescendants(nsINode* aNode) |
michael@0 | 306 | { |
michael@0 | 307 | // Set NodeIsDescendantOfCommonAncestorForRangeInSelection on aNode's |
michael@0 | 308 | // descendants unless aNode is already marked as a range common ancestor |
michael@0 | 309 | // or a descendant of one, in which case all of our descendants have the |
michael@0 | 310 | // bit set already. |
michael@0 | 311 | if (!aNode->IsSelectionDescendant()) { |
michael@0 | 312 | // don't set the Descendant bit on |aNode| itself |
michael@0 | 313 | nsINode* node = aNode->GetNextNode(aNode); |
michael@0 | 314 | while (node) { |
michael@0 | 315 | node->SetDescendantOfCommonAncestorForRangeInSelection(); |
michael@0 | 316 | if (!node->IsCommonAncestorForRangeInSelection()) { |
michael@0 | 317 | node = node->GetNextNode(aNode); |
michael@0 | 318 | } else { |
michael@0 | 319 | // optimize: skip this sub-tree since it's marked already. |
michael@0 | 320 | node = node->GetNextNonChildNode(aNode); |
michael@0 | 321 | } |
michael@0 | 322 | } |
michael@0 | 323 | } |
michael@0 | 324 | } |
michael@0 | 325 | |
michael@0 | 326 | static void UnmarkDescendants(nsINode* aNode) |
michael@0 | 327 | { |
michael@0 | 328 | // Unset NodeIsDescendantOfCommonAncestorForRangeInSelection on aNode's |
michael@0 | 329 | // descendants unless aNode is a descendant of another range common ancestor. |
michael@0 | 330 | // Also, exclude descendants of range common ancestors (but not the common |
michael@0 | 331 | // ancestor itself). |
michael@0 | 332 | if (!aNode->IsDescendantOfCommonAncestorForRangeInSelection()) { |
michael@0 | 333 | // we know |aNode| doesn't have any bit set |
michael@0 | 334 | nsINode* node = aNode->GetNextNode(aNode); |
michael@0 | 335 | while (node) { |
michael@0 | 336 | node->ClearDescendantOfCommonAncestorForRangeInSelection(); |
michael@0 | 337 | if (!node->IsCommonAncestorForRangeInSelection()) { |
michael@0 | 338 | node = node->GetNextNode(aNode); |
michael@0 | 339 | } else { |
michael@0 | 340 | // We found an ancestor of an overlapping range, skip its descendants. |
michael@0 | 341 | node = node->GetNextNonChildNode(aNode); |
michael@0 | 342 | } |
michael@0 | 343 | } |
michael@0 | 344 | } |
michael@0 | 345 | } |
michael@0 | 346 | |
michael@0 | 347 | void |
michael@0 | 348 | nsRange::RegisterCommonAncestor(nsINode* aNode) |
michael@0 | 349 | { |
michael@0 | 350 | NS_PRECONDITION(aNode, "bad arg"); |
michael@0 | 351 | NS_ASSERTION(IsInSelection(), "registering range not in selection"); |
michael@0 | 352 | |
michael@0 | 353 | MarkDescendants(aNode); |
michael@0 | 354 | |
michael@0 | 355 | RangeHashTable* ranges = |
michael@0 | 356 | static_cast<RangeHashTable*>(aNode->GetProperty(nsGkAtoms::range)); |
michael@0 | 357 | if (!ranges) { |
michael@0 | 358 | ranges = new RangeHashTable; |
michael@0 | 359 | aNode->SetProperty(nsGkAtoms::range, ranges, |
michael@0 | 360 | nsINode::DeleteProperty<nsRange::RangeHashTable>, true); |
michael@0 | 361 | } |
michael@0 | 362 | ranges->PutEntry(this); |
michael@0 | 363 | aNode->SetCommonAncestorForRangeInSelection(); |
michael@0 | 364 | } |
michael@0 | 365 | |
michael@0 | 366 | void |
michael@0 | 367 | nsRange::UnregisterCommonAncestor(nsINode* aNode) |
michael@0 | 368 | { |
michael@0 | 369 | NS_PRECONDITION(aNode, "bad arg"); |
michael@0 | 370 | NS_ASSERTION(aNode->IsCommonAncestorForRangeInSelection(), "wrong node"); |
michael@0 | 371 | RangeHashTable* ranges = |
michael@0 | 372 | static_cast<RangeHashTable*>(aNode->GetProperty(nsGkAtoms::range)); |
michael@0 | 373 | NS_ASSERTION(ranges->GetEntry(this), "unknown range"); |
michael@0 | 374 | |
michael@0 | 375 | if (ranges->Count() == 1) { |
michael@0 | 376 | aNode->ClearCommonAncestorForRangeInSelection(); |
michael@0 | 377 | aNode->DeleteProperty(nsGkAtoms::range); |
michael@0 | 378 | UnmarkDescendants(aNode); |
michael@0 | 379 | } else { |
michael@0 | 380 | ranges->RemoveEntry(this); |
michael@0 | 381 | } |
michael@0 | 382 | } |
michael@0 | 383 | |
michael@0 | 384 | /****************************************************** |
michael@0 | 385 | * nsIMutationObserver implementation |
michael@0 | 386 | ******************************************************/ |
michael@0 | 387 | |
michael@0 | 388 | void |
michael@0 | 389 | nsRange::CharacterDataChanged(nsIDocument* aDocument, |
michael@0 | 390 | nsIContent* aContent, |
michael@0 | 391 | CharacterDataChangeInfo* aInfo) |
michael@0 | 392 | { |
michael@0 | 393 | MOZ_ASSERT(mAssertNextInsertOrAppendIndex == -1, |
michael@0 | 394 | "splitText failed to notify insert/append?"); |
michael@0 | 395 | NS_ASSERTION(mIsPositioned, "shouldn't be notified if not positioned"); |
michael@0 | 396 | |
michael@0 | 397 | nsINode* newRoot = nullptr; |
michael@0 | 398 | nsINode* newStartNode = nullptr; |
michael@0 | 399 | nsINode* newEndNode = nullptr; |
michael@0 | 400 | uint32_t newStartOffset = 0; |
michael@0 | 401 | uint32_t newEndOffset = 0; |
michael@0 | 402 | |
michael@0 | 403 | if (aInfo->mDetails && |
michael@0 | 404 | aInfo->mDetails->mType == CharacterDataChangeInfo::Details::eSplit) { |
michael@0 | 405 | // If the splitted text node is immediately before a range boundary point |
michael@0 | 406 | // that refers to a child index (i.e. its parent is the boundary container) |
michael@0 | 407 | // then we need to increment the corresponding offset to account for the new |
michael@0 | 408 | // text node that will be inserted. If so, we need to prevent the next |
michael@0 | 409 | // ContentInserted or ContentAppended for this range from incrementing it |
michael@0 | 410 | // again (when the new text node is notified). |
michael@0 | 411 | nsINode* parentNode = aContent->GetParentNode(); |
michael@0 | 412 | int32_t index = -1; |
michael@0 | 413 | if (parentNode == mEndParent && mEndOffset > 0 && |
michael@0 | 414 | (index = parentNode->IndexOf(aContent)) + 1 == mEndOffset) { |
michael@0 | 415 | ++mEndOffset; |
michael@0 | 416 | mEndOffsetWasIncremented = true; |
michael@0 | 417 | } |
michael@0 | 418 | if (parentNode == mStartParent && mStartOffset > 0 && |
michael@0 | 419 | (index != -1 ? index : parentNode->IndexOf(aContent)) + 1 == mStartOffset) { |
michael@0 | 420 | ++mStartOffset; |
michael@0 | 421 | mStartOffsetWasIncremented = true; |
michael@0 | 422 | } |
michael@0 | 423 | #ifdef DEBUG |
michael@0 | 424 | if (mStartOffsetWasIncremented || mEndOffsetWasIncremented) { |
michael@0 | 425 | mAssertNextInsertOrAppendIndex = |
michael@0 | 426 | (mStartOffsetWasIncremented ? mStartOffset : mEndOffset) - 1; |
michael@0 | 427 | mAssertNextInsertOrAppendNode = aInfo->mDetails->mNextSibling; |
michael@0 | 428 | } |
michael@0 | 429 | #endif |
michael@0 | 430 | } |
michael@0 | 431 | |
michael@0 | 432 | // If the changed node contains our start boundary and the change starts |
michael@0 | 433 | // before the boundary we'll need to adjust the offset. |
michael@0 | 434 | if (aContent == mStartParent && |
michael@0 | 435 | aInfo->mChangeStart < static_cast<uint32_t>(mStartOffset)) { |
michael@0 | 436 | if (aInfo->mDetails) { |
michael@0 | 437 | // splitText(), aInfo->mDetails->mNextSibling is the new text node |
michael@0 | 438 | NS_ASSERTION(aInfo->mDetails->mType == |
michael@0 | 439 | CharacterDataChangeInfo::Details::eSplit, |
michael@0 | 440 | "only a split can start before the end"); |
michael@0 | 441 | NS_ASSERTION(static_cast<uint32_t>(mStartOffset) <= aInfo->mChangeEnd + 1, |
michael@0 | 442 | "mStartOffset is beyond the end of this node"); |
michael@0 | 443 | newStartOffset = static_cast<uint32_t>(mStartOffset) - aInfo->mChangeStart; |
michael@0 | 444 | newStartNode = aInfo->mDetails->mNextSibling; |
michael@0 | 445 | if (MOZ_UNLIKELY(aContent == mRoot)) { |
michael@0 | 446 | newRoot = IsValidBoundary(newStartNode); |
michael@0 | 447 | } |
michael@0 | 448 | |
michael@0 | 449 | bool isCommonAncestor = IsInSelection() && mStartParent == mEndParent; |
michael@0 | 450 | if (isCommonAncestor) { |
michael@0 | 451 | UnregisterCommonAncestor(mStartParent); |
michael@0 | 452 | RegisterCommonAncestor(newStartNode); |
michael@0 | 453 | } |
michael@0 | 454 | if (mStartParent->IsDescendantOfCommonAncestorForRangeInSelection()) { |
michael@0 | 455 | newStartNode->SetDescendantOfCommonAncestorForRangeInSelection(); |
michael@0 | 456 | } |
michael@0 | 457 | } else { |
michael@0 | 458 | // If boundary is inside changed text, position it before change |
michael@0 | 459 | // else adjust start offset for the change in length. |
michael@0 | 460 | mStartOffset = static_cast<uint32_t>(mStartOffset) <= aInfo->mChangeEnd ? |
michael@0 | 461 | aInfo->mChangeStart : |
michael@0 | 462 | mStartOffset + aInfo->mChangeStart - aInfo->mChangeEnd + |
michael@0 | 463 | aInfo->mReplaceLength; |
michael@0 | 464 | } |
michael@0 | 465 | } |
michael@0 | 466 | |
michael@0 | 467 | // Do the same thing for the end boundary, except for splitText of a node |
michael@0 | 468 | // with no parent then only switch to the new node if the start boundary |
michael@0 | 469 | // did so too (otherwise the range would end up with disconnected nodes). |
michael@0 | 470 | if (aContent == mEndParent && |
michael@0 | 471 | aInfo->mChangeStart < static_cast<uint32_t>(mEndOffset)) { |
michael@0 | 472 | if (aInfo->mDetails && (aContent->GetParentNode() || newStartNode)) { |
michael@0 | 473 | // splitText(), aInfo->mDetails->mNextSibling is the new text node |
michael@0 | 474 | NS_ASSERTION(aInfo->mDetails->mType == |
michael@0 | 475 | CharacterDataChangeInfo::Details::eSplit, |
michael@0 | 476 | "only a split can start before the end"); |
michael@0 | 477 | NS_ASSERTION(static_cast<uint32_t>(mEndOffset) <= aInfo->mChangeEnd + 1, |
michael@0 | 478 | "mEndOffset is beyond the end of this node"); |
michael@0 | 479 | newEndOffset = static_cast<uint32_t>(mEndOffset) - aInfo->mChangeStart; |
michael@0 | 480 | newEndNode = aInfo->mDetails->mNextSibling; |
michael@0 | 481 | |
michael@0 | 482 | bool isCommonAncestor = IsInSelection() && mStartParent == mEndParent; |
michael@0 | 483 | if (isCommonAncestor && !newStartNode) { |
michael@0 | 484 | // The split occurs inside the range. |
michael@0 | 485 | UnregisterCommonAncestor(mStartParent); |
michael@0 | 486 | RegisterCommonAncestor(mStartParent->GetParentNode()); |
michael@0 | 487 | newEndNode->SetDescendantOfCommonAncestorForRangeInSelection(); |
michael@0 | 488 | } else if (mEndParent->IsDescendantOfCommonAncestorForRangeInSelection()) { |
michael@0 | 489 | newEndNode->SetDescendantOfCommonAncestorForRangeInSelection(); |
michael@0 | 490 | } |
michael@0 | 491 | } else { |
michael@0 | 492 | mEndOffset = static_cast<uint32_t>(mEndOffset) <= aInfo->mChangeEnd ? |
michael@0 | 493 | aInfo->mChangeStart : |
michael@0 | 494 | mEndOffset + aInfo->mChangeStart - aInfo->mChangeEnd + |
michael@0 | 495 | aInfo->mReplaceLength; |
michael@0 | 496 | } |
michael@0 | 497 | } |
michael@0 | 498 | |
michael@0 | 499 | if (aInfo->mDetails && |
michael@0 | 500 | aInfo->mDetails->mType == CharacterDataChangeInfo::Details::eMerge) { |
michael@0 | 501 | // normalize(), aInfo->mDetails->mNextSibling is the merged text node |
michael@0 | 502 | // that will be removed |
michael@0 | 503 | nsIContent* removed = aInfo->mDetails->mNextSibling; |
michael@0 | 504 | if (removed == mStartParent) { |
michael@0 | 505 | newStartOffset = static_cast<uint32_t>(mStartOffset) + aInfo->mChangeStart; |
michael@0 | 506 | newStartNode = aContent; |
michael@0 | 507 | if (MOZ_UNLIKELY(removed == mRoot)) { |
michael@0 | 508 | newRoot = IsValidBoundary(newStartNode); |
michael@0 | 509 | } |
michael@0 | 510 | } |
michael@0 | 511 | if (removed == mEndParent) { |
michael@0 | 512 | newEndOffset = static_cast<uint32_t>(mEndOffset) + aInfo->mChangeStart; |
michael@0 | 513 | newEndNode = aContent; |
michael@0 | 514 | if (MOZ_UNLIKELY(removed == mRoot)) { |
michael@0 | 515 | newRoot = IsValidBoundary(newEndNode); |
michael@0 | 516 | } |
michael@0 | 517 | } |
michael@0 | 518 | // When the removed text node's parent is one of our boundary nodes we may |
michael@0 | 519 | // need to adjust the offset to account for the removed node. However, |
michael@0 | 520 | // there will also be a ContentRemoved notification later so the only cases |
michael@0 | 521 | // we need to handle here is when the removed node is the text node after |
michael@0 | 522 | // the boundary. (The m*Offset > 0 check is an optimization - a boundary |
michael@0 | 523 | // point before the first child is never affected by normalize().) |
michael@0 | 524 | nsINode* parentNode = aContent->GetParentNode(); |
michael@0 | 525 | if (parentNode == mStartParent && mStartOffset > 0 && |
michael@0 | 526 | uint32_t(mStartOffset) < parentNode->GetChildCount() && |
michael@0 | 527 | removed == parentNode->GetChildAt(mStartOffset)) { |
michael@0 | 528 | newStartNode = aContent; |
michael@0 | 529 | newStartOffset = aInfo->mChangeStart; |
michael@0 | 530 | } |
michael@0 | 531 | if (parentNode == mEndParent && mEndOffset > 0 && |
michael@0 | 532 | uint32_t(mEndOffset) < parentNode->GetChildCount() && |
michael@0 | 533 | removed == parentNode->GetChildAt(mEndOffset)) { |
michael@0 | 534 | newEndNode = aContent; |
michael@0 | 535 | newEndOffset = aInfo->mChangeEnd; |
michael@0 | 536 | } |
michael@0 | 537 | } |
michael@0 | 538 | |
michael@0 | 539 | if (newStartNode || newEndNode) { |
michael@0 | 540 | if (!newStartNode) { |
michael@0 | 541 | newStartNode = mStartParent; |
michael@0 | 542 | newStartOffset = mStartOffset; |
michael@0 | 543 | } |
michael@0 | 544 | if (!newEndNode) { |
michael@0 | 545 | newEndNode = mEndParent; |
michael@0 | 546 | newEndOffset = mEndOffset; |
michael@0 | 547 | } |
michael@0 | 548 | DoSetRange(newStartNode, newStartOffset, newEndNode, newEndOffset, |
michael@0 | 549 | newRoot ? newRoot : mRoot.get(), |
michael@0 | 550 | !newEndNode->GetParentNode() || !newStartNode->GetParentNode()); |
michael@0 | 551 | } |
michael@0 | 552 | } |
michael@0 | 553 | |
michael@0 | 554 | void |
michael@0 | 555 | nsRange::ContentAppended(nsIDocument* aDocument, |
michael@0 | 556 | nsIContent* aContainer, |
michael@0 | 557 | nsIContent* aFirstNewContent, |
michael@0 | 558 | int32_t aNewIndexInContainer) |
michael@0 | 559 | { |
michael@0 | 560 | NS_ASSERTION(mIsPositioned, "shouldn't be notified if not positioned"); |
michael@0 | 561 | |
michael@0 | 562 | nsINode* container = NODE_FROM(aContainer, aDocument); |
michael@0 | 563 | if (container->IsSelectionDescendant() && IsInSelection()) { |
michael@0 | 564 | nsINode* child = aFirstNewContent; |
michael@0 | 565 | while (child) { |
michael@0 | 566 | if (!child->IsDescendantOfCommonAncestorForRangeInSelection()) { |
michael@0 | 567 | MarkDescendants(child); |
michael@0 | 568 | child->SetDescendantOfCommonAncestorForRangeInSelection(); |
michael@0 | 569 | } |
michael@0 | 570 | child = child->GetNextSibling(); |
michael@0 | 571 | } |
michael@0 | 572 | } |
michael@0 | 573 | |
michael@0 | 574 | if (mStartOffsetWasIncremented || mEndOffsetWasIncremented) { |
michael@0 | 575 | MOZ_ASSERT(mAssertNextInsertOrAppendIndex == aNewIndexInContainer); |
michael@0 | 576 | MOZ_ASSERT(mAssertNextInsertOrAppendNode == aFirstNewContent); |
michael@0 | 577 | MOZ_ASSERT(aFirstNewContent->IsNodeOfType(nsINode::eDATA_NODE)); |
michael@0 | 578 | mStartOffsetWasIncremented = mEndOffsetWasIncremented = false; |
michael@0 | 579 | #ifdef DEBUG |
michael@0 | 580 | mAssertNextInsertOrAppendIndex = -1; |
michael@0 | 581 | mAssertNextInsertOrAppendNode = nullptr; |
michael@0 | 582 | #endif |
michael@0 | 583 | } |
michael@0 | 584 | } |
michael@0 | 585 | |
michael@0 | 586 | void |
michael@0 | 587 | nsRange::ContentInserted(nsIDocument* aDocument, |
michael@0 | 588 | nsIContent* aContainer, |
michael@0 | 589 | nsIContent* aChild, |
michael@0 | 590 | int32_t aIndexInContainer) |
michael@0 | 591 | { |
michael@0 | 592 | NS_ASSERTION(mIsPositioned, "shouldn't be notified if not positioned"); |
michael@0 | 593 | |
michael@0 | 594 | nsINode* container = NODE_FROM(aContainer, aDocument); |
michael@0 | 595 | |
michael@0 | 596 | // Adjust position if a sibling was inserted. |
michael@0 | 597 | if (container == mStartParent && aIndexInContainer < mStartOffset && |
michael@0 | 598 | !mStartOffsetWasIncremented) { |
michael@0 | 599 | ++mStartOffset; |
michael@0 | 600 | } |
michael@0 | 601 | if (container == mEndParent && aIndexInContainer < mEndOffset && |
michael@0 | 602 | !mEndOffsetWasIncremented) { |
michael@0 | 603 | ++mEndOffset; |
michael@0 | 604 | } |
michael@0 | 605 | if (container->IsSelectionDescendant() && |
michael@0 | 606 | !aChild->IsDescendantOfCommonAncestorForRangeInSelection()) { |
michael@0 | 607 | MarkDescendants(aChild); |
michael@0 | 608 | aChild->SetDescendantOfCommonAncestorForRangeInSelection(); |
michael@0 | 609 | } |
michael@0 | 610 | |
michael@0 | 611 | if (mStartOffsetWasIncremented || mEndOffsetWasIncremented) { |
michael@0 | 612 | MOZ_ASSERT(mAssertNextInsertOrAppendIndex == aIndexInContainer); |
michael@0 | 613 | MOZ_ASSERT(mAssertNextInsertOrAppendNode == aChild); |
michael@0 | 614 | MOZ_ASSERT(aChild->IsNodeOfType(nsINode::eDATA_NODE)); |
michael@0 | 615 | mStartOffsetWasIncremented = mEndOffsetWasIncremented = false; |
michael@0 | 616 | #ifdef DEBUG |
michael@0 | 617 | mAssertNextInsertOrAppendIndex = -1; |
michael@0 | 618 | mAssertNextInsertOrAppendNode = nullptr; |
michael@0 | 619 | #endif |
michael@0 | 620 | } |
michael@0 | 621 | } |
michael@0 | 622 | |
michael@0 | 623 | void |
michael@0 | 624 | nsRange::ContentRemoved(nsIDocument* aDocument, |
michael@0 | 625 | nsIContent* aContainer, |
michael@0 | 626 | nsIContent* aChild, |
michael@0 | 627 | int32_t aIndexInContainer, |
michael@0 | 628 | nsIContent* aPreviousSibling) |
michael@0 | 629 | { |
michael@0 | 630 | NS_ASSERTION(mIsPositioned, "shouldn't be notified if not positioned"); |
michael@0 | 631 | MOZ_ASSERT(!mStartOffsetWasIncremented && !mEndOffsetWasIncremented && |
michael@0 | 632 | mAssertNextInsertOrAppendIndex == -1, |
michael@0 | 633 | "splitText failed to notify insert/append?"); |
michael@0 | 634 | |
michael@0 | 635 | nsINode* container = NODE_FROM(aContainer, aDocument); |
michael@0 | 636 | bool gravitateStart = false; |
michael@0 | 637 | bool gravitateEnd = false; |
michael@0 | 638 | bool didCheckStartParentDescendant = false; |
michael@0 | 639 | |
michael@0 | 640 | // Adjust position if a sibling was removed... |
michael@0 | 641 | if (container == mStartParent) { |
michael@0 | 642 | if (aIndexInContainer < mStartOffset) { |
michael@0 | 643 | --mStartOffset; |
michael@0 | 644 | } |
michael@0 | 645 | } else { // ...or gravitate if an ancestor was removed. |
michael@0 | 646 | didCheckStartParentDescendant = true; |
michael@0 | 647 | gravitateStart = nsContentUtils::ContentIsDescendantOf(mStartParent, aChild); |
michael@0 | 648 | } |
michael@0 | 649 | |
michael@0 | 650 | // Do same thing for end boundry. |
michael@0 | 651 | if (container == mEndParent) { |
michael@0 | 652 | if (aIndexInContainer < mEndOffset) { |
michael@0 | 653 | --mEndOffset; |
michael@0 | 654 | } |
michael@0 | 655 | } else if (didCheckStartParentDescendant && mStartParent == mEndParent) { |
michael@0 | 656 | gravitateEnd = gravitateStart; |
michael@0 | 657 | } else { |
michael@0 | 658 | gravitateEnd = nsContentUtils::ContentIsDescendantOf(mEndParent, aChild); |
michael@0 | 659 | } |
michael@0 | 660 | |
michael@0 | 661 | if (!mEnableGravitationOnElementRemoval) { |
michael@0 | 662 | // Do not gravitate. |
michael@0 | 663 | return; |
michael@0 | 664 | } |
michael@0 | 665 | |
michael@0 | 666 | if (gravitateStart || gravitateEnd) { |
michael@0 | 667 | DoSetRange(gravitateStart ? container : mStartParent.get(), |
michael@0 | 668 | gravitateStart ? aIndexInContainer : mStartOffset, |
michael@0 | 669 | gravitateEnd ? container : mEndParent.get(), |
michael@0 | 670 | gravitateEnd ? aIndexInContainer : mEndOffset, |
michael@0 | 671 | mRoot); |
michael@0 | 672 | } |
michael@0 | 673 | if (container->IsSelectionDescendant() && |
michael@0 | 674 | aChild->IsDescendantOfCommonAncestorForRangeInSelection()) { |
michael@0 | 675 | aChild->ClearDescendantOfCommonAncestorForRangeInSelection(); |
michael@0 | 676 | UnmarkDescendants(aChild); |
michael@0 | 677 | } |
michael@0 | 678 | } |
michael@0 | 679 | |
michael@0 | 680 | void |
michael@0 | 681 | nsRange::ParentChainChanged(nsIContent *aContent) |
michael@0 | 682 | { |
michael@0 | 683 | MOZ_ASSERT(!mStartOffsetWasIncremented && !mEndOffsetWasIncremented && |
michael@0 | 684 | mAssertNextInsertOrAppendIndex == -1, |
michael@0 | 685 | "splitText failed to notify insert/append?"); |
michael@0 | 686 | NS_ASSERTION(mRoot == aContent, "Wrong ParentChainChanged notification?"); |
michael@0 | 687 | nsINode* newRoot = IsValidBoundary(mStartParent); |
michael@0 | 688 | NS_ASSERTION(newRoot, "No valid boundary or root found!"); |
michael@0 | 689 | if (newRoot != IsValidBoundary(mEndParent)) { |
michael@0 | 690 | // Sometimes ordering involved in cycle collection can lead to our |
michael@0 | 691 | // start parent and/or end parent being disconnected from our root |
michael@0 | 692 | // without our getting a ContentRemoved notification. |
michael@0 | 693 | // See bug 846096 for more details. |
michael@0 | 694 | NS_ASSERTION(mEndParent->IsInNativeAnonymousSubtree(), |
michael@0 | 695 | "This special case should happen only with " |
michael@0 | 696 | "native-anonymous content"); |
michael@0 | 697 | // When that happens, bail out and set pointers to null; since we're |
michael@0 | 698 | // in cycle collection and unreachable it shouldn't matter. |
michael@0 | 699 | Reset(); |
michael@0 | 700 | return; |
michael@0 | 701 | } |
michael@0 | 702 | // This is safe without holding a strong ref to self as long as the change |
michael@0 | 703 | // of mRoot is the last thing in DoSetRange. |
michael@0 | 704 | DoSetRange(mStartParent, mStartOffset, mEndParent, mEndOffset, newRoot); |
michael@0 | 705 | } |
michael@0 | 706 | |
michael@0 | 707 | /****************************************************** |
michael@0 | 708 | * Utilities for comparing points: API from nsIDOMRange |
michael@0 | 709 | ******************************************************/ |
michael@0 | 710 | NS_IMETHODIMP |
michael@0 | 711 | nsRange::IsPointInRange(nsIDOMNode* aParent, int32_t aOffset, bool* aResult) |
michael@0 | 712 | { |
michael@0 | 713 | nsCOMPtr<nsINode> parent = do_QueryInterface(aParent); |
michael@0 | 714 | if (!parent) { |
michael@0 | 715 | return NS_ERROR_DOM_NOT_OBJECT_ERR; |
michael@0 | 716 | } |
michael@0 | 717 | |
michael@0 | 718 | ErrorResult rv; |
michael@0 | 719 | *aResult = IsPointInRange(*parent, aOffset, rv); |
michael@0 | 720 | return rv.ErrorCode(); |
michael@0 | 721 | } |
michael@0 | 722 | |
michael@0 | 723 | bool |
michael@0 | 724 | nsRange::IsPointInRange(nsINode& aParent, uint32_t aOffset, ErrorResult& aRv) |
michael@0 | 725 | { |
michael@0 | 726 | uint16_t compareResult = ComparePoint(aParent, aOffset, aRv); |
michael@0 | 727 | // If the node isn't in the range's document, it clearly isn't in the range. |
michael@0 | 728 | if (aRv.ErrorCode() == NS_ERROR_DOM_WRONG_DOCUMENT_ERR) { |
michael@0 | 729 | aRv = NS_OK; |
michael@0 | 730 | return false; |
michael@0 | 731 | } |
michael@0 | 732 | |
michael@0 | 733 | return compareResult == 0; |
michael@0 | 734 | } |
michael@0 | 735 | |
michael@0 | 736 | // returns -1 if point is before range, 0 if point is in range, |
michael@0 | 737 | // 1 if point is after range. |
michael@0 | 738 | NS_IMETHODIMP |
michael@0 | 739 | nsRange::ComparePoint(nsIDOMNode* aParent, int32_t aOffset, int16_t* aResult) |
michael@0 | 740 | { |
michael@0 | 741 | nsCOMPtr<nsINode> parent = do_QueryInterface(aParent); |
michael@0 | 742 | NS_ENSURE_TRUE(parent, NS_ERROR_DOM_HIERARCHY_REQUEST_ERR); |
michael@0 | 743 | |
michael@0 | 744 | ErrorResult rv; |
michael@0 | 745 | *aResult = ComparePoint(*parent, aOffset, rv); |
michael@0 | 746 | return rv.ErrorCode(); |
michael@0 | 747 | } |
michael@0 | 748 | |
michael@0 | 749 | int16_t |
michael@0 | 750 | nsRange::ComparePoint(nsINode& aParent, uint32_t aOffset, ErrorResult& aRv) |
michael@0 | 751 | { |
michael@0 | 752 | // our range is in a good state? |
michael@0 | 753 | if (!mIsPositioned) { |
michael@0 | 754 | aRv.Throw(NS_ERROR_NOT_INITIALIZED); |
michael@0 | 755 | return 0; |
michael@0 | 756 | } |
michael@0 | 757 | |
michael@0 | 758 | if (!nsContentUtils::ContentIsDescendantOf(&aParent, mRoot)) { |
michael@0 | 759 | aRv.Throw(NS_ERROR_DOM_WRONG_DOCUMENT_ERR); |
michael@0 | 760 | return 0; |
michael@0 | 761 | } |
michael@0 | 762 | |
michael@0 | 763 | if (aParent.NodeType() == nsIDOMNode::DOCUMENT_TYPE_NODE) { |
michael@0 | 764 | aRv.Throw(NS_ERROR_DOM_INVALID_NODE_TYPE_ERR); |
michael@0 | 765 | return 0; |
michael@0 | 766 | } |
michael@0 | 767 | |
michael@0 | 768 | if (aOffset > aParent.Length()) { |
michael@0 | 769 | aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR); |
michael@0 | 770 | return 0; |
michael@0 | 771 | } |
michael@0 | 772 | |
michael@0 | 773 | int32_t cmp; |
michael@0 | 774 | if ((cmp = nsContentUtils::ComparePoints(&aParent, aOffset, |
michael@0 | 775 | mStartParent, mStartOffset)) <= 0) { |
michael@0 | 776 | |
michael@0 | 777 | return cmp; |
michael@0 | 778 | } |
michael@0 | 779 | if (nsContentUtils::ComparePoints(mEndParent, mEndOffset, |
michael@0 | 780 | &aParent, aOffset) == -1) { |
michael@0 | 781 | return 1; |
michael@0 | 782 | } |
michael@0 | 783 | |
michael@0 | 784 | return 0; |
michael@0 | 785 | } |
michael@0 | 786 | |
michael@0 | 787 | NS_IMETHODIMP |
michael@0 | 788 | nsRange::IntersectsNode(nsIDOMNode* aNode, bool* aResult) |
michael@0 | 789 | { |
michael@0 | 790 | *aResult = false; |
michael@0 | 791 | |
michael@0 | 792 | nsCOMPtr<nsINode> node = do_QueryInterface(aNode); |
michael@0 | 793 | // TODO: This should throw a TypeError. |
michael@0 | 794 | NS_ENSURE_ARG(node); |
michael@0 | 795 | |
michael@0 | 796 | ErrorResult rv; |
michael@0 | 797 | *aResult = IntersectsNode(*node, rv); |
michael@0 | 798 | return rv.ErrorCode(); |
michael@0 | 799 | } |
michael@0 | 800 | |
michael@0 | 801 | bool |
michael@0 | 802 | nsRange::IntersectsNode(nsINode& aNode, ErrorResult& aRv) |
michael@0 | 803 | { |
michael@0 | 804 | if (!mIsPositioned) { |
michael@0 | 805 | aRv.Throw(NS_ERROR_NOT_INITIALIZED); |
michael@0 | 806 | return false; |
michael@0 | 807 | } |
michael@0 | 808 | |
michael@0 | 809 | // Step 3. |
michael@0 | 810 | nsINode* parent = aNode.GetParentNode(); |
michael@0 | 811 | if (!parent) { |
michael@0 | 812 | // Steps 2 and 4. |
michael@0 | 813 | // |parent| is null, so |node|'s root is |node| itself. |
michael@0 | 814 | return GetRoot() == &aNode; |
michael@0 | 815 | } |
michael@0 | 816 | |
michael@0 | 817 | // Step 5. |
michael@0 | 818 | int32_t nodeIndex = parent->IndexOf(&aNode); |
michael@0 | 819 | |
michael@0 | 820 | // Steps 6-7. |
michael@0 | 821 | // Note: if disconnected is true, ComparePoints returns 1. |
michael@0 | 822 | bool disconnected = false; |
michael@0 | 823 | bool result = nsContentUtils::ComparePoints(mStartParent, mStartOffset, |
michael@0 | 824 | parent, nodeIndex + 1, |
michael@0 | 825 | &disconnected) < 0 && |
michael@0 | 826 | nsContentUtils::ComparePoints(parent, nodeIndex, |
michael@0 | 827 | mEndParent, mEndOffset, |
michael@0 | 828 | &disconnected) < 0; |
michael@0 | 829 | |
michael@0 | 830 | // Step 2. |
michael@0 | 831 | if (disconnected) { |
michael@0 | 832 | result = false; |
michael@0 | 833 | } |
michael@0 | 834 | return result; |
michael@0 | 835 | } |
michael@0 | 836 | |
michael@0 | 837 | /****************************************************** |
michael@0 | 838 | * Private helper routines |
michael@0 | 839 | ******************************************************/ |
michael@0 | 840 | |
michael@0 | 841 | // It's important that all setting of the range start/end points |
michael@0 | 842 | // go through this function, which will do all the right voodoo |
michael@0 | 843 | // for content notification of range ownership. |
michael@0 | 844 | // Calling DoSetRange with either parent argument null will collapse |
michael@0 | 845 | // the range to have both endpoints point to the other node |
michael@0 | 846 | void |
michael@0 | 847 | nsRange::DoSetRange(nsINode* aStartN, int32_t aStartOffset, |
michael@0 | 848 | nsINode* aEndN, int32_t aEndOffset, |
michael@0 | 849 | nsINode* aRoot, bool aNotInsertedYet) |
michael@0 | 850 | { |
michael@0 | 851 | NS_PRECONDITION((aStartN && aEndN && aRoot) || |
michael@0 | 852 | (!aStartN && !aEndN && !aRoot), |
michael@0 | 853 | "Set all or none"); |
michael@0 | 854 | NS_PRECONDITION(!aRoot || aNotInsertedYet || |
michael@0 | 855 | (nsContentUtils::ContentIsDescendantOf(aStartN, aRoot) && |
michael@0 | 856 | nsContentUtils::ContentIsDescendantOf(aEndN, aRoot) && |
michael@0 | 857 | aRoot == IsValidBoundary(aStartN) && |
michael@0 | 858 | aRoot == IsValidBoundary(aEndN)), |
michael@0 | 859 | "Wrong root"); |
michael@0 | 860 | NS_PRECONDITION(!aRoot || |
michael@0 | 861 | (aStartN->IsNodeOfType(nsINode::eCONTENT) && |
michael@0 | 862 | aEndN->IsNodeOfType(nsINode::eCONTENT) && |
michael@0 | 863 | aRoot == |
michael@0 | 864 | static_cast<nsIContent*>(aStartN)->GetBindingParent() && |
michael@0 | 865 | aRoot == |
michael@0 | 866 | static_cast<nsIContent*>(aEndN)->GetBindingParent()) || |
michael@0 | 867 | (!aRoot->GetParentNode() && |
michael@0 | 868 | (aRoot->IsNodeOfType(nsINode::eDOCUMENT) || |
michael@0 | 869 | aRoot->IsNodeOfType(nsINode::eATTRIBUTE) || |
michael@0 | 870 | aRoot->IsNodeOfType(nsINode::eDOCUMENT_FRAGMENT) || |
michael@0 | 871 | /*For backward compatibility*/ |
michael@0 | 872 | aRoot->IsNodeOfType(nsINode::eCONTENT))), |
michael@0 | 873 | "Bad root"); |
michael@0 | 874 | |
michael@0 | 875 | if (mRoot != aRoot) { |
michael@0 | 876 | if (mRoot) { |
michael@0 | 877 | mRoot->RemoveMutationObserver(this); |
michael@0 | 878 | } |
michael@0 | 879 | if (aRoot) { |
michael@0 | 880 | aRoot->AddMutationObserver(this); |
michael@0 | 881 | } |
michael@0 | 882 | } |
michael@0 | 883 | bool checkCommonAncestor = (mStartParent != aStartN || mEndParent != aEndN) && |
michael@0 | 884 | IsInSelection() && !aNotInsertedYet; |
michael@0 | 885 | nsINode* oldCommonAncestor = checkCommonAncestor ? GetCommonAncestor() : nullptr; |
michael@0 | 886 | mStartParent = aStartN; |
michael@0 | 887 | mStartOffset = aStartOffset; |
michael@0 | 888 | mEndParent = aEndN; |
michael@0 | 889 | mEndOffset = aEndOffset; |
michael@0 | 890 | mIsPositioned = !!mStartParent; |
michael@0 | 891 | if (checkCommonAncestor) { |
michael@0 | 892 | nsINode* newCommonAncestor = GetCommonAncestor(); |
michael@0 | 893 | if (newCommonAncestor != oldCommonAncestor) { |
michael@0 | 894 | if (oldCommonAncestor) { |
michael@0 | 895 | UnregisterCommonAncestor(oldCommonAncestor); |
michael@0 | 896 | } |
michael@0 | 897 | if (newCommonAncestor) { |
michael@0 | 898 | RegisterCommonAncestor(newCommonAncestor); |
michael@0 | 899 | } else { |
michael@0 | 900 | NS_ASSERTION(!mIsPositioned, "unexpected disconnected nodes"); |
michael@0 | 901 | mInSelection = false; |
michael@0 | 902 | } |
michael@0 | 903 | } |
michael@0 | 904 | } |
michael@0 | 905 | |
michael@0 | 906 | // This needs to be the last thing this function does. See comment |
michael@0 | 907 | // in ParentChainChanged. |
michael@0 | 908 | mRoot = aRoot; |
michael@0 | 909 | } |
michael@0 | 910 | |
michael@0 | 911 | static int32_t |
michael@0 | 912 | IndexOf(nsINode* aChild) |
michael@0 | 913 | { |
michael@0 | 914 | nsINode* parent = aChild->GetParentNode(); |
michael@0 | 915 | |
michael@0 | 916 | return parent ? parent->IndexOf(aChild) : -1; |
michael@0 | 917 | } |
michael@0 | 918 | |
michael@0 | 919 | nsINode* |
michael@0 | 920 | nsRange::GetCommonAncestor() const |
michael@0 | 921 | { |
michael@0 | 922 | return mIsPositioned ? |
michael@0 | 923 | nsContentUtils::GetCommonAncestor(mStartParent, mEndParent) : |
michael@0 | 924 | nullptr; |
michael@0 | 925 | } |
michael@0 | 926 | |
michael@0 | 927 | void |
michael@0 | 928 | nsRange::Reset() |
michael@0 | 929 | { |
michael@0 | 930 | DoSetRange(nullptr, 0, nullptr, 0, nullptr); |
michael@0 | 931 | } |
michael@0 | 932 | |
michael@0 | 933 | /****************************************************** |
michael@0 | 934 | * public functionality |
michael@0 | 935 | ******************************************************/ |
michael@0 | 936 | |
michael@0 | 937 | NS_IMETHODIMP |
michael@0 | 938 | nsRange::GetStartContainer(nsIDOMNode** aStartParent) |
michael@0 | 939 | { |
michael@0 | 940 | if (!mIsPositioned) |
michael@0 | 941 | return NS_ERROR_NOT_INITIALIZED; |
michael@0 | 942 | |
michael@0 | 943 | return CallQueryInterface(mStartParent, aStartParent); |
michael@0 | 944 | } |
michael@0 | 945 | |
michael@0 | 946 | nsINode* |
michael@0 | 947 | nsRange::GetStartContainer(ErrorResult& aRv) const |
michael@0 | 948 | { |
michael@0 | 949 | if (!mIsPositioned) { |
michael@0 | 950 | aRv.Throw(NS_ERROR_NOT_INITIALIZED); |
michael@0 | 951 | return nullptr; |
michael@0 | 952 | } |
michael@0 | 953 | |
michael@0 | 954 | return mStartParent; |
michael@0 | 955 | } |
michael@0 | 956 | |
michael@0 | 957 | NS_IMETHODIMP |
michael@0 | 958 | nsRange::GetStartOffset(int32_t* aStartOffset) |
michael@0 | 959 | { |
michael@0 | 960 | if (!mIsPositioned) |
michael@0 | 961 | return NS_ERROR_NOT_INITIALIZED; |
michael@0 | 962 | |
michael@0 | 963 | *aStartOffset = mStartOffset; |
michael@0 | 964 | |
michael@0 | 965 | return NS_OK; |
michael@0 | 966 | } |
michael@0 | 967 | |
michael@0 | 968 | uint32_t |
michael@0 | 969 | nsRange::GetStartOffset(ErrorResult& aRv) const |
michael@0 | 970 | { |
michael@0 | 971 | if (!mIsPositioned) { |
michael@0 | 972 | aRv.Throw(NS_ERROR_NOT_INITIALIZED); |
michael@0 | 973 | return 0; |
michael@0 | 974 | } |
michael@0 | 975 | |
michael@0 | 976 | return mStartOffset; |
michael@0 | 977 | } |
michael@0 | 978 | |
michael@0 | 979 | NS_IMETHODIMP |
michael@0 | 980 | nsRange::GetEndContainer(nsIDOMNode** aEndParent) |
michael@0 | 981 | { |
michael@0 | 982 | if (!mIsPositioned) |
michael@0 | 983 | return NS_ERROR_NOT_INITIALIZED; |
michael@0 | 984 | |
michael@0 | 985 | return CallQueryInterface(mEndParent, aEndParent); |
michael@0 | 986 | } |
michael@0 | 987 | |
michael@0 | 988 | nsINode* |
michael@0 | 989 | nsRange::GetEndContainer(ErrorResult& aRv) const |
michael@0 | 990 | { |
michael@0 | 991 | if (!mIsPositioned) { |
michael@0 | 992 | aRv.Throw(NS_ERROR_NOT_INITIALIZED); |
michael@0 | 993 | return nullptr; |
michael@0 | 994 | } |
michael@0 | 995 | |
michael@0 | 996 | return mEndParent; |
michael@0 | 997 | } |
michael@0 | 998 | |
michael@0 | 999 | NS_IMETHODIMP |
michael@0 | 1000 | nsRange::GetEndOffset(int32_t* aEndOffset) |
michael@0 | 1001 | { |
michael@0 | 1002 | if (!mIsPositioned) |
michael@0 | 1003 | return NS_ERROR_NOT_INITIALIZED; |
michael@0 | 1004 | |
michael@0 | 1005 | *aEndOffset = mEndOffset; |
michael@0 | 1006 | |
michael@0 | 1007 | return NS_OK; |
michael@0 | 1008 | } |
michael@0 | 1009 | |
michael@0 | 1010 | uint32_t |
michael@0 | 1011 | nsRange::GetEndOffset(ErrorResult& aRv) const |
michael@0 | 1012 | { |
michael@0 | 1013 | if (!mIsPositioned) { |
michael@0 | 1014 | aRv.Throw(NS_ERROR_NOT_INITIALIZED); |
michael@0 | 1015 | return 0; |
michael@0 | 1016 | } |
michael@0 | 1017 | |
michael@0 | 1018 | return mEndOffset; |
michael@0 | 1019 | } |
michael@0 | 1020 | |
michael@0 | 1021 | NS_IMETHODIMP |
michael@0 | 1022 | nsRange::GetCollapsed(bool* aIsCollapsed) |
michael@0 | 1023 | { |
michael@0 | 1024 | if (!mIsPositioned) |
michael@0 | 1025 | return NS_ERROR_NOT_INITIALIZED; |
michael@0 | 1026 | |
michael@0 | 1027 | *aIsCollapsed = Collapsed(); |
michael@0 | 1028 | |
michael@0 | 1029 | return NS_OK; |
michael@0 | 1030 | } |
michael@0 | 1031 | |
michael@0 | 1032 | nsINode* |
michael@0 | 1033 | nsRange::GetCommonAncestorContainer(ErrorResult& aRv) const |
michael@0 | 1034 | { |
michael@0 | 1035 | if (!mIsPositioned) { |
michael@0 | 1036 | aRv.Throw(NS_ERROR_NOT_INITIALIZED); |
michael@0 | 1037 | return nullptr; |
michael@0 | 1038 | } |
michael@0 | 1039 | |
michael@0 | 1040 | return nsContentUtils::GetCommonAncestor(mStartParent, mEndParent); |
michael@0 | 1041 | } |
michael@0 | 1042 | |
michael@0 | 1043 | NS_IMETHODIMP |
michael@0 | 1044 | nsRange::GetCommonAncestorContainer(nsIDOMNode** aCommonParent) |
michael@0 | 1045 | { |
michael@0 | 1046 | ErrorResult rv; |
michael@0 | 1047 | nsINode* commonAncestor = GetCommonAncestorContainer(rv); |
michael@0 | 1048 | if (commonAncestor) { |
michael@0 | 1049 | NS_ADDREF(*aCommonParent = commonAncestor->AsDOMNode()); |
michael@0 | 1050 | } else { |
michael@0 | 1051 | *aCommonParent = nullptr; |
michael@0 | 1052 | } |
michael@0 | 1053 | |
michael@0 | 1054 | return rv.ErrorCode(); |
michael@0 | 1055 | } |
michael@0 | 1056 | |
michael@0 | 1057 | nsINode* |
michael@0 | 1058 | nsRange::IsValidBoundary(nsINode* aNode) |
michael@0 | 1059 | { |
michael@0 | 1060 | if (!aNode) { |
michael@0 | 1061 | return nullptr; |
michael@0 | 1062 | } |
michael@0 | 1063 | |
michael@0 | 1064 | if (aNode->IsNodeOfType(nsINode::eCONTENT)) { |
michael@0 | 1065 | nsIContent* content = static_cast<nsIContent*>(aNode); |
michael@0 | 1066 | if (content->Tag() == nsGkAtoms::documentTypeNodeName) { |
michael@0 | 1067 | return nullptr; |
michael@0 | 1068 | } |
michael@0 | 1069 | |
michael@0 | 1070 | if (!mMaySpanAnonymousSubtrees) { |
michael@0 | 1071 | // If the node is in a shadow tree then the ShadowRoot is the root. |
michael@0 | 1072 | ShadowRoot* containingShadow = content->GetContainingShadow(); |
michael@0 | 1073 | if (containingShadow) { |
michael@0 | 1074 | return containingShadow; |
michael@0 | 1075 | } |
michael@0 | 1076 | |
michael@0 | 1077 | // If the node has a binding parent, that should be the root. |
michael@0 | 1078 | // XXXbz maybe only for native anonymous content? |
michael@0 | 1079 | nsINode* root = content->GetBindingParent(); |
michael@0 | 1080 | if (root) { |
michael@0 | 1081 | return root; |
michael@0 | 1082 | } |
michael@0 | 1083 | } |
michael@0 | 1084 | } |
michael@0 | 1085 | |
michael@0 | 1086 | // Elements etc. must be in document or in document fragment, |
michael@0 | 1087 | // text nodes in document, in document fragment or in attribute. |
michael@0 | 1088 | nsINode* root = aNode->GetCurrentDoc(); |
michael@0 | 1089 | if (root) { |
michael@0 | 1090 | return root; |
michael@0 | 1091 | } |
michael@0 | 1092 | |
michael@0 | 1093 | root = aNode->SubtreeRoot(); |
michael@0 | 1094 | |
michael@0 | 1095 | NS_ASSERTION(!root->IsNodeOfType(nsINode::eDOCUMENT), |
michael@0 | 1096 | "GetCurrentDoc should have returned a doc"); |
michael@0 | 1097 | |
michael@0 | 1098 | // We allow this because of backward compatibility. |
michael@0 | 1099 | return root; |
michael@0 | 1100 | } |
michael@0 | 1101 | |
michael@0 | 1102 | void |
michael@0 | 1103 | nsRange::SetStart(nsINode& aNode, uint32_t aOffset, ErrorResult& aRv) |
michael@0 | 1104 | { |
michael@0 | 1105 | if (!nsContentUtils::CanCallerAccess(&aNode)) { |
michael@0 | 1106 | aRv.Throw(NS_ERROR_DOM_SECURITY_ERR); |
michael@0 | 1107 | return; |
michael@0 | 1108 | } |
michael@0 | 1109 | |
michael@0 | 1110 | AutoInvalidateSelection atEndOfBlock(this); |
michael@0 | 1111 | aRv = SetStart(&aNode, aOffset); |
michael@0 | 1112 | } |
michael@0 | 1113 | |
michael@0 | 1114 | NS_IMETHODIMP |
michael@0 | 1115 | nsRange::SetStart(nsIDOMNode* aParent, int32_t aOffset) |
michael@0 | 1116 | { |
michael@0 | 1117 | nsCOMPtr<nsINode> parent = do_QueryInterface(aParent); |
michael@0 | 1118 | if (!parent) { |
michael@0 | 1119 | return NS_ERROR_DOM_NOT_OBJECT_ERR; |
michael@0 | 1120 | } |
michael@0 | 1121 | |
michael@0 | 1122 | ErrorResult rv; |
michael@0 | 1123 | SetStart(*parent, aOffset, rv); |
michael@0 | 1124 | return rv.ErrorCode(); |
michael@0 | 1125 | } |
michael@0 | 1126 | |
michael@0 | 1127 | /* virtual */ nsresult |
michael@0 | 1128 | nsRange::SetStart(nsINode* aParent, int32_t aOffset) |
michael@0 | 1129 | { |
michael@0 | 1130 | nsINode* newRoot = IsValidBoundary(aParent); |
michael@0 | 1131 | NS_ENSURE_TRUE(newRoot, NS_ERROR_DOM_INVALID_NODE_TYPE_ERR); |
michael@0 | 1132 | |
michael@0 | 1133 | if (aOffset < 0 || uint32_t(aOffset) > aParent->Length()) { |
michael@0 | 1134 | return NS_ERROR_DOM_INDEX_SIZE_ERR; |
michael@0 | 1135 | } |
michael@0 | 1136 | |
michael@0 | 1137 | // Collapse if not positioned yet, if positioned in another doc or |
michael@0 | 1138 | // if the new start is after end. |
michael@0 | 1139 | if (!mIsPositioned || newRoot != mRoot || |
michael@0 | 1140 | nsContentUtils::ComparePoints(aParent, aOffset, |
michael@0 | 1141 | mEndParent, mEndOffset) == 1) { |
michael@0 | 1142 | DoSetRange(aParent, aOffset, aParent, aOffset, newRoot); |
michael@0 | 1143 | |
michael@0 | 1144 | return NS_OK; |
michael@0 | 1145 | } |
michael@0 | 1146 | |
michael@0 | 1147 | DoSetRange(aParent, aOffset, mEndParent, mEndOffset, mRoot); |
michael@0 | 1148 | |
michael@0 | 1149 | return NS_OK; |
michael@0 | 1150 | } |
michael@0 | 1151 | |
michael@0 | 1152 | void |
michael@0 | 1153 | nsRange::SetStartBefore(nsINode& aNode, ErrorResult& aRv) |
michael@0 | 1154 | { |
michael@0 | 1155 | if (!nsContentUtils::CanCallerAccess(&aNode)) { |
michael@0 | 1156 | aRv.Throw(NS_ERROR_DOM_SECURITY_ERR); |
michael@0 | 1157 | return; |
michael@0 | 1158 | } |
michael@0 | 1159 | |
michael@0 | 1160 | AutoInvalidateSelection atEndOfBlock(this); |
michael@0 | 1161 | aRv = SetStart(aNode.GetParentNode(), IndexOf(&aNode)); |
michael@0 | 1162 | } |
michael@0 | 1163 | |
michael@0 | 1164 | NS_IMETHODIMP |
michael@0 | 1165 | nsRange::SetStartBefore(nsIDOMNode* aSibling) |
michael@0 | 1166 | { |
michael@0 | 1167 | nsCOMPtr<nsINode> sibling = do_QueryInterface(aSibling); |
michael@0 | 1168 | if (!sibling) { |
michael@0 | 1169 | return NS_ERROR_DOM_NOT_OBJECT_ERR; |
michael@0 | 1170 | } |
michael@0 | 1171 | |
michael@0 | 1172 | ErrorResult rv; |
michael@0 | 1173 | SetStartBefore(*sibling, rv); |
michael@0 | 1174 | return rv.ErrorCode(); |
michael@0 | 1175 | } |
michael@0 | 1176 | |
michael@0 | 1177 | void |
michael@0 | 1178 | nsRange::SetStartAfter(nsINode& aNode, ErrorResult& aRv) |
michael@0 | 1179 | { |
michael@0 | 1180 | if (!nsContentUtils::CanCallerAccess(&aNode)) { |
michael@0 | 1181 | aRv.Throw(NS_ERROR_DOM_SECURITY_ERR); |
michael@0 | 1182 | return; |
michael@0 | 1183 | } |
michael@0 | 1184 | |
michael@0 | 1185 | AutoInvalidateSelection atEndOfBlock(this); |
michael@0 | 1186 | aRv = SetStart(aNode.GetParentNode(), IndexOf(&aNode) + 1); |
michael@0 | 1187 | } |
michael@0 | 1188 | |
michael@0 | 1189 | NS_IMETHODIMP |
michael@0 | 1190 | nsRange::SetStartAfter(nsIDOMNode* aSibling) |
michael@0 | 1191 | { |
michael@0 | 1192 | nsCOMPtr<nsINode> sibling = do_QueryInterface(aSibling); |
michael@0 | 1193 | if (!sibling) { |
michael@0 | 1194 | return NS_ERROR_DOM_NOT_OBJECT_ERR; |
michael@0 | 1195 | } |
michael@0 | 1196 | |
michael@0 | 1197 | ErrorResult rv; |
michael@0 | 1198 | SetStartAfter(*sibling, rv); |
michael@0 | 1199 | return rv.ErrorCode(); |
michael@0 | 1200 | } |
michael@0 | 1201 | |
michael@0 | 1202 | void |
michael@0 | 1203 | nsRange::SetEnd(nsINode& aNode, uint32_t aOffset, ErrorResult& aRv) |
michael@0 | 1204 | { |
michael@0 | 1205 | if (!nsContentUtils::CanCallerAccess(&aNode)) { |
michael@0 | 1206 | aRv.Throw(NS_ERROR_DOM_SECURITY_ERR); |
michael@0 | 1207 | return; |
michael@0 | 1208 | } |
michael@0 | 1209 | AutoInvalidateSelection atEndOfBlock(this); |
michael@0 | 1210 | aRv = SetEnd(&aNode, aOffset); |
michael@0 | 1211 | } |
michael@0 | 1212 | |
michael@0 | 1213 | NS_IMETHODIMP |
michael@0 | 1214 | nsRange::SetEnd(nsIDOMNode* aParent, int32_t aOffset) |
michael@0 | 1215 | { |
michael@0 | 1216 | nsCOMPtr<nsINode> parent = do_QueryInterface(aParent); |
michael@0 | 1217 | if (!parent) { |
michael@0 | 1218 | return NS_ERROR_DOM_NOT_OBJECT_ERR; |
michael@0 | 1219 | } |
michael@0 | 1220 | |
michael@0 | 1221 | ErrorResult rv; |
michael@0 | 1222 | SetEnd(*parent, aOffset, rv); |
michael@0 | 1223 | return rv.ErrorCode(); |
michael@0 | 1224 | } |
michael@0 | 1225 | |
michael@0 | 1226 | /* virtual */ nsresult |
michael@0 | 1227 | nsRange::SetEnd(nsINode* aParent, int32_t aOffset) |
michael@0 | 1228 | { |
michael@0 | 1229 | nsINode* newRoot = IsValidBoundary(aParent); |
michael@0 | 1230 | NS_ENSURE_TRUE(newRoot, NS_ERROR_DOM_INVALID_NODE_TYPE_ERR); |
michael@0 | 1231 | |
michael@0 | 1232 | if (aOffset < 0 || uint32_t(aOffset) > aParent->Length()) { |
michael@0 | 1233 | return NS_ERROR_DOM_INDEX_SIZE_ERR; |
michael@0 | 1234 | } |
michael@0 | 1235 | |
michael@0 | 1236 | // Collapse if not positioned yet, if positioned in another doc or |
michael@0 | 1237 | // if the new end is before start. |
michael@0 | 1238 | if (!mIsPositioned || newRoot != mRoot || |
michael@0 | 1239 | nsContentUtils::ComparePoints(mStartParent, mStartOffset, |
michael@0 | 1240 | aParent, aOffset) == 1) { |
michael@0 | 1241 | DoSetRange(aParent, aOffset, aParent, aOffset, newRoot); |
michael@0 | 1242 | |
michael@0 | 1243 | return NS_OK; |
michael@0 | 1244 | } |
michael@0 | 1245 | |
michael@0 | 1246 | DoSetRange(mStartParent, mStartOffset, aParent, aOffset, mRoot); |
michael@0 | 1247 | |
michael@0 | 1248 | return NS_OK; |
michael@0 | 1249 | } |
michael@0 | 1250 | |
michael@0 | 1251 | void |
michael@0 | 1252 | nsRange::SetEndBefore(nsINode& aNode, ErrorResult& aRv) |
michael@0 | 1253 | { |
michael@0 | 1254 | if (!nsContentUtils::CanCallerAccess(&aNode)) { |
michael@0 | 1255 | aRv.Throw(NS_ERROR_DOM_SECURITY_ERR); |
michael@0 | 1256 | return; |
michael@0 | 1257 | } |
michael@0 | 1258 | |
michael@0 | 1259 | AutoInvalidateSelection atEndOfBlock(this); |
michael@0 | 1260 | aRv = SetEnd(aNode.GetParentNode(), IndexOf(&aNode)); |
michael@0 | 1261 | } |
michael@0 | 1262 | |
michael@0 | 1263 | NS_IMETHODIMP |
michael@0 | 1264 | nsRange::SetEndBefore(nsIDOMNode* aSibling) |
michael@0 | 1265 | { |
michael@0 | 1266 | nsCOMPtr<nsINode> sibling = do_QueryInterface(aSibling); |
michael@0 | 1267 | if (!sibling) { |
michael@0 | 1268 | return NS_ERROR_DOM_NOT_OBJECT_ERR; |
michael@0 | 1269 | } |
michael@0 | 1270 | |
michael@0 | 1271 | ErrorResult rv; |
michael@0 | 1272 | SetEndBefore(*sibling, rv); |
michael@0 | 1273 | return rv.ErrorCode(); |
michael@0 | 1274 | } |
michael@0 | 1275 | |
michael@0 | 1276 | void |
michael@0 | 1277 | nsRange::SetEndAfter(nsINode& aNode, ErrorResult& aRv) |
michael@0 | 1278 | { |
michael@0 | 1279 | if (!nsContentUtils::CanCallerAccess(&aNode)) { |
michael@0 | 1280 | aRv.Throw(NS_ERROR_DOM_SECURITY_ERR); |
michael@0 | 1281 | return; |
michael@0 | 1282 | } |
michael@0 | 1283 | |
michael@0 | 1284 | AutoInvalidateSelection atEndOfBlock(this); |
michael@0 | 1285 | aRv = SetEnd(aNode.GetParentNode(), IndexOf(&aNode) + 1); |
michael@0 | 1286 | } |
michael@0 | 1287 | |
michael@0 | 1288 | NS_IMETHODIMP |
michael@0 | 1289 | nsRange::SetEndAfter(nsIDOMNode* aSibling) |
michael@0 | 1290 | { |
michael@0 | 1291 | nsCOMPtr<nsINode> sibling = do_QueryInterface(aSibling); |
michael@0 | 1292 | if (!sibling) { |
michael@0 | 1293 | return NS_ERROR_DOM_NOT_OBJECT_ERR; |
michael@0 | 1294 | } |
michael@0 | 1295 | |
michael@0 | 1296 | ErrorResult rv; |
michael@0 | 1297 | SetEndAfter(*sibling, rv); |
michael@0 | 1298 | return rv.ErrorCode(); |
michael@0 | 1299 | } |
michael@0 | 1300 | |
michael@0 | 1301 | NS_IMETHODIMP |
michael@0 | 1302 | nsRange::Collapse(bool aToStart) |
michael@0 | 1303 | { |
michael@0 | 1304 | if (!mIsPositioned) |
michael@0 | 1305 | return NS_ERROR_NOT_INITIALIZED; |
michael@0 | 1306 | |
michael@0 | 1307 | AutoInvalidateSelection atEndOfBlock(this); |
michael@0 | 1308 | if (aToStart) |
michael@0 | 1309 | DoSetRange(mStartParent, mStartOffset, mStartParent, mStartOffset, mRoot); |
michael@0 | 1310 | else |
michael@0 | 1311 | DoSetRange(mEndParent, mEndOffset, mEndParent, mEndOffset, mRoot); |
michael@0 | 1312 | |
michael@0 | 1313 | return NS_OK; |
michael@0 | 1314 | } |
michael@0 | 1315 | |
michael@0 | 1316 | NS_IMETHODIMP |
michael@0 | 1317 | nsRange::SelectNode(nsIDOMNode* aN) |
michael@0 | 1318 | { |
michael@0 | 1319 | nsCOMPtr<nsINode> node = do_QueryInterface(aN); |
michael@0 | 1320 | NS_ENSURE_TRUE(node, NS_ERROR_DOM_INVALID_NODE_TYPE_ERR); |
michael@0 | 1321 | |
michael@0 | 1322 | ErrorResult rv; |
michael@0 | 1323 | SelectNode(*node, rv); |
michael@0 | 1324 | return rv.ErrorCode(); |
michael@0 | 1325 | } |
michael@0 | 1326 | |
michael@0 | 1327 | void |
michael@0 | 1328 | nsRange::SelectNode(nsINode& aNode, ErrorResult& aRv) |
michael@0 | 1329 | { |
michael@0 | 1330 | if (!nsContentUtils::CanCallerAccess(&aNode)) { |
michael@0 | 1331 | aRv.Throw(NS_ERROR_DOM_SECURITY_ERR); |
michael@0 | 1332 | return; |
michael@0 | 1333 | } |
michael@0 | 1334 | |
michael@0 | 1335 | nsINode* parent = aNode.GetParentNode(); |
michael@0 | 1336 | nsINode* newRoot = IsValidBoundary(parent); |
michael@0 | 1337 | if (!newRoot) { |
michael@0 | 1338 | aRv.Throw(NS_ERROR_DOM_INVALID_NODE_TYPE_ERR); |
michael@0 | 1339 | return; |
michael@0 | 1340 | } |
michael@0 | 1341 | |
michael@0 | 1342 | int32_t index = parent->IndexOf(&aNode); |
michael@0 | 1343 | if (index < 0) { |
michael@0 | 1344 | aRv.Throw(NS_ERROR_DOM_INVALID_NODE_TYPE_ERR); |
michael@0 | 1345 | return; |
michael@0 | 1346 | } |
michael@0 | 1347 | |
michael@0 | 1348 | AutoInvalidateSelection atEndOfBlock(this); |
michael@0 | 1349 | DoSetRange(parent, index, parent, index + 1, newRoot); |
michael@0 | 1350 | } |
michael@0 | 1351 | |
michael@0 | 1352 | NS_IMETHODIMP |
michael@0 | 1353 | nsRange::SelectNodeContents(nsIDOMNode* aN) |
michael@0 | 1354 | { |
michael@0 | 1355 | nsCOMPtr<nsINode> node = do_QueryInterface(aN); |
michael@0 | 1356 | NS_ENSURE_TRUE(node, NS_ERROR_DOM_INVALID_NODE_TYPE_ERR); |
michael@0 | 1357 | |
michael@0 | 1358 | ErrorResult rv; |
michael@0 | 1359 | SelectNodeContents(*node, rv); |
michael@0 | 1360 | return rv.ErrorCode(); |
michael@0 | 1361 | } |
michael@0 | 1362 | |
michael@0 | 1363 | void |
michael@0 | 1364 | nsRange::SelectNodeContents(nsINode& aNode, ErrorResult& aRv) |
michael@0 | 1365 | { |
michael@0 | 1366 | if (!nsContentUtils::CanCallerAccess(&aNode)) { |
michael@0 | 1367 | aRv.Throw(NS_ERROR_DOM_SECURITY_ERR); |
michael@0 | 1368 | return; |
michael@0 | 1369 | } |
michael@0 | 1370 | |
michael@0 | 1371 | nsINode* newRoot = IsValidBoundary(&aNode); |
michael@0 | 1372 | if (!newRoot) { |
michael@0 | 1373 | aRv.Throw(NS_ERROR_DOM_INVALID_NODE_TYPE_ERR); |
michael@0 | 1374 | return; |
michael@0 | 1375 | } |
michael@0 | 1376 | |
michael@0 | 1377 | AutoInvalidateSelection atEndOfBlock(this); |
michael@0 | 1378 | DoSetRange(&aNode, 0, &aNode, aNode.Length(), newRoot); |
michael@0 | 1379 | } |
michael@0 | 1380 | |
michael@0 | 1381 | // The Subtree Content Iterator only returns subtrees that are |
michael@0 | 1382 | // completely within a given range. It doesn't return a CharacterData |
michael@0 | 1383 | // node that contains either the start or end point of the range., |
michael@0 | 1384 | // nor does it return element nodes when nothing in the element is selected. |
michael@0 | 1385 | // We need an iterator that will also include these start/end points |
michael@0 | 1386 | // so that our methods/algorithms aren't cluttered with special |
michael@0 | 1387 | // case code that tries to include these points while iterating. |
michael@0 | 1388 | // |
michael@0 | 1389 | // The RangeSubtreeIterator class mimics the nsIContentIterator |
michael@0 | 1390 | // methods we need, so should the Content Iterator support the |
michael@0 | 1391 | // start/end points in the future, we can switchover relatively |
michael@0 | 1392 | // easy. |
michael@0 | 1393 | |
michael@0 | 1394 | class MOZ_STACK_CLASS RangeSubtreeIterator |
michael@0 | 1395 | { |
michael@0 | 1396 | private: |
michael@0 | 1397 | |
michael@0 | 1398 | enum RangeSubtreeIterState { eDone=0, |
michael@0 | 1399 | eUseStart, |
michael@0 | 1400 | eUseIterator, |
michael@0 | 1401 | eUseEnd }; |
michael@0 | 1402 | |
michael@0 | 1403 | nsCOMPtr<nsIContentIterator> mIter; |
michael@0 | 1404 | RangeSubtreeIterState mIterState; |
michael@0 | 1405 | |
michael@0 | 1406 | nsCOMPtr<nsINode> mStart; |
michael@0 | 1407 | nsCOMPtr<nsINode> mEnd; |
michael@0 | 1408 | |
michael@0 | 1409 | public: |
michael@0 | 1410 | |
michael@0 | 1411 | RangeSubtreeIterator() |
michael@0 | 1412 | : mIterState(eDone) |
michael@0 | 1413 | { |
michael@0 | 1414 | } |
michael@0 | 1415 | ~RangeSubtreeIterator() |
michael@0 | 1416 | { |
michael@0 | 1417 | } |
michael@0 | 1418 | |
michael@0 | 1419 | nsresult Init(nsRange *aRange); |
michael@0 | 1420 | already_AddRefed<nsINode> GetCurrentNode(); |
michael@0 | 1421 | void First(); |
michael@0 | 1422 | void Last(); |
michael@0 | 1423 | void Next(); |
michael@0 | 1424 | void Prev(); |
michael@0 | 1425 | |
michael@0 | 1426 | bool IsDone() |
michael@0 | 1427 | { |
michael@0 | 1428 | return mIterState == eDone; |
michael@0 | 1429 | } |
michael@0 | 1430 | }; |
michael@0 | 1431 | |
michael@0 | 1432 | nsresult |
michael@0 | 1433 | RangeSubtreeIterator::Init(nsRange *aRange) |
michael@0 | 1434 | { |
michael@0 | 1435 | mIterState = eDone; |
michael@0 | 1436 | if (aRange->Collapsed()) { |
michael@0 | 1437 | return NS_OK; |
michael@0 | 1438 | } |
michael@0 | 1439 | |
michael@0 | 1440 | // Grab the start point of the range and QI it to |
michael@0 | 1441 | // a CharacterData pointer. If it is CharacterData store |
michael@0 | 1442 | // a pointer to the node. |
michael@0 | 1443 | |
michael@0 | 1444 | ErrorResult rv; |
michael@0 | 1445 | nsCOMPtr<nsINode> node = aRange->GetStartContainer(rv); |
michael@0 | 1446 | if (!node) return NS_ERROR_FAILURE; |
michael@0 | 1447 | |
michael@0 | 1448 | nsCOMPtr<nsIDOMCharacterData> startData = do_QueryInterface(node); |
michael@0 | 1449 | if (startData || (node->IsElement() && |
michael@0 | 1450 | node->AsElement()->GetChildCount() == aRange->GetStartOffset(rv))) { |
michael@0 | 1451 | mStart = node; |
michael@0 | 1452 | } |
michael@0 | 1453 | |
michael@0 | 1454 | // Grab the end point of the range and QI it to |
michael@0 | 1455 | // a CharacterData pointer. If it is CharacterData store |
michael@0 | 1456 | // a pointer to the node. |
michael@0 | 1457 | |
michael@0 | 1458 | node = aRange->GetEndContainer(rv); |
michael@0 | 1459 | if (!node) return NS_ERROR_FAILURE; |
michael@0 | 1460 | |
michael@0 | 1461 | nsCOMPtr<nsIDOMCharacterData> endData = do_QueryInterface(node); |
michael@0 | 1462 | if (endData || (node->IsElement() && aRange->GetEndOffset(rv) == 0)) { |
michael@0 | 1463 | mEnd = node; |
michael@0 | 1464 | } |
michael@0 | 1465 | |
michael@0 | 1466 | if (mStart && mStart == mEnd) |
michael@0 | 1467 | { |
michael@0 | 1468 | // The range starts and stops in the same CharacterData |
michael@0 | 1469 | // node. Null out the end pointer so we only visit the |
michael@0 | 1470 | // node once! |
michael@0 | 1471 | |
michael@0 | 1472 | mEnd = nullptr; |
michael@0 | 1473 | } |
michael@0 | 1474 | else |
michael@0 | 1475 | { |
michael@0 | 1476 | // Now create a Content Subtree Iterator to be used |
michael@0 | 1477 | // for the subtrees between the end points! |
michael@0 | 1478 | |
michael@0 | 1479 | mIter = NS_NewContentSubtreeIterator(); |
michael@0 | 1480 | |
michael@0 | 1481 | nsresult res = mIter->Init(aRange); |
michael@0 | 1482 | if (NS_FAILED(res)) return res; |
michael@0 | 1483 | |
michael@0 | 1484 | if (mIter->IsDone()) |
michael@0 | 1485 | { |
michael@0 | 1486 | // The subtree iterator thinks there's nothing |
michael@0 | 1487 | // to iterate over, so just free it up so we |
michael@0 | 1488 | // don't accidentally call into it. |
michael@0 | 1489 | |
michael@0 | 1490 | mIter = nullptr; |
michael@0 | 1491 | } |
michael@0 | 1492 | } |
michael@0 | 1493 | |
michael@0 | 1494 | // Initialize the iterator by calling First(). |
michael@0 | 1495 | // Note that we are ignoring the return value on purpose! |
michael@0 | 1496 | |
michael@0 | 1497 | First(); |
michael@0 | 1498 | |
michael@0 | 1499 | return NS_OK; |
michael@0 | 1500 | } |
michael@0 | 1501 | |
michael@0 | 1502 | already_AddRefed<nsINode> |
michael@0 | 1503 | RangeSubtreeIterator::GetCurrentNode() |
michael@0 | 1504 | { |
michael@0 | 1505 | nsCOMPtr<nsINode> node; |
michael@0 | 1506 | |
michael@0 | 1507 | if (mIterState == eUseStart && mStart) { |
michael@0 | 1508 | node = mStart; |
michael@0 | 1509 | } else if (mIterState == eUseEnd && mEnd) { |
michael@0 | 1510 | node = mEnd; |
michael@0 | 1511 | } else if (mIterState == eUseIterator && mIter) { |
michael@0 | 1512 | node = mIter->GetCurrentNode(); |
michael@0 | 1513 | } |
michael@0 | 1514 | |
michael@0 | 1515 | return node.forget(); |
michael@0 | 1516 | } |
michael@0 | 1517 | |
michael@0 | 1518 | void |
michael@0 | 1519 | RangeSubtreeIterator::First() |
michael@0 | 1520 | { |
michael@0 | 1521 | if (mStart) |
michael@0 | 1522 | mIterState = eUseStart; |
michael@0 | 1523 | else if (mIter) |
michael@0 | 1524 | { |
michael@0 | 1525 | mIter->First(); |
michael@0 | 1526 | |
michael@0 | 1527 | mIterState = eUseIterator; |
michael@0 | 1528 | } |
michael@0 | 1529 | else if (mEnd) |
michael@0 | 1530 | mIterState = eUseEnd; |
michael@0 | 1531 | else |
michael@0 | 1532 | mIterState = eDone; |
michael@0 | 1533 | } |
michael@0 | 1534 | |
michael@0 | 1535 | void |
michael@0 | 1536 | RangeSubtreeIterator::Last() |
michael@0 | 1537 | { |
michael@0 | 1538 | if (mEnd) |
michael@0 | 1539 | mIterState = eUseEnd; |
michael@0 | 1540 | else if (mIter) |
michael@0 | 1541 | { |
michael@0 | 1542 | mIter->Last(); |
michael@0 | 1543 | |
michael@0 | 1544 | mIterState = eUseIterator; |
michael@0 | 1545 | } |
michael@0 | 1546 | else if (mStart) |
michael@0 | 1547 | mIterState = eUseStart; |
michael@0 | 1548 | else |
michael@0 | 1549 | mIterState = eDone; |
michael@0 | 1550 | } |
michael@0 | 1551 | |
michael@0 | 1552 | void |
michael@0 | 1553 | RangeSubtreeIterator::Next() |
michael@0 | 1554 | { |
michael@0 | 1555 | if (mIterState == eUseStart) |
michael@0 | 1556 | { |
michael@0 | 1557 | if (mIter) |
michael@0 | 1558 | { |
michael@0 | 1559 | mIter->First(); |
michael@0 | 1560 | |
michael@0 | 1561 | mIterState = eUseIterator; |
michael@0 | 1562 | } |
michael@0 | 1563 | else if (mEnd) |
michael@0 | 1564 | mIterState = eUseEnd; |
michael@0 | 1565 | else |
michael@0 | 1566 | mIterState = eDone; |
michael@0 | 1567 | } |
michael@0 | 1568 | else if (mIterState == eUseIterator) |
michael@0 | 1569 | { |
michael@0 | 1570 | mIter->Next(); |
michael@0 | 1571 | |
michael@0 | 1572 | if (mIter->IsDone()) |
michael@0 | 1573 | { |
michael@0 | 1574 | if (mEnd) |
michael@0 | 1575 | mIterState = eUseEnd; |
michael@0 | 1576 | else |
michael@0 | 1577 | mIterState = eDone; |
michael@0 | 1578 | } |
michael@0 | 1579 | } |
michael@0 | 1580 | else |
michael@0 | 1581 | mIterState = eDone; |
michael@0 | 1582 | } |
michael@0 | 1583 | |
michael@0 | 1584 | void |
michael@0 | 1585 | RangeSubtreeIterator::Prev() |
michael@0 | 1586 | { |
michael@0 | 1587 | if (mIterState == eUseEnd) |
michael@0 | 1588 | { |
michael@0 | 1589 | if (mIter) |
michael@0 | 1590 | { |
michael@0 | 1591 | mIter->Last(); |
michael@0 | 1592 | |
michael@0 | 1593 | mIterState = eUseIterator; |
michael@0 | 1594 | } |
michael@0 | 1595 | else if (mStart) |
michael@0 | 1596 | mIterState = eUseStart; |
michael@0 | 1597 | else |
michael@0 | 1598 | mIterState = eDone; |
michael@0 | 1599 | } |
michael@0 | 1600 | else if (mIterState == eUseIterator) |
michael@0 | 1601 | { |
michael@0 | 1602 | mIter->Prev(); |
michael@0 | 1603 | |
michael@0 | 1604 | if (mIter->IsDone()) |
michael@0 | 1605 | { |
michael@0 | 1606 | if (mStart) |
michael@0 | 1607 | mIterState = eUseStart; |
michael@0 | 1608 | else |
michael@0 | 1609 | mIterState = eDone; |
michael@0 | 1610 | } |
michael@0 | 1611 | } |
michael@0 | 1612 | else |
michael@0 | 1613 | mIterState = eDone; |
michael@0 | 1614 | } |
michael@0 | 1615 | |
michael@0 | 1616 | |
michael@0 | 1617 | // CollapseRangeAfterDelete() is a utility method that is used by |
michael@0 | 1618 | // DeleteContents() and ExtractContents() to collapse the range |
michael@0 | 1619 | // in the correct place, under the range's root container (the |
michael@0 | 1620 | // range end points common container) as outlined by the Range spec: |
michael@0 | 1621 | // |
michael@0 | 1622 | // http://www.w3.org/TR/2000/REC-DOM-Level-2-Traversal-Range-20001113/ranges.html |
michael@0 | 1623 | // The assumption made by this method is that the delete or extract |
michael@0 | 1624 | // has been done already, and left the range in a state where there is |
michael@0 | 1625 | // no content between the 2 end points. |
michael@0 | 1626 | |
michael@0 | 1627 | static nsresult |
michael@0 | 1628 | CollapseRangeAfterDelete(nsRange* aRange) |
michael@0 | 1629 | { |
michael@0 | 1630 | NS_ENSURE_ARG_POINTER(aRange); |
michael@0 | 1631 | |
michael@0 | 1632 | // Check if range gravity took care of collapsing the range for us! |
michael@0 | 1633 | if (aRange->Collapsed()) |
michael@0 | 1634 | { |
michael@0 | 1635 | // aRange is collapsed so there's nothing for us to do. |
michael@0 | 1636 | // |
michael@0 | 1637 | // There are 2 possible scenarios here: |
michael@0 | 1638 | // |
michael@0 | 1639 | // 1. aRange could've been collapsed prior to the delete/extract, |
michael@0 | 1640 | // which would've resulted in nothing being removed, so aRange |
michael@0 | 1641 | // is already where it should be. |
michael@0 | 1642 | // |
michael@0 | 1643 | // 2. Prior to the delete/extract, aRange's start and end were in |
michael@0 | 1644 | // the same container which would mean everything between them |
michael@0 | 1645 | // was removed, causing range gravity to collapse the range. |
michael@0 | 1646 | |
michael@0 | 1647 | return NS_OK; |
michael@0 | 1648 | } |
michael@0 | 1649 | |
michael@0 | 1650 | // aRange isn't collapsed so figure out the appropriate place to collapse! |
michael@0 | 1651 | // First get both end points and their common ancestor. |
michael@0 | 1652 | |
michael@0 | 1653 | ErrorResult rv; |
michael@0 | 1654 | nsCOMPtr<nsINode> commonAncestor = aRange->GetCommonAncestorContainer(rv); |
michael@0 | 1655 | if (rv.Failed()) return rv.ErrorCode(); |
michael@0 | 1656 | |
michael@0 | 1657 | nsCOMPtr<nsINode> startContainer = aRange->GetStartContainer(rv); |
michael@0 | 1658 | if (rv.Failed()) return rv.ErrorCode(); |
michael@0 | 1659 | nsCOMPtr<nsINode> endContainer = aRange->GetEndContainer(rv); |
michael@0 | 1660 | if (rv.Failed()) return rv.ErrorCode(); |
michael@0 | 1661 | |
michael@0 | 1662 | // Collapse to one of the end points if they are already in the |
michael@0 | 1663 | // commonAncestor. This should work ok since this method is called |
michael@0 | 1664 | // immediately after a delete or extract that leaves no content |
michael@0 | 1665 | // between the 2 end points! |
michael@0 | 1666 | |
michael@0 | 1667 | if (startContainer == commonAncestor) |
michael@0 | 1668 | return aRange->Collapse(true); |
michael@0 | 1669 | if (endContainer == commonAncestor) |
michael@0 | 1670 | return aRange->Collapse(false); |
michael@0 | 1671 | |
michael@0 | 1672 | // End points are at differing levels. We want to collapse to the |
michael@0 | 1673 | // point that is between the 2 subtrees that contain each point, |
michael@0 | 1674 | // under the common ancestor. |
michael@0 | 1675 | |
michael@0 | 1676 | nsCOMPtr<nsINode> nodeToSelect(startContainer); |
michael@0 | 1677 | |
michael@0 | 1678 | while (nodeToSelect) |
michael@0 | 1679 | { |
michael@0 | 1680 | nsCOMPtr<nsINode> parent = nodeToSelect->GetParentNode(); |
michael@0 | 1681 | if (parent == commonAncestor) |
michael@0 | 1682 | break; // We found the nodeToSelect! |
michael@0 | 1683 | |
michael@0 | 1684 | nodeToSelect = parent; |
michael@0 | 1685 | } |
michael@0 | 1686 | |
michael@0 | 1687 | if (!nodeToSelect) |
michael@0 | 1688 | return NS_ERROR_FAILURE; // This should never happen! |
michael@0 | 1689 | |
michael@0 | 1690 | aRange->SelectNode(*nodeToSelect, rv); |
michael@0 | 1691 | if (rv.Failed()) return rv.ErrorCode(); |
michael@0 | 1692 | |
michael@0 | 1693 | return aRange->Collapse(false); |
michael@0 | 1694 | } |
michael@0 | 1695 | |
michael@0 | 1696 | /** |
michael@0 | 1697 | * Split a data node into two parts. |
michael@0 | 1698 | * |
michael@0 | 1699 | * @param aStartNode The original node we are trying to split. |
michael@0 | 1700 | * @param aStartIndex The index at which to split. |
michael@0 | 1701 | * @param aEndNode The second node. |
michael@0 | 1702 | * @param aCloneAfterOriginal Set false if the original node should be the |
michael@0 | 1703 | * latter one after split. |
michael@0 | 1704 | */ |
michael@0 | 1705 | static nsresult SplitDataNode(nsIDOMCharacterData* aStartNode, |
michael@0 | 1706 | uint32_t aStartIndex, |
michael@0 | 1707 | nsIDOMCharacterData** aEndNode, |
michael@0 | 1708 | bool aCloneAfterOriginal = true) |
michael@0 | 1709 | { |
michael@0 | 1710 | nsresult rv; |
michael@0 | 1711 | nsCOMPtr<nsINode> node = do_QueryInterface(aStartNode); |
michael@0 | 1712 | NS_ENSURE_STATE(node && node->IsNodeOfType(nsINode::eDATA_NODE)); |
michael@0 | 1713 | nsGenericDOMDataNode* dataNode = static_cast<nsGenericDOMDataNode*>(node.get()); |
michael@0 | 1714 | |
michael@0 | 1715 | nsCOMPtr<nsIContent> newData; |
michael@0 | 1716 | rv = dataNode->SplitData(aStartIndex, getter_AddRefs(newData), |
michael@0 | 1717 | aCloneAfterOriginal); |
michael@0 | 1718 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 1719 | return CallQueryInterface(newData, aEndNode); |
michael@0 | 1720 | } |
michael@0 | 1721 | |
michael@0 | 1722 | NS_IMETHODIMP |
michael@0 | 1723 | PrependChild(nsINode* aParent, nsINode* aChild) |
michael@0 | 1724 | { |
michael@0 | 1725 | nsCOMPtr<nsINode> first = aParent->GetFirstChild(); |
michael@0 | 1726 | ErrorResult rv; |
michael@0 | 1727 | aParent->InsertBefore(*aChild, first, rv); |
michael@0 | 1728 | return rv.ErrorCode(); |
michael@0 | 1729 | } |
michael@0 | 1730 | |
michael@0 | 1731 | // Helper function for CutContents, making sure that the current node wasn't |
michael@0 | 1732 | // removed by mutation events (bug 766426) |
michael@0 | 1733 | static bool |
michael@0 | 1734 | ValidateCurrentNode(nsRange* aRange, RangeSubtreeIterator& aIter) |
michael@0 | 1735 | { |
michael@0 | 1736 | bool before, after; |
michael@0 | 1737 | nsCOMPtr<nsINode> node = aIter.GetCurrentNode(); |
michael@0 | 1738 | if (!node) { |
michael@0 | 1739 | // We don't have to worry that the node was removed if it doesn't exist, |
michael@0 | 1740 | // e.g., the iterator is done. |
michael@0 | 1741 | return true; |
michael@0 | 1742 | } |
michael@0 | 1743 | |
michael@0 | 1744 | nsresult res = nsRange::CompareNodeToRange(node, aRange, &before, &after); |
michael@0 | 1745 | |
michael@0 | 1746 | return NS_SUCCEEDED(res) && !before && !after; |
michael@0 | 1747 | } |
michael@0 | 1748 | |
michael@0 | 1749 | nsresult |
michael@0 | 1750 | nsRange::CutContents(DocumentFragment** aFragment) |
michael@0 | 1751 | { |
michael@0 | 1752 | if (aFragment) { |
michael@0 | 1753 | *aFragment = nullptr; |
michael@0 | 1754 | } |
michael@0 | 1755 | |
michael@0 | 1756 | nsCOMPtr<nsIDocument> doc = mStartParent->OwnerDoc(); |
michael@0 | 1757 | |
michael@0 | 1758 | ErrorResult res; |
michael@0 | 1759 | nsCOMPtr<nsINode> commonAncestor = GetCommonAncestorContainer(res); |
michael@0 | 1760 | NS_ENSURE_SUCCESS(res.ErrorCode(), res.ErrorCode()); |
michael@0 | 1761 | |
michael@0 | 1762 | // If aFragment isn't null, create a temporary fragment to hold our return. |
michael@0 | 1763 | nsRefPtr<DocumentFragment> retval; |
michael@0 | 1764 | if (aFragment) { |
michael@0 | 1765 | retval = new DocumentFragment(doc->NodeInfoManager()); |
michael@0 | 1766 | } |
michael@0 | 1767 | nsCOMPtr<nsINode> commonCloneAncestor = retval.get(); |
michael@0 | 1768 | |
michael@0 | 1769 | // Batch possible DOMSubtreeModified events. |
michael@0 | 1770 | mozAutoSubtreeModified subtree(mRoot ? mRoot->OwnerDoc(): nullptr, nullptr); |
michael@0 | 1771 | |
michael@0 | 1772 | // Save the range end points locally to avoid interference |
michael@0 | 1773 | // of Range gravity during our edits! |
michael@0 | 1774 | |
michael@0 | 1775 | nsCOMPtr<nsINode> startContainer = mStartParent; |
michael@0 | 1776 | int32_t startOffset = mStartOffset; |
michael@0 | 1777 | nsCOMPtr<nsINode> endContainer = mEndParent; |
michael@0 | 1778 | int32_t endOffset = mEndOffset; |
michael@0 | 1779 | |
michael@0 | 1780 | if (retval) { |
michael@0 | 1781 | // For extractContents(), abort early if there's a doctype (bug 719533). |
michael@0 | 1782 | // This can happen only if the common ancestor is a document, in which case |
michael@0 | 1783 | // we just need to find its doctype child and check if that's in the range. |
michael@0 | 1784 | nsCOMPtr<nsIDocument> commonAncestorDocument = do_QueryInterface(commonAncestor); |
michael@0 | 1785 | if (commonAncestorDocument) { |
michael@0 | 1786 | nsRefPtr<DocumentType> doctype = commonAncestorDocument->GetDoctype(); |
michael@0 | 1787 | |
michael@0 | 1788 | if (doctype && |
michael@0 | 1789 | nsContentUtils::ComparePoints(startContainer, startOffset, |
michael@0 | 1790 | doctype, 0) < 0 && |
michael@0 | 1791 | nsContentUtils::ComparePoints(doctype, 0, |
michael@0 | 1792 | endContainer, endOffset) < 0) { |
michael@0 | 1793 | return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR; |
michael@0 | 1794 | } |
michael@0 | 1795 | } |
michael@0 | 1796 | } |
michael@0 | 1797 | |
michael@0 | 1798 | // Create and initialize a subtree iterator that will give |
michael@0 | 1799 | // us all the subtrees within the range. |
michael@0 | 1800 | |
michael@0 | 1801 | RangeSubtreeIterator iter; |
michael@0 | 1802 | |
michael@0 | 1803 | nsresult rv = iter.Init(this); |
michael@0 | 1804 | if (NS_FAILED(rv)) return rv; |
michael@0 | 1805 | |
michael@0 | 1806 | if (iter.IsDone()) |
michael@0 | 1807 | { |
michael@0 | 1808 | // There's nothing for us to delete. |
michael@0 | 1809 | rv = CollapseRangeAfterDelete(this); |
michael@0 | 1810 | if (NS_SUCCEEDED(rv) && aFragment) { |
michael@0 | 1811 | NS_ADDREF(*aFragment = retval); |
michael@0 | 1812 | } |
michael@0 | 1813 | return rv; |
michael@0 | 1814 | } |
michael@0 | 1815 | |
michael@0 | 1816 | // We delete backwards to avoid iterator problems! |
michael@0 | 1817 | |
michael@0 | 1818 | iter.Last(); |
michael@0 | 1819 | |
michael@0 | 1820 | bool handled = false; |
michael@0 | 1821 | |
michael@0 | 1822 | // With the exception of text nodes that contain one of the range |
michael@0 | 1823 | // end points, the subtree iterator should only give us back subtrees |
michael@0 | 1824 | // that are completely contained between the range's end points. |
michael@0 | 1825 | |
michael@0 | 1826 | while (!iter.IsDone()) |
michael@0 | 1827 | { |
michael@0 | 1828 | nsCOMPtr<nsINode> nodeToResult; |
michael@0 | 1829 | nsCOMPtr<nsINode> node = iter.GetCurrentNode(); |
michael@0 | 1830 | |
michael@0 | 1831 | // Before we delete anything, advance the iterator to the |
michael@0 | 1832 | // next subtree. |
michael@0 | 1833 | |
michael@0 | 1834 | iter.Prev(); |
michael@0 | 1835 | |
michael@0 | 1836 | handled = false; |
michael@0 | 1837 | |
michael@0 | 1838 | // If it's CharacterData, make sure we might need to delete |
michael@0 | 1839 | // part of the data, instead of removing the whole node. |
michael@0 | 1840 | // |
michael@0 | 1841 | // XXX_kin: We need to also handle ProcessingInstruction |
michael@0 | 1842 | // XXX_kin: according to the spec. |
michael@0 | 1843 | |
michael@0 | 1844 | nsCOMPtr<nsIDOMCharacterData> charData(do_QueryInterface(node)); |
michael@0 | 1845 | |
michael@0 | 1846 | if (charData) |
michael@0 | 1847 | { |
michael@0 | 1848 | uint32_t dataLength = 0; |
michael@0 | 1849 | |
michael@0 | 1850 | if (node == startContainer) |
michael@0 | 1851 | { |
michael@0 | 1852 | if (node == endContainer) |
michael@0 | 1853 | { |
michael@0 | 1854 | // This range is completely contained within a single text node. |
michael@0 | 1855 | // Delete or extract the data between startOffset and endOffset. |
michael@0 | 1856 | |
michael@0 | 1857 | if (endOffset > startOffset) |
michael@0 | 1858 | { |
michael@0 | 1859 | if (retval) { |
michael@0 | 1860 | nsAutoString cutValue; |
michael@0 | 1861 | rv = charData->SubstringData(startOffset, endOffset - startOffset, |
michael@0 | 1862 | cutValue); |
michael@0 | 1863 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 1864 | nsCOMPtr<nsIDOMNode> clone; |
michael@0 | 1865 | rv = charData->CloneNode(false, 1, getter_AddRefs(clone)); |
michael@0 | 1866 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 1867 | clone->SetNodeValue(cutValue); |
michael@0 | 1868 | nodeToResult = do_QueryInterface(clone); |
michael@0 | 1869 | } |
michael@0 | 1870 | |
michael@0 | 1871 | nsMutationGuard guard; |
michael@0 | 1872 | rv = charData->DeleteData(startOffset, endOffset - startOffset); |
michael@0 | 1873 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 1874 | NS_ENSURE_STATE(!guard.Mutated(0) || |
michael@0 | 1875 | ValidateCurrentNode(this, iter)); |
michael@0 | 1876 | } |
michael@0 | 1877 | |
michael@0 | 1878 | handled = true; |
michael@0 | 1879 | } |
michael@0 | 1880 | else |
michael@0 | 1881 | { |
michael@0 | 1882 | // Delete or extract everything after startOffset. |
michael@0 | 1883 | |
michael@0 | 1884 | rv = charData->GetLength(&dataLength); |
michael@0 | 1885 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 1886 | |
michael@0 | 1887 | if (dataLength >= (uint32_t)startOffset) |
michael@0 | 1888 | { |
michael@0 | 1889 | nsMutationGuard guard; |
michael@0 | 1890 | nsCOMPtr<nsIDOMCharacterData> cutNode; |
michael@0 | 1891 | rv = SplitDataNode(charData, startOffset, getter_AddRefs(cutNode)); |
michael@0 | 1892 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 1893 | NS_ENSURE_STATE(!guard.Mutated(1) || |
michael@0 | 1894 | ValidateCurrentNode(this, iter)); |
michael@0 | 1895 | nodeToResult = do_QueryInterface(cutNode); |
michael@0 | 1896 | } |
michael@0 | 1897 | |
michael@0 | 1898 | handled = true; |
michael@0 | 1899 | } |
michael@0 | 1900 | } |
michael@0 | 1901 | else if (node == endContainer) |
michael@0 | 1902 | { |
michael@0 | 1903 | // Delete or extract everything before endOffset. |
michael@0 | 1904 | |
michael@0 | 1905 | if (endOffset >= 0) |
michael@0 | 1906 | { |
michael@0 | 1907 | nsMutationGuard guard; |
michael@0 | 1908 | nsCOMPtr<nsIDOMCharacterData> cutNode; |
michael@0 | 1909 | /* The Range spec clearly states clones get cut and original nodes |
michael@0 | 1910 | remain behind, so use false as the last parameter. |
michael@0 | 1911 | */ |
michael@0 | 1912 | rv = SplitDataNode(charData, endOffset, getter_AddRefs(cutNode), |
michael@0 | 1913 | false); |
michael@0 | 1914 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 1915 | NS_ENSURE_STATE(!guard.Mutated(1) || |
michael@0 | 1916 | ValidateCurrentNode(this, iter)); |
michael@0 | 1917 | nodeToResult = do_QueryInterface(cutNode); |
michael@0 | 1918 | } |
michael@0 | 1919 | |
michael@0 | 1920 | handled = true; |
michael@0 | 1921 | } |
michael@0 | 1922 | } |
michael@0 | 1923 | |
michael@0 | 1924 | if (!handled && (node == endContainer || node == startContainer)) |
michael@0 | 1925 | { |
michael@0 | 1926 | if (node && node->IsElement() && |
michael@0 | 1927 | ((node == endContainer && endOffset == 0) || |
michael@0 | 1928 | (node == startContainer && |
michael@0 | 1929 | int32_t(node->AsElement()->GetChildCount()) == startOffset))) |
michael@0 | 1930 | { |
michael@0 | 1931 | if (retval) { |
michael@0 | 1932 | ErrorResult rv; |
michael@0 | 1933 | nodeToResult = node->CloneNode(false, rv); |
michael@0 | 1934 | NS_ENSURE_SUCCESS(rv.ErrorCode(), rv.ErrorCode()); |
michael@0 | 1935 | } |
michael@0 | 1936 | handled = true; |
michael@0 | 1937 | } |
michael@0 | 1938 | } |
michael@0 | 1939 | |
michael@0 | 1940 | if (!handled) |
michael@0 | 1941 | { |
michael@0 | 1942 | // node was not handled above, so it must be completely contained |
michael@0 | 1943 | // within the range. Just remove it from the tree! |
michael@0 | 1944 | nodeToResult = node; |
michael@0 | 1945 | } |
michael@0 | 1946 | |
michael@0 | 1947 | uint32_t parentCount = 0; |
michael@0 | 1948 | // Set the result to document fragment if we have 'retval'. |
michael@0 | 1949 | if (retval) { |
michael@0 | 1950 | nsCOMPtr<nsINode> oldCommonAncestor = commonAncestor; |
michael@0 | 1951 | if (!iter.IsDone()) { |
michael@0 | 1952 | // Setup the parameters for the next iteration of the loop. |
michael@0 | 1953 | nsCOMPtr<nsINode> prevNode = iter.GetCurrentNode(); |
michael@0 | 1954 | NS_ENSURE_STATE(prevNode); |
michael@0 | 1955 | |
michael@0 | 1956 | // Get node's and prevNode's common parent. Do this before moving |
michael@0 | 1957 | // nodes from original DOM to result fragment. |
michael@0 | 1958 | commonAncestor = nsContentUtils::GetCommonAncestor(node, prevNode); |
michael@0 | 1959 | NS_ENSURE_STATE(commonAncestor); |
michael@0 | 1960 | |
michael@0 | 1961 | nsCOMPtr<nsINode> parentCounterNode = node; |
michael@0 | 1962 | while (parentCounterNode && parentCounterNode != commonAncestor) |
michael@0 | 1963 | { |
michael@0 | 1964 | ++parentCount; |
michael@0 | 1965 | parentCounterNode = parentCounterNode->GetParentNode(); |
michael@0 | 1966 | NS_ENSURE_STATE(parentCounterNode); |
michael@0 | 1967 | } |
michael@0 | 1968 | } |
michael@0 | 1969 | |
michael@0 | 1970 | // Clone the parent hierarchy between commonAncestor and node. |
michael@0 | 1971 | nsCOMPtr<nsINode> closestAncestor, farthestAncestor; |
michael@0 | 1972 | rv = CloneParentsBetween(oldCommonAncestor, node, |
michael@0 | 1973 | getter_AddRefs(closestAncestor), |
michael@0 | 1974 | getter_AddRefs(farthestAncestor)); |
michael@0 | 1975 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 1976 | |
michael@0 | 1977 | if (farthestAncestor) |
michael@0 | 1978 | { |
michael@0 | 1979 | nsCOMPtr<nsINode> n = do_QueryInterface(commonCloneAncestor); |
michael@0 | 1980 | rv = PrependChild(n, farthestAncestor); |
michael@0 | 1981 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 1982 | } |
michael@0 | 1983 | |
michael@0 | 1984 | nsMutationGuard guard; |
michael@0 | 1985 | nsCOMPtr<nsINode> parent = nodeToResult->GetParentNode(); |
michael@0 | 1986 | rv = closestAncestor ? PrependChild(closestAncestor, nodeToResult) |
michael@0 | 1987 | : PrependChild(commonCloneAncestor, nodeToResult); |
michael@0 | 1988 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 1989 | NS_ENSURE_STATE(!guard.Mutated(parent ? 2 : 1) || |
michael@0 | 1990 | ValidateCurrentNode(this, iter)); |
michael@0 | 1991 | } else if (nodeToResult) { |
michael@0 | 1992 | nsMutationGuard guard; |
michael@0 | 1993 | nsCOMPtr<nsINode> node = nodeToResult; |
michael@0 | 1994 | nsINode* parent = node->GetParentNode(); |
michael@0 | 1995 | if (parent) { |
michael@0 | 1996 | mozilla::ErrorResult error; |
michael@0 | 1997 | parent->RemoveChild(*node, error); |
michael@0 | 1998 | NS_ENSURE_FALSE(error.Failed(), error.ErrorCode()); |
michael@0 | 1999 | } |
michael@0 | 2000 | NS_ENSURE_STATE(!guard.Mutated(1) || |
michael@0 | 2001 | ValidateCurrentNode(this, iter)); |
michael@0 | 2002 | } |
michael@0 | 2003 | |
michael@0 | 2004 | if (!iter.IsDone() && retval) { |
michael@0 | 2005 | // Find the equivalent of commonAncestor in the cloned tree. |
michael@0 | 2006 | nsCOMPtr<nsINode> newCloneAncestor = nodeToResult; |
michael@0 | 2007 | for (uint32_t i = parentCount; i; --i) |
michael@0 | 2008 | { |
michael@0 | 2009 | newCloneAncestor = newCloneAncestor->GetParentNode(); |
michael@0 | 2010 | NS_ENSURE_STATE(newCloneAncestor); |
michael@0 | 2011 | } |
michael@0 | 2012 | commonCloneAncestor = newCloneAncestor; |
michael@0 | 2013 | } |
michael@0 | 2014 | } |
michael@0 | 2015 | |
michael@0 | 2016 | rv = CollapseRangeAfterDelete(this); |
michael@0 | 2017 | if (NS_SUCCEEDED(rv) && aFragment) { |
michael@0 | 2018 | NS_ADDREF(*aFragment = retval); |
michael@0 | 2019 | } |
michael@0 | 2020 | return rv; |
michael@0 | 2021 | } |
michael@0 | 2022 | |
michael@0 | 2023 | NS_IMETHODIMP |
michael@0 | 2024 | nsRange::DeleteContents() |
michael@0 | 2025 | { |
michael@0 | 2026 | return CutContents(nullptr); |
michael@0 | 2027 | } |
michael@0 | 2028 | |
michael@0 | 2029 | void |
michael@0 | 2030 | nsRange::DeleteContents(ErrorResult& aRv) |
michael@0 | 2031 | { |
michael@0 | 2032 | aRv = CutContents(nullptr); |
michael@0 | 2033 | } |
michael@0 | 2034 | |
michael@0 | 2035 | NS_IMETHODIMP |
michael@0 | 2036 | nsRange::ExtractContents(nsIDOMDocumentFragment** aReturn) |
michael@0 | 2037 | { |
michael@0 | 2038 | NS_ENSURE_ARG_POINTER(aReturn); |
michael@0 | 2039 | nsRefPtr<DocumentFragment> fragment; |
michael@0 | 2040 | nsresult rv = CutContents(getter_AddRefs(fragment)); |
michael@0 | 2041 | fragment.forget(aReturn); |
michael@0 | 2042 | return rv; |
michael@0 | 2043 | } |
michael@0 | 2044 | |
michael@0 | 2045 | already_AddRefed<DocumentFragment> |
michael@0 | 2046 | nsRange::ExtractContents(ErrorResult& rv) |
michael@0 | 2047 | { |
michael@0 | 2048 | nsRefPtr<DocumentFragment> fragment; |
michael@0 | 2049 | rv = CutContents(getter_AddRefs(fragment)); |
michael@0 | 2050 | return fragment.forget(); |
michael@0 | 2051 | } |
michael@0 | 2052 | |
michael@0 | 2053 | NS_IMETHODIMP |
michael@0 | 2054 | nsRange::CompareBoundaryPoints(uint16_t aHow, nsIDOMRange* aOtherRange, |
michael@0 | 2055 | int16_t* aCmpRet) |
michael@0 | 2056 | { |
michael@0 | 2057 | nsRange* otherRange = static_cast<nsRange*>(aOtherRange); |
michael@0 | 2058 | NS_ENSURE_TRUE(otherRange, NS_ERROR_NULL_POINTER); |
michael@0 | 2059 | |
michael@0 | 2060 | ErrorResult rv; |
michael@0 | 2061 | *aCmpRet = CompareBoundaryPoints(aHow, *otherRange, rv); |
michael@0 | 2062 | return rv.ErrorCode(); |
michael@0 | 2063 | } |
michael@0 | 2064 | |
michael@0 | 2065 | int16_t |
michael@0 | 2066 | nsRange::CompareBoundaryPoints(uint16_t aHow, nsRange& aOtherRange, |
michael@0 | 2067 | ErrorResult& rv) |
michael@0 | 2068 | { |
michael@0 | 2069 | if (!mIsPositioned || !aOtherRange.IsPositioned()) { |
michael@0 | 2070 | rv.Throw(NS_ERROR_NOT_INITIALIZED); |
michael@0 | 2071 | return 0; |
michael@0 | 2072 | } |
michael@0 | 2073 | |
michael@0 | 2074 | nsINode *ourNode, *otherNode; |
michael@0 | 2075 | int32_t ourOffset, otherOffset; |
michael@0 | 2076 | |
michael@0 | 2077 | switch (aHow) { |
michael@0 | 2078 | case nsIDOMRange::START_TO_START: |
michael@0 | 2079 | ourNode = mStartParent; |
michael@0 | 2080 | ourOffset = mStartOffset; |
michael@0 | 2081 | otherNode = aOtherRange.GetStartParent(); |
michael@0 | 2082 | otherOffset = aOtherRange.StartOffset(); |
michael@0 | 2083 | break; |
michael@0 | 2084 | case nsIDOMRange::START_TO_END: |
michael@0 | 2085 | ourNode = mEndParent; |
michael@0 | 2086 | ourOffset = mEndOffset; |
michael@0 | 2087 | otherNode = aOtherRange.GetStartParent(); |
michael@0 | 2088 | otherOffset = aOtherRange.StartOffset(); |
michael@0 | 2089 | break; |
michael@0 | 2090 | case nsIDOMRange::END_TO_START: |
michael@0 | 2091 | ourNode = mStartParent; |
michael@0 | 2092 | ourOffset = mStartOffset; |
michael@0 | 2093 | otherNode = aOtherRange.GetEndParent(); |
michael@0 | 2094 | otherOffset = aOtherRange.EndOffset(); |
michael@0 | 2095 | break; |
michael@0 | 2096 | case nsIDOMRange::END_TO_END: |
michael@0 | 2097 | ourNode = mEndParent; |
michael@0 | 2098 | ourOffset = mEndOffset; |
michael@0 | 2099 | otherNode = aOtherRange.GetEndParent(); |
michael@0 | 2100 | otherOffset = aOtherRange.EndOffset(); |
michael@0 | 2101 | break; |
michael@0 | 2102 | default: |
michael@0 | 2103 | // We were passed an illegal value |
michael@0 | 2104 | rv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR); |
michael@0 | 2105 | return 0; |
michael@0 | 2106 | } |
michael@0 | 2107 | |
michael@0 | 2108 | if (mRoot != aOtherRange.GetRoot()) { |
michael@0 | 2109 | rv.Throw(NS_ERROR_DOM_WRONG_DOCUMENT_ERR); |
michael@0 | 2110 | return 0; |
michael@0 | 2111 | } |
michael@0 | 2112 | |
michael@0 | 2113 | return nsContentUtils::ComparePoints(ourNode, ourOffset, |
michael@0 | 2114 | otherNode, otherOffset); |
michael@0 | 2115 | } |
michael@0 | 2116 | |
michael@0 | 2117 | /* static */ nsresult |
michael@0 | 2118 | nsRange::CloneParentsBetween(nsINode *aAncestor, |
michael@0 | 2119 | nsINode *aNode, |
michael@0 | 2120 | nsINode **aClosestAncestor, |
michael@0 | 2121 | nsINode **aFarthestAncestor) |
michael@0 | 2122 | { |
michael@0 | 2123 | NS_ENSURE_ARG_POINTER((aAncestor && aNode && aClosestAncestor && aFarthestAncestor)); |
michael@0 | 2124 | |
michael@0 | 2125 | *aClosestAncestor = nullptr; |
michael@0 | 2126 | *aFarthestAncestor = nullptr; |
michael@0 | 2127 | |
michael@0 | 2128 | if (aAncestor == aNode) |
michael@0 | 2129 | return NS_OK; |
michael@0 | 2130 | |
michael@0 | 2131 | nsCOMPtr<nsINode> firstParent, lastParent; |
michael@0 | 2132 | nsCOMPtr<nsINode> parent = aNode->GetParentNode(); |
michael@0 | 2133 | |
michael@0 | 2134 | while(parent && parent != aAncestor) |
michael@0 | 2135 | { |
michael@0 | 2136 | ErrorResult rv; |
michael@0 | 2137 | nsCOMPtr<nsINode> clone = parent->CloneNode(false, rv); |
michael@0 | 2138 | |
michael@0 | 2139 | if (rv.Failed()) { |
michael@0 | 2140 | return rv.ErrorCode(); |
michael@0 | 2141 | } |
michael@0 | 2142 | if (!clone) { |
michael@0 | 2143 | return NS_ERROR_FAILURE; |
michael@0 | 2144 | } |
michael@0 | 2145 | |
michael@0 | 2146 | if (! firstParent) { |
michael@0 | 2147 | firstParent = lastParent = clone; |
michael@0 | 2148 | } else { |
michael@0 | 2149 | clone->AppendChild(*lastParent, rv); |
michael@0 | 2150 | if (rv.Failed()) return rv.ErrorCode(); |
michael@0 | 2151 | |
michael@0 | 2152 | lastParent = clone; |
michael@0 | 2153 | } |
michael@0 | 2154 | |
michael@0 | 2155 | parent = parent->GetParentNode(); |
michael@0 | 2156 | } |
michael@0 | 2157 | |
michael@0 | 2158 | *aClosestAncestor = firstParent; |
michael@0 | 2159 | NS_IF_ADDREF(*aClosestAncestor); |
michael@0 | 2160 | |
michael@0 | 2161 | *aFarthestAncestor = lastParent; |
michael@0 | 2162 | NS_IF_ADDREF(*aFarthestAncestor); |
michael@0 | 2163 | |
michael@0 | 2164 | return NS_OK; |
michael@0 | 2165 | } |
michael@0 | 2166 | |
michael@0 | 2167 | NS_IMETHODIMP |
michael@0 | 2168 | nsRange::CloneContents(nsIDOMDocumentFragment** aReturn) |
michael@0 | 2169 | { |
michael@0 | 2170 | ErrorResult rv; |
michael@0 | 2171 | *aReturn = CloneContents(rv).take(); |
michael@0 | 2172 | return rv.ErrorCode(); |
michael@0 | 2173 | } |
michael@0 | 2174 | |
michael@0 | 2175 | already_AddRefed<DocumentFragment> |
michael@0 | 2176 | nsRange::CloneContents(ErrorResult& aRv) |
michael@0 | 2177 | { |
michael@0 | 2178 | nsCOMPtr<nsINode> commonAncestor = GetCommonAncestorContainer(aRv); |
michael@0 | 2179 | MOZ_ASSERT(!aRv.Failed(), "GetCommonAncestorContainer() shouldn't fail!"); |
michael@0 | 2180 | |
michael@0 | 2181 | nsCOMPtr<nsIDocument> doc = mStartParent->OwnerDoc(); |
michael@0 | 2182 | NS_ASSERTION(doc, "CloneContents needs a document to continue."); |
michael@0 | 2183 | if (!doc) { |
michael@0 | 2184 | aRv.Throw(NS_ERROR_FAILURE); |
michael@0 | 2185 | return nullptr; |
michael@0 | 2186 | } |
michael@0 | 2187 | |
michael@0 | 2188 | // Create a new document fragment in the context of this document, |
michael@0 | 2189 | // which might be null |
michael@0 | 2190 | |
michael@0 | 2191 | |
michael@0 | 2192 | nsRefPtr<DocumentFragment> clonedFrag = |
michael@0 | 2193 | new DocumentFragment(doc->NodeInfoManager()); |
michael@0 | 2194 | |
michael@0 | 2195 | nsCOMPtr<nsINode> commonCloneAncestor = clonedFrag.get(); |
michael@0 | 2196 | |
michael@0 | 2197 | // Create and initialize a subtree iterator that will give |
michael@0 | 2198 | // us all the subtrees within the range. |
michael@0 | 2199 | |
michael@0 | 2200 | RangeSubtreeIterator iter; |
michael@0 | 2201 | |
michael@0 | 2202 | aRv = iter.Init(this); |
michael@0 | 2203 | if (aRv.Failed()) { |
michael@0 | 2204 | return nullptr; |
michael@0 | 2205 | } |
michael@0 | 2206 | |
michael@0 | 2207 | if (iter.IsDone()) |
michael@0 | 2208 | { |
michael@0 | 2209 | // There's nothing to add to the doc frag, we must be done! |
michael@0 | 2210 | return clonedFrag.forget(); |
michael@0 | 2211 | } |
michael@0 | 2212 | |
michael@0 | 2213 | iter.First(); |
michael@0 | 2214 | |
michael@0 | 2215 | // With the exception of text nodes that contain one of the range |
michael@0 | 2216 | // end points and elements which don't have any content selected the subtree |
michael@0 | 2217 | // iterator should only give us back subtrees that are completely contained |
michael@0 | 2218 | // between the range's end points. |
michael@0 | 2219 | // |
michael@0 | 2220 | // Unfortunately these subtrees don't contain the parent hierarchy/context |
michael@0 | 2221 | // that the Range spec requires us to return. This loop clones the |
michael@0 | 2222 | // parent hierarchy, adds a cloned version of the subtree, to it, then |
michael@0 | 2223 | // correctly places this new subtree into the doc fragment. |
michael@0 | 2224 | |
michael@0 | 2225 | while (!iter.IsDone()) |
michael@0 | 2226 | { |
michael@0 | 2227 | nsCOMPtr<nsINode> node = iter.GetCurrentNode(); |
michael@0 | 2228 | bool deepClone = !node->IsElement() || |
michael@0 | 2229 | (!(node == mEndParent && mEndOffset == 0) && |
michael@0 | 2230 | !(node == mStartParent && |
michael@0 | 2231 | mStartOffset == |
michael@0 | 2232 | int32_t(node->AsElement()->GetChildCount()))); |
michael@0 | 2233 | |
michael@0 | 2234 | // Clone the current subtree! |
michael@0 | 2235 | |
michael@0 | 2236 | nsCOMPtr<nsINode> clone = node->CloneNode(deepClone, aRv); |
michael@0 | 2237 | if (aRv.Failed()) { |
michael@0 | 2238 | return nullptr; |
michael@0 | 2239 | } |
michael@0 | 2240 | |
michael@0 | 2241 | // If it's CharacterData, make sure we only clone what |
michael@0 | 2242 | // is in the range. |
michael@0 | 2243 | // |
michael@0 | 2244 | // XXX_kin: We need to also handle ProcessingInstruction |
michael@0 | 2245 | // XXX_kin: according to the spec. |
michael@0 | 2246 | |
michael@0 | 2247 | nsCOMPtr<nsIDOMCharacterData> charData(do_QueryInterface(clone)); |
michael@0 | 2248 | |
michael@0 | 2249 | if (charData) |
michael@0 | 2250 | { |
michael@0 | 2251 | if (node == mEndParent) |
michael@0 | 2252 | { |
michael@0 | 2253 | // We only need the data before mEndOffset, so get rid of any |
michael@0 | 2254 | // data after it. |
michael@0 | 2255 | |
michael@0 | 2256 | uint32_t dataLength = 0; |
michael@0 | 2257 | aRv = charData->GetLength(&dataLength); |
michael@0 | 2258 | if (aRv.Failed()) { |
michael@0 | 2259 | return nullptr; |
michael@0 | 2260 | } |
michael@0 | 2261 | |
michael@0 | 2262 | if (dataLength > (uint32_t)mEndOffset) |
michael@0 | 2263 | { |
michael@0 | 2264 | aRv = charData->DeleteData(mEndOffset, dataLength - mEndOffset); |
michael@0 | 2265 | if (aRv.Failed()) { |
michael@0 | 2266 | return nullptr; |
michael@0 | 2267 | } |
michael@0 | 2268 | } |
michael@0 | 2269 | } |
michael@0 | 2270 | |
michael@0 | 2271 | if (node == mStartParent) |
michael@0 | 2272 | { |
michael@0 | 2273 | // We don't need any data before mStartOffset, so just |
michael@0 | 2274 | // delete it! |
michael@0 | 2275 | |
michael@0 | 2276 | if (mStartOffset > 0) |
michael@0 | 2277 | { |
michael@0 | 2278 | aRv = charData->DeleteData(0, mStartOffset); |
michael@0 | 2279 | if (aRv.Failed()) { |
michael@0 | 2280 | return nullptr; |
michael@0 | 2281 | } |
michael@0 | 2282 | } |
michael@0 | 2283 | } |
michael@0 | 2284 | } |
michael@0 | 2285 | |
michael@0 | 2286 | // Clone the parent hierarchy between commonAncestor and node. |
michael@0 | 2287 | |
michael@0 | 2288 | nsCOMPtr<nsINode> closestAncestor, farthestAncestor; |
michael@0 | 2289 | |
michael@0 | 2290 | aRv = CloneParentsBetween(commonAncestor, node, |
michael@0 | 2291 | getter_AddRefs(closestAncestor), |
michael@0 | 2292 | getter_AddRefs(farthestAncestor)); |
michael@0 | 2293 | |
michael@0 | 2294 | if (aRv.Failed()) { |
michael@0 | 2295 | return nullptr; |
michael@0 | 2296 | } |
michael@0 | 2297 | |
michael@0 | 2298 | // Hook the parent hierarchy/context of the subtree into the clone tree. |
michael@0 | 2299 | |
michael@0 | 2300 | if (farthestAncestor) |
michael@0 | 2301 | { |
michael@0 | 2302 | commonCloneAncestor->AppendChild(*farthestAncestor, aRv); |
michael@0 | 2303 | |
michael@0 | 2304 | if (aRv.Failed()) { |
michael@0 | 2305 | return nullptr; |
michael@0 | 2306 | } |
michael@0 | 2307 | } |
michael@0 | 2308 | |
michael@0 | 2309 | // Place the cloned subtree into the cloned doc frag tree! |
michael@0 | 2310 | |
michael@0 | 2311 | nsCOMPtr<nsINode> cloneNode = do_QueryInterface(clone); |
michael@0 | 2312 | if (closestAncestor) |
michael@0 | 2313 | { |
michael@0 | 2314 | // Append the subtree under closestAncestor since it is the |
michael@0 | 2315 | // immediate parent of the subtree. |
michael@0 | 2316 | |
michael@0 | 2317 | closestAncestor->AppendChild(*cloneNode, aRv); |
michael@0 | 2318 | } |
michael@0 | 2319 | else |
michael@0 | 2320 | { |
michael@0 | 2321 | // If we get here, there is no missing parent hierarchy between |
michael@0 | 2322 | // commonAncestor and node, so just append clone to commonCloneAncestor. |
michael@0 | 2323 | |
michael@0 | 2324 | commonCloneAncestor->AppendChild(*cloneNode, aRv); |
michael@0 | 2325 | } |
michael@0 | 2326 | if (aRv.Failed()) { |
michael@0 | 2327 | return nullptr; |
michael@0 | 2328 | } |
michael@0 | 2329 | |
michael@0 | 2330 | // Get the next subtree to be processed. The idea here is to setup |
michael@0 | 2331 | // the parameters for the next iteration of the loop. |
michael@0 | 2332 | |
michael@0 | 2333 | iter.Next(); |
michael@0 | 2334 | |
michael@0 | 2335 | if (iter.IsDone()) |
michael@0 | 2336 | break; // We must be done! |
michael@0 | 2337 | |
michael@0 | 2338 | nsCOMPtr<nsINode> nextNode = iter.GetCurrentNode(); |
michael@0 | 2339 | if (!nextNode) { |
michael@0 | 2340 | aRv.Throw(NS_ERROR_FAILURE); |
michael@0 | 2341 | return nullptr; |
michael@0 | 2342 | } |
michael@0 | 2343 | |
michael@0 | 2344 | // Get node and nextNode's common parent. |
michael@0 | 2345 | commonAncestor = nsContentUtils::GetCommonAncestor(node, nextNode); |
michael@0 | 2346 | |
michael@0 | 2347 | if (!commonAncestor) { |
michael@0 | 2348 | aRv.Throw(NS_ERROR_FAILURE); |
michael@0 | 2349 | return nullptr; |
michael@0 | 2350 | } |
michael@0 | 2351 | |
michael@0 | 2352 | // Find the equivalent of commonAncestor in the cloned tree! |
michael@0 | 2353 | |
michael@0 | 2354 | while (node && node != commonAncestor) |
michael@0 | 2355 | { |
michael@0 | 2356 | node = node->GetParentNode(); |
michael@0 | 2357 | if (aRv.Failed()) { |
michael@0 | 2358 | return nullptr; |
michael@0 | 2359 | } |
michael@0 | 2360 | |
michael@0 | 2361 | if (!node) { |
michael@0 | 2362 | aRv.Throw(NS_ERROR_FAILURE); |
michael@0 | 2363 | return nullptr; |
michael@0 | 2364 | } |
michael@0 | 2365 | |
michael@0 | 2366 | cloneNode = cloneNode->GetParentNode(); |
michael@0 | 2367 | if (!cloneNode) { |
michael@0 | 2368 | aRv.Throw(NS_ERROR_FAILURE); |
michael@0 | 2369 | return nullptr; |
michael@0 | 2370 | } |
michael@0 | 2371 | } |
michael@0 | 2372 | |
michael@0 | 2373 | commonCloneAncestor = cloneNode; |
michael@0 | 2374 | } |
michael@0 | 2375 | |
michael@0 | 2376 | return clonedFrag.forget(); |
michael@0 | 2377 | } |
michael@0 | 2378 | |
michael@0 | 2379 | already_AddRefed<nsRange> |
michael@0 | 2380 | nsRange::CloneRange() const |
michael@0 | 2381 | { |
michael@0 | 2382 | nsRefPtr<nsRange> range = new nsRange(mOwner); |
michael@0 | 2383 | |
michael@0 | 2384 | range->SetMaySpanAnonymousSubtrees(mMaySpanAnonymousSubtrees); |
michael@0 | 2385 | |
michael@0 | 2386 | range->DoSetRange(mStartParent, mStartOffset, mEndParent, mEndOffset, mRoot); |
michael@0 | 2387 | |
michael@0 | 2388 | return range.forget(); |
michael@0 | 2389 | } |
michael@0 | 2390 | |
michael@0 | 2391 | NS_IMETHODIMP |
michael@0 | 2392 | nsRange::CloneRange(nsIDOMRange** aReturn) |
michael@0 | 2393 | { |
michael@0 | 2394 | *aReturn = CloneRange().take(); |
michael@0 | 2395 | return NS_OK; |
michael@0 | 2396 | } |
michael@0 | 2397 | |
michael@0 | 2398 | NS_IMETHODIMP |
michael@0 | 2399 | nsRange::InsertNode(nsIDOMNode* aNode) |
michael@0 | 2400 | { |
michael@0 | 2401 | nsCOMPtr<nsINode> node = do_QueryInterface(aNode); |
michael@0 | 2402 | if (!node) { |
michael@0 | 2403 | return NS_ERROR_DOM_NOT_OBJECT_ERR; |
michael@0 | 2404 | } |
michael@0 | 2405 | |
michael@0 | 2406 | ErrorResult rv; |
michael@0 | 2407 | InsertNode(*node, rv); |
michael@0 | 2408 | return rv.ErrorCode(); |
michael@0 | 2409 | } |
michael@0 | 2410 | |
michael@0 | 2411 | void |
michael@0 | 2412 | nsRange::InsertNode(nsINode& aNode, ErrorResult& aRv) |
michael@0 | 2413 | { |
michael@0 | 2414 | if (!nsContentUtils::CanCallerAccess(&aNode)) { |
michael@0 | 2415 | aRv.Throw(NS_ERROR_DOM_SECURITY_ERR); |
michael@0 | 2416 | return; |
michael@0 | 2417 | } |
michael@0 | 2418 | |
michael@0 | 2419 | int32_t tStartOffset = StartOffset(); |
michael@0 | 2420 | |
michael@0 | 2421 | nsCOMPtr<nsINode> tStartContainer = GetStartContainer(aRv); |
michael@0 | 2422 | if (aRv.Failed()) { |
michael@0 | 2423 | return; |
michael@0 | 2424 | } |
michael@0 | 2425 | |
michael@0 | 2426 | // This is the node we'll be inserting before, and its parent |
michael@0 | 2427 | nsCOMPtr<nsINode> referenceNode; |
michael@0 | 2428 | nsCOMPtr<nsINode> referenceParentNode = tStartContainer; |
michael@0 | 2429 | |
michael@0 | 2430 | nsCOMPtr<nsIDOMText> startTextNode(do_QueryInterface(tStartContainer)); |
michael@0 | 2431 | nsCOMPtr<nsIDOMNodeList> tChildList; |
michael@0 | 2432 | if (startTextNode) { |
michael@0 | 2433 | referenceParentNode = tStartContainer->GetParentNode(); |
michael@0 | 2434 | if (!referenceParentNode) { |
michael@0 | 2435 | aRv.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR); |
michael@0 | 2436 | return; |
michael@0 | 2437 | } |
michael@0 | 2438 | |
michael@0 | 2439 | nsCOMPtr<nsIDOMText> secondPart; |
michael@0 | 2440 | aRv = startTextNode->SplitText(tStartOffset, getter_AddRefs(secondPart)); |
michael@0 | 2441 | if (aRv.Failed()) { |
michael@0 | 2442 | return; |
michael@0 | 2443 | } |
michael@0 | 2444 | |
michael@0 | 2445 | referenceNode = do_QueryInterface(secondPart); |
michael@0 | 2446 | } else { |
michael@0 | 2447 | aRv = tStartContainer->AsDOMNode()->GetChildNodes(getter_AddRefs(tChildList)); |
michael@0 | 2448 | if (aRv.Failed()) { |
michael@0 | 2449 | return; |
michael@0 | 2450 | } |
michael@0 | 2451 | |
michael@0 | 2452 | // find the insertion point in the DOM and insert the Node |
michael@0 | 2453 | nsCOMPtr<nsIDOMNode> q; |
michael@0 | 2454 | aRv = tChildList->Item(tStartOffset, getter_AddRefs(q)); |
michael@0 | 2455 | referenceNode = do_QueryInterface(q); |
michael@0 | 2456 | if (aRv.Failed()) { |
michael@0 | 2457 | return; |
michael@0 | 2458 | } |
michael@0 | 2459 | } |
michael@0 | 2460 | |
michael@0 | 2461 | // We might need to update the end to include the new node (bug 433662). |
michael@0 | 2462 | // Ideally we'd only do this if needed, but it's tricky to know when it's |
michael@0 | 2463 | // needed in advance (bug 765799). |
michael@0 | 2464 | int32_t newOffset; |
michael@0 | 2465 | |
michael@0 | 2466 | if (referenceNode) { |
michael@0 | 2467 | newOffset = IndexOf(referenceNode); |
michael@0 | 2468 | } else { |
michael@0 | 2469 | uint32_t length; |
michael@0 | 2470 | aRv = tChildList->GetLength(&length); |
michael@0 | 2471 | if (aRv.Failed()) { |
michael@0 | 2472 | return; |
michael@0 | 2473 | } |
michael@0 | 2474 | |
michael@0 | 2475 | newOffset = length; |
michael@0 | 2476 | } |
michael@0 | 2477 | |
michael@0 | 2478 | if (aNode.NodeType() == nsIDOMNode::DOCUMENT_FRAGMENT_NODE) { |
michael@0 | 2479 | newOffset += aNode.GetChildCount(); |
michael@0 | 2480 | } else { |
michael@0 | 2481 | newOffset++; |
michael@0 | 2482 | } |
michael@0 | 2483 | |
michael@0 | 2484 | // Now actually insert the node |
michael@0 | 2485 | nsCOMPtr<nsINode> tResultNode; |
michael@0 | 2486 | tResultNode = referenceParentNode->InsertBefore(aNode, referenceNode, aRv); |
michael@0 | 2487 | if (aRv.Failed()) { |
michael@0 | 2488 | return; |
michael@0 | 2489 | } |
michael@0 | 2490 | |
michael@0 | 2491 | if (Collapsed()) { |
michael@0 | 2492 | aRv = SetEnd(referenceParentNode, newOffset); |
michael@0 | 2493 | } |
michael@0 | 2494 | } |
michael@0 | 2495 | |
michael@0 | 2496 | NS_IMETHODIMP |
michael@0 | 2497 | nsRange::SurroundContents(nsIDOMNode* aNewParent) |
michael@0 | 2498 | { |
michael@0 | 2499 | nsCOMPtr<nsINode> node = do_QueryInterface(aNewParent); |
michael@0 | 2500 | if (!node) { |
michael@0 | 2501 | return NS_ERROR_DOM_NOT_OBJECT_ERR; |
michael@0 | 2502 | } |
michael@0 | 2503 | ErrorResult rv; |
michael@0 | 2504 | SurroundContents(*node, rv); |
michael@0 | 2505 | return rv.ErrorCode(); |
michael@0 | 2506 | } |
michael@0 | 2507 | |
michael@0 | 2508 | void |
michael@0 | 2509 | nsRange::SurroundContents(nsINode& aNewParent, ErrorResult& aRv) |
michael@0 | 2510 | { |
michael@0 | 2511 | if (!nsContentUtils::CanCallerAccess(&aNewParent)) { |
michael@0 | 2512 | aRv.Throw(NS_ERROR_DOM_SECURITY_ERR); |
michael@0 | 2513 | return; |
michael@0 | 2514 | } |
michael@0 | 2515 | |
michael@0 | 2516 | if (!mRoot) { |
michael@0 | 2517 | aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR); |
michael@0 | 2518 | return; |
michael@0 | 2519 | } |
michael@0 | 2520 | // INVALID_STATE_ERROR: Raised if the Range partially selects a non-text |
michael@0 | 2521 | // node. |
michael@0 | 2522 | if (mStartParent != mEndParent) { |
michael@0 | 2523 | bool startIsText = mStartParent->IsNodeOfType(nsINode::eTEXT); |
michael@0 | 2524 | bool endIsText = mEndParent->IsNodeOfType(nsINode::eTEXT); |
michael@0 | 2525 | nsINode* startGrandParent = mStartParent->GetParentNode(); |
michael@0 | 2526 | nsINode* endGrandParent = mEndParent->GetParentNode(); |
michael@0 | 2527 | if (!((startIsText && endIsText && |
michael@0 | 2528 | startGrandParent && |
michael@0 | 2529 | startGrandParent == endGrandParent) || |
michael@0 | 2530 | (startIsText && |
michael@0 | 2531 | startGrandParent && |
michael@0 | 2532 | startGrandParent == mEndParent) || |
michael@0 | 2533 | (endIsText && |
michael@0 | 2534 | endGrandParent && |
michael@0 | 2535 | endGrandParent == mStartParent))) { |
michael@0 | 2536 | aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR); |
michael@0 | 2537 | return; |
michael@0 | 2538 | } |
michael@0 | 2539 | } |
michael@0 | 2540 | |
michael@0 | 2541 | // INVALID_NODE_TYPE_ERROR if aNewParent is something that can't be inserted |
michael@0 | 2542 | // (Document, DocumentType, DocumentFragment) |
michael@0 | 2543 | uint16_t nodeType = aNewParent.NodeType(); |
michael@0 | 2544 | if (nodeType == nsIDOMNode::DOCUMENT_NODE || |
michael@0 | 2545 | nodeType == nsIDOMNode::DOCUMENT_TYPE_NODE || |
michael@0 | 2546 | nodeType == nsIDOMNode::DOCUMENT_FRAGMENT_NODE) { |
michael@0 | 2547 | aRv.Throw(NS_ERROR_DOM_INVALID_NODE_TYPE_ERR); |
michael@0 | 2548 | return; |
michael@0 | 2549 | } |
michael@0 | 2550 | |
michael@0 | 2551 | // Extract the contents within the range. |
michael@0 | 2552 | |
michael@0 | 2553 | nsRefPtr<DocumentFragment> docFrag = ExtractContents(aRv); |
michael@0 | 2554 | |
michael@0 | 2555 | if (aRv.Failed()) { |
michael@0 | 2556 | return; |
michael@0 | 2557 | } |
michael@0 | 2558 | |
michael@0 | 2559 | if (!docFrag) { |
michael@0 | 2560 | aRv.Throw(NS_ERROR_FAILURE); |
michael@0 | 2561 | return; |
michael@0 | 2562 | } |
michael@0 | 2563 | |
michael@0 | 2564 | // Spec says we need to remove all of aNewParent's |
michael@0 | 2565 | // children prior to insertion. |
michael@0 | 2566 | |
michael@0 | 2567 | nsCOMPtr<nsINodeList> children = aNewParent.ChildNodes(); |
michael@0 | 2568 | if (!children) { |
michael@0 | 2569 | aRv.Throw(NS_ERROR_FAILURE); |
michael@0 | 2570 | return; |
michael@0 | 2571 | } |
michael@0 | 2572 | |
michael@0 | 2573 | uint32_t numChildren = children->Length(); |
michael@0 | 2574 | |
michael@0 | 2575 | while (numChildren) |
michael@0 | 2576 | { |
michael@0 | 2577 | nsCOMPtr<nsINode> child = children->Item(--numChildren); |
michael@0 | 2578 | if (!child) { |
michael@0 | 2579 | aRv.Throw(NS_ERROR_FAILURE); |
michael@0 | 2580 | return; |
michael@0 | 2581 | } |
michael@0 | 2582 | |
michael@0 | 2583 | aNewParent.RemoveChild(*child, aRv); |
michael@0 | 2584 | if (aRv.Failed()) { |
michael@0 | 2585 | return; |
michael@0 | 2586 | } |
michael@0 | 2587 | } |
michael@0 | 2588 | |
michael@0 | 2589 | // Insert aNewParent at the range's start point. |
michael@0 | 2590 | |
michael@0 | 2591 | InsertNode(aNewParent, aRv); |
michael@0 | 2592 | if (aRv.Failed()) { |
michael@0 | 2593 | return; |
michael@0 | 2594 | } |
michael@0 | 2595 | |
michael@0 | 2596 | // Append the content we extracted under aNewParent. |
michael@0 | 2597 | aNewParent.AppendChild(*docFrag, aRv); |
michael@0 | 2598 | if (aRv.Failed()) { |
michael@0 | 2599 | return; |
michael@0 | 2600 | } |
michael@0 | 2601 | |
michael@0 | 2602 | // Select aNewParent, and its contents. |
michael@0 | 2603 | |
michael@0 | 2604 | SelectNode(aNewParent, aRv); |
michael@0 | 2605 | } |
michael@0 | 2606 | |
michael@0 | 2607 | NS_IMETHODIMP |
michael@0 | 2608 | nsRange::ToString(nsAString& aReturn) |
michael@0 | 2609 | { |
michael@0 | 2610 | // clear the string |
michael@0 | 2611 | aReturn.Truncate(); |
michael@0 | 2612 | |
michael@0 | 2613 | // If we're unpositioned, return the empty string |
michael@0 | 2614 | if (!mIsPositioned) { |
michael@0 | 2615 | return NS_OK; |
michael@0 | 2616 | } |
michael@0 | 2617 | |
michael@0 | 2618 | #ifdef DEBUG_range |
michael@0 | 2619 | printf("Range dump: -----------------------\n"); |
michael@0 | 2620 | #endif /* DEBUG */ |
michael@0 | 2621 | |
michael@0 | 2622 | // effeciency hack for simple case |
michael@0 | 2623 | if (mStartParent == mEndParent) |
michael@0 | 2624 | { |
michael@0 | 2625 | nsCOMPtr<nsIDOMText> textNode( do_QueryInterface(mStartParent) ); |
michael@0 | 2626 | |
michael@0 | 2627 | if (textNode) |
michael@0 | 2628 | { |
michael@0 | 2629 | #ifdef DEBUG_range |
michael@0 | 2630 | // If debug, dump it: |
michael@0 | 2631 | nsCOMPtr<nsIContent> cN (do_QueryInterface(mStartParent)); |
michael@0 | 2632 | if (cN) cN->List(stdout); |
michael@0 | 2633 | printf("End Range dump: -----------------------\n"); |
michael@0 | 2634 | #endif /* DEBUG */ |
michael@0 | 2635 | |
michael@0 | 2636 | // grab the text |
michael@0 | 2637 | if (NS_FAILED(textNode->SubstringData(mStartOffset,mEndOffset-mStartOffset,aReturn))) |
michael@0 | 2638 | return NS_ERROR_UNEXPECTED; |
michael@0 | 2639 | return NS_OK; |
michael@0 | 2640 | } |
michael@0 | 2641 | } |
michael@0 | 2642 | |
michael@0 | 2643 | /* complex case: mStartParent != mEndParent, or mStartParent not a text node |
michael@0 | 2644 | revisit - there are potential optimizations here and also tradeoffs. |
michael@0 | 2645 | */ |
michael@0 | 2646 | |
michael@0 | 2647 | nsCOMPtr<nsIContentIterator> iter = NS_NewContentIterator(); |
michael@0 | 2648 | nsresult rv = iter->Init(this); |
michael@0 | 2649 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 2650 | |
michael@0 | 2651 | nsString tempString; |
michael@0 | 2652 | |
michael@0 | 2653 | // loop through the content iterator, which returns nodes in the range in |
michael@0 | 2654 | // close tag order, and grab the text from any text node |
michael@0 | 2655 | while (!iter->IsDone()) |
michael@0 | 2656 | { |
michael@0 | 2657 | nsINode *n = iter->GetCurrentNode(); |
michael@0 | 2658 | |
michael@0 | 2659 | #ifdef DEBUG_range |
michael@0 | 2660 | // If debug, dump it: |
michael@0 | 2661 | n->List(stdout); |
michael@0 | 2662 | #endif /* DEBUG */ |
michael@0 | 2663 | nsCOMPtr<nsIDOMText> textNode(do_QueryInterface(n)); |
michael@0 | 2664 | if (textNode) // if it's a text node, get the text |
michael@0 | 2665 | { |
michael@0 | 2666 | if (n == mStartParent) // only include text past start offset |
michael@0 | 2667 | { |
michael@0 | 2668 | uint32_t strLength; |
michael@0 | 2669 | textNode->GetLength(&strLength); |
michael@0 | 2670 | textNode->SubstringData(mStartOffset,strLength-mStartOffset,tempString); |
michael@0 | 2671 | aReturn += tempString; |
michael@0 | 2672 | } |
michael@0 | 2673 | else if (n == mEndParent) // only include text before end offset |
michael@0 | 2674 | { |
michael@0 | 2675 | textNode->SubstringData(0,mEndOffset,tempString); |
michael@0 | 2676 | aReturn += tempString; |
michael@0 | 2677 | } |
michael@0 | 2678 | else // grab the whole kit-n-kaboodle |
michael@0 | 2679 | { |
michael@0 | 2680 | textNode->GetData(tempString); |
michael@0 | 2681 | aReturn += tempString; |
michael@0 | 2682 | } |
michael@0 | 2683 | } |
michael@0 | 2684 | |
michael@0 | 2685 | iter->Next(); |
michael@0 | 2686 | } |
michael@0 | 2687 | |
michael@0 | 2688 | #ifdef DEBUG_range |
michael@0 | 2689 | printf("End Range dump: -----------------------\n"); |
michael@0 | 2690 | #endif /* DEBUG */ |
michael@0 | 2691 | return NS_OK; |
michael@0 | 2692 | } |
michael@0 | 2693 | |
michael@0 | 2694 | |
michael@0 | 2695 | |
michael@0 | 2696 | NS_IMETHODIMP |
michael@0 | 2697 | nsRange::Detach() |
michael@0 | 2698 | { |
michael@0 | 2699 | // No-op, but still set mIsDetached for telemetry (bug 702948) |
michael@0 | 2700 | mIsDetached = true; |
michael@0 | 2701 | return NS_OK; |
michael@0 | 2702 | } |
michael@0 | 2703 | |
michael@0 | 2704 | NS_IMETHODIMP |
michael@0 | 2705 | nsRange::CreateContextualFragment(const nsAString& aFragment, |
michael@0 | 2706 | nsIDOMDocumentFragment** aReturn) |
michael@0 | 2707 | { |
michael@0 | 2708 | if (mIsPositioned) { |
michael@0 | 2709 | return nsContentUtils::CreateContextualFragment(mStartParent, aFragment, |
michael@0 | 2710 | false, aReturn); |
michael@0 | 2711 | } |
michael@0 | 2712 | return NS_ERROR_FAILURE; |
michael@0 | 2713 | } |
michael@0 | 2714 | |
michael@0 | 2715 | already_AddRefed<DocumentFragment> |
michael@0 | 2716 | nsRange::CreateContextualFragment(const nsAString& aFragment, ErrorResult& aRv) |
michael@0 | 2717 | { |
michael@0 | 2718 | if (!mIsPositioned) { |
michael@0 | 2719 | aRv.Throw(NS_ERROR_FAILURE); |
michael@0 | 2720 | return nullptr; |
michael@0 | 2721 | } |
michael@0 | 2722 | |
michael@0 | 2723 | return nsContentUtils::CreateContextualFragment(mStartParent, aFragment, |
michael@0 | 2724 | false, aRv); |
michael@0 | 2725 | } |
michael@0 | 2726 | |
michael@0 | 2727 | static void ExtractRectFromOffset(nsIFrame* aFrame, |
michael@0 | 2728 | const nsIFrame* aRelativeTo, |
michael@0 | 2729 | const int32_t aOffset, nsRect* aR, bool aKeepLeft) |
michael@0 | 2730 | { |
michael@0 | 2731 | nsPoint point; |
michael@0 | 2732 | aFrame->GetPointFromOffset(aOffset, &point); |
michael@0 | 2733 | |
michael@0 | 2734 | point += aFrame->GetOffsetTo(aRelativeTo); |
michael@0 | 2735 | |
michael@0 | 2736 | //given a point.x, extract left or right portion of rect aR |
michael@0 | 2737 | //point.x has to be within this rect |
michael@0 | 2738 | NS_ASSERTION(aR->x <= point.x && point.x <= aR->XMost(), |
michael@0 | 2739 | "point.x should not be outside of rect r"); |
michael@0 | 2740 | |
michael@0 | 2741 | if (aKeepLeft) { |
michael@0 | 2742 | aR->width = point.x - aR->x; |
michael@0 | 2743 | } else { |
michael@0 | 2744 | aR->width = aR->XMost() - point.x; |
michael@0 | 2745 | aR->x = point.x; |
michael@0 | 2746 | } |
michael@0 | 2747 | } |
michael@0 | 2748 | |
michael@0 | 2749 | static nsTextFrame* |
michael@0 | 2750 | GetTextFrameForContent(nsIContent* aContent) |
michael@0 | 2751 | { |
michael@0 | 2752 | nsIPresShell* presShell = aContent->OwnerDoc()->GetShell(); |
michael@0 | 2753 | if (presShell) { |
michael@0 | 2754 | presShell->FrameConstructor()->EnsureFrameForTextNode( |
michael@0 | 2755 | static_cast<nsGenericDOMDataNode*>(aContent)); |
michael@0 | 2756 | aContent->OwnerDoc()->FlushPendingNotifications(Flush_Layout); |
michael@0 | 2757 | nsIFrame* frame = aContent->GetPrimaryFrame(); |
michael@0 | 2758 | if (frame && frame->GetType() == nsGkAtoms::textFrame) { |
michael@0 | 2759 | return static_cast<nsTextFrame*>(frame); |
michael@0 | 2760 | } |
michael@0 | 2761 | } |
michael@0 | 2762 | return nullptr; |
michael@0 | 2763 | } |
michael@0 | 2764 | |
michael@0 | 2765 | static nsresult GetPartialTextRect(nsLayoutUtils::RectCallback* aCallback, |
michael@0 | 2766 | nsIContent* aContent, int32_t aStartOffset, int32_t aEndOffset) |
michael@0 | 2767 | { |
michael@0 | 2768 | nsTextFrame* textFrame = GetTextFrameForContent(aContent); |
michael@0 | 2769 | if (textFrame) { |
michael@0 | 2770 | nsIFrame* relativeTo = nsLayoutUtils::GetContainingBlockForClientRect(textFrame); |
michael@0 | 2771 | for (nsTextFrame* f = textFrame; f; f = static_cast<nsTextFrame*>(f->GetNextContinuation())) { |
michael@0 | 2772 | int32_t fstart = f->GetContentOffset(), fend = f->GetContentEnd(); |
michael@0 | 2773 | if (fend <= aStartOffset || fstart >= aEndOffset) |
michael@0 | 2774 | continue; |
michael@0 | 2775 | |
michael@0 | 2776 | // overlapping with the offset we want |
michael@0 | 2777 | f->EnsureTextRun(nsTextFrame::eInflated); |
michael@0 | 2778 | NS_ENSURE_TRUE(f->GetTextRun(nsTextFrame::eInflated), NS_ERROR_OUT_OF_MEMORY); |
michael@0 | 2779 | bool rtl = f->GetTextRun(nsTextFrame::eInflated)->IsRightToLeft(); |
michael@0 | 2780 | nsRect r(f->GetOffsetTo(relativeTo), f->GetSize()); |
michael@0 | 2781 | if (fstart < aStartOffset) { |
michael@0 | 2782 | // aStartOffset is within this frame |
michael@0 | 2783 | ExtractRectFromOffset(f, relativeTo, aStartOffset, &r, rtl); |
michael@0 | 2784 | } |
michael@0 | 2785 | if (fend > aEndOffset) { |
michael@0 | 2786 | // aEndOffset is in the middle of this frame |
michael@0 | 2787 | ExtractRectFromOffset(f, relativeTo, aEndOffset, &r, !rtl); |
michael@0 | 2788 | } |
michael@0 | 2789 | aCallback->AddRect(r); |
michael@0 | 2790 | } |
michael@0 | 2791 | } |
michael@0 | 2792 | return NS_OK; |
michael@0 | 2793 | } |
michael@0 | 2794 | |
michael@0 | 2795 | static void CollectClientRects(nsLayoutUtils::RectCallback* aCollector, |
michael@0 | 2796 | nsRange* aRange, |
michael@0 | 2797 | nsINode* aStartParent, int32_t aStartOffset, |
michael@0 | 2798 | nsINode* aEndParent, int32_t aEndOffset) |
michael@0 | 2799 | { |
michael@0 | 2800 | // Hold strong pointers across the flush |
michael@0 | 2801 | nsCOMPtr<nsINode> startContainer = aStartParent; |
michael@0 | 2802 | nsCOMPtr<nsINode> endContainer = aEndParent; |
michael@0 | 2803 | |
michael@0 | 2804 | // Flush out layout so our frames are up to date. |
michael@0 | 2805 | if (!aStartParent->IsInDoc()) { |
michael@0 | 2806 | return; |
michael@0 | 2807 | } |
michael@0 | 2808 | |
michael@0 | 2809 | aStartParent->OwnerDoc()->FlushPendingNotifications(Flush_Layout); |
michael@0 | 2810 | |
michael@0 | 2811 | // Recheck whether we're still in the document |
michael@0 | 2812 | if (!aStartParent->IsInDoc()) { |
michael@0 | 2813 | return; |
michael@0 | 2814 | } |
michael@0 | 2815 | |
michael@0 | 2816 | RangeSubtreeIterator iter; |
michael@0 | 2817 | |
michael@0 | 2818 | nsresult rv = iter.Init(aRange); |
michael@0 | 2819 | if (NS_FAILED(rv)) return; |
michael@0 | 2820 | |
michael@0 | 2821 | if (iter.IsDone()) { |
michael@0 | 2822 | // the range is collapsed, only continue if the cursor is in a text node |
michael@0 | 2823 | nsCOMPtr<nsIContent> content = do_QueryInterface(aStartParent); |
michael@0 | 2824 | if (content && content->IsNodeOfType(nsINode::eTEXT)) { |
michael@0 | 2825 | nsTextFrame* textFrame = GetTextFrameForContent(content); |
michael@0 | 2826 | if (textFrame) { |
michael@0 | 2827 | int32_t outOffset; |
michael@0 | 2828 | nsIFrame* outFrame; |
michael@0 | 2829 | textFrame->GetChildFrameContainingOffset(aStartOffset, false, |
michael@0 | 2830 | &outOffset, &outFrame); |
michael@0 | 2831 | if (outFrame) { |
michael@0 | 2832 | nsIFrame* relativeTo = |
michael@0 | 2833 | nsLayoutUtils::GetContainingBlockForClientRect(outFrame); |
michael@0 | 2834 | nsRect r(outFrame->GetOffsetTo(relativeTo), outFrame->GetSize()); |
michael@0 | 2835 | ExtractRectFromOffset(outFrame, relativeTo, aStartOffset, &r, false); |
michael@0 | 2836 | r.width = 0; |
michael@0 | 2837 | aCollector->AddRect(r); |
michael@0 | 2838 | } |
michael@0 | 2839 | } |
michael@0 | 2840 | } |
michael@0 | 2841 | return; |
michael@0 | 2842 | } |
michael@0 | 2843 | |
michael@0 | 2844 | do { |
michael@0 | 2845 | nsCOMPtr<nsINode> node = iter.GetCurrentNode(); |
michael@0 | 2846 | iter.Next(); |
michael@0 | 2847 | nsCOMPtr<nsIContent> content = do_QueryInterface(node); |
michael@0 | 2848 | if (!content) |
michael@0 | 2849 | continue; |
michael@0 | 2850 | if (content->IsNodeOfType(nsINode::eTEXT)) { |
michael@0 | 2851 | if (node == startContainer) { |
michael@0 | 2852 | int32_t offset = startContainer == endContainer ? |
michael@0 | 2853 | aEndOffset : content->GetText()->GetLength(); |
michael@0 | 2854 | GetPartialTextRect(aCollector, content, aStartOffset, offset); |
michael@0 | 2855 | continue; |
michael@0 | 2856 | } else if (node == endContainer) { |
michael@0 | 2857 | GetPartialTextRect(aCollector, content, 0, aEndOffset); |
michael@0 | 2858 | continue; |
michael@0 | 2859 | } |
michael@0 | 2860 | } |
michael@0 | 2861 | |
michael@0 | 2862 | nsIFrame* frame = content->GetPrimaryFrame(); |
michael@0 | 2863 | if (frame) { |
michael@0 | 2864 | nsLayoutUtils::GetAllInFlowRects(frame, |
michael@0 | 2865 | nsLayoutUtils::GetContainingBlockForClientRect(frame), aCollector); |
michael@0 | 2866 | } |
michael@0 | 2867 | } while (!iter.IsDone()); |
michael@0 | 2868 | } |
michael@0 | 2869 | |
michael@0 | 2870 | NS_IMETHODIMP |
michael@0 | 2871 | nsRange::GetBoundingClientRect(nsIDOMClientRect** aResult) |
michael@0 | 2872 | { |
michael@0 | 2873 | *aResult = GetBoundingClientRect().take(); |
michael@0 | 2874 | return NS_OK; |
michael@0 | 2875 | } |
michael@0 | 2876 | |
michael@0 | 2877 | already_AddRefed<DOMRect> |
michael@0 | 2878 | nsRange::GetBoundingClientRect() |
michael@0 | 2879 | { |
michael@0 | 2880 | nsRefPtr<DOMRect> rect = new DOMRect(ToSupports(this)); |
michael@0 | 2881 | if (!mStartParent) { |
michael@0 | 2882 | return rect.forget(); |
michael@0 | 2883 | } |
michael@0 | 2884 | |
michael@0 | 2885 | nsLayoutUtils::RectAccumulator accumulator; |
michael@0 | 2886 | CollectClientRects(&accumulator, this, mStartParent, mStartOffset, |
michael@0 | 2887 | mEndParent, mEndOffset); |
michael@0 | 2888 | |
michael@0 | 2889 | nsRect r = accumulator.mResultRect.IsEmpty() ? accumulator.mFirstRect : |
michael@0 | 2890 | accumulator.mResultRect; |
michael@0 | 2891 | rect->SetLayoutRect(r); |
michael@0 | 2892 | return rect.forget(); |
michael@0 | 2893 | } |
michael@0 | 2894 | |
michael@0 | 2895 | NS_IMETHODIMP |
michael@0 | 2896 | nsRange::GetClientRects(nsIDOMClientRectList** aResult) |
michael@0 | 2897 | { |
michael@0 | 2898 | *aResult = GetClientRects().take(); |
michael@0 | 2899 | return NS_OK; |
michael@0 | 2900 | } |
michael@0 | 2901 | |
michael@0 | 2902 | already_AddRefed<DOMRectList> |
michael@0 | 2903 | nsRange::GetClientRects() |
michael@0 | 2904 | { |
michael@0 | 2905 | if (!mStartParent) { |
michael@0 | 2906 | return nullptr; |
michael@0 | 2907 | } |
michael@0 | 2908 | |
michael@0 | 2909 | nsRefPtr<DOMRectList> rectList = |
michael@0 | 2910 | new DOMRectList(static_cast<nsIDOMRange*>(this)); |
michael@0 | 2911 | |
michael@0 | 2912 | nsLayoutUtils::RectListBuilder builder(rectList); |
michael@0 | 2913 | |
michael@0 | 2914 | CollectClientRects(&builder, this, mStartParent, mStartOffset, |
michael@0 | 2915 | mEndParent, mEndOffset); |
michael@0 | 2916 | return rectList.forget(); |
michael@0 | 2917 | } |
michael@0 | 2918 | |
michael@0 | 2919 | NS_IMETHODIMP |
michael@0 | 2920 | nsRange::GetUsedFontFaces(nsIDOMFontFaceList** aResult) |
michael@0 | 2921 | { |
michael@0 | 2922 | *aResult = nullptr; |
michael@0 | 2923 | |
michael@0 | 2924 | NS_ENSURE_TRUE(mStartParent, NS_ERROR_UNEXPECTED); |
michael@0 | 2925 | |
michael@0 | 2926 | nsCOMPtr<nsINode> startContainer = do_QueryInterface(mStartParent); |
michael@0 | 2927 | nsCOMPtr<nsINode> endContainer = do_QueryInterface(mEndParent); |
michael@0 | 2928 | |
michael@0 | 2929 | // Flush out layout so our frames are up to date. |
michael@0 | 2930 | nsIDocument* doc = mStartParent->OwnerDoc(); |
michael@0 | 2931 | NS_ENSURE_TRUE(doc, NS_ERROR_UNEXPECTED); |
michael@0 | 2932 | doc->FlushPendingNotifications(Flush_Frames); |
michael@0 | 2933 | |
michael@0 | 2934 | // Recheck whether we're still in the document |
michael@0 | 2935 | NS_ENSURE_TRUE(mStartParent->IsInDoc(), NS_ERROR_UNEXPECTED); |
michael@0 | 2936 | |
michael@0 | 2937 | nsRefPtr<nsFontFaceList> fontFaceList = new nsFontFaceList(); |
michael@0 | 2938 | |
michael@0 | 2939 | RangeSubtreeIterator iter; |
michael@0 | 2940 | nsresult rv = iter.Init(this); |
michael@0 | 2941 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 2942 | |
michael@0 | 2943 | while (!iter.IsDone()) { |
michael@0 | 2944 | // only collect anything if the range is not collapsed |
michael@0 | 2945 | nsCOMPtr<nsINode> node = iter.GetCurrentNode(); |
michael@0 | 2946 | iter.Next(); |
michael@0 | 2947 | |
michael@0 | 2948 | nsCOMPtr<nsIContent> content = do_QueryInterface(node); |
michael@0 | 2949 | if (!content) { |
michael@0 | 2950 | continue; |
michael@0 | 2951 | } |
michael@0 | 2952 | nsIFrame* frame = content->GetPrimaryFrame(); |
michael@0 | 2953 | if (!frame) { |
michael@0 | 2954 | continue; |
michael@0 | 2955 | } |
michael@0 | 2956 | |
michael@0 | 2957 | if (content->IsNodeOfType(nsINode::eTEXT)) { |
michael@0 | 2958 | if (node == startContainer) { |
michael@0 | 2959 | int32_t offset = startContainer == endContainer ? |
michael@0 | 2960 | mEndOffset : content->GetText()->GetLength(); |
michael@0 | 2961 | nsLayoutUtils::GetFontFacesForText(frame, mStartOffset, offset, |
michael@0 | 2962 | true, fontFaceList); |
michael@0 | 2963 | continue; |
michael@0 | 2964 | } |
michael@0 | 2965 | if (node == endContainer) { |
michael@0 | 2966 | nsLayoutUtils::GetFontFacesForText(frame, 0, mEndOffset, |
michael@0 | 2967 | true, fontFaceList); |
michael@0 | 2968 | continue; |
michael@0 | 2969 | } |
michael@0 | 2970 | } |
michael@0 | 2971 | |
michael@0 | 2972 | nsLayoutUtils::GetFontFacesForFrames(frame, fontFaceList); |
michael@0 | 2973 | } |
michael@0 | 2974 | |
michael@0 | 2975 | fontFaceList.forget(aResult); |
michael@0 | 2976 | return NS_OK; |
michael@0 | 2977 | } |
michael@0 | 2978 | |
michael@0 | 2979 | nsINode* |
michael@0 | 2980 | nsRange::GetRegisteredCommonAncestor() |
michael@0 | 2981 | { |
michael@0 | 2982 | NS_ASSERTION(IsInSelection(), |
michael@0 | 2983 | "GetRegisteredCommonAncestor only valid for range in selection"); |
michael@0 | 2984 | nsINode* ancestor = GetNextRangeCommonAncestor(mStartParent); |
michael@0 | 2985 | while (ancestor) { |
michael@0 | 2986 | RangeHashTable* ranges = |
michael@0 | 2987 | static_cast<RangeHashTable*>(ancestor->GetProperty(nsGkAtoms::range)); |
michael@0 | 2988 | if (ranges->GetEntry(this)) { |
michael@0 | 2989 | break; |
michael@0 | 2990 | } |
michael@0 | 2991 | ancestor = GetNextRangeCommonAncestor(ancestor->GetParentNode()); |
michael@0 | 2992 | } |
michael@0 | 2993 | NS_ASSERTION(ancestor, "can't find common ancestor for selected range"); |
michael@0 | 2994 | return ancestor; |
michael@0 | 2995 | } |
michael@0 | 2996 | |
michael@0 | 2997 | /* static */ bool nsRange::AutoInvalidateSelection::mIsNested; |
michael@0 | 2998 | |
michael@0 | 2999 | nsRange::AutoInvalidateSelection::~AutoInvalidateSelection() |
michael@0 | 3000 | { |
michael@0 | 3001 | NS_ASSERTION(mWasInSelection == mRange->IsInSelection(), |
michael@0 | 3002 | "Range got unselected in AutoInvalidateSelection block"); |
michael@0 | 3003 | if (!mCommonAncestor) { |
michael@0 | 3004 | return; |
michael@0 | 3005 | } |
michael@0 | 3006 | mIsNested = false; |
michael@0 | 3007 | ::InvalidateAllFrames(mCommonAncestor); |
michael@0 | 3008 | nsINode* commonAncestor = mRange->GetRegisteredCommonAncestor(); |
michael@0 | 3009 | if (commonAncestor != mCommonAncestor) { |
michael@0 | 3010 | ::InvalidateAllFrames(commonAncestor); |
michael@0 | 3011 | } |
michael@0 | 3012 | } |
michael@0 | 3013 | |
michael@0 | 3014 | /* static */ already_AddRefed<nsRange> |
michael@0 | 3015 | nsRange::Constructor(const GlobalObject& aGlobal, |
michael@0 | 3016 | ErrorResult& aRv) |
michael@0 | 3017 | { |
michael@0 | 3018 | nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.GetAsSupports()); |
michael@0 | 3019 | if (!window || !window->GetDoc()) { |
michael@0 | 3020 | aRv.Throw(NS_ERROR_FAILURE); |
michael@0 | 3021 | return nullptr; |
michael@0 | 3022 | } |
michael@0 | 3023 | |
michael@0 | 3024 | return window->GetDoc()->CreateRange(aRv); |
michael@0 | 3025 | } |