1 /*
2 * Copyright (C) 2007-2009 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "config.h"
32 #include "Document.h"
33
34 #include "ExceptionCode.h"
35 #include "Node.h"
36 #include "XPathNSResolver.h"
37 #include "XPathResult.h"
38
39 #include "V8Binding.h"
40 #include "V8CustomBinding.h"
41 #include "V8CustomXPathNSResolver.h"
42 #include "V8Node.h"
43 #include "V8Proxy.h"
44
45 #if ENABLE(XPATH)
46 #include "V8XPathNSResolver.h"
47 #include "V8XPathResult.h"
48 #endif
49
50 #include <wtf/RefPtr.h>
51
52 namespace WebCore {
53
CALLBACK_FUNC_DECL(DocumentEvaluate)54 CALLBACK_FUNC_DECL(DocumentEvaluate)
55 {
56 INC_STATS("DOM.Document.evaluate()");
57 #if ENABLE(XPATH)
58 RefPtr<Document> document = V8DOMWrapper::convertDOMWrapperToNode<Document>(args.Holder());
59 ExceptionCode ec = 0;
60 String expression = toWebCoreString(args[0]);
61 RefPtr<Node> contextNode;
62 if (V8Node::HasInstance(args[1]))
63 contextNode = V8DOMWrapper::convertDOMWrapperToNode<Node>(v8::Handle<v8::Object>::Cast(args[1]));
64
65 RefPtr<XPathNSResolver> resolver;
66 if (V8XPathNSResolver::HasInstance(args[2]))
67 resolver = V8DOMWrapper::convertToNativeObject<XPathNSResolver>(V8ClassIndex::XPATHNSRESOLVER, v8::Handle<v8::Object>::Cast(args[2]));
68 else if (args[2]->IsObject())
69 resolver = V8CustomXPathNSResolver::create(args[2]->ToObject());
70 else if (!args[2]->IsNull() && !args[2]->IsUndefined())
71 return throwError(TYPE_MISMATCH_ERR);
72
73 int type = toInt32(args[3]);
74 RefPtr<XPathResult> inResult;
75 if (V8XPathResult::HasInstance(args[4]))
76 inResult = V8DOMWrapper::convertToNativeObject<XPathResult>(V8ClassIndex::XPATHRESULT, v8::Handle<v8::Object>::Cast(args[4]));
77
78 v8::TryCatch exceptionCatcher;
79 RefPtr<XPathResult> result = document->evaluate(expression, contextNode.get(), resolver.get(), type, inResult.get(), ec);
80 if (exceptionCatcher.HasCaught())
81 return throwError(exceptionCatcher.Exception());
82
83 if (ec)
84 return throwError(ec);
85
86 return V8DOMWrapper::convertToV8Object(V8ClassIndex::XPATHRESULT, result.release());
87 #else
88 return throwError(NOT_SUPPORTED_ERR);
89 #endif
90
91 }
92
CALLBACK_FUNC_DECL(DocumentGetCSSCanvasContext)93 CALLBACK_FUNC_DECL(DocumentGetCSSCanvasContext)
94 {
95 INC_STATS("DOM.Document.getCSSCanvasContext");
96 v8::Handle<v8::Object> holder = args.Holder();
97 Document* imp = V8DOMWrapper::convertDOMWrapperToNode<Document>(holder);
98 String contextId = toWebCoreString(args[0]);
99 String name = toWebCoreString(args[1]);
100 int width = toInt32(args[2]);
101 int height = toInt32(args[3]);
102 CanvasRenderingContext2D* result = imp->getCSSCanvasContext(contextId, name, width, height);
103 return V8DOMWrapper::convertToV8Object(V8ClassIndex::CANVASRENDERINGCONTEXT2D, result);
104 }
105
106 } // namespace WebCore
107