Coverage Report

Created: 2024-08-27 12:19

/src/libxslt/libexslt/common.c
Line
Count
Source (jump to first uncovered line)
1
#define IN_LIBEXSLT
2
#include "libexslt/libexslt.h"
3
4
#include <libxml/tree.h>
5
#include <libxml/xpath.h>
6
#include <libxml/xpathInternals.h>
7
8
#include <libxslt/xsltutils.h>
9
#include <libxslt/xsltInternals.h>
10
#include <libxslt/extensions.h>
11
#include <libxslt/transform.h>
12
#include <libxslt/extra.h>
13
#include <libxslt/preproc.h>
14
15
#include "exslt.h"
16
17
static void
18
19
exsltNodeSetFunction (xmlXPathParserContextPtr ctxt, int nargs) {
19
19
    if (nargs != 1) {
20
2
  xmlXPathSetArityError(ctxt);
21
2
  return;
22
2
    }
23
17
    if (xmlXPathStackIsNodeSet (ctxt)) {
24
1
  xsltFunctionNodeSet (ctxt, nargs);
25
1
  return;
26
16
    } else {
27
16
  xmlDocPtr fragment;
28
16
  xsltTransformContextPtr tctxt = xsltXPathGetTransformContext(ctxt);
29
16
  xmlNodePtr txt;
30
16
  xmlChar *strval;
31
16
  xmlXPathObjectPtr obj;
32
  /*
33
  * SPEC EXSLT:
34
  * "You can also use this function to turn a string into a text
35
  * node, which is helpful if you want to pass a string to a
36
  * function that only accepts a node-set."
37
  */
38
16
  fragment = xsltCreateRVT(tctxt);
39
16
  if (fragment == NULL) {
40
0
      xsltTransformError(tctxt, NULL, tctxt->inst,
41
0
    "exsltNodeSetFunction: Failed to create a tree fragment.\n");
42
0
      tctxt->state = XSLT_STATE_STOPPED;
43
0
      return;
44
0
  }
45
16
  xsltRegisterLocalRVT(tctxt, fragment);
46
47
16
  strval = xmlXPathPopString (ctxt);
48
49
16
  txt = xmlNewDocText (fragment, strval);
50
16
  xmlAddChild((xmlNodePtr) fragment, txt);
51
16
  obj = xmlXPathNewNodeSet(txt);
52
16
  if (obj == NULL) {
53
0
      xsltTransformError(tctxt, NULL, tctxt->inst,
54
0
    "exsltNodeSetFunction: Failed to create a node set object.\n");
55
0
      tctxt->state = XSLT_STATE_STOPPED;
56
0
  }
57
16
  if (strval != NULL)
58
16
      xmlFree (strval);
59
60
16
  valuePush (ctxt, obj);
61
16
    }
62
17
}
63
64
static void
65
18
exsltObjectTypeFunction (xmlXPathParserContextPtr ctxt, int nargs) {
66
18
    xmlXPathObjectPtr obj, ret;
67
68
18
    if (nargs != 1) {
69
2
  xmlXPathSetArityError(ctxt);
70
2
  return;
71
2
    }
72
73
16
    obj = valuePop(ctxt);
74
75
16
    switch (obj->type) {
76
0
    case XPATH_STRING:
77
0
  ret = xmlXPathNewCString("string");
78
0
  break;
79
16
    case XPATH_NUMBER:
80
16
  ret = xmlXPathNewCString("number");
81
16
  break;
82
0
    case XPATH_BOOLEAN:
83
0
  ret = xmlXPathNewCString("boolean");
84
0
  break;
85
0
    case XPATH_NODESET:
86
0
  ret = xmlXPathNewCString("node-set");
87
0
  break;
88
0
    case XPATH_XSLT_TREE:
89
0
  ret = xmlXPathNewCString("RTF");
90
0
  break;
91
0
    case XPATH_USERS:
92
0
  ret = xmlXPathNewCString("external");
93
0
  break;
94
0
    default:
95
0
  xsltGenericError(xsltGenericErrorContext,
96
0
    "object-type() invalid arg\n");
97
0
  ctxt->error = XPATH_INVALID_TYPE;
98
0
  xmlXPathFreeObject(obj);
99
0
  return;
100
16
    }
101
16
    xmlXPathFreeObject(obj);
102
16
    valuePush(ctxt, ret);
103
16
}
104
105
106
/**
107
 * exsltCommonRegister:
108
 *
109
 * Registers the EXSLT - Common module
110
 */
111
112
void
113
1.20k
exsltCommonRegister (void) {
114
1.20k
    xsltRegisterExtModuleFunction((const xmlChar *) "node-set",
115
1.20k
          EXSLT_COMMON_NAMESPACE,
116
1.20k
          exsltNodeSetFunction);
117
1.20k
    xsltRegisterExtModuleFunction((const xmlChar *) "object-type",
118
1.20k
          EXSLT_COMMON_NAMESPACE,
119
1.20k
          exsltObjectTypeFunction);
120
1.20k
    xsltRegisterExtModuleElement((const xmlChar *) "document",
121
1.20k
         EXSLT_COMMON_NAMESPACE,
122
1.20k
         xsltDocumentComp,
123
1.20k
         xsltDocumentElem);
124
1.20k
}