JSNodeFilterCondition.cpp 2.44 KB
Newer Older
weinig's avatar
weinig committed
1 2
/*
 *  Copyright (C) 2001 Peter Kelly (pmk@post.com)
darin@apple.com's avatar
darin@apple.com committed
3
 *  Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
weinig's avatar
weinig committed
4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
ddkilzer's avatar
ddkilzer committed
17
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
weinig's avatar
weinig committed
18 19 20 21 22 23 24
 */

#include "config.h"
#include "JSNodeFilterCondition.h"

#include "Document.h"
#include "Frame.h"
weinig's avatar
weinig committed
25
#include "JSNode.h"
weinig's avatar
weinig committed
26 27
#include "JSNodeFilter.h"
#include "NodeFilter.h"
darin@apple.com's avatar
darin@apple.com committed
28
#include "ScriptController.h"
weinig's avatar
weinig committed
29 30 31

namespace WebCore {

darin@apple.com's avatar
darin@apple.com committed
32 33 34 35 36 37 38 39 40 41 42
using namespace KJS;

// FIXME: Add takeException as a member of ExecState?
static JSValue* takeException(ExecState* exec)
{
    JSValue* exception = exec->exception();
    exec->clearException();
    return exception;
}

JSNodeFilterCondition::JSNodeFilterCondition(JSObject* filter)
weinig's avatar
weinig committed
43 44 45 46
    : m_filter(filter)
{
}

47
void JSNodeFilterCondition::mark()
weinig's avatar
weinig committed
48
{
49
    m_filter->mark();
weinig's avatar
weinig committed
50 51
}

darin@apple.com's avatar
darin@apple.com committed
52
short JSNodeFilterCondition::acceptNode(Node* filterNode, JSValue*& exception) const
weinig's avatar
weinig committed
53
{
darin@apple.com's avatar
darin@apple.com committed
54 55 56 57
    // FIXME: It makes no sense for this to depend on the document being in a frame!
    Frame* frame = filterNode->document()->frame();
    if (!frame)
        return NodeFilter::FILTER_REJECT;
weinig's avatar
weinig committed
58

darin@apple.com's avatar
darin@apple.com committed
59 60 61 62 63
    JSLock lock;

    if (!m_filter->implementsCall())
        return NodeFilter::FILTER_REJECT;

darin@apple.com's avatar
darin@apple.com committed
64
    ExecState* exec = frame->script()->globalObject()->globalExec();
darin@apple.com's avatar
darin@apple.com committed
65
    ArgList args;
darin@apple.com's avatar
darin@apple.com committed
66 67 68 69 70
    args.append(toJS(exec, filterNode));
    if (exec->hadException()) {
        exception = takeException(exec);
        return NodeFilter::FILTER_REJECT;
    }
ggaren@apple.com's avatar
ggaren@apple.com committed
71
    JSValue* result = m_filter->callAsFunction(exec, m_filter, args);
darin@apple.com's avatar
darin@apple.com committed
72 73 74 75 76 77 78 79 80 81
    if (exec->hadException()) {
        exception = takeException(exec);
        return NodeFilter::FILTER_REJECT;
    }
    int intResult = result->toInt32(exec);
    if (exec->hadException()) {
        exception = takeException(exec);
        return NodeFilter::FILTER_REJECT;
    }
    return intResult;
weinig's avatar
weinig committed
82 83 84
}

} // namespace WebCore