xpath/
functions.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use crate::ast::CoreFunction;
6use crate::context::EvaluationCtx;
7use crate::eval::try_extract_nodeset;
8use crate::value::{NodeSet, parse_number_from_string};
9use crate::{Document, Dom, Element, Error, Node, Value};
10
11/// Returns e.g. "rect" for `<svg:rect>`
12fn local_name<N: Node>(node: &N) -> Option<String> {
13    node.as_element()
14        .map(|element| element.local_name().to_string())
15}
16
17/// Returns e.g. "svg:rect" for `<svg:rect>`
18fn name<N: Node>(node: &N) -> Option<String> {
19    node.as_element().map(|element| {
20        if let Some(prefix) = element.prefix().as_ref() {
21            format!("{}:{}", prefix, element.local_name())
22        } else {
23            element.local_name().to_string()
24        }
25    })
26}
27
28/// Returns e.g. the SVG namespace URI for `<svg:rect>`
29fn namespace_uri<N: Node>(node: &N) -> Option<String> {
30    node.as_element()
31        .map(|element| element.namespace().to_string())
32}
33
34/// If s2 is found inside s1, return everything *before* s2. Return all of s1 otherwise.
35fn substring_before(s1: &str, s2: &str) -> String {
36    match s1.find(s2) {
37        Some(pos) => s1[..pos].to_string(),
38        None => String::new(),
39    }
40}
41
42/// If s2 is found inside s1, return everything *after* s2. Return all of s1 otherwise.
43fn substring_after(s1: &str, s2: &str) -> String {
44    match s1.find(s2) {
45        Some(pos) => s1[pos + s2.len()..].to_string(),
46        None => String::new(),
47    }
48}
49
50/// <https://www.w3.org/TR/xpath-10/#function-substring>
51fn substring(source: &str, start: isize, length: Option<isize>) -> String {
52    let start_index = start.max(0) as usize;
53    let length = length
54        .map(|length| length.max(0) as usize)
55        .unwrap_or(usize::MAX);
56
57    // The specification doesn't tell us whether the term "length" refers
58    // to bytes, codepoints, graphemes etc. We choose code points.
59    // Firefox uses bytes and allows slicing at indices that are not char boundaries... Let's not do that.
60    source.chars().skip(start_index).take(length).collect()
61}
62
63/// <https://www.w3.org/TR/1999/REC-xpath-19991116/#function-normalize-space>
64pub(crate) fn normalize_space(input: &str) -> String {
65    // Trim leading and trailing whitespace
66    let input = input.trim_ascii();
67
68    let mut result = String::with_capacity(input.len());
69    input
70        .split([' ', '\x09', '\x0D', '\x0A'])
71        .filter(|segment| !segment.is_empty())
72        .for_each(|segment| {
73            if !result.is_empty() {
74                result.push(' ');
75            }
76
77            result.push_str(segment);
78        });
79
80    result
81}
82
83/// <https://www.w3.org/TR/1999/REC-xpath-19991116/#function-lang>
84fn lang_matches(context_lang: Option<&str>, target_lang: &str) -> bool {
85    let Some(context_lang) = context_lang else {
86        return false;
87    };
88
89    let context_lower = context_lang.to_ascii_lowercase();
90    let target_lower = target_lang.to_ascii_lowercase();
91
92    if context_lower == target_lower {
93        return true;
94    }
95
96    // Check if context is target with additional suffix
97    if context_lower.starts_with(&target_lower) {
98        // Make sure the next character is a hyphen to avoid matching
99        // e.g. "england" when target is "en"
100        if let Some(next_char) = context_lower.chars().nth(target_lower.len()) {
101            return next_char == '-';
102        }
103    }
104
105    false
106}
107
108/// <https://www.w3.org/TR/1999/REC-xpath-19991116/#function-translate>
109fn translate(input: &str, from: &str, to: &str) -> String {
110    let mut result = String::with_capacity(input.len());
111
112    for character in input.chars() {
113        let Some(replacement_index) = from.chars().position(|to_replace| to_replace == character)
114        else {
115            result.push(character);
116            continue;
117        };
118
119        if let Some(replace_with) = to.chars().nth(replacement_index) {
120            result.push(replace_with);
121        }
122    }
123
124    result
125}
126
127impl CoreFunction {
128    pub(crate) fn evaluate<D: Dom>(
129        &self,
130        context: &EvaluationCtx<D>,
131    ) -> Result<Value<D::Node>, Error> {
132        match self {
133            CoreFunction::Last => {
134                let predicate_ctx = context.predicate_ctx.ok_or_else(|| Error::Internal {
135                    msg: "[CoreFunction] last() is only usable as a predicate".to_string(),
136                })?;
137                Ok(Value::Number(predicate_ctx.size as f64))
138            },
139            CoreFunction::Position => {
140                let predicate_ctx = context.predicate_ctx.ok_or_else(|| Error::Internal {
141                    msg: "[CoreFunction] position() is only usable as a predicate".to_string(),
142                })?;
143                Ok(Value::Number(predicate_ctx.index as f64))
144            },
145            CoreFunction::Count(expr) => {
146                let nodes = expr.evaluate(context).and_then(try_extract_nodeset)?;
147                Ok(Value::Number(nodes.len() as f64))
148            },
149            CoreFunction::String(expr_opt) => match expr_opt {
150                Some(expr) => Ok(Value::String(expr.evaluate(context)?.convert_to_string())),
151                None => Ok(Value::String(context.context_node.text_content())),
152            },
153            CoreFunction::Concat(exprs) => {
154                let strings: Result<Vec<_>, _> = exprs
155                    .iter()
156                    .map(|e| Ok(e.evaluate(context)?.convert_to_string()))
157                    .collect();
158                Ok(Value::String(strings?.join("")))
159            },
160            CoreFunction::Id(expr) => {
161                let argument = expr.evaluate(context)?;
162                let document = context.context_node.owner_document();
163                let mut result = NodeSet::default();
164
165                // https://www.w3.org/TR/1999/REC-xpath-19991116/#function-id
166                // > When the argument to id is of type node-set, then the result is the union of the result
167                // > of applying id to the string-value of each of the nodes in the argument node-set.
168                let mut extend_result_with_matching_nodes = |input: &str| {
169                    result.extend(
170                        normalize_space(input)
171                            .split(' ')
172                            .flat_map(|id| document.get_elements_with_id(id))
173                            .map(|element| element.as_node()),
174                    );
175                };
176                if let Value::NodeSet(node_set) = argument {
177                    for node in node_set.iter() {
178                        extend_result_with_matching_nodes(&node.text_content())
179                    }
180                } else {
181                    extend_result_with_matching_nodes(&argument.convert_to_string())
182                }
183
184                result.sort();
185                Ok(Value::NodeSet(result))
186            },
187            CoreFunction::LocalName(expr_opt) => {
188                let node = match expr_opt {
189                    Some(expr) => expr
190                        .evaluate(context)
191                        .and_then(try_extract_nodeset)?
192                        .first(),
193                    None => Some(context.context_node.clone()),
194                };
195                let name = node.and_then(|n| local_name(&n)).unwrap_or_default();
196                Ok(Value::String(name.to_string()))
197            },
198            CoreFunction::NamespaceUri(expr_opt) => {
199                let node = match expr_opt {
200                    Some(expr) => expr
201                        .evaluate(context)
202                        .and_then(try_extract_nodeset)?
203                        .first(),
204                    None => Some(context.context_node.clone()),
205                };
206                let ns = node.and_then(|n| namespace_uri(&n)).unwrap_or_default();
207                Ok(Value::String(ns.to_string()))
208            },
209            CoreFunction::Name(expr_opt) => {
210                let node = match expr_opt {
211                    Some(expr) => expr
212                        .evaluate(context)
213                        .and_then(try_extract_nodeset)?
214                        .first(),
215                    None => Some(context.context_node.clone()),
216                };
217                let name = node.and_then(|n| name(&n)).unwrap_or_default();
218                Ok(Value::String(name))
219            },
220            CoreFunction::StartsWith(str1, str2) => {
221                let s1 = str1.evaluate(context)?.convert_to_string();
222                let s2 = str2.evaluate(context)?.convert_to_string();
223                Ok(Value::Boolean(s1.starts_with(&s2)))
224            },
225            CoreFunction::Contains(str1, str2) => {
226                let s1 = str1.evaluate(context)?.convert_to_string();
227                let s2 = str2.evaluate(context)?.convert_to_string();
228                Ok(Value::Boolean(s1.contains(&s2)))
229            },
230            CoreFunction::SubstringBefore(str1, str2) => {
231                let s1 = str1.evaluate(context)?.convert_to_string();
232                let s2 = str2.evaluate(context)?.convert_to_string();
233                Ok(Value::String(substring_before(&s1, &s2)))
234            },
235            CoreFunction::SubstringAfter(str1, str2) => {
236                let s1 = str1.evaluate(context)?.convert_to_string();
237                let s2 = str2.evaluate(context)?.convert_to_string();
238                Ok(Value::String(substring_after(&s1, &s2)))
239            },
240            CoreFunction::Substring(source_expression, start, length) => {
241                let source = source_expression.evaluate(context)?.convert_to_string();
242                let start_idx = start.evaluate(context)?.convert_to_number().round() as isize - 1;
243                let result = if let Some(length_expression) = length {
244                    let length = length_expression
245                        .evaluate(context)?
246                        .convert_to_number()
247                        .round() as isize;
248                    substring(&source, start_idx, Some(length))
249                } else {
250                    substring(&source, start_idx, None)
251                };
252                Ok(Value::String(result))
253            },
254            CoreFunction::StringLength(expr_opt) => {
255                let string = match expr_opt {
256                    Some(expr) => expr.evaluate(context)?.convert_to_string(),
257                    None => context.context_node.text_content(),
258                };
259                Ok(Value::Number(string.chars().count() as f64))
260            },
261            CoreFunction::NormalizeSpace(expr_opt) => {
262                let string = match expr_opt {
263                    Some(expr) => expr.evaluate(context)?.convert_to_string(),
264                    None => context.context_node.text_content(),
265                };
266
267                Ok(Value::String(normalize_space(&string)))
268            },
269            CoreFunction::Translate(str1, str2, str3) => {
270                let string = str1.evaluate(context)?.convert_to_string();
271                let from = str2.evaluate(context)?.convert_to_string();
272                let to = str3.evaluate(context)?.convert_to_string();
273                Ok(Value::String(translate(&string, &from, &to)))
274            },
275            CoreFunction::Number(expr_opt) => {
276                let val = match expr_opt {
277                    Some(expr) => expr.evaluate(context)?,
278                    None => Value::String(context.context_node.text_content()),
279                };
280                Ok(Value::Number(val.convert_to_number()))
281            },
282            CoreFunction::Sum(expr) => {
283                let nodes = expr.evaluate(context).and_then(try_extract_nodeset)?;
284                let sum = nodes
285                    .iter()
286                    .map(|node| parse_number_from_string(&node.text_content()))
287                    .sum();
288                Ok(Value::Number(sum))
289            },
290            CoreFunction::Floor(expr) => {
291                let num = expr.evaluate(context)?.convert_to_number();
292                Ok(Value::Number(num.floor()))
293            },
294            CoreFunction::Ceiling(expr) => {
295                let num = expr.evaluate(context)?.convert_to_number();
296                Ok(Value::Number(num.ceil()))
297            },
298            CoreFunction::Round(expr) => {
299                let num = expr.evaluate(context)?.convert_to_number();
300                Ok(Value::Number(num.round()))
301            },
302            CoreFunction::Boolean(expr) => {
303                Ok(Value::Boolean(expr.evaluate(context)?.convert_to_boolean()))
304            },
305            CoreFunction::Not(expr) => Ok(Value::Boolean(
306                !expr.evaluate(context)?.convert_to_boolean(),
307            )),
308            CoreFunction::True => Ok(Value::Boolean(true)),
309            CoreFunction::False => Ok(Value::Boolean(false)),
310            CoreFunction::Lang(expr) => {
311                let context_lang = context.context_node.language();
312                let lang = expr.evaluate(context)?.convert_to_string();
313                Ok(Value::Boolean(lang_matches(context_lang.as_deref(), &lang)))
314            },
315        }
316    }
317}
318#[cfg(test)]
319mod tests {
320    use super::{lang_matches, substring, substring_after, substring_before};
321    use crate::functions::{normalize_space, translate};
322
323    #[test]
324    fn test_substring_before() {
325        assert_eq!(substring_before("hello world", "world"), "hello ");
326        assert_eq!(substring_before("prefix:name", ":"), "prefix");
327        assert_eq!(substring_before("no-separator", "xyz"), "");
328        assert_eq!(substring_before("", "anything"), "");
329        assert_eq!(substring_before("multiple:colons:here", ":"), "multiple");
330        assert_eq!(substring_before("start-match-test", "start"), "");
331    }
332
333    #[test]
334    fn test_substring_after() {
335        assert_eq!(substring_after("hello world", "hello "), "world");
336        assert_eq!(substring_after("prefix:name", ":"), "name");
337        assert_eq!(substring_after("no-separator", "xyz"), "");
338        assert_eq!(substring_after("", "anything"), "");
339        assert_eq!(substring_after("multiple:colons:here", ":"), "colons:here");
340        assert_eq!(substring_after("test-end-match", "match"), "");
341    }
342
343    #[test]
344    fn test_substring() {
345        assert_eq!(substring("hello world", 0, Some(5)), "hello");
346        assert_eq!(substring("hello world", 6, Some(5)), "world");
347        assert_eq!(substring("hello", 1, Some(3)), "ell");
348        assert_eq!(substring("hello", -5, Some(2)), "he");
349        assert_eq!(substring("hello", 0, None), "hello");
350        assert_eq!(substring("hello", 2, Some(10)), "llo");
351        assert_eq!(substring("hello", 5, Some(1)), "");
352        assert_eq!(substring("", 0, Some(5)), "");
353        assert_eq!(substring("hello", 0, Some(0)), "");
354        assert_eq!(substring("hello", 0, Some(-5)), "");
355    }
356
357    #[test]
358    fn test_substring_with_out_of_bounds_index() {
359        assert_eq!(substring("Servo", 42, None), "");
360    }
361
362    #[test]
363    fn test_substring_with_multi_byte_characters() {
364        assert_eq!(substring("๐Ÿฆž๐Ÿฆž๐Ÿฆž", 1, None), "๐Ÿฆž๐Ÿฆž");
365    }
366
367    #[test]
368    fn test_lang_matches() {
369        assert!(lang_matches(Some("en"), "en"));
370        assert!(lang_matches(Some("EN"), "en"));
371        assert!(lang_matches(Some("en"), "EN"));
372        assert!(lang_matches(Some("en-US"), "en"));
373        assert!(lang_matches(Some("en-GB"), "en"));
374
375        assert!(!lang_matches(Some("eng"), "en"));
376        assert!(!lang_matches(Some("fr"), "en"));
377        assert!(!lang_matches(Some("fr-en"), "en"));
378        assert!(!lang_matches(None, "en"));
379    }
380
381    #[test]
382    fn test_normalize_space() {
383        assert_eq!(normalize_space(" "), "");
384        assert_eq!(normalize_space("\n\t\r "), "");
385        assert_eq!(normalize_space("no-space"), "no-space");
386        assert_eq!(normalize_space("one space"), "one space");
387        assert_eq!(normalize_space("more    whitespace"), "more whitespace");
388        assert_eq!(
389            normalize_space("  \t leading  and trailing\n"),
390            "leading and trailing"
391        );
392    }
393
394    #[test]
395    fn test_translate() {
396        assert_eq!(translate("", "", ""), "");
397        assert_eq!(translate("", "abc", ""), "");
398        assert_eq!(translate("abcd", "abc", ""), "d");
399        assert_eq!(translate("abcd", "abc", "cba"), "cbad");
400        assert_eq!(translate("abc", "", "abc"), "abc");
401    }
402
403    #[test]
404    fn test_translate_with_multi_byte_characters() {
405        assert_eq!(translate("a๐Ÿฆžb๐Ÿ˜c๐Ÿฆžd", "๐Ÿ˜c", "๐Ÿคจ๐Ÿค–"), "a๐Ÿฆžb๐Ÿคจ๐Ÿค–๐Ÿฆžd");
406    }
407}