diff --git a/src/xpath.c b/src/xpath.c index b44784dc6..24d1a4338 100644 --- a/src/xpath.c +++ b/src/xpath.c @@ -5867,6 +5867,31 @@ xpath_pi_node(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options) return moveto_node(set, "*", 1, axis, NULL, NULL); } +/** + * @brief Execute the XPath comment() node type. Data trees have no comment nodes so the result is always + * an empty LYXP_SET_NODE_SET. + * + * @param[in,out] set Context and result set at the same time. + * @param[in] options XPath options. + * @return LY_ERR + */ +static LY_ERR +xpath_pi_comment(struct lyxp_set *set, uint32_t options) +{ + if (options & LYXP_SCNODE_ALL) { + set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); + return LY_SUCCESS; + } + + if (set->type != LYXP_SET_NODE_SET) { + LOGVAL_DXPATH(set, LY_VCODE_XP_INCTX, print_set_type(set), "comment()"); + return LY_EVALID; + } + + lyxp_set_free_content(set); + return LY_SUCCESS; +} + /** * @brief Execute the XPath text() processing instruction (node type). Returns LYXP_SET_NODE_SET * with the text content of the nodes in the context. @@ -8817,7 +8842,7 @@ eval_name_test_with_predicate_scnode(const struct lyxp_expr *exp, uint32_t *tok_ * * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' * [6] NodeTest ::= NameTest | NodeType '(' ')' - * [8] NodeType ::= 'text' | 'node' + * [8] NodeType ::= 'text' | 'node' | 'comment' * * @param[in] exp Parsed XPath expression. * @param[in] tok_idx Position in the expression @p exp. @@ -8836,12 +8861,13 @@ eval_node_type_with_predicate(const struct lyxp_expr *exp, uint32_t *tok_idx, en (void)all_desc; if (!(options & LYXP_SKIP_EXPR)) { - assert(exp->tok_len[*tok_idx] == 4); - if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) { + if ((exp->tok_len[*tok_idx] == 4) && !strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) { rc = xpath_pi_node(set, axis, options); - } else { - assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4)); + } else if ((exp->tok_len[*tok_idx] == 4) && !strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4)) { rc = xpath_pi_text(set, axis, options); + } else { + assert((exp->tok_len[*tok_idx] == 7) && !strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "comment", 7)); + rc = xpath_pi_comment(set, options); } LY_CHECK_RET(rc); } diff --git a/tests/utests/basic/test_xpath.c b/tests/utests/basic/test_xpath.c index f28df3cbd..7eabc4a31 100644 --- a/tests/utests/basic/test_xpath.c +++ b/tests/utests/basic/test_xpath.c @@ -1374,6 +1374,14 @@ test_no_value(void **state) assert_int_equal(LY_SUCCESS, lyd_eval_xpath(tree, "deref(/)", &r)); assert_int_equal(r, 0); + /* there are no comment nodes, comment() must not select the text of the node */ + assert_int_equal(LY_SUCCESS, lyd_eval_xpath(tree, "/a:foo/text()", &r)); + assert_int_equal(r, 1); + assert_int_equal(LY_SUCCESS, lyd_eval_xpath(tree, "/a:foo/comment()", &r)); + assert_int_equal(r, 0); + assert_int_equal(LY_SUCCESS, lyd_eval_xpath(tree, "count(//comment())", &r)); + assert_int_equal(r, 0); + lyd_free_all(tree); }