summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/dom/node_iterator_base.cc
blob: 066ff9de138393d572f32db98ae53f7a72e74091 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
 * Copyright (C) 2000 Frederik Holljen (frederik.holljen@hig.no)
 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
 * Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
 * Copyright (C) 2004, 2008 Apple Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */

#include "third_party/blink/renderer/core/dom/node_iterator_base.h"

#include "third_party/blink/renderer/bindings/core/v8/v8_node_filter.h"
#include "third_party/blink/renderer/core/dom/node.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/frame/web_feature.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/instrumentation/use_counter.h"

namespace blink {

NodeIteratorBase::NodeIteratorBase(Node* root_node,
                                   unsigned what_to_show,
                                   V8NodeFilter* node_filter)
    : root_(root_node), what_to_show_(what_to_show), filter_(node_filter) {}

unsigned NodeIteratorBase::AcceptNode(Node* node,
                                      ExceptionState& exception_state) {
  // DOM 6. Traversal
  // https://dom.spec.whatwg.org/#traversal
  // Each NodeIterator and TreeWalker object has an associated active flag to
  // avoid recursive invocations.
  if (active_flag_) {
    // 1. If the active flag is set, then throw an "InvalidStateError"
    // DOMException.
    exception_state.ThrowDOMException(DOMExceptionCode::kInvalidStateError,
                                      "Filter function can't be recursive");
    return V8NodeFilter::FILTER_REJECT;
  }

  // 2. Let n be node’s nodeType attribute value minus 1.
  // 3. If the nth bit (where 0 is the least significant bit) of whatToShow is
  // not set, then return FILTER_SKIP.
  //
  // The bit twiddling here is done to map DOM node types, which are given as
  // integers from 1 through 14, to whatToShow bit masks.
  if (!(((1 << (node->getNodeType() - 1)) & what_to_show_)))
    return V8NodeFilter::FILTER_SKIP;

  // 4. If filter is null, then return FILTER_ACCEPT.
  if (!filter_)
    return V8NodeFilter::FILTER_ACCEPT;

  // 5. Set the active flag.
  base::AutoReset<bool> set_active_flag(&active_flag_, true);

  // 6. Let result be the return value of call a user object’s operation with
  // filter, "acceptNode", and « node ». If this throws an exception, then unset
  // the active flag and rethrow the exception.
  v8::TryCatch try_catch(filter_->GetIsolate());
  uint16_t result = 0;
  if (!filter_->acceptNode(nullptr, node).To(&result)) {
    exception_state.RethrowV8Exception(try_catch.Exception());
    return 0;
  }

  UseCounter::Count(
      ExecutionContext::From(filter_->CallbackRelevantScriptState()),
      filter_->IsCallbackObjectCallableForNodeIteratorBase()
          ? WebFeature::kNodeFilterIsFunction
          : WebFeature::kNodeFilterIsObject);

  // 7. Unset the active flag.
  // 8. Return result.
  return result;
}

void NodeIteratorBase::Trace(Visitor* visitor) {
  visitor->Trace(root_);
  visitor->Trace(filter_);
}

}  // namespace blink