Example #1
0
JSValue* JSXPathResultPrototypeFunction::callAsFunction(ExecState* exec, JSObject* thisObj, const List& args)
{
    if (!thisObj->inherits(&JSXPathResult::info))
      return throwError(exec, TypeError);

    XPathResult* imp = static_cast<XPathResult*>(static_cast<JSXPathResult*>(thisObj)->impl());

    switch (id) {
    case JSXPathResult::IterateNextFuncNum: {
        ExceptionCode ec = 0;


        KJS::JSValue* result = toJS(exec, WTF::getPtr(imp->iterateNext(ec)));
        setDOMException(exec, ec);
        return result;
    }
    case JSXPathResult::SnapshotItemFuncNum: {
        ExceptionCode ec = 0;
        bool indexOk;
        unsigned index = args[0]->toInt32(exec, indexOk);
        if (!indexOk) {
            setDOMException(exec, TYPE_MISMATCH_ERR);
            return jsUndefined();
        }


        KJS::JSValue* result = toJS(exec, WTF::getPtr(imp->snapshotItem(index, ec)));
        setDOMException(exec, ec);
        return result;
    }
    }
    return 0;
}
Example #2
0
XPathResult* XPathExpression::evaluate(Node* contextNode,
                                       unsigned short type,
                                       const ScriptValue&,
                                       ExceptionState& exceptionState) {
    if (!isValidContextNode(contextNode)) {
        exceptionState.throwDOMException(
            NotSupportedError, "The node provided is '" + contextNode->nodeName() +
            "', which is not a valid context node type.");
        return nullptr;
    }

    EvaluationContext evaluationContext(*contextNode);
    XPathResult* result = XPathResult::create(
                              evaluationContext, m_topExpression->evaluate(evaluationContext));

    if (evaluationContext.hadTypeConversionError) {
        // It is not specified what to do if type conversion fails while evaluating
        // an expression.
        exceptionState.throwDOMException(
            SyntaxError, "Type conversion failed while evaluating the expression.");
        return nullptr;
    }

    if (type != XPathResult::kAnyType) {
        result->convertTo(type, exceptionState);
        if (exceptionState.hadException())
            return nullptr;
    }

    return result;
}
JSValue jsXPathResultResultType(ExecState* exec, JSValue slotBase, const Identifier&)
{
    JSXPathResult* castedThis = static_cast<JSXPathResult*>(asObject(slotBase));
    UNUSED_PARAM(exec);
    XPathResult* imp = static_cast<XPathResult*>(castedThis->impl());
    JSValue result = jsNumber(imp->resultType());
    return result;
}
JSValue jsXPathResultInvalidIteratorState(ExecState* exec, JSValue slotBase, const Identifier&)
{
    JSXPathResult* castedThis = static_cast<JSXPathResult*>(asObject(slotBase));
    UNUSED_PARAM(exec);
    XPathResult* imp = static_cast<XPathResult*>(castedThis->impl());
    JSValue result = jsBoolean(imp->invalidIteratorState());
    return result;
}
JSValue jsXPathResultBooleanValue(ExecState* exec, JSValue slotBase, const Identifier&)
{
    JSXPathResult* castedThis = static_cast<JSXPathResult*>(asObject(slotBase));
    ExceptionCode ec = 0;
    XPathResult* imp = static_cast<XPathResult*>(castedThis->impl());
    JSC::JSValue result = jsBoolean(imp->booleanValue(ec));
    setDOMException(exec, ec);
    return result;
}
JSValue jsXPathResultSingleNodeValue(ExecState* exec, JSValue slotBase, const Identifier&)
{
    JSXPathResult* castedThis = static_cast<JSXPathResult*>(asObject(slotBase));
    ExceptionCode ec = 0;
    XPathResult* imp = static_cast<XPathResult*>(castedThis->impl());
    JSC::JSValue result = toJS(exec, castedThis->globalObject(), WTF::getPtr(imp->singleNodeValue(ec)));
    setDOMException(exec, ec);
    return result;
}
JSValue jsXPathResultSnapshotLength(ExecState* exec, JSValue slotBase, const Identifier&)
{
    JSXPathResult* castedThis = static_cast<JSXPathResult*>(asObject(slotBase));
    ExceptionCode ec = 0;
    XPathResult* imp = static_cast<XPathResult*>(castedThis->impl());
    JSC::JSValue result = jsNumber(imp->snapshotLength(ec));
    setDOMException(exec, ec);
    return result;
}
Example #8
0
nsINode*
nsXMLBindingValues::GetNodeAssignmentFor(nsXULTemplateResultXML* aResult,
                                         nsXMLBinding* aBinding,
                                         int32_t aIndex)
{
  XPathResult* result = GetAssignmentFor(aResult, aBinding, aIndex,
                                         XPathResult::FIRST_ORDERED_NODE_TYPE);

  ErrorResult rv;
  return result ? result->GetSingleNodeValue(rv) : nullptr;
}
static v8::Handle<v8::Value> snapshotLengthAttrGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
{
    INC_STATS("DOM.XPathResult.snapshotLength._get");
    XPathResult* imp = V8XPathResult::toNative(info.Holder());
    ExceptionCode ec = 0;
    unsigned v = imp->snapshotLength(ec);
    if (UNLIKELY(ec)) {
        V8Proxy::setDOMException(ec);
        return v8::Handle<v8::Value>();
    }
    return v8::Integer::NewFromUnsigned(v);
}
static v8::Handle<v8::Value> singleNodeValueAttrGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
{
    INC_STATS("DOM.XPathResult.singleNodeValue._get");
    XPathResult* imp = V8XPathResult::toNative(info.Holder());
    ExceptionCode ec = 0;
    RefPtr<Node> v = imp->singleNodeValue(ec);
    if (UNLIKELY(ec)) {
        V8Proxy::setDOMException(ec);
        return v8::Handle<v8::Value>();
    }
    return toV8(v.release());
}
static v8::Handle<v8::Value> booleanValueAttrGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
{
    INC_STATS("DOM.XPathResult.booleanValue._get");
    XPathResult* imp = V8XPathResult::toNative(info.Holder());
    ExceptionCode ec = 0;
    bool v = imp->booleanValue(ec);
    if (UNLIKELY(ec)) {
        V8Proxy::setDOMException(ec);
        return v8::Handle<v8::Value>();
    }
    return v8Boolean(v);
}
Example #12
0
JSValue* jsXPathResultPrototypeFunctionIterateNext(ExecState* exec, JSObject*, JSValue* thisValue, const ArgList& args)
{
    if (!thisValue->isObject(&JSXPathResult::s_info))
        return throwError(exec, TypeError);
    JSXPathResult* castedThisObj = static_cast<JSXPathResult*>(thisValue);
    XPathResult* imp = static_cast<XPathResult*>(castedThisObj->impl());
    ExceptionCode ec = 0;


    KJS::JSValue* result = toJS(exec, WTF::getPtr(imp->iterateNext(ec)));
    setDOMException(exec, ec);
    return result;
}
Example #13
0
JSValue* jsXPathResultPrototypeFunctionSnapshotItem(ExecState* exec, JSObject*, JSValue* thisValue, const ArgList& args)
{
    if (!thisValue->isObject(&JSXPathResult::s_info))
        return throwError(exec, TypeError);
    JSXPathResult* castedThisObj = static_cast<JSXPathResult*>(thisValue);
    XPathResult* imp = static_cast<XPathResult*>(castedThisObj->impl());
    ExceptionCode ec = 0;
    unsigned index = args[0]->toInt32(exec);


    KJS::JSValue* result = toJS(exec, WTF::getPtr(imp->snapshotItem(index, ec)));
    setDOMException(exec, ec);
    return result;
}
Example #14
0
EncodedJSValue JSC_HOST_CALL jsXPathResultPrototypeFunctionIterateNext(ExecState* exec)
{
    JSValue thisValue = exec->hostThisValue();
    if (!thisValue.inherits(&JSXPathResult::s_info))
        return throwVMTypeError(exec);
    JSXPathResult* castedThis = static_cast<JSXPathResult*>(asObject(thisValue));
    XPathResult* imp = static_cast<XPathResult*>(castedThis->impl());
    ExceptionCode ec = 0;


    JSC::JSValue result = toJS(exec, castedThis->globalObject(), WTF::getPtr(imp->iterateNext(ec)));
    setDOMException(exec, ec);
    return JSValue::encode(result);
}
static v8::Handle<v8::Value> iterateNextCallback(const v8::Arguments& args)
{
    INC_STATS("DOM.XPathResult.iterateNext");
    XPathResult* imp = V8XPathResult::toNative(args.Holder());
    ExceptionCode ec = 0;
    {
    RefPtr<Node> result = imp->iterateNext(ec);
    if (UNLIKELY(ec))
        goto fail;
    return toV8(result.release());
    }
    fail:
    V8Proxy::setDOMException(ec);
    return v8::Handle<v8::Value>();
}
static v8::Handle<v8::Value> snapshotItemCallback(const v8::Arguments& args)
{
    INC_STATS("DOM.XPathResult.snapshotItem");
    XPathResult* imp = V8XPathResult::toNative(args.Holder());
    ExceptionCode ec = 0;
    {
    EXCEPTION_BLOCK(unsigned, index, toUInt32(args[0]));
    RefPtr<Node> result = imp->snapshotItem(index, ec);
    if (UNLIKELY(ec))
        goto fail;
    return toV8(result.release());
    }
    fail:
    V8Proxy::setDOMException(ec);
    return v8::Handle<v8::Value>();
}
Example #17
0
void
nsXMLBindingValues::GetStringAssignmentFor(nsXULTemplateResultXML* aResult,
                                           nsXMLBinding* aBinding,
                                           int32_t aIndex,
                                           nsAString& aValue)
{
  XPathResult* result = GetAssignmentFor(aResult, aBinding, aIndex,
                                         XPathResult::STRING_TYPE);

  if (result) {
    ErrorResult rv;
    result->GetStringValue(aValue, rv);
  } else {
    aValue.Truncate();
  }
}
Example #18
0
EncodedJSValue JSC_HOST_CALL jsXPathResultPrototypeFunctionSnapshotItem(ExecState* exec)
{
    JSValue thisValue = exec->hostThisValue();
    if (!thisValue.inherits(&JSXPathResult::s_info))
        return throwVMTypeError(exec);
    JSXPathResult* castedThis = static_cast<JSXPathResult*>(asObject(thisValue));
    XPathResult* imp = static_cast<XPathResult*>(castedThis->impl());
    ExceptionCode ec = 0;
    unsigned index(exec->argument(0).toUInt32(exec));
    if (exec->hadException())
        return JSValue::encode(jsUndefined());


    JSC::JSValue result = toJS(exec, castedThis->globalObject(), WTF::getPtr(imp->snapshotItem(index, ec)));
    setDOMException(exec, ec);
    return JSValue::encode(result);
}
Example #19
0
VXPathResult * XPathContext::evaluate(string xpath) throw (invalid_argument) {




    //-- Evaluate
    xmlXPathObjectPtr results = xmlXPathEvalExpression((const xmlChar*)xpath.c_str(),this->xpathCtx);
    if (results == NULL) {

        //-- Return error
        stringstream ss;
        ss << "Could not evaluate XPATH expression /"<<xpath<<"/ failed: " << (this->xpathCtx->lastError.message);
        throw invalid_argument(ss.str());

    }

    //-- Fill up results
    //--------------------
    XPathResult * result = new XPathResult();
    unsigned int nodesetSize = results->nodesetval ? results->nodesetval->nodeNr : 0;

    for (unsigned int i=0;i<nodesetSize ; i++) {

        // Get Node Pointer
        xmlNodePtr node = results->nodesetval->nodeTab[i];

        // Convert to Object
        VDOMNode * nodeObject = DOMNode::toNode(node,this->document);
        if (nodeObject!=NULL)
            result->getNodeList().push_back(nodeObject);

    }

    //-- Free
    xmlXPathFreeObject(results);

    return result;

}
Example #20
0
void MainThreadDebugger::xpathSelectorCallback(
    const v8::FunctionCallbackInfo<v8::Value>& info) {
  if (info.Length() < 1)
    return;
  String selector = toCoreStringWithUndefinedOrNullCheck(info[0]);
  if (selector.isEmpty())
    return;
  Node* node = secondArgumentAsNode(info);
  if (!node || !node->isContainerNode())
    return;

  ExceptionState exceptionState(ExceptionState::ExecutionContext, "$x",
                                "CommandLineAPI", info.Holder(),
                                info.GetIsolate());
  XPathResult* result = XPathEvaluator::create()->evaluate(
      selector, node, nullptr, XPathResult::kAnyType, ScriptValue(),
      exceptionState);
  if (exceptionState.hadException() || !result)
    return;
  if (result->resultType() == XPathResult::kNumberType) {
    info.GetReturnValue().Set(toV8(result->numberValue(exceptionState),
                                   info.Holder(), info.GetIsolate()));
  } else if (result->resultType() == XPathResult::kStringType) {
    info.GetReturnValue().Set(toV8(result->stringValue(exceptionState),
                                   info.Holder(), info.GetIsolate()));
  } else if (result->resultType() == XPathResult::kBooleanType) {
    info.GetReturnValue().Set(toV8(result->booleanValue(exceptionState),
                                   info.Holder(), info.GetIsolate()));
  } else {
    v8::Isolate* isolate = info.GetIsolate();
    v8::Local<v8::Context> context = isolate->GetCurrentContext();
    v8::Local<v8::Array> nodes = v8::Array::New(isolate);
    size_t index = 0;
    while (Node* node = result->iterateNext(exceptionState)) {
      if (exceptionState.hadException())
        return;
      if (!createDataPropertyInArray(
               context, nodes, index++,
               toV8(node, info.Holder(), info.GetIsolate()))
               .FromMaybe(false))
        return;
    }
    info.GetReturnValue().Set(nodes);
  }
}
Example #21
0
JSValue* JSXPathResult::getValueProperty(ExecState* exec, int token) const
{
    switch (token) {
    case ResultTypeAttrNum: {
        XPathResult* imp = static_cast<XPathResult*>(impl());
        return jsNumber(exec, imp->resultType());
    }
    case NumberValueAttrNum: {
        ExceptionCode ec = 0;
        XPathResult* imp = static_cast<XPathResult*>(impl());
        KJS::JSValue* result = jsNumber(exec, imp->numberValue(ec));
        setDOMException(exec, ec);
        return result;
    }
    case StringValueAttrNum: {
        ExceptionCode ec = 0;
        XPathResult* imp = static_cast<XPathResult*>(impl());
        KJS::JSValue* result = jsString(exec, imp->stringValue(ec));
        setDOMException(exec, ec);
        return result;
    }
    case BooleanValueAttrNum: {
        ExceptionCode ec = 0;
        XPathResult* imp = static_cast<XPathResult*>(impl());
        KJS::JSValue* result = jsBoolean(imp->booleanValue(ec));
        setDOMException(exec, ec);
        return result;
    }
    case SingleNodeValueAttrNum: {
        ExceptionCode ec = 0;
        XPathResult* imp = static_cast<XPathResult*>(impl());
        KJS::JSValue* result = toJS(exec, WTF::getPtr(imp->singleNodeValue(ec)));
        setDOMException(exec, ec);
        return result;
    }
    case InvalidIteratorStateAttrNum: {
        XPathResult* imp = static_cast<XPathResult*>(impl());
        return jsBoolean(imp->invalidIteratorState());
    }
    case SnapshotLengthAttrNum: {
        ExceptionCode ec = 0;
        XPathResult* imp = static_cast<XPathResult*>(impl());
        KJS::JSValue* result = jsNumber(exec, imp->snapshotLength(ec));
        setDOMException(exec, ec);
        return result;
    }
    case ConstructorAttrNum:
        return getConstructor(exec);
    }
    return 0;
}
static v8::Handle<v8::Value> resultTypeAttrGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
{
    INC_STATS("DOM.XPathResult.resultType._get");
    XPathResult* imp = V8XPathResult::toNative(info.Holder());
    return v8::Integer::New(imp->resultType());
}
Example #23
0
int main( )
{
    try {
        // Initialize Xerces and retrieve a DOMImplementation.
        XercesInitializer   init;
        DOMImplementation*  impl = 
            DOMImplementationRegistry::getDOMImplementation(
                fromNative("LS").c_str( )
            );
        if (impl == 0) {
            cout << "couldn't create DOM implementation\n";
            return EXIT_FAILURE;
        }

        // Construct a DOMBuilder to parse animals.xml.
        DOMPtr<DOMBuilder>  parser = 
            static_cast<DOMImplementationLS*>(impl)->
                createDOMBuilder(
                    DOMImplementationLS::MODE_SYNCHRONOUS, 0
                );
        CircusErrorHandler  err;
        parser->setErrorHandler(&err);

        // Parse animals.xml.
        DOMDocument*  doc = 
            parser->parseURI("animals.xml");
        DOMElement*   animalList = doc->getDocumentElement( );

        // Create XPath expression.
        auto_ptr<XPathEvaluator>   
            evaluator(XPathEvaluator::createEvaluator( ));
        auto_ptr<XPathNSResolver>  
            resolver(evaluator->createNSResolver(animalList));
        auto_ptr<XPathExpression>  
            xpath(
                evaluator->createExpression(
                    fromNative(
                        "animalList/animal[child::name='Herby']"
                    ).c_str( ),
                    resolver.get( )
                )
            );
auto_ptr<XPathEvaluator>   evaluator(XPathEvaluator::createEvaluator( ));
auto_ptr<XPathNSResolver>  resolver(evaluator->createNSResolver(animalList));
auto_ptr<XPathExpression>  xpath(
      evaluator->createExpression(
            fromNative("animalList/animal[child::name='Herby']").c_str( ),
            resolver.get( )
      ));

        // Evaluate the expression.
        XPathResult* result = 
            xpath->evaluate(
                doc, 
                XPathResult::ORDERED_NODE_ITERATOR_TYPE, 
                0
            );

        DOMNode* herby;
        if (herby = result->iterateNext( )) {
            animalList->removeChild(herby);
            herby->release( ); // optional.
        }

        // Construct a DOMWriter to save animals.xml.
        DOMPtr<DOMWriter> writer = 
            static_cast<DOMImplementationLS*>(impl)->createDOMWriter( );
        writer->setErrorHandler(&err);

        // Save animals.xml.
        LocalFileFormatTarget file("circus.xml");
        writer->writeNode(&file, *animalList);
    } catch (const DOMException& e) {
        cout << toNative(e.getMessage( )) << "\n";
        return EXIT_FAILURE;
    } catch (const XPathException &e) {
        cout << e.getString( ) << "\n";
        return EXIT_FAILURE;
    } catch (const exception& e) {
        cout << e.what( ) << "\n";
        return EXIT_FAILURE;
    }
}
static v8::Handle<v8::Value> invalidIteratorStateAttrGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
{
    INC_STATS("DOM.XPathResult.invalidIteratorState._get");
    XPathResult* imp = V8XPathResult::toNative(info.Holder());
    return v8Boolean(imp->invalidIteratorState());
}