content/base/src/TreeWalker.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* vim: set ts=4 et sw=4 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 /*
michael@0 8 * Implementation of DOM Traversal's nsIDOMTreeWalker
michael@0 9 */
michael@0 10
michael@0 11 #include "mozilla/dom/TreeWalker.h"
michael@0 12
michael@0 13 #include "nsIContent.h"
michael@0 14 #include "nsIDOMNode.h"
michael@0 15 #include "nsError.h"
michael@0 16 #include "nsINode.h"
michael@0 17 #include "nsContentUtils.h"
michael@0 18 #include "mozilla/dom/TreeWalkerBinding.h"
michael@0 19
michael@0 20 namespace mozilla {
michael@0 21 namespace dom {
michael@0 22
michael@0 23 /*
michael@0 24 * Factories, constructors and destructors
michael@0 25 */
michael@0 26
michael@0 27 TreeWalker::TreeWalker(nsINode *aRoot,
michael@0 28 uint32_t aWhatToShow,
michael@0 29 const NodeFilterHolder &aFilter) :
michael@0 30 nsTraversal(aRoot, aWhatToShow, aFilter),
michael@0 31 mCurrentNode(aRoot)
michael@0 32 {
michael@0 33 }
michael@0 34
michael@0 35 TreeWalker::~TreeWalker()
michael@0 36 {
michael@0 37 /* destructor code */
michael@0 38 }
michael@0 39
michael@0 40 /*
michael@0 41 * nsISupports and cycle collection stuff
michael@0 42 */
michael@0 43
michael@0 44 NS_IMPL_CYCLE_COLLECTION(TreeWalker, mFilter, mCurrentNode, mRoot)
michael@0 45
michael@0 46 // QueryInterface implementation for TreeWalker
michael@0 47 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TreeWalker)
michael@0 48 NS_INTERFACE_MAP_ENTRY(nsIDOMTreeWalker)
michael@0 49 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMTreeWalker)
michael@0 50 NS_INTERFACE_MAP_END
michael@0 51
michael@0 52 // Have to pass in dom::TreeWalker because a11y has an a11y::TreeWalker that
michael@0 53 // passes TreeWalker so refcount logging would get confused on the name
michael@0 54 // collision.
michael@0 55 NS_IMPL_CYCLE_COLLECTING_ADDREF(dom::TreeWalker)
michael@0 56 NS_IMPL_CYCLE_COLLECTING_RELEASE(dom::TreeWalker)
michael@0 57
michael@0 58
michael@0 59
michael@0 60 /*
michael@0 61 * nsIDOMTreeWalker Getters/Setters
michael@0 62 */
michael@0 63
michael@0 64 /* readonly attribute nsIDOMNode root; */
michael@0 65 NS_IMETHODIMP TreeWalker::GetRoot(nsIDOMNode * *aRoot)
michael@0 66 {
michael@0 67 NS_ADDREF(*aRoot = Root()->AsDOMNode());
michael@0 68 return NS_OK;
michael@0 69 }
michael@0 70
michael@0 71 /* readonly attribute unsigned long whatToShow; */
michael@0 72 NS_IMETHODIMP TreeWalker::GetWhatToShow(uint32_t *aWhatToShow)
michael@0 73 {
michael@0 74 *aWhatToShow = WhatToShow();
michael@0 75 return NS_OK;
michael@0 76 }
michael@0 77
michael@0 78 /* readonly attribute nsIDOMNodeFilter filter; */
michael@0 79 NS_IMETHODIMP TreeWalker::GetFilter(nsIDOMNodeFilter * *aFilter)
michael@0 80 {
michael@0 81 NS_ENSURE_ARG_POINTER(aFilter);
michael@0 82
michael@0 83 *aFilter = mFilter.ToXPCOMCallback().take();
michael@0 84
michael@0 85 return NS_OK;
michael@0 86 }
michael@0 87
michael@0 88 /* attribute nsIDOMNode currentNode; */
michael@0 89 NS_IMETHODIMP TreeWalker::GetCurrentNode(nsIDOMNode * *aCurrentNode)
michael@0 90 {
michael@0 91 if (mCurrentNode) {
michael@0 92 return CallQueryInterface(mCurrentNode, aCurrentNode);
michael@0 93 }
michael@0 94
michael@0 95 *aCurrentNode = nullptr;
michael@0 96
michael@0 97 return NS_OK;
michael@0 98 }
michael@0 99 NS_IMETHODIMP TreeWalker::SetCurrentNode(nsIDOMNode * aCurrentNode)
michael@0 100 {
michael@0 101 NS_ENSURE_TRUE(aCurrentNode, NS_ERROR_DOM_NOT_SUPPORTED_ERR);
michael@0 102 NS_ENSURE_TRUE(mRoot, NS_ERROR_UNEXPECTED);
michael@0 103
michael@0 104 nsCOMPtr<nsINode> node = do_QueryInterface(aCurrentNode);
michael@0 105 NS_ENSURE_TRUE(node, NS_ERROR_UNEXPECTED);
michael@0 106
michael@0 107 ErrorResult rv;
michael@0 108 SetCurrentNode(*node, rv);
michael@0 109 return rv.ErrorCode();
michael@0 110 }
michael@0 111
michael@0 112 void
michael@0 113 TreeWalker::SetCurrentNode(nsINode& aNode, ErrorResult& aResult)
michael@0 114 {
michael@0 115 aResult = nsContentUtils::CheckSameOrigin(mRoot, &aNode);
michael@0 116 if (aResult.Failed()) {
michael@0 117 return;
michael@0 118 }
michael@0 119
michael@0 120 mCurrentNode = &aNode;
michael@0 121 }
michael@0 122
michael@0 123 /*
michael@0 124 * nsIDOMTreeWalker functions
michael@0 125 */
michael@0 126
michael@0 127 /* nsIDOMNode parentNode (); */
michael@0 128 NS_IMETHODIMP TreeWalker::ParentNode(nsIDOMNode **_retval)
michael@0 129 {
michael@0 130 return ImplNodeGetter(&TreeWalker::ParentNode, _retval);
michael@0 131 }
michael@0 132
michael@0 133 already_AddRefed<nsINode>
michael@0 134 TreeWalker::ParentNode(ErrorResult& aResult)
michael@0 135 {
michael@0 136 nsCOMPtr<nsINode> node = mCurrentNode;
michael@0 137
michael@0 138 while (node && node != mRoot) {
michael@0 139 node = node->GetParentNode();
michael@0 140
michael@0 141 if (node) {
michael@0 142 int16_t filtered = TestNode(node, aResult);
michael@0 143 if (aResult.Failed()) {
michael@0 144 return nullptr;
michael@0 145 }
michael@0 146 if (filtered == nsIDOMNodeFilter::FILTER_ACCEPT) {
michael@0 147 mCurrentNode = node;
michael@0 148 return node.forget();
michael@0 149 }
michael@0 150 }
michael@0 151 }
michael@0 152
michael@0 153 return nullptr;
michael@0 154 }
michael@0 155
michael@0 156 /* nsIDOMNode firstChild (); */
michael@0 157 NS_IMETHODIMP TreeWalker::FirstChild(nsIDOMNode **_retval)
michael@0 158 {
michael@0 159 return ImplNodeGetter(&TreeWalker::FirstChild, _retval);
michael@0 160 }
michael@0 161
michael@0 162 already_AddRefed<nsINode>
michael@0 163 TreeWalker::FirstChild(ErrorResult& aResult)
michael@0 164 {
michael@0 165 return FirstChildInternal(false, aResult);
michael@0 166 }
michael@0 167
michael@0 168 /* nsIDOMNode lastChild (); */
michael@0 169 NS_IMETHODIMP TreeWalker::LastChild(nsIDOMNode **_retval)
michael@0 170 {
michael@0 171 return ImplNodeGetter(&TreeWalker::LastChild, _retval);
michael@0 172 }
michael@0 173
michael@0 174 already_AddRefed<nsINode>
michael@0 175 TreeWalker::LastChild(ErrorResult& aResult)
michael@0 176 {
michael@0 177 return FirstChildInternal(true, aResult);
michael@0 178 }
michael@0 179
michael@0 180 /* nsIDOMNode previousSibling (); */
michael@0 181 NS_IMETHODIMP TreeWalker::PreviousSibling(nsIDOMNode **_retval)
michael@0 182 {
michael@0 183 return ImplNodeGetter(&TreeWalker::PreviousSibling, _retval);
michael@0 184 }
michael@0 185
michael@0 186 already_AddRefed<nsINode>
michael@0 187 TreeWalker::PreviousSibling(ErrorResult& aResult)
michael@0 188 {
michael@0 189 return NextSiblingInternal(true, aResult);
michael@0 190 }
michael@0 191
michael@0 192 /* nsIDOMNode nextSibling (); */
michael@0 193 NS_IMETHODIMP TreeWalker::NextSibling(nsIDOMNode **_retval)
michael@0 194 {
michael@0 195 return ImplNodeGetter(&TreeWalker::NextSibling, _retval);
michael@0 196 }
michael@0 197
michael@0 198 already_AddRefed<nsINode>
michael@0 199 TreeWalker::NextSibling(ErrorResult& aResult)
michael@0 200 {
michael@0 201 return NextSiblingInternal(false, aResult);
michael@0 202 }
michael@0 203
michael@0 204 /* nsIDOMNode previousNode (); */
michael@0 205 NS_IMETHODIMP TreeWalker::PreviousNode(nsIDOMNode **_retval)
michael@0 206 {
michael@0 207 return ImplNodeGetter(&TreeWalker::PreviousNode, _retval);
michael@0 208 }
michael@0 209
michael@0 210 already_AddRefed<nsINode>
michael@0 211 TreeWalker::PreviousNode(ErrorResult& aResult)
michael@0 212 {
michael@0 213 nsCOMPtr<nsINode> node = mCurrentNode;
michael@0 214
michael@0 215 while (node != mRoot) {
michael@0 216 while (nsINode *previousSibling = node->GetPreviousSibling()) {
michael@0 217 node = previousSibling;
michael@0 218
michael@0 219 int16_t filtered = TestNode(node, aResult);
michael@0 220 if (aResult.Failed()) {
michael@0 221 return nullptr;
michael@0 222 }
michael@0 223
michael@0 224 nsINode *lastChild;
michael@0 225 while (filtered != nsIDOMNodeFilter::FILTER_REJECT &&
michael@0 226 (lastChild = node->GetLastChild())) {
michael@0 227 node = lastChild;
michael@0 228 filtered = TestNode(node, aResult);
michael@0 229 if (aResult.Failed()) {
michael@0 230 return nullptr;
michael@0 231 }
michael@0 232 }
michael@0 233
michael@0 234 if (filtered == nsIDOMNodeFilter::FILTER_ACCEPT) {
michael@0 235 mCurrentNode = node;
michael@0 236 return node.forget();
michael@0 237 }
michael@0 238 }
michael@0 239
michael@0 240 if (node == mRoot) {
michael@0 241 break;
michael@0 242 }
michael@0 243
michael@0 244 node = node->GetParentNode();
michael@0 245 if (!node) {
michael@0 246 break;
michael@0 247 }
michael@0 248
michael@0 249 int16_t filtered = TestNode(node, aResult);
michael@0 250 if (aResult.Failed()) {
michael@0 251 return nullptr;
michael@0 252 }
michael@0 253
michael@0 254 if (filtered == nsIDOMNodeFilter::FILTER_ACCEPT) {
michael@0 255 mCurrentNode = node;
michael@0 256 return node.forget();
michael@0 257 }
michael@0 258 }
michael@0 259
michael@0 260 return nullptr;
michael@0 261 }
michael@0 262
michael@0 263 /* nsIDOMNode nextNode (); */
michael@0 264 NS_IMETHODIMP TreeWalker::NextNode(nsIDOMNode **_retval)
michael@0 265 {
michael@0 266 return ImplNodeGetter(&TreeWalker::NextNode, _retval);
michael@0 267 }
michael@0 268
michael@0 269 already_AddRefed<nsINode>
michael@0 270 TreeWalker::NextNode(ErrorResult& aResult)
michael@0 271 {
michael@0 272 int16_t filtered = nsIDOMNodeFilter::FILTER_ACCEPT; // pre-init for inner loop
michael@0 273
michael@0 274 nsCOMPtr<nsINode> node = mCurrentNode;
michael@0 275
michael@0 276 while (1) {
michael@0 277
michael@0 278 nsINode *firstChild;
michael@0 279 while (filtered != nsIDOMNodeFilter::FILTER_REJECT &&
michael@0 280 (firstChild = node->GetFirstChild())) {
michael@0 281 node = firstChild;
michael@0 282
michael@0 283 filtered = TestNode(node, aResult);
michael@0 284 if (aResult.Failed()) {
michael@0 285 return nullptr;
michael@0 286 }
michael@0 287
michael@0 288 if (filtered == nsIDOMNodeFilter::FILTER_ACCEPT) {
michael@0 289 // Node found
michael@0 290 mCurrentNode = node;
michael@0 291 return node.forget();
michael@0 292 }
michael@0 293 }
michael@0 294
michael@0 295 nsINode *sibling = nullptr;
michael@0 296 nsINode *temp = node;
michael@0 297 do {
michael@0 298 if (temp == mRoot)
michael@0 299 break;
michael@0 300
michael@0 301 sibling = temp->GetNextSibling();
michael@0 302 if (sibling)
michael@0 303 break;
michael@0 304
michael@0 305 temp = temp->GetParentNode();
michael@0 306 } while (temp);
michael@0 307
michael@0 308 if (!sibling)
michael@0 309 break;
michael@0 310
michael@0 311 node = sibling;
michael@0 312
michael@0 313 // Found a sibling. Either ours or ancestor's
michael@0 314 filtered = TestNode(node, aResult);
michael@0 315 if (aResult.Failed()) {
michael@0 316 return nullptr;
michael@0 317 }
michael@0 318
michael@0 319 if (filtered == nsIDOMNodeFilter::FILTER_ACCEPT) {
michael@0 320 // Node found
michael@0 321 mCurrentNode = node;
michael@0 322 return node.forget();
michael@0 323 }
michael@0 324 }
michael@0 325
michael@0 326 return nullptr;
michael@0 327 }
michael@0 328
michael@0 329 /*
michael@0 330 * TreeWalker helper functions
michael@0 331 */
michael@0 332
michael@0 333 /*
michael@0 334 * Implements FirstChild and LastChild which only vary in which direction
michael@0 335 * they search.
michael@0 336 * @param aReversed Controls whether we search forwards or backwards
michael@0 337 * @param aResult Whether we threw or not.
michael@0 338 * @returns The desired node. Null if no child is found
michael@0 339 */
michael@0 340 already_AddRefed<nsINode>
michael@0 341 TreeWalker::FirstChildInternal(bool aReversed, ErrorResult& aResult)
michael@0 342 {
michael@0 343 nsCOMPtr<nsINode> node = aReversed ? mCurrentNode->GetLastChild()
michael@0 344 : mCurrentNode->GetFirstChild();
michael@0 345
michael@0 346 while (node) {
michael@0 347 int16_t filtered = TestNode(node, aResult);
michael@0 348 if (aResult.Failed()) {
michael@0 349 return nullptr;
michael@0 350 }
michael@0 351
michael@0 352 switch (filtered) {
michael@0 353 case nsIDOMNodeFilter::FILTER_ACCEPT:
michael@0 354 // Node found
michael@0 355 mCurrentNode = node;
michael@0 356 return node.forget();
michael@0 357 case nsIDOMNodeFilter::FILTER_SKIP: {
michael@0 358 nsINode *child = aReversed ? node->GetLastChild()
michael@0 359 : node->GetFirstChild();
michael@0 360 if (child) {
michael@0 361 node = child;
michael@0 362 continue;
michael@0 363 }
michael@0 364 break;
michael@0 365 }
michael@0 366 case nsIDOMNodeFilter::FILTER_REJECT:
michael@0 367 // Keep searching
michael@0 368 break;
michael@0 369 }
michael@0 370
michael@0 371 do {
michael@0 372 nsINode *sibling = aReversed ? node->GetPreviousSibling()
michael@0 373 : node->GetNextSibling();
michael@0 374 if (sibling) {
michael@0 375 node = sibling;
michael@0 376 break;
michael@0 377 }
michael@0 378
michael@0 379 nsINode *parent = node->GetParentNode();
michael@0 380
michael@0 381 if (!parent || parent == mRoot || parent == mCurrentNode) {
michael@0 382 return nullptr;
michael@0 383 }
michael@0 384
michael@0 385 node = parent;
michael@0 386
michael@0 387 } while (node);
michael@0 388 }
michael@0 389
michael@0 390 return nullptr;
michael@0 391 }
michael@0 392
michael@0 393 /*
michael@0 394 * Implements NextSibling and PreviousSibling which only vary in which
michael@0 395 * direction they search.
michael@0 396 * @param aReversed Controls whether we search forwards or backwards
michael@0 397 * @param aResult Whether we threw or not.
michael@0 398 * @returns The desired node. Null if no child is found
michael@0 399 */
michael@0 400 already_AddRefed<nsINode>
michael@0 401 TreeWalker::NextSiblingInternal(bool aReversed, ErrorResult& aResult)
michael@0 402 {
michael@0 403 nsCOMPtr<nsINode> node = mCurrentNode;
michael@0 404
michael@0 405 if (node == mRoot) {
michael@0 406 return nullptr;
michael@0 407 }
michael@0 408
michael@0 409 while (1) {
michael@0 410 nsINode* sibling = aReversed ? node->GetPreviousSibling()
michael@0 411 : node->GetNextSibling();
michael@0 412
michael@0 413 while (sibling) {
michael@0 414 node = sibling;
michael@0 415
michael@0 416 int16_t filtered = TestNode(node, aResult);
michael@0 417 if (aResult.Failed()) {
michael@0 418 return nullptr;
michael@0 419 }
michael@0 420
michael@0 421 if (filtered == nsIDOMNodeFilter::FILTER_ACCEPT) {
michael@0 422 // Node found
michael@0 423 mCurrentNode = node;
michael@0 424 return node.forget();
michael@0 425 }
michael@0 426
michael@0 427 // If rejected or no children, try a sibling
michael@0 428 if (filtered == nsIDOMNodeFilter::FILTER_REJECT ||
michael@0 429 !(sibling = aReversed ? node->GetLastChild()
michael@0 430 : node->GetFirstChild())) {
michael@0 431 sibling = aReversed ? node->GetPreviousSibling()
michael@0 432 : node->GetNextSibling();
michael@0 433 }
michael@0 434 }
michael@0 435
michael@0 436 node = node->GetParentNode();
michael@0 437
michael@0 438 if (!node || node == mRoot) {
michael@0 439 return nullptr;
michael@0 440 }
michael@0 441
michael@0 442 // Is parent transparent in filtered view?
michael@0 443 int16_t filtered = TestNode(node, aResult);
michael@0 444 if (aResult.Failed()) {
michael@0 445 return nullptr;
michael@0 446 }
michael@0 447 if (filtered == nsIDOMNodeFilter::FILTER_ACCEPT) {
michael@0 448 return nullptr;
michael@0 449 }
michael@0 450 }
michael@0 451 }
michael@0 452
michael@0 453 JSObject*
michael@0 454 TreeWalker::WrapObject(JSContext *cx)
michael@0 455 {
michael@0 456 return TreeWalkerBinding::Wrap(cx, this);
michael@0 457 }
michael@0 458
michael@0 459 } // namespace dom
michael@0 460 } // namespace mozilla

mercurial