1 /*
2  * Copyright (C) 2013 Google, Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include "third_party/blink/renderer/core/xml/document_xpath_evaluator.h"
27 
28 #include "third_party/blink/renderer/bindings/core/v8/script_value.h"
29 #include "third_party/blink/renderer/core/xml/xpath_expression.h"
30 #include "third_party/blink/renderer/core/xml/xpath_result.h"
31 #include "third_party/blink/renderer/platform/bindings/exception_state.h"
32 
33 namespace blink {
34 
35 // static
36 const char DocumentXPathEvaluator::kSupplementName[] = "DocumentXPathEvaluator";
37 
DocumentXPathEvaluator(Document & document)38 DocumentXPathEvaluator::DocumentXPathEvaluator(Document& document)
39     : Supplement<Document>(document) {}
40 
From(Document & document)41 DocumentXPathEvaluator& DocumentXPathEvaluator::From(Document& document) {
42   DocumentXPathEvaluator* cache =
43       Supplement<Document>::From<DocumentXPathEvaluator>(document);
44   if (!cache) {
45     cache = MakeGarbageCollected<DocumentXPathEvaluator>(document);
46     Supplement<Document>::ProvideTo(document, cache);
47   }
48   return *cache;
49 }
50 
createExpression(Document & document,const String & expression,XPathNSResolver * resolver,ExceptionState & exception_state)51 XPathExpression* DocumentXPathEvaluator::createExpression(
52     Document& document,
53     const String& expression,
54     XPathNSResolver* resolver,
55     ExceptionState& exception_state) {
56   DocumentXPathEvaluator& suplement = From(document);
57   if (!suplement.xpath_evaluator_)
58     suplement.xpath_evaluator_ = XPathEvaluator::Create();
59   return suplement.xpath_evaluator_->createExpression(expression, resolver,
60                                                       exception_state);
61 }
62 
createNSResolver(Document & document,Node * node_resolver)63 XPathNSResolver* DocumentXPathEvaluator::createNSResolver(Document& document,
64                                                           Node* node_resolver) {
65   DocumentXPathEvaluator& suplement = From(document);
66   if (!suplement.xpath_evaluator_)
67     suplement.xpath_evaluator_ = XPathEvaluator::Create();
68   return suplement.xpath_evaluator_->createNSResolver(node_resolver);
69 }
70 
evaluate(Document & document,const String & expression,Node * context_node,XPathNSResolver * resolver,uint16_t type,const ScriptValue &,ExceptionState & exception_state)71 XPathResult* DocumentXPathEvaluator::evaluate(Document& document,
72                                               const String& expression,
73                                               Node* context_node,
74                                               XPathNSResolver* resolver,
75                                               uint16_t type,
76                                               const ScriptValue&,
77                                               ExceptionState& exception_state) {
78   DocumentXPathEvaluator& suplement = From(document);
79   if (!suplement.xpath_evaluator_)
80     suplement.xpath_evaluator_ = XPathEvaluator::Create();
81   return suplement.xpath_evaluator_->evaluate(
82       expression, context_node, resolver, type, ScriptValue(), exception_state);
83 }
84 
Trace(Visitor * visitor)85 void DocumentXPathEvaluator::Trace(Visitor* visitor) {
86   visitor->Trace(xpath_evaluator_);
87   Supplement<Document>::Trace(visitor);
88 }
89 
90 }  // namespace blink
91