script/dom/xpath/
xpathexpression.rs1use dom_struct::dom_struct;
6use js::context::JSContext;
7use js::rust::HandleObject;
8use script_bindings::codegen::InheritTypes::{CharacterDataTypeId, NodeTypeId};
9use script_bindings::reflector::{Reflector, reflect_dom_object_with_proto_and_cx};
10use xpath::{Expression, evaluate_parsed_xpath};
11
12use crate::dom::bindings::codegen::Bindings::XPathExpressionBinding::XPathExpressionMethods;
13use crate::dom::bindings::error::{Error, Fallible};
14use crate::dom::bindings::reflector::DomGlobal;
15use crate::dom::bindings::root::{Dom, DomRoot};
16use crate::dom::node::Node;
17use crate::dom::window::Window;
18use crate::dom::xpathresult::{XPathResult, XPathResultType};
19use crate::xpath::{Value, XPathImplementation};
20
21#[dom_struct]
22pub(crate) struct XPathExpression {
23 reflector_: Reflector,
24 window: Dom<Window>,
25 #[no_trace]
26 parsed_expression: Expression,
27}
28
29impl XPathExpression {
30 fn new_inherited(window: &Window, parsed_expression: Expression) -> XPathExpression {
31 XPathExpression {
32 reflector_: Reflector::new(),
33 window: Dom::from_ref(window),
34 parsed_expression,
35 }
36 }
37
38 pub(crate) fn new(
39 cx: &mut JSContext,
40 window: &Window,
41 proto: Option<HandleObject>,
42 parsed_expression: Expression,
43 ) -> DomRoot<XPathExpression> {
44 reflect_dom_object_with_proto_and_cx(
45 Box::new(XPathExpression::new_inherited(window, parsed_expression)),
46 window,
47 proto,
48 cx,
49 )
50 }
51
52 pub(crate) fn evaluate_internal(
53 &self,
54 cx: &mut JSContext,
55 context_node: &Node,
56 result_type_num: u16,
57 result: Option<&XPathResult>,
58 ) -> Fallible<DomRoot<XPathResult>> {
59 let is_allowed_context_node_type = matches!(
60 context_node.type_id(),
61 NodeTypeId::Attr |
62 NodeTypeId::CharacterData(
63 CharacterDataTypeId::Comment |
64 CharacterDataTypeId::Text(_) |
65 CharacterDataTypeId::ProcessingInstruction
66 ) |
67 NodeTypeId::Document(_) |
68 NodeTypeId::Element(_)
69 );
70 if !is_allowed_context_node_type {
71 return Err(Error::NotSupported(None));
72 }
73
74 let result_type = XPathResultType::try_from(result_type_num)
75 .map_err(|()| Error::Type(c"Invalid XPath result type".to_owned()))?;
76
77 let global = self.global();
78 let window = global.as_window();
79
80 let result_value = evaluate_parsed_xpath::<XPathImplementation>(
81 cx,
82 &self.parsed_expression,
83 DomRoot::from_ref(context_node).into(),
84 )
85 .map_err(|_| Error::Operation(None))?;
86
87 let result_value: Value = match result_type {
89 XPathResultType::Boolean => result_value.convert_to_boolean().into(),
90 XPathResultType::Number => result_value.convert_to_number().into(),
91 XPathResultType::String => result_value.convert_to_string().into(),
92 _ => result_value,
93 };
94
95 let inferred_result_type = if result_type == XPathResultType::Any {
98 match result_value {
99 Value::Boolean(_) => XPathResultType::Boolean,
100 Value::Number(_) => XPathResultType::Number,
101 Value::String(_) => XPathResultType::String,
102 Value::NodeSet(_) => XPathResultType::UnorderedNodeIterator,
103 }
104 } else {
105 result_type
106 };
107
108 if let Some(result) = result {
109 result.reinitialize_with(inferred_result_type, result_value.into());
112 Ok(DomRoot::from_ref(result))
113 } else {
114 Ok(XPathResult::new(
115 cx,
116 window,
117 None,
118 inferred_result_type,
119 result_value.into(),
120 ))
121 }
122 }
123}
124
125impl XPathExpressionMethods<crate::DomTypeHolder> for XPathExpression {
126 fn Evaluate(
128 &self,
129 cx: &mut JSContext,
130 context_node: &Node,
131 result_type_num: u16,
132 result: Option<&XPathResult>,
133 ) -> Fallible<DomRoot<XPathResult>> {
134 self.evaluate_internal(cx, context_node, result_type_num, result)
135 }
136}